aboutsummaryrefslogtreecommitdiffstats
path: root/backend/internal/api/v1/transactions/transactions.go
blob: c1ec42894de925e233909119ec2d7af5128b1083 (plain) (blame)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package transactions

import (
	"net/http"
	"strconv"
	"time"

	"finance/backend/internal/database"
	"finance/backend/internal/models"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

// GetTransactions returns all transactions for the authenticated user
// Can be filtered by type (income/expense) and date range
func GetTransactions() gin.HandlerFunc {
	return func(c *gin.Context) {
		// Get user from context (set by auth middleware)
		user, exists := c.Get("user")
		if !exists {
			c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
			return
		}

		userObj := user.(models.User)
		var transactions []models.Transaction

		// Build query
		query := database.DB.Where("user_id = ?", userObj.ID)

		// Filter by transaction type if provided
		if transactionType := c.Query("type"); transactionType != "" {
			if transactionType != "Income" && transactionType != "Expense" {
				c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid transaction type. Use 'Income' or 'Expense'"})
				return
			}
			query = query.Where("type = ?", transactionType)
		}

		// Filter by account if provided
		if accountID := c.Query("account_id"); accountID != "" {
			query = query.Where("account_id = ?", accountID)
		}

		// Filter by category if provided
		if category := c.Query("category"); category != "" {
			query = query.Where("category = ?", category)
		}

		// Filter by start date if provided
		if startDate := c.Query("start_date"); startDate != "" {
			date, err := time.Parse("2006-01-02", startDate)
			if err != nil {
				c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid start date format. Use YYYY-MM-DD"})
				return
			}
			query = query.Where("date >= ?", date)
		}

		// Filter by end date if provided
		if endDate := c.Query("end_date"); endDate != "" {
			date, err := time.Parse("2006-01-02", endDate)
			if err != nil {
				c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid end date format. Use YYYY-MM-DD"})
				return
			}
			// Add a day to include all transactions on the end date
			date = date.Add(24 * time.Hour)
			query = query.Where("date < ?", date)
		}

		// Order by date (newest first)
		query = query.Order("date DESC")

		// Execute query
		if err := query.Find(&transactions).Error; err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch transactions"})
			return
		}

		c.JSON(http.StatusOK, gin.H{"transactions": transactions})
	}
}

// GetTransactionByID returns a specific transaction by ID
func GetTransactionByID() gin.HandlerFunc {
	return func(c *gin.Context) {
		// Get user from context (set by auth middleware)
		user, exists := c.Get("user")
		if !exists {
			c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
			return
		}
		userObj := user.(models.User)

		// Get transaction ID from URL parameter
		transactionID, err := strconv.ParseUint(c.Param("id"), 10, 32)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid transaction ID format"})
			return
		}

		var transaction models.Transaction

		// Fetch the transaction and ensure it belongs to the authenticated user
		if err := database.DB.Where("id = ? AND user_id = ?", transactionID, userObj.ID).First(&transaction).Error; err != nil {
			if err == gorm.ErrRecordNotFound {
				c.JSON(http.StatusNotFound, gin.H{"error": "Transaction not found"})
			} else {
				c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch transaction"})
			}
			return
		}

		c.JSON(http.StatusOK, gin.H{"transaction": transaction})
	}
}

