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,7 +83,7 @@ func (UserDB) TableName() string {
return "users"
}
func (u *UserDB) ToUserAuthenticate(signedToken string) *AuthenticateUser {
func (u *UserDB) ToUserAuthenticate(signedToken string, license PartnerLicense) *AuthenticateUser {
return &AuthenticateUser{
Token: signedToken,
Name: u.Name,
@ -95,6 +95,7 @@ func (u *UserDB) ToUserAuthenticate(signedToken string) *AuthenticateUser {
SiteID: u.SiteID,
SiteName: u.SiteName,
ResetPassword: u.ResetPassword,
PartnerLicense: license,
}
}

View File

@ -93,3 +93,29 @@ func (o *LicenseDB) ToUpdatedLicense(updatedBy int64, req License) {
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

@ -39,6 +39,7 @@ type AuthenticateUser struct {
SiteID *int64
SiteName string
ResetPassword bool
PartnerLicense PartnerLicense
}
type UserRoleDB struct {

View File

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

View File

@ -7,9 +7,15 @@ type LoginResponse struct {
Partner *Partner `json:"partner"`
Site *SiteName `json:"site,omitempty"`
ResetPassword bool `json:"reset_password"`
PartnerLicense *PartnerLicense `json:"partner_license,omitempty"`
}
type Role struct {
ID int64 `json:"id"`
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
}
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)
FindByID(ctx context.Context, id string) (*entity.LicenseDB, 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 {

View File

@ -21,11 +21,13 @@ type AuthServiceImpl struct {
emailSvc repository.EmailService
emailCfg config.Email
trxRepo repository.TransactionManager
license repository.License
}
func New(authRepo repository.Auth,
crypto repository.Crypto, user repository.User, emailSvc repository.EmailService,
emailCfg config.Email, trxRepo repository.TransactionManager,
license repository.License,
) *AuthServiceImpl {
return &AuthServiceImpl{
authRepo: authRepo,
@ -34,6 +36,7 @@ func New(authRepo repository.Auth,
emailSvc: emailSvc,
emailCfg: emailCfg,
trxRepo: trxRepo,
license: license,
}
}
@ -57,8 +60,18 @@ func (u *AuthServiceImpl) AuthenticateUser(ctx context.Context, email, password
if err != nil {
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 {

View File

@ -42,7 +42,7 @@ type ServiceManagerImpl struct {
func NewServiceManagerImpl(cfg *config.Config, repo *repository.RepoManagerImpl) *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),
UserSvc: users.NewUserService(repo.User, repo.Branch),
BranchSvc: branch.NewBranchService(repo.Branch),