Add License Integration

This commit is contained in:
aditya.siregar 2024-08-02 00:51:54 +07:00
parent 5e26402c10
commit c1ae2fd14a
9 changed files with 92 additions and 29 deletions

View File

@ -83,18 +83,19 @@ func (UserDB) TableName() string {
return "users" return "users"
} }
func (u *UserDB) ToUserAuthenticate(signedToken string) *AuthenticateUser { func (u *UserDB) ToUserAuthenticate(signedToken string, license PartnerLicense) *AuthenticateUser {
return &AuthenticateUser{ return &AuthenticateUser{
Token: signedToken, Token: signedToken,
Name: u.Name, Name: u.Name,
RoleID: role.Role(u.RoleID), RoleID: role.Role(u.RoleID),
RoleName: u.RoleName, RoleName: u.RoleName,
PartnerID: u.PartnerID, PartnerID: u.PartnerID,
PartnerName: u.PartnerName, PartnerName: u.PartnerName,
PartnerStatus: u.PartnerStatus, PartnerStatus: u.PartnerStatus,
SiteID: u.SiteID, SiteID: u.SiteID,
SiteName: u.SiteName, SiteName: u.SiteName,
ResetPassword: u.ResetPassword, ResetPassword: u.ResetPassword,
PartnerLicense: license,
} }
} }

View File

@ -93,3 +93,29 @@ func (o *LicenseDB) ToUpdatedLicense(updatedBy int64, req License) {
o.SerialNumber = 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 daysToExpire < 0 {
licenseStatus = "EXPIRED"
} else if daysToExpire <= 30 {
licenseStatus = "EXPIRING_SOON"
} else {
licenseStatus = "ACTIVE"
}
return PartnerLicense{
PartnerID: l.PartnerID,
DaysToExpire: daysToExpire,
LicenseStatus: licenseStatus,
}
}

View File

