summaryrefslogtreecommitdiff
path: root/internal/commands/add_project.go
blob: 6c37e2afda4859a766e35d63b4cec83f4260c7e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package commands

import (
	"context"
	"database/sql"
	"fmt"
	"strconv"

	punchctx "punchcard/internal/context"
	"punchcard/internal/queries"

	"github.com/spf13/cobra"
)

func NewAddProjectCmd() *cobra.Command {
	cmd := &cobra.Command{
		Use:   "project <name>",
		Short: "Add a new project",
		Long: `Add a new project to the database. Client can be specified by ID or name using the -c/--client flag.
	
Examples:
  punch add project "Website Redesign" -c "Acme Corp"
  punch add project "Mobile App" --client 1`,
		Args: cobra.ExactArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			projectName := args[0]

			clientRef, err := cmd.Flags().GetString("client")
			if err != nil {
				return fmt.Errorf("failed to get client flag: %w", err)
			}
			if clientRef == "" {
				return fmt.Errorf("client is required, use -c/--client flag")
			}

			billableRateFloat, _ := cmd.Flags().GetFloat64("hourly-rate")
			billableRate := int64(billableRateFloat * 100) // Convert dollars to cents

			q := punchctx.GetDB(cmd.Context())
			if q == nil {
				return fmt.Errorf("database not available in context")
			}

			// Find client by ID or name
			client, err := findClient(cmd.Context(), q, clientRef)
			if err != nil {
				return fmt.Errorf("failed to find client: %w", err)
			}

			// Create project
			var billableRateParam sql.NullInt64
			if billableRate > 0 {
				billableRateParam = sql.NullInt64{Int64: billableRate, Valid: true}
			}

			project, err := q.CreateProject(cmd.Context(), queries.CreateProjectParams{
				Name:         projectName,
				ClientID:     client.ID,
				BillableRate: billableRateParam,
			})
			if err != nil {
				return fmt.Errorf("failed to create project: %w", err)
			}

			output := fmt.Sprintf("Created project: %s for client %s (ID: %d)", project.Name, client.Name, project.ID)
			cmd.Print(output + "\n")

			return nil
		},
	}

	cmd.Flags().StringP("client", "c", "", "Client name or ID (required)")
	cmd.Flags().Float64P("hourly-rate", "r", 0, "Default hourly billable rate for this project")
	if err := cmd.MarkFlagRequired("client"); err != nil {
		panic(fmt.Sprintf("Failed to mark client flag as required: %v", err))
	}

	return cmd
}

func findClient(ctx context.Context, q *queries.Queries, clientRef string) (queries.Client, error) {
	// Parse clientRef as ID if possible, otherwise use 0
	var idParam int64
	if id, err := strconv.ParseInt(clientRef, 10, 64); err == nil {
		idParam = id
	}

	// Search by both ID and name using UNION ALL
	clients, err := q.FindClient(ctx, queries.FindClientParams{
		ID:   idParam,
		Name: clientRef,
	})
	if err != nil {
		return queries.Client{}, fmt.Errorf("database error looking up client: %w", err)
	}

	// Check results
	switch len(clients) {
	case 0:
		return queries.Client{}, fmt.Errorf("client not found: %s", clientRef)
	case 1:
		return clients[0], nil
	default:
		return queries.Client{}, fmt.Errorf("ambiguous client: %s", clientRef)
	}
}