Protected routes

This commit is contained in:
Nedim
2023-11-10 17:32:17 +01:00
parent 367b5d51f2
commit 99b9df5066
14 changed files with 172 additions and 100 deletions

View File

@@ -9,7 +9,7 @@ import (
"gitlab.com/pactual1/backend/shared"
)
func GetInvoices(buyerName string, sortBy string, limit int, offset int, ids []int64, status []string) ([]models.Invoice, int64, error) {
func GetInvoices(buyerName string, sortBy string, limit int, offset int, ids []int64, status []string, company int) ([]models.Invoice, int64, error) {
var invoices []models.Invoice
// Default sort by InvoiceDate DESC
@@ -36,7 +36,7 @@ func GetInvoices(buyerName string, sortBy string, limit int, offset int, ids []i
countDb = countDb.Where("id in (?)", ids)
}
// Added conditional for status search
// Added conditional for status search
if len(status) > 0 {
db = db.Where("status in (?)", status)
countDb = countDb.Where("status in (?)", status)
@@ -69,46 +69,46 @@ func GetInvoices(buyerName string, sortBy string, limit int, offset int, ids []i
}
func CountInvoicesByMultipleStatuses(companyID uint, startTime, endTime time.Time) (int64, int64, map[string]map[string]int64, error) {
var claimed, issued int64
monthlyCounts := make(map[string]map[string]int64)
var claimed, issued int64
monthlyCounts := make(map[string]map[string]int64)
// Iterate through each month within the specified date range
for dt := startTime; dt.Before(endTime); dt = dt.AddDate(0, 1, 0) {
monthEnd := dt.AddDate(0, 1, 0)
if monthEnd.After(endTime) {
monthEnd = endTime
}
// Iterate through each month within the specified date range
for dt := startTime; dt.Before(endTime); dt = dt.AddDate(0, 1, 0) {
monthEnd := dt.AddDate(0, 1, 0)
if monthEnd.After(endTime) {
monthEnd = endTime
}
// Initialize monthlyCounts for the current month
monthKey := dt.Format("2006-01")
monthlyCounts[monthKey] = map[string]int64{"insurance_claimed": 0, "invoice_issued": 0}
// Initialize monthlyCounts for the current month
monthKey := dt.Format("2006-01")
monthlyCounts[monthKey] = map[string]int64{"insurance_claimed": 0, "invoice_issued": 0}
// Query for 'insurance_claimed' invoices for the current month
var monthlyClaimed int64
err := shared.GetDb().Model(&models.Invoice{}).
Where("status = ? AND (buyer_id = ? OR seller_id = ?) AND invoice_date >= ? AND invoice_due_date <= ?", "insurance_claimed", companyID, companyID, dt, monthEnd).
Count(&monthlyClaimed).Error
if err != nil {
return 0, 0, nil, err
}
// Query for 'insurance_claimed' invoices for the current month
var monthlyClaimed int64
err := shared.GetDb().Model(&models.Invoice{}).
Where("status = ? AND (buyer_id = ? OR seller_id = ?) AND invoice_date >= ? AND invoice_due_date <= ?", "insurance_claimed", companyID, companyID, dt, monthEnd).
Count(&monthlyClaimed).Error
if err != nil {
return 0, 0, nil, err
}
// Query for 'invoice_issued' invoices for the current month
var monthlyIssued int64
err = shared.GetDb().Model(&models.Invoice{}).
Where("status = ? AND (buyer_id = ? OR seller_id = ?) AND invoice_date >= ? AND invoice_due_date <= ?", "invoice_issued", companyID, companyID, dt, monthEnd).
Count(&monthlyIssued).Error
if err != nil {
return 0, 0, nil, err
}
// Query for 'invoice_issued' invoices for the current month
var monthlyIssued int64
err = shared.GetDb().Model(&models.Invoice{}).
Where("status = ? AND (buyer_id = ? OR seller_id = ?) AND invoice_date >= ? AND invoice_due_date <= ?", "invoice_issued", companyID, companyID, dt, monthEnd).
Count(&monthlyIssued).Error
if err != nil {
return 0, 0, nil, err
}
// Update the counts for the current month
monthlyCounts[monthKey]["insurance_claimed"] = monthlyClaimed
monthlyCounts[monthKey]["invoice_issued"] = monthlyIssued
// Update the counts for the current month
monthlyCounts[monthKey]["insurance_claimed"] = monthlyClaimed
monthlyCounts[monthKey]["invoice_issued"] = monthlyIssued
// Update the total counts
claimed += monthlyClaimed
issued += monthlyIssued
}
// Update the total counts
claimed += monthlyClaimed
issued += monthlyIssued
}
return claimed, issued, monthlyCounts, nil
return claimed, issued, monthlyCounts, nil
}