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 { // 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) } // 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) var licenseStatus string if startDateInGMT7.After(startOfDay) { licenseStatus = "INACTIVE" } 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) { licenseStatus = "EXPIRED" } else { licenseStatus = "ACTIVE" } return PartnerLicense{ PartnerID: l.PartnerID, DaysToExpire: daysToExpire, LicenseStatus: licenseStatus, } }