Skip to content

Commit

Permalink
use condition in deduct
Browse files Browse the repository at this point in the history
  • Loading branch information
idprm committed Oct 18, 2024
1 parent 758d9de commit d49d84b
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 255 deletions.
20 changes: 20 additions & 0 deletions internal/domain/repository/subscription_respository.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type ISubscriptionRepository interface {
Count(int, string) (int64, error)
CountActive(int, string) (int64, error)
CountActiveByCategory(string, string) (int64, error)
CountRenewal(int, string) (int64, error)
CountRetry(int, string) (int64, error)
GetAllPaginate(*entity.Pagination) (*entity.Pagination, error)
GetByCategory(string, string) (*entity.Subscription, error)
Get(int, string) (*entity.Subscription, error)
Expand Down Expand Up @@ -66,6 +68,24 @@ func (r *SubscriptionRepository) CountActiveByCategory(category string, msisdn s
return count, nil
}

func (r *SubscriptionRepository) CountRenewal(serviceId int, msisdn string) (int64, error) {
var count int64
err := r.db.Model(&entity.Subscription{}).Where("service_id = ?", serviceId).Where("msisdn = ?", msisdn).Where("is_active = true AND (UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(renewal_at)) / 3600 > 0").Count(&count).Error
if err != nil {
return count, err
}
return count, nil
}

func (r *SubscriptionRepository) CountRetry(serviceId int, msisdn string) (int64, error) {
var count int64
err := r.db.Model(&entity.Subscription{}).Where("service_id = ?", serviceId).Where("msisdn = ?", msisdn).Where("is_active = true AND is_retry = true AND (UNIX_TIMESTAMP(NOW() + INTERVAL 1 DAY) - UNIX_TIMESTAMP(renewal_at)) / 3600 > 0").Count(&count).Error
if err != nil {
return count, err
}
return count, nil
}

func (r *SubscriptionRepository) GetAllPaginate(pagination *entity.Pagination) (*entity.Pagination, error) {
var subscriptions []*entity.Subscription
err := r.db.Scopes(Paginate(subscriptions, pagination, r.db)).Find(&subscriptions).Error
Expand Down
224 changes: 114 additions & 110 deletions internal/handler/renewal_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,86 +48,122 @@ func NewRenewalHandler(
}

func (h *RenewalHandler) Dailypush() {
// check is active
if h.subscriptionService.IsActiveSubscription(h.sub.GetServiceId(), h.sub.GetMsisdn()) {
// check is renewal
if h.subscriptionService.IsRenewal(h.sub.GetServiceId(), h.sub.GetMsisdn()) {
trxId := utils.GenerateTrxId()

trxId := utils.GenerateTrxId()

sub, err := h.subscriptionService.Get(h.sub.GetServiceId(), h.sub.GetMsisdn())
if err != nil {
log.Println(err.Error())
}

service, err := h.serviceService.GetById(h.sub.GetServiceId())
if err != nil {
log.Println(err.Error())
}

summary := &entity.Summary{
ServiceID: service.GetId(),
CreatedAt: time.Now(),
}

t := telco.NewTelco(h.logger, service, h.sub, trxId)

respBal, err := t.QueryProfileAndBal()
if err != nil {
log.Println(err.Error())
}

var respBalance *model.QueryProfileAndBalResponse
xml.Unmarshal(respBal, &respBalance)
sub, err := h.subscriptionService.Get(h.sub.GetServiceId(), h.sub.GetMsisdn())
if err != nil {
log.Println(err.Error())
}

if respBalance.IsEnoughBalance(service) {
// if balance
respDFee, err := t.DeductFee()
service, err := h.serviceService.GetById(h.sub.GetServiceId())
if err != nil {
log.Println(err.Error())
}

var respDeduct *model.DeductResponse
xml.Unmarshal(respDFee, &respDeduct)
summary := &entity.Summary{
ServiceID: service.GetId(),
CreatedAt: time.Now(),
}

if respDeduct.IsSuccess() {
h.subscriptionService.Update(
&entity.Subscription{
ServiceID: service.GetId(),
Msisdn: h.sub.GetMsisdn(),
LatestTrxId: trxId,
LatestSubject: SUBJECT_RENEWAL,
LatestStatus: STATUS_SUCCESS,
TotalAmount: service.GetPrice(),
RenewalAt: time.Now().AddDate(0, 0, service.GetRenewalDay()),
ChargeAt: time.Now(),
TotalSuccess: sub.TotalSuccess + 1,
IsRetry: false,
TotalRenewal: sub.TotalRenewal + 1,
TotalAmountRenewal: sub.TotalAmountRenewal + service.GetPrice(),
BeforeBalance: respDeduct.GetBeforeBalanceToFloat(),
AfterBalance: respDeduct.GetAfterBalanceToFloat(),
LatestPayload: string(respDFee),
},
)
// is_retry set to false
h.subscriptionService.UpdateNotRetry(sub)
t := telco.NewTelco(h.logger, service, h.sub, trxId)

h.transactionService.Save(
&entity.Transaction{
ServiceID: service.GetId(),
Msisdn: h.sub.GetMsisdn(),
Keyword: sub.GetLatestKeyword(),
Amount: service.GetPrice(),
Status: STATUS_SUCCESS,
StatusCode: respDeduct.GetAcctResCode(),
StatusDetail: respDeduct.GetAcctResName(),
Subject: SUBJECT_RENEWAL,
Payload: string(respDFee),
},
)
// setter summary
summary.SetTotalChargeSuccess(1)
respBal, err := t.QueryProfileAndBal()
if err != nil {
log.Println(err.Error())
}

if respDeduct.IsFailed() {
var respBalance *model.QueryProfileAndBalResponse
xml.Unmarshal(respBal, &respBalance)

if respBalance.IsEnoughBalance(service) {
// if balance
respDFee, err := t.DeductFee()
if err != nil {
log.Println(err.Error())
}

var respDeduct *model.DeductResponse
xml.Unmarshal(respDFee, &respDeduct)

if respDeduct.IsSuccess() {
h.subscriptionService.Update(
&entity.Subscription{
ServiceID: service.GetId(),
Msisdn: h.sub.GetMsisdn(),
LatestTrxId: trxId,
LatestSubject: SUBJECT_RENEWAL,
LatestStatus: STATUS_SUCCESS,
TotalAmount: service.GetPrice(),
RenewalAt: time.Now().AddDate(0, 0, service.GetRenewalDay()),
ChargeAt: time.Now(),
TotalSuccess: sub.TotalSuccess + 1,
IsRetry: false,
TotalRenewal: sub.TotalRenewal + 1,
TotalAmountRenewal: sub.TotalAmountRenewal + service.GetPrice(),
BeforeBalance: respDeduct.GetBeforeBalanceToFloat(),
AfterBalance: respDeduct.GetAfterBalanceToFloat(),
LatestPayload: string(respDFee),
},
)
// is_retry set to false
h.subscriptionService.UpdateNotRetry(sub)

h.transactionService.Save(
&entity.Transaction{
TrxId: trxId,
ServiceID: service.GetId(),
Msisdn: h.sub.GetMsisdn(),
Keyword: sub.GetLatestKeyword(),
Amount: service.GetPrice(),
Status: STATUS_SUCCESS,
StatusCode: respDeduct.GetAcctResCode(),
StatusDetail: respDeduct.GetAcctResName(),
Subject: SUBJECT_RENEWAL,
Payload: string(respDFee),
},
)
// setter summary
summary.SetTotalChargeSuccess(1)
}

if respDeduct.IsFailed() {
h.subscriptionService.Update(
&entity.Subscription{
ServiceID: service.GetId(),
Msisdn: h.sub.GetMsisdn(),
LatestTrxId: trxId,
LatestSubject: SUBJECT_RENEWAL,
LatestStatus: STATUS_FAILED,
RenewalAt: time.Now().AddDate(0, 0, 1),
RetryAt: time.Now(),
TotalFailed: sub.TotalFailed + 1,
IsRetry: true,
LatestPayload: string(respDFee),
},
)

h.transactionService.Save(
&entity.Transaction{
TrxId: trxId,
ServiceID: service.GetId(),
Msisdn: h.sub.GetMsisdn(),
Keyword: sub.GetLatestKeyword(),
Status: STATUS_FAILED,
StatusCode: respDeduct.GetFaultCode(),
StatusDetail: respDeduct.GetFaultString(),
Subject: SUBJECT_RENEWAL,
Payload: string(respDFee),
},
)

// setter summary
summary.SetTotalChargeFailed(1)
}
} else {
h.subscriptionService.Update(
&entity.Subscription{
ServiceID: service.GetId(),
Expand All @@ -139,7 +175,7 @@ func (h *RenewalHandler) Dailypush() {
RetryAt: time.Now(),
TotalFailed: sub.TotalFailed + 1,
IsRetry: true,
LatestPayload: string(respDFee),
LatestPayload: string(respBal),
},
)

Expand All @@ -150,54 +186,22 @@ func (h *RenewalHandler) Dailypush() {
Msisdn: h.sub.GetMsisdn(),
Keyword: sub.GetLatestKeyword(),
Status: STATUS_FAILED,
StatusCode: respDeduct.GetFaultCode(),
StatusDetail: respDeduct.GetFaultString(),
StatusCode: "",
StatusDetail: "INSUFF_BALANCE",
Subject: SUBJECT_RENEWAL,
Payload: string(respDFee),
Payload: string(respBal),
},
)

// setter summary
summary.SetTotalChargeFailed(1)
}
} else {
h.subscriptionService.Update(
&entity.Subscription{
ServiceID: service.GetId(),
Msisdn: h.sub.GetMsisdn(),
LatestTrxId: trxId,
LatestSubject: SUBJECT_RENEWAL,
LatestStatus: STATUS_FAILED,
RenewalAt: time.Now().AddDate(0, 0, 1),
RetryAt: time.Now(),
TotalFailed: sub.TotalFailed + 1,
IsRetry: true,
LatestPayload: string(respBal),
},
)

h.transactionService.Save(
&entity.Transaction{
TrxId: trxId,
ServiceID: service.GetId(),
Msisdn: h.sub.GetMsisdn(),
Keyword: sub.GetLatestKeyword(),
Status: STATUS_FAILED,
StatusCode: "",
StatusDetail: "INSUFF_BALANCE",
Subject: SUBJECT_RENEWAL,
Payload: string(respBal),
},
)

// setter summary
summary.SetTotalChargeFailed(1)
}

// setter renewal
summary.SetTotalRenewal(1)
// summary save
h.summaryService.Save(summary)
// setter renewal
summary.SetTotalRenewal(1)
// summary save
h.summaryService.Save(summary)

}
}
}
Loading

0 comments on commit d49d84b

Please sign in to comment.