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	"time"
12)
13
14// TrackHabitActivityRequest represents the request to track a habit activity.
15type TrackHabitActivityRequest struct {
16	// PerformedOn is the ISO-8601 date when the activity was performed (e.g., "2024-08-26").
17	PerformedOn string `json:"performed_on"`
18}
19
20// TrackHabitActivityResponse represents the response from Lunatask API when tracking a habit activity.
21type TrackHabitActivityResponse struct {
22	Status  string `json:"status"`
23	Message string `json:"message,omitempty"`
24}
25
26// TrackHabitActivity tracks an activity for a habit in Lunatask.
27// The PerformedOn field must be an ISO-8601 date (e.g., "2024-08-26").
28func (c *Client) TrackHabitActivity(ctx context.Context, habitID string, request *TrackHabitActivityRequest) (*TrackHabitActivityResponse, error) {
29	if habitID == "" {
30		return nil, fmt.Errorf("%w: habit ID cannot be empty", ErrBadRequest)
31	}
32	if request.PerformedOn == "" {
33		return nil, fmt.Errorf("%w: performed_on is required", ErrBadRequest)
34	}
35
36	// Validate date format (YYYY-MM-DD)
37	if _, err := time.Parse("2006-01-02", request.PerformedOn); err != nil {
38		return nil, fmt.Errorf("%w: performed_on must be a valid ISO-8601 date (YYYY-MM-DD)", ErrBadRequest)
39	}
40
41	resp, _, err := doJSON[TrackHabitActivityResponse](c, ctx, http.MethodPost, "/habits/"+habitID+"/track", request)
42	if err != nil {
43		return nil, err
44	}
45
46	return resp, nil
47}