Update Voucher

This commit is contained in:
aditya.siregar
2025-06-06 14:48:09 +07:00
parent b5d6f7ff5b
commit 54144b2eba
7 changed files with 641 additions and 29 deletions
+2 -2
View File
@@ -59,7 +59,7 @@ func NewServiceManagerImpl(cfg *config.Config, repo *repository.RepoManagerImpl)
productSvcV2 := productSvc.New(repo.ProductRepo)
partnerSettings := partner_settings.NewPartnerSettingsService(repo.PartnerSetting)
orderService := orderSvc.New(repo.OrderRepo, productSvcV2, custSvcV2, repo.TransactionRepo, repo.Crypto, &cfg.Order, repo.EmailService, partnerSettings)
orderService := orderSvc.New(repo.OrderRepo, productSvcV2, custSvcV2, repo.TransactionRepo, repo.Crypto, &cfg.Order, repo.EmailService, partnerSettings, repo.UndianRepository)
inprogressOrder := inprogress_order.NewInProgressOrderService(repo.OrderRepo, orderService, productSvcV2)
return &ServiceManagerImpl{
AuthSvc: auth.New(repo.Auth, repo.Crypto, repo.User, repo.EmailService, cfg.Email, repo.Trx, repo.License),
@@ -75,7 +75,7 @@ func NewServiceManagerImpl(cfg *config.Config, repo *repository.RepoManagerImpl)
Transaction: transaction.New(repo.Transaction, repo.Wallet, repo.Trx),
Balance: balance.NewBalanceService(repo.Wallet, repo.Trx, repo.Crypto, &cfg.Withdraw, repo.Transaction),
DiscoverService: discovery.NewDiscoveryService(repo.Site, cfg.Discovery, repo.Product),
OrderV2Svc: orderSvc.New(repo.OrderRepo, productSvcV2, custSvcV2, repo.TransactionRepo, repo.Crypto, &cfg.Order, repo.EmailService, partnerSettings),
OrderV2Svc: orderSvc.New(repo.OrderRepo, productSvcV2, custSvcV2, repo.TransactionRepo, repo.Crypto, &cfg.Order, repo.EmailService, partnerSettings, repo.UndianRepository),
MemberRegistrationSvc: member.NewMemberRegistrationService(repo.MemberRepository, repo.EmailService, custSvcV2, repo.Crypto),
CustomerV2Svc: custSvcV2,
InProgressSvc: inprogressOrder,
@@ -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
+60 -4
View File
@@ -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 {
+27 -17
View File
@@ -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,
}
}