2024-08-15 23:13:33 +07:00

125 lines
3.2 KiB
Go

package entity
import (
"github.com/google/uuid"
"time"
)
type License struct {
ID uuid.UUID `gorm:"type:uuid;primaryKey;default:uuid_generate_v4()"`
PartnerID int64 `gorm:"type:bigint;not null"`
Name string `gorm:"type:varchar(255);not null"`
StartDate time.Time `gorm:"type:date;not null"`
EndDate time.Time `gorm:"type:date;not null"`
RenewalDate *time.Time `gorm:"type:date"`
SerialNumber string `gorm:"type:varchar(255);unique;not null"`
CreatedBy int64 `gorm:"type:bigint;not null"`
UpdatedBy int64 `gorm:"type:bigint;not null"`
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
}
type LicenseGetAll struct {
ID uuid.UUID `gorm:"type:uuid;primaryKey;default:uuid_generate_v4()"`
PartnerID int64 `gorm:"type:bigint;not null"`
Name string `gorm:"type:varchar(255);not null"`
StartDate time.Time `gorm:"type:date;not null"`
EndDate time.Time `gorm:"type:date;not null"`
RenewalDate *time.Time `gorm:"type:date"`
SerialNumber string `gorm:"type:varchar(255);unique;not null"`
CreatedBy int64 `gorm:"type:bigint;not null"`
UpdatedBy int64 `gorm:"type:bigint;not null"`
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
PartnerName string `gorm:"type:varchar(255);not null"`
LicenseStatus string `gorm:"type:string(255);not null"`
CreatedByName string `gorm:"type:string(255);not null"`
DaysToExpire int64 `gorm:"type:bigint"`
}
func (License) TableName() string {
return "licenses"
}
type LicenseDB struct {
License
}
func (l *License) ToLicenseDB() *LicenseDB {
return &LicenseDB{
License: *l,
}
}
func (LicenseDB) TableName() string {
return "licenses"
}
func (e *LicenseDB) ToLicense() *License {
return &License{
ID: e.ID,
PartnerID: e.PartnerID,
Name: e.Name,
StartDate: e.StartDate,
EndDate: e.EndDate,
RenewalDate: e.RenewalDate,
SerialNumber: e.SerialNumber,
CreatedBy: e.CreatedBy,
UpdatedBy: e.UpdatedBy,
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
}
}
func (o *LicenseDB) ToUpdatedLicense(updatedBy int64, req License) {
o.UpdatedBy = updatedBy
if req.Name != "" {
o.Name = req.Name
}
if !req.StartDate.IsZero() {
o.StartDate = req.StartDate
}
if !req.EndDate.IsZero() {
o.EndDate = req.EndDate
}
if req.RenewalDate != nil {
o.RenewalDate = req.RenewalDate
}
if req.SerialNumber != "" {
o.SerialNumber = req.SerialNumber
}
}
type PartnerLicense struct {
PartnerID int64 `json:"partner_id"`
LicenseStatus string `json:"license_status"`
DaysToExpire int64 `json:"days_to_expire"`
}
func (l *LicenseDB) ToPartnerLicense() PartnerLicense {
now := time.Now()
daysToExpire := int64(l.EndDate.Sub(now).Hours() / 24)
var licenseStatus string
if now.Before(l.StartDate) {
licenseStatus = "INACTIVE"
} else if daysToExpire < 0 {
licenseStatus = "EXPIRED"
} else if daysToExpire <= 30 {
licenseStatus = "EXPIRING_SOON"
} else {
licenseStatus = "ACTIVE"
}
return PartnerLicense{
PartnerID: l.PartnerID,
DaysToExpire: daysToExpire,
LicenseStatus: licenseStatus,
}
}