Update Voucher
This commit is contained in:
@@ -29,12 +29,17 @@ func (s *orderSvc) CreateOrderInquiry(ctx mycontext.Context,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
customerID, err := s.customer.ResolveCustomer(ctx, &entity.CustomerResolutionRequest{
|
||||
ID: req.CustomerID,
|
||||
Name: req.CustomerName,
|
||||
Email: req.CustomerEmail,
|
||||
PhoneNumber: req.CustomerPhoneNumber,
|
||||
})
|
||||
customerID := int64(0)
|
||||
|
||||
if req.CustomerID != nil {
|
||||
customer, err := s.customer.GetCustomer(ctx, *req.CustomerID)
|
||||
if err != nil {
|
||||
logger.ContextLogger(ctx).Error("customer is not found", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
customerID = customer.ID
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
logger.ContextLogger(ctx).Error("failed to resolve customer", zap.Error(err))
|
||||
return nil, err
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -116,16 +116,24 @@ type InProgressOrderRepository interface {
|
||||
GetListByPartnerID(ctx mycontext.Context, partnerID int64, limit, offset int) ([]*entity.InProgressOrder, error)
|
||||
}
|
||||
|
||||
type VoucherUndianRepo interface {
|
||||
GetActiveUndianEvents(ctx context.Context) ([]*entity.UndianEventDB, error)
|
||||
CreateUndianVouchers(ctx context.Context, vouchers []*entity.UndianVoucherDB) error
|
||||
GetNextVoucherSequence(ctx mycontext.Context) (int64, error)
|
||||
GetNextVoucherSequenceBatch(ctx mycontext.Context, count int) (int64, error)
|
||||
}
|
||||
|
||||
type orderSvc struct {
|
||||
repo Repository
|
||||
product ProductService
|
||||
customer CustomerService
|
||||
transaction TransactionService
|
||||
crypt CryptService
|
||||
cfg Config
|
||||
notification NotificationService
|
||||
partnerSetting PartnerSettings
|
||||
inprogressOrder InProgressOrderRepository
|
||||
repo Repository
|
||||
product ProductService
|
||||
customer CustomerService
|
||||
transaction TransactionService
|
||||
crypt CryptService
|
||||
cfg Config
|
||||
notification NotificationService
|
||||
partnerSetting PartnerSettings
|
||||
inprogressOrder InProgressOrderRepository
|
||||
voucherUndianRepo VoucherUndianRepo
|
||||
}
|
||||
|
||||
func New(
|
||||
@@ -137,15 +145,17 @@ func New(
|
||||
cfg Config,
|
||||
notification NotificationService,
|
||||
partnerSetting PartnerSettings,
|
||||
voucherUndianRepo VoucherUndianRepo,
|
||||
) Service {
|
||||
return &orderSvc{
|
||||
repo: repo,
|
||||
product: product,
|
||||
customer: customer,
|
||||
transaction: transaction,
|
||||
crypt: crypt,
|
||||
cfg: cfg,
|
||||
notification: notification,
|
||||
partnerSetting: partnerSetting,
|
||||
repo: repo,
|
||||
product: product,
|
||||
customer: customer,
|
||||
transaction: transaction,
|
||||
crypt: crypt,
|
||||
cfg: cfg,
|
||||
notification: notification,
|
||||
partnerSetting: partnerSetting,
|
||||
voucherUndianRepo: voucherUndianRepo,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user