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

@@ -21,7 +21,7 @@ import (
func GetContracts(status []string, companyName string, companyAddress string,
companyEmail string, companyPhone string, startTime *time.Time, endTime *time.Time,
contractName string, deviceIDs []int64, contractIDs []int64, dateCreated *time.Time, limit, offset int) ([]models.Contract, int64, int, error) {
contractName string, deviceIDs []int64, contractIDs []int64, dateCreated *time.Time, company, limit, offset int) ([]models.Contract, int64, int, error) {
var contracts []models.Contract
db := shared.GetDb()

View File

@@ -15,7 +15,7 @@ import (
"gitlab.com/pactual1/backend/shared"
)
func GetDevicesForContract(contractID uint64) ([]models.Device, int, error) {
func GetDevicesForContract(contractID uint64, companyID int) ([]models.Device, int, error) {
// Fetch the contract from the database
var contract models.Contract
if err := shared.GetDb().Where("id = ?", contractID).First(&contract).Error; err != nil {

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
}

View File

@@ -3,10 +3,12 @@ package user
import (
"errors"
"fmt"
"strconv"
"time"
"github.com/golang-jwt/jwt"
"github.com/jinzhu/gorm"
"gitlab.com/pactual1/backend/config"
"gitlab.com/pactual1/backend/models"
"gitlab.com/pactual1/backend/shared"
"golang.org/x/crypto/bcrypt"
@@ -50,7 +52,7 @@ func CheckPassword(hashedPassword, password string) bool {
return err == nil
}
func CreateSessionToken(userID uint) (string, error) {
func CreateSessionToken(userID, companyID uint) (string, error) {
// Generate JWT token
tokenString, err := CreateJWTToken(userID)
if err != nil {
@@ -59,9 +61,10 @@ func CreateSessionToken(userID uint) (string, error) {
// Create and save the session token in the database
sessionToken := models.SessionToken{
UserID: userID,
Token: tokenString,
IsActive: true,
UserID: userID,
Token: tokenString,
CompanyID: companyID,
IsActive: true,
}
if result := shared.GetDb().Create(&sessionToken); result.Error != nil {
return "", result.Error
@@ -84,10 +87,15 @@ func IncrementLoginAttempts(user models.User) {
shared.GetDb().Save(&user)
}
var jwtKey = []byte("MDQsCiJwYWNrZXRWZXJzaW9uIjogMSwKImhhcm")
func CreateJWTToken(userID uint) (string, error) {
expirationTime := time.Now().Add(24 * time.Hour)
var jwtKey = []byte(config.AppConfig.Service.JwtSecretKey)
expiryHours, err := strconv.Atoi(config.AppConfig.Service.JwtSecretKeyExpiryHours)
if err != nil {
return "", err
}
expirationTime := time.Now().Add(time.Duration(expiryHours) * time.Hour)
claims := &jwt.StandardClaims{
Subject: fmt.Sprint(userID),
ExpiresAt: expirationTime.Unix(),