@ -29,16 +29,17 @@ type User struct {
} }
type AuthenticateUser struct { type AuthenticateUser struct {
Token string Token string
Name string Name string
RoleID role.Role RoleID role.Role
RoleName string RoleName string
PartnerID *int64 PartnerID *int64
PartnerName string PartnerName string
PartnerStatus string PartnerStatus string
SiteID *int64 SiteID *int64
SiteName string SiteName string
ResetPassword bool ResetPassword bool
PartnerLicense PartnerLicense
} }
type UserRoleDB struct { type UserRoleDB struct {

View File

@ -82,6 +82,10 @@ func (h *AuthHandler) AuthLogin(c *gin.Context) {
}, },
Site: site, Site: site,
ResetPassword: authUser.ResetPassword, ResetPassword: authUser.ResetPassword,
PartnerLicense: &response.PartnerLicense{
DaysToExpire: authUser.PartnerLicense.DaysToExpire,
Status: authUser.PartnerLicense.LicenseStatus,
},
} }
c.JSON(http.StatusOK, response.BaseResponse{ c.JSON(http.StatusOK, response.BaseResponse{

View File

@ -1,15 +1,21 @@
package response package response
type LoginResponse struct { type LoginResponse struct {
Token string `json:"token"` Token string `json:"token"`
Name string `json:"name"` Name string `json:"name"`
Role Role `json:"role"` Role Role `json:"role"`
Partner *Partner `json:"partner"` Partner *Partner `json:"partner"`
Site *SiteName `json:"site,omitempty"` Site *SiteName `json:"site,omitempty"`
ResetPassword bool `json:"reset_password"` ResetPassword bool `json:"reset_password"`
PartnerLicense *PartnerLicense `json:"partner_license,omitempty"`
} }
type Role struct { type Role struct {
ID int64 `json:"id"` ID int64 `json:"id"`
Role string `json:"role_name"` Role string `json:"role_name"`
} }
type PartnerLicense struct {
DaysToExpire int64 `json:"days_to_expire,omitempty"`
Status string `json:"status,omitempty"`
}

View File

@ -84,3 +84,14 @@ func (r *LicenseRepository) GetAll(ctx context.Context, limit, offset int, statu
return licenses, total, nil return licenses, total, nil
} }
func (r *LicenseRepository) FindByPartnerIDMaxEndDate(ctx context.Context, partnerID *int64) (*entity.LicenseDB, error) {
var licenseDB entity.LicenseDB
if err := r.db.WithContext(ctx).
Where("partner_id = ?", partnerID).
Order("end_date DESC").
First(&licenseDB).Error; err != nil {
return nil, err
}
return &licenseDB, nil
}

View File

@ -205,6 +205,7 @@ type License interface {
Update(ctx context.Context, license *entity.LicenseDB) (*entity.LicenseDB, error) Update(ctx context.Context, license *entity.LicenseDB) (*entity.LicenseDB, error)
FindByID(ctx context.Context, id string) (*entity.LicenseDB, error) FindByID(ctx context.Context, id string) (*entity.LicenseDB, error)
GetAll(ctx context.Context, limit, offset int, statusFilter string) ([]*entity.LicenseGetAll, int64, error) GetAll(ctx context.Context, limit, offset int, statusFilter string) ([]*entity.LicenseGetAll, int64, error)
FindByPartnerIDMaxEndDate(ctx context.Context, partnerID *int64) (*entity.LicenseDB, error)
} }
type TransactionRepository interface { type TransactionRepository interface {

View File

@ -21,11 +21,13 @@ type AuthServiceImpl struct {
emailSvc repository.EmailService emailSvc repository.EmailService
emailCfg config.Email emailCfg config.Email
trxRepo repository.TransactionManager trxRepo repository.TransactionManager
license repository.License
} }
func New(authRepo repository.Auth, func New(authRepo repository.Auth,
crypto repository.Crypto, user repository.User, emailSvc repository.EmailService, crypto repository.Crypto, user repository.User, emailSvc repository.EmailService,
emailCfg config.Email, trxRepo repository.TransactionManager, emailCfg config.Email, trxRepo repository.TransactionManager,
license repository.License,
) *AuthServiceImpl { ) *AuthServiceImpl {
return &AuthServiceImpl{ return &AuthServiceImpl{
authRepo: authRepo, authRepo: authRepo,
@ -34,6 +36,7 @@ func New(authRepo repository.Auth,
emailSvc: emailSvc, emailSvc: emailSvc,
emailCfg: emailCfg, emailCfg: emailCfg,
trxRepo: trxRepo, trxRepo: trxRepo,
license: license,
} }
} }
@ -57,8 +60,18 @@ func (u *AuthServiceImpl) AuthenticateUser(ctx context.Context, email, password
if err != nil { if err != nil {
return nil, err return nil, err
} }
var licensePartner entity.PartnerLicense
return user.ToUserAuthenticate(signedToken), nil if user.PartnerID != nil {
parterLicense, err := u.license.FindByPartnerIDMaxEndDate(ctx, user.PartnerID)
if err != nil {
logger.ContextLogger(ctx).Error("error when get user license", zap.Error(err))
return nil, errors.ErrorInternalServer
}
licensePartner = parterLicense.ToPartnerLicense()
}
return user.ToUserAuthenticate(signedToken, licensePartner), nil
} }
func (u *AuthServiceImpl) SendPasswordResetLink(ctx context.Context, email string) error { func (u *AuthServiceImpl) SendPasswordResetLink(ctx context.Context, email string) error {

View File

@ -42,7 +42,7 @@ type ServiceManagerImpl struct {
func NewServiceManagerImpl(cfg *config.Config, repo *repository.RepoManagerImpl) *ServiceManagerImpl { func NewServiceManagerImpl(cfg *config.Config, repo *repository.RepoManagerImpl) *ServiceManagerImpl {
return &ServiceManagerImpl{ return &ServiceManagerImpl{
AuthSvc: auth.New(repo.Auth, repo.Crypto, repo.User, repo.EmailService, cfg.Email, repo.Trx), AuthSvc: auth.New(repo.Auth, repo.Crypto, repo.User, repo.EmailService, cfg.Email, repo.Trx, repo.License),
EventSvc: event.NewEventService(repo.Event), EventSvc: event.NewEventService(repo.Event),
UserSvc: users.NewUserService(repo.User, repo.Branch), UserSvc: users.NewUserService(repo.User, repo.Branch),
BranchSvc: branch.NewBranchService(repo.Branch), BranchSvc: branch.NewBranchService(repo.Branch),