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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
|
package handlers
import (
"log"
"net/http"
"strconv"
"time"
"finance/backend/internal/database"
"finance/backend/internal/models"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// LoanHandler handles loan-related operations
type LoanHandler struct {
}
// NewLoanHandler creates and returns a new LoanHandler instance
func NewLoanHandler() *LoanHandler {
return &LoanHandler{}
}
// GetLoans retrieves all loans for the authenticated user
func (h *LoanHandler) GetLoans(c *gin.Context) {
userID := c.MustGet("userID").(uint)
var loans []models.Loan
if err := database.DB.Where("user_id = ?", userID).Find(&loans).Error; err != nil {
log.Printf("Error fetching loans for user %d: %v", userID, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get loans"})
return
}
c.JSON(http.StatusOK, loans)
}
// GetLoanByID retrieves a specific loan by ID for the authenticated user
func (h *LoanHandler) GetLoanByID(c *gin.Context) {
userID := c.MustGet("userID").(uint)
loanID, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid loan ID"})
return
}
var loan models.Loan
if err := database.DB.Where("id = ? AND user_id = ?", loanID, userID).First(&loan).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Loan not found"})
return
}
c.JSON(http.StatusOK, loan)
}
// CreateLoan creates a new loan for the authenticated user
func (h *LoanHandler) CreateLoan(c *gin.Context) {
userID := c.MustGet("userID").(uint)
// Define input structure
type CreateLoanInput struct {
Name string `json:"name" binding:"required"`
OriginalAmount int64 `json:"originalAmount" binding:"required"`
CurrentBalance int64 `json:"currentBalance" binding:"required"`
InterestRate float64 `json:"interestRate"`
StartDate string `json:"startDate"`
EndDate string `json:"endDate"`
AccountID *uint `json:"accountId"`
}
var input CreateLoanInput
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Parse dates if provided
var startDate, endDate time.Time
var err error
if input.StartDate != "" {
startDate, err = time.Parse("2006-01-02", input.StartDate)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid start date format. Use YYYY-MM-DD"})
return
}
}
if input.EndDate != "" {
endDate, err = time.Parse("2006-01-02", input.EndDate)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid end date format. Use YYYY-MM-DD"})
return
}
}
// Validate account if provided
if input.AccountID != nil {
var account models.Account
if err := database.DB.Where("id = ? AND user_id = ?", *input.AccountID, userID).First(&account).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Associated account not found or doesn't belong to user"})
return
}
}
// Create loan
loan := models.Loan{
UserID: userID,
AccountID: input.AccountID,
Name: input.Name,
OriginalAmount: input.OriginalAmount,
CurrentBalance: input.CurrentBalance,
InterestRate: input.InterestRate,
}
// Only set dates if they were provided
if !startDate.IsZero() {
loan.StartDate = startDate
}
if !endDate.IsZero() {
loan.EndDate = endDate
}
if err := database.DB.Create(&loan).Error; err != nil {
log.Printf("Error creating loan for user %d: %v", userID, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create loan"})
return
}
c.JSON(http.StatusCreated, loan)
}
// UpdateLoan updates an existing loan for the authenticated user
func (h *LoanHandler) UpdateLoan(c *gin.Context) {
userID := c.MustGet("userID").(uint)
loanID, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid loan ID"})
return
}
// Find loan
var loan models.Loan
if err := database.DB.Where("id = ? AND user_id = ?", loanID, userID).First(&loan).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Loan not found"})
return
}
// Define update structure
type UpdateLoanInput struct {
Name string `json:"name"`
OriginalAmount int64 `json:"originalAmount"`
CurrentBalance int64 `json:"currentBalance"`
InterestRate float64 `json:"interestRate"`
StartDate string `json:"startDate"`
EndDate string `json:"endDate"`
AccountID *uint `json:"accountId"`
}
var input UpdateLoanInput
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Update fields if provided
if input.Name != "" {
loan.Name = input.Name
}
if input.OriginalAmount != 0 {
loan.OriginalAmount = input.OriginalAmount
}
if input.CurrentBalance != 0 {
loan.CurrentBalance = input.CurrentBalance
}
if input.InterestRate != 0 {
loan.InterestRate = input.InterestRate
}
if input.StartDate != "" {
startDate, err := time.Parse("2006-01-02", input.StartDate)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid start date format. Use YYYY-MM-DD"})
return
}
loan.StartDate = startDate
}
if input.EndDate != "" {
endDate, err := time.Parse("2006-01-02", input.EndDate)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid end date format. Use YYYY-MM-DD"})
return
}
loan.EndDate = endDate
}
if input.AccountID != nil {
// Validate account if provided
var account models.Account
if err := database.DB.Where("id = ? AND user_id = ?", *input.AccountID, userID).First(&account).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Associated account not found or doesn't belong to user"})
return
}
loan.AccountID = input.AccountID
}
// Save changes
if err := database.DB.Save(&loan).Error; err != nil {
log.Printf("Error updating loan ID %d for user %d: %v", loanID, userID, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update loan"})
return
}
c.JSON(http.StatusOK, loan)
}
// DeleteLoan deletes a loan belonging to the authenticated user
func (h *LoanHandler) DeleteLoan(c *gin.Context) {
userID := c.MustGet("userID").(uint)
loanID, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid loan ID"})
return
}
// Check if loan exists and belongs to user
var loan models.Loan
if err := database.DB.Where("id = ? AND user_id = ?", loanID, userID).First(&loan).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Loan not found"})
return
}
// Delete loan
if err := database.DB.Delete(&loan).Error; err != nil {
log.Printf("Error deleting loan ID %d for user %d: %v", loanID, userID, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete loan"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Loan deleted successfully"})
}
// CreateLoanPayment creates a new payment for a loan
func (h *LoanHandler) CreateLoanPayment(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 loan ID from URL parameter
loanID, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid loan ID format"})
return
}
// Check if the loan exists and belongs to the user
var loan models.Loan
if err := database.DB.Where("id = ? AND user_id = ?", loanID, userObj.ID).First(&loan).Error; err != nil {
if err == gorm.ErrRecordNotFound {
c.JSON(http.StatusNotFound, gin.H{"error": "Loan not found"})
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch loan"})
}
return
}
// Define a struct to bind the request JSON
var input struct {
Amount int64 `json:"amount" binding:"required"`
PaymentDate string `json:"paymentDate" binding:"required"`
Principal int64 `json:"principal"`
Interest int64 `json:"interest"`
TransactionID *uint `json:"transactionId"`
Notes string `json:"notes"`
}
// Bind JSON to struct
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Parse date
paymentDate, err := time.Parse("2006-01-02", input.PaymentDate)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid payment date format"})
return
}
// Create payment object
payment := models.LoanPayment{
UserID: userObj.ID,
LoanID: uint(loanID),
Amount: input.Amount,
PaymentDate: paymentDate,
Principal: input.Principal,
Interest: input.Interest,
TransactionID: input.TransactionID,
Notes: input.Notes,
}
// Save to database in a transaction
tx := database.DB.Begin()
if err := tx.Create(&payment).Error; err != nil {
tx.Rollback()
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create payment"})
return
}
// Update loan balance
loan.CurrentBalance -= input.Principal
if err := tx.Save(&loan).Error; err != nil {
tx.Rollback()
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update loan balance"})
return
}
tx.Commit()
c.JSON(http.StatusCreated, gin.H{"payment": payment, "updatedLoanBalance": loan.CurrentBalance})
}
// GetLoanPayments returns all payments for a specific loan
func (h *LoanHandler) GetLoanPayments(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 loan ID from URL parameter
loanID, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid loan ID format"})
return
}
// Check if the loan exists and belongs to the user
var loan models.Loan
if err := database.DB.Where("id = ? AND user_id = ?", loanID, userObj.ID).First(&loan).Error; err != nil {
if err == gorm.ErrRecordNotFound {
c.JSON(http.StatusNotFound, gin.H{"error": "Loan not found"})
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch loan"})
}
return
}
// Fetch all payments for the loan
var payments []models.LoanPayment
if err := database.DB.Where("loan_id = ?", loanID).Order("payment_date DESC").Find(&payments).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch loan payments"})
return
}
c.JSON(http.StatusOK, gin.H{"payments": payments})
}
// DeleteLoanPayment deletes a payment for a loan
func (h *LoanHandler) DeleteLoanPayment(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 payment ID from URL parameter
paymentID, err := strconv.ParseUint(c.Param("paymentId"), 10, 32)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid payment ID format"})
return
}
// Check if the payment exists and belongs to the user
var payment models.LoanPayment
if err := database.DB.Where("id = ? AND user_id = ?", paymentID, userObj.ID).First(&payment).Error; err != nil {
if err == gorm.ErrRecordNotFound {
c.JSON(http.StatusNotFound, gin.H{"error": "Payment not found"})
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch payment"})
}
return
}
// Get the loan to update its balance
var loan models.Loan
if err := database.DB.First(&loan, payment.LoanID).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch related loan"})
return
}
// Update loan and delete payment in a transaction
tx := database.DB.Begin()
// Reverse the principal payment (add it back to the loan balance)
loan.CurrentBalance += payment.Principal
if err := tx.Save(&loan).Error; err != nil {
tx.Rollback()
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update loan balance"})
return
}
// Delete the payment
if err := tx.Delete(&payment).Error; err != nil {
tx.Rollback()
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete payment"})
return
}
tx.Commit()
c.JSON(http.StatusOK, gin.H{"message": "Payment deleted successfully", "updatedLoanBalance": loan.CurrentBalance})
}
// GetLoanPaymentSchedule generates an estimated payment schedule for a loan
func (h *LoanHandler) GetLoanPaymentSchedule(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 loan ID from URL parameter
loanID, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid loan ID format"})
return
}
// Check if the loan exists and belongs to the user
var loan models.Loan
if err := database.DB.Where("id = ? AND user_id = ?", loanID, userObj.ID).First(&loan).Error; err != nil {
if err == gorm.ErrRecordNotFound {
c.JSON(http.StatusNotFound, gin.H{"error": "Loan not found"})
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch loan"})
}
return
}
// Parse payment frequency parameter (defaults to monthly)
frequency := c.DefaultQuery("frequency", "monthly")
if frequency != "monthly" && frequency != "biweekly" && frequency != "weekly" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid frequency. Allowed values: monthly, biweekly, weekly"})
return
}
// Calculate remaining months (approximately)
now := time.Now()
remainingMonths := int(loan.EndDate.Sub(now).Hours() / 24 / 30)
if remainingMonths <= 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "Loan end date is in the past"})
return
}
// Calculate number of payments based on frequency
paymentsCount := remainingMonths
var intervalDays int
if frequency == "weekly" {
paymentsCount = remainingMonths * 4 // ~4 weeks per month
intervalDays = 7
} else if frequency == "biweekly" {
paymentsCount = remainingMonths * 2 // ~2 bi-weeks per month
intervalDays = 14
} else {
// Monthly
intervalDays = 30
}
// Simple amortization calculation
rate := loan.InterestRate / 12 / 100 // Monthly interest rate
if frequency == "weekly" {
rate = loan.InterestRate / 52 / 100
} else if frequency == "biweekly" {
rate = loan.InterestRate / 26 / 100
}
// For zero interest loans
var paymentAmount int64
if loan.InterestRate <= 0 {
paymentAmount = loan.CurrentBalance / int64(paymentsCount)
} else {
// Formula: PMT = P * (r * (1+r)^n) / ((1+r)^n - 1)
// Where PMT = payment, P = principal, r = rate per period, n = number of periods
// Simplified calculation (not exact but good approximation)
totalWithInterest := float64(loan.CurrentBalance) * (1 + float64(paymentsCount)*rate)
paymentAmount = int64(totalWithInterest) / int64(paymentsCount)
}
// Generate payment schedule
var schedule []map[string]interface{}
balance := loan.CurrentBalance
currentDate := now
for i := 0; i < paymentsCount && balance > 0; i++ {
// Calculate interest for this period
interestPayment := int64(float64(balance) * rate)
principalPayment := paymentAmount - interestPayment
// Adjust last payment if needed
if principalPayment > balance {
principalPayment = balance
paymentAmount = principalPayment + interestPayment
}
// Update remaining balance
balance -= principalPayment
// Create payment entry
payment := map[string]interface{}{
"paymentNumber": i + 1,
"date": currentDate.Format("2006-01-02"),
"totalPayment": paymentAmount,
"principalPayment": principalPayment,
"interestPayment": interestPayment,
"remainingBalance": balance,
}
schedule = append(schedule, payment)
// Increment date based on interval
currentDate = currentDate.AddDate(0, 0, intervalDays)
}
c.JSON(http.StatusOK, gin.H{
"loanDetails": loan,
"schedule": schedule,
"summary": map[string]interface{}{
"totalPayments": len(schedule),
"frequency": frequency,
"estimatedPayoffDate": schedule[len(schedule)-1]["date"],
"totalInterestPaid": sumInterest(schedule),
},
})
}
// Helper function to sum up total interest in a payment schedule
func sumInterest(schedule []map[string]interface{}) int64 {
var total int64
for _, payment := range schedule {
total += payment["interestPayment"].(int64)
}
return total
}
|