habits.go

 1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
 2//
 3// SPDX-License-Identifier: AGPL-3.0-or-later
 4
 5package lunatask
 6
 7import (
 8	"context"
 9	"fmt"
10	"net/http"
11)
12
13// TrackHabitActivityRequest specifies when a habit was performed.
14type TrackHabitActivityRequest struct {
15	PerformedOn Date `json:"performed_on"`
16}
17
18// TrackHabitActivityResponse contains the API's acknowledgement.
19type TrackHabitActivityResponse struct {
20	Status  string `json:"status"`
21	Message string `json:"message,omitempty"`
22}
23
24// TrackHabitActivity records that a habit was performed on the given date.
25// Habit IDs are found in the Lunatask app's habit settings.
26func (c *Client) TrackHabitActivity(
27	ctx context.Context,
28	habitID string,
29	request *TrackHabitActivityRequest,
30) (*TrackHabitActivityResponse, error) {
31	if habitID == "" {
32		return nil, fmt.Errorf("%w: habit ID cannot be empty", ErrBadRequest)
33	}
34
35	if request.PerformedOn.IsZero() {
36		return nil, fmt.Errorf("%w: performed_on is required", ErrBadRequest)
37	}
38
39	resp, _, err := doJSON[TrackHabitActivityResponse](ctx, c, http.MethodPost, "/habits/"+habitID+"/track", request)
40	if err != nil {
41		return nil, err
42	}
43
44	return resp, nil
45}