// CreateTransaction creates a new transaction
func CreateTransaction() gin.HandlerFunc {
	return func(c *gin.Context) {
		// Get user from context (set by auth middleware)
		user, exists := c.Get("user")
		if !exists {
			c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
			return
		}
		userObj := user.(models.User)

		// Define a struct to bind the request JSON
		var input struct {
			Description string `json:"description" binding:"required"`
			Amount      int64  `json:"amount" binding:"required"`
			Type        string `json:"type" binding:"required"` // "Income" or "Expense"
			Date        string `json:"date" binding:"required"` // YYYY-MM-DD format
			Category    string `json:"category" binding:"required"`
			AccountID   *uint  `json:"accountId"`
		}

		// Bind JSON to struct
		if err := c.ShouldBindJSON(&input); err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}

		// Validate transaction type
		if input.Type != "Income" && input.Type != "Expense" {
			c.JSON(http.StatusBadRequest, gin.H{"error": "Transaction type must be 'Income' or 'Expense'"})
			return
		}

		// Parse date
		date, err := time.Parse("2006-01-02", input.Date)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid date format. Use YYYY-MM-DD"})
			return
		}

		// Create transaction object
		transaction := models.Transaction{
			UserID:      userObj.ID,
			Description: input.Description,
			Amount:      input.Amount,
			Type:        input.Type,
			Date:        date,
			Category:    input.Category,
			AccountID:   input.AccountID,
		}

		// If an account is specified, verify that it exists and belongs to the user
		if input.AccountID != nil {
			var account models.Account
			if err := database.DB.Where("id = ? AND user_id = ?", *input.AccountID, userObj.ID).First(&account).Error; err != nil {
				if err == gorm.ErrRecordNotFound {
					c.JSON(http.StatusBadRequest, gin.H{"error": "Specified account not found or does not belong to you"})
				} else {
					c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to verify account"})
				}
				return
			}

			// Update account balance based on transaction type
			// For income, add to the balance; for expense, subtract from the balance
			tx := database.DB.Begin()

			if input.Type == "Income" {
				account.Balance += input.Amount
			} else { // Expense
				account.Balance -= input.Amount
			}

			// Save updated account balance
			if err := tx.Save(&account).Error; err != nil {
				tx.Rollback()
				c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update account balance"})
				return
			}

			// Save transaction
			if err := tx.Create(&transaction).Error; err != nil {
				tx.Rollback()
				c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create transaction"})
				return
			}

			// Commit transaction
			if err := tx.Commit().Error; err != nil {
				c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to commit transaction"})
				return
			}
		} else {
			// No account specified, just save the transaction
			if err := database.DB.Create(&transaction).Error; err != nil {
				c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create transaction"})
				return
			}
		}

		c.JSON(http.StatusCreated, gin.H{"transaction": transaction})
	}
}

// UpdateTransaction updates an existing transaction
func UpdateTransaction() gin.HandlerFunc {
	return func(c *gin.Context) {
		// Get user from context (set by auth middleware)
		user, exists := c.Get("user")
		if !exists {
			c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
			return
		}
		userObj := user.(models.User)

		// Get transaction ID from URL parameter
		transactionID, err := strconv.ParseUint(c.Param("id"), 10, 32)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid transaction ID format"})
			return
		}

		// Find existing transaction
		var transaction models.Transaction
		if err := database.DB.Where("id = ? AND user_id = ?", transactionID, userObj.ID).First(&transaction).Error; err != nil {
			if err == gorm.ErrRecordNotFound {
				c.JSON(http.StatusNotFound, gin.H{"error": "Transaction not found"})
			} else {
				c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch transaction"})
			}
			return
		}

		// Store original values for comparison
		originalAccountID := transaction.AccountID
		originalAmount := transaction.Amount
		originalType := transaction.Type

		// Define a struct to bind the request JSON
		var input struct {
			Description string `json:"description"`
			Amount      int64  `json:"amount"`
			Type        string `json:"type"` // "Income" or "Expense"
			Date        string `json:"date"` // YYYY-MM-DD format
			Category    string `json:"category"`
			AccountID   *uint  `json:"accountId"`
		}

		// Bind JSON to struct
		if err := c.ShouldBindJSON(&input); err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}

		// Begin database transaction for updating both transaction and account balance
		tx := database.DB.Begin()

		// Revert original account balance if the transaction affected an account
		if originalAccountID != nil {
			var originalAccount models.Account
			if err := tx.First(&originalAccount, *originalAccountID).Error; err != nil {
				tx.Rollback()
				c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch original account"})
				return
			}

			// Reverse the effect of the original transaction
			if originalType == "Income" {
				originalAccount.Balance -= originalAmount
			} else { // Expense
				originalAccount.Balance += originalAmount
			}

			if err := tx.Save(&originalAccount).Error; err != nil {
				tx.Rollback()
				c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update original account balance"})
				return
			}
		}

		// Update transaction fields if provided
		if input.Description != "" {
			transaction.Description = input.Description
		}
		if input.Amount != 0 {
			transaction.Amount = input.Amount
		}
		if input.Type != "" {
			if input.Type != "Income" && input.Type != "Expense" {
				tx.Rollback()
				c.JSON(http.StatusBadRequest, gin.H{"error": "Transaction type must be 'Income' or 'Expense'"})
				return
			}
			transaction.Type = input.Type
		}
		if input.Date != "" {
			date, err := time.Parse("2006-01-02", input.Date)
			if err != nil {
				tx.Rollback()
				c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid date format. Use YYYY-MM-DD"})
				return
			}
			transaction.Date = date
		}
		if input.Category != "" {
			transaction.Category = input.Category
		}
		if input.AccountID != nil || c.GetHeader("Content-Type") == "application/json" {
			transaction.AccountID = input.AccountID
		}

		// Update new account balance if the transaction affects an account
		if transaction.AccountID != nil {
			var newAccount models.Account
			if err := tx.First(&newAccount, *transaction.AccountID).Error; err != nil {
				tx.Rollback()
				c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch new account"})
				return
			}

			// Apply the effect of the updated transaction
			if transaction.Type == "Income" {
				newAccount.Balance += transaction.Amount
			} else { // Expense
				newAccount.Balance -= transaction.Amount
			}

			if err := tx.Save(&newAccount).Error; err != nil {
				tx.Rollback()
				c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update new account balance"})
				return
			}
		}

		// Save transaction
		if err := tx.Save(&transaction).Error; err != nil {
			tx.Rollback()
			c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update transaction"})
			return
		}

		// Commit transaction
		if err := tx.Commit().Error; err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to commit changes"})
			return
		}

		c.JSON(http.StatusOK, gin.H{"transaction": transaction})
	}
}

// DeleteTransaction deletes a transaction
func DeleteTransaction() gin.HandlerFunc {
	return func(c *gin.Context) {
		// Get user from context (set by auth middleware)
		user, exists := c.Get("user")
		if !exists {
			c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
			return
		}
		userObj := user.(models.User)

		// Get transaction ID from URL parameter
		transactionID, err := strconv.ParseUint(c.Param("id"), 10, 32)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid transaction ID format"})
			return
		}

		// Find the transaction
		var transaction models.Transaction
		if err := database.DB.Where("id = ? AND user_id = ?", transactionID, userObj.ID).First(&transaction).Error; err != nil {
			if err == gorm.ErrRecordNotFound {
				c.JSON(http.StatusNotFound, gin.H{"error": "Transaction not found"})
			} else {
				c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch transaction"})
			}
			return
		}

		// Begin database transaction
		tx := database.DB.Begin()

		// Update account balance if this transaction was linked to an account
		if transaction.AccountID != nil {
			var account models.Account
			if err := tx.First(&account, *transaction.AccountID).Error; err != nil {
				tx.Rollback()
				c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch account"})
				return
			}

			// Reverse the effect of the transaction on the account balance
			if transaction.Type == "Income" {
				account.Balance -= transaction.Amount
			} else { // Expense
				account.Balance += transaction.Amount
			}

			if err := tx.Save(&account).Error; err != nil {
				tx.Rollback()
				c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update account balance"})
				return
			}
		}

		// Delete the transaction
		if err := tx.Delete(&transaction).Error; err != nil {
			tx.Rollback()
			c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete transaction"})
			return
		}

		// Commit transaction
		if err := tx.Commit().Error; err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to commit changes"})
			return
		}

		c.JSON(http.StatusOK, gin.H{"message": "Transaction deleted successfully"})
	}
}