Update Voucher
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"enaklo-pos-be/internal/entity"
|
||||
"fmt"
|
||||
"go.uber.org/zap"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (s *orderSvc) ExecuteOrderInquiry(ctx mycontext.Context,
|
||||
@@ -52,13 +53,12 @@ func (s *orderSvc) processPostOrderActions(
|
||||
}
|
||||
|
||||
if order.CustomerID != nil && *order.CustomerID > 0 {
|
||||
err = s.addCustomerPoints(ctx, *order.CustomerID, int(order.Total/50000), fmt.Sprintf("TRX #%s", trx.ID))
|
||||
err = s.addCustomerVouchers(ctx, *order.CustomerID, int64(order.Total), trx.OrderID)
|
||||
if err != nil {
|
||||
logger.ContextLogger(ctx).Error("error when adding points", zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
s.sendTransactionReceipt(ctx, order, trx, "CASH")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -79,8 +79,64 @@ func (s *orderSvc) createTransaction(ctx mycontext.Context, order *entity.Order,
|
||||
return transaction, err
|
||||
}
|
||||
|
||||
func (s *orderSvc) addCustomerPoints(ctx mycontext.Context, customerID int64, points int, reference string) error {
|
||||
return s.customer.AddPoints(ctx, customerID, points, reference)
|
||||
func (s *orderSvc) addCustomerVouchers(ctx mycontext.Context, customerID int64, total int64, reference int64) error {
|
||||
undians, err := s.voucherUndianRepo.GetActiveUndianEvents(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
eligibleVoucher := []*entity.UndianVoucherDB{}
|
||||
totalVouchersNeeded := 0
|
||||
|
||||
for _, v := range undians {
|
||||
if total >= int64(v.MinimumPurchase) {
|
||||
voucherCount := int(total / int64(v.MinimumPurchase))
|
||||
totalVouchersNeeded += voucherCount
|
||||
}
|
||||
}
|
||||
|
||||
if totalVouchersNeeded == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
startSequence, err := s.voucherUndianRepo.GetNextVoucherSequenceBatch(ctx, totalVouchersNeeded)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
currentSequence := startSequence
|
||||
|
||||
for _, v := range undians {
|
||||
if total >= int64(v.MinimumPurchase) {
|
||||
voucherCount := int(total / int64(v.MinimumPurchase))
|
||||
|
||||
for i := 0; i < voucherCount; i++ {
|
||||
voucherCode := s.generateVoucherCode(v.ID, reference, currentSequence)
|
||||
|
||||
voucher := &entity.UndianVoucherDB{
|
||||
UndianEventID: v.ID,
|
||||
CustomerID: customerID,
|
||||
VoucherCode: voucherCode,
|
||||
VoucherNumber: &i,
|
||||
IsWinner: false,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
eligibleVoucher = append(eligibleVoucher, voucher)
|
||||
currentSequence++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return s.voucherUndianRepo.CreateUndianVouchers(ctx, eligibleVoucher)
|
||||
}
|
||||
|
||||
func (s *orderSvc) generateVoucherCode(eventID int64, reference int64, sequence int64) string {
|
||||
eventPart := eventID % 100 // Last 2 digits of event ID
|
||||
sequencePart := sequence % 100000 // Last 5 digits of sequence
|
||||
orderPart := reference % 1000 // Last 3 digits of order ID
|
||||
|
||||
return fmt.Sprintf("%02d%05d%03d", eventPart, sequencePart, orderPart)
|
||||
}
|
||||
|
||||
func (s *orderSvc) sendTransactionReceipt(ctx mycontext.Context, order *entity.Order, transaction *entity.Transaction, paymentMethod string) error {
|
||||
|
||||
Reference in New Issue
Block a user