164 lines
4.3 KiB
Go
Raw Normal View History

2024-07-28 13:00:01 +07:00
package entity
import (
"github.com/google/uuid"
"time"
)
type License struct {
2024-07-30 00:55:20 +07:00
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 {
2024-07-29 22:30:29 +07:00
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"`
2024-08-02 01:21:21 +07:00
DaysToExpire int64 `gorm:"type:bigint"`
2024-07-28 13:00:01 +07:00
}
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
}
}
2024-08-02 00:51:54 +07:00
type PartnerLicense struct {
PartnerID int64 `json:"partner_id"`
LicenseStatus string `json:"license_status"`
DaysToExpire int64 `json:"days_to_expire"`
}
func (l *LicenseDB) ToPartnerLicense() PartnerLicense {
2024-08-23 00:27:42 +07:00
// Define the GMT+7 timezone
location, err := time.LoadLocation("Asia/Jakarta")
if err != nil {
// Handle the error appropriately, but for simplicity, we'll default to UTC
location = time.FixedZone("GMT+7", 7*60*60)
}
2024-08-02 00:51:54 +07:00
2024-08-23 00:27:42 +07:00
// Reinterpret StartDate as GMT+7 without changing the actual time values
startDateInGMT7 := time.Date(
l.StartDate.Year(),
l.StartDate.Month(),
l.StartDate.Day(),
l.StartDate.Hour(),
l.StartDate.Minute(),
l.StartDate.Second(),
l.StartDate.Nanosecond(),
location,
)
// Convert EndDate similarly, if needed
endDateInGMT7 := time.Date(
l.EndDate.Year(),
l.EndDate.Month(),
l.EndDate.Day(),
l.EndDate.Hour(),
l.EndDate.Minute(),
l.EndDate.Second(),
l.EndDate.Nanosecond(),
location,
)
now := time.Now().In(location)
startOfDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
daysToExpire := int64(endDateInGMT7.Sub(startOfDay).Hours() / 24)
2024-08-02 00:51:54 +07:00
var licenseStatus string
2024-08-23 00:27:42 +07:00
if startDateInGMT7.After(startOfDay) {
2024-08-15 23:13:33 +07:00
licenseStatus = "INACTIVE"
2024-08-23 00:27:42 +07:00
} else if startDateInGMT7.Equal(startOfDay) || (startDateInGMT7.Before(startOfDay) && endDateInGMT7.After(startOfDay)) {
if daysToExpire < 0 {
licenseStatus = "EXPIRED"
} else if daysToExpire <= 30 {
licenseStatus = "EXPIRING_SOON"
} else {
licenseStatus = "ACTIVE"
}
} else if endDateInGMT7.Before(startOfDay) {
2024-08-02 00:51:54 +07:00
licenseStatus = "EXPIRED"
} else {
licenseStatus = "ACTIVE"
}
return PartnerLicense{
PartnerID: l.PartnerID,
DaysToExpire: daysToExpire,
LicenseStatus: licenseStatus,
}
}