package order import ( "enaklo-pos-be/internal/common/logger" "enaklo-pos-be/internal/common/mycontext" "enaklo-pos-be/internal/constants" "enaklo-pos-be/internal/entity" "fmt" "go.uber.org/zap" ) func (s *orderSvc) ExecuteOrderInquiry(ctx mycontext.Context, token string, paymentMethod, paymentProvider string, inprogressOrderID int64) (*entity.OrderResponse, error) { inquiry, err := s.validateInquiry(ctx, token) if err != nil { return nil, err } order := inquiry.ToOrder(paymentMethod, paymentProvider) order.InProgressOrderID = inprogressOrderID savedOrder, err := s.repo.Create(ctx, order) if err != nil { logger.ContextLogger(ctx).Error("failed to create order", zap.Error(err)) return nil, err } err = s.processPostOrderActions(ctx, savedOrder, inquiry.ID, paymentMethod) if err != nil { logger.ContextLogger(ctx).Warn("some post-order actions failed", zap.Error(err)) } return &entity.OrderResponse{ Order: savedOrder, }, nil } func (s *orderSvc) processPostOrderActions( ctx mycontext.Context, order *entity.Order, inquiryID string, paymentMethod string, ) error { err := s.repo.UpdateInquiryStatus(ctx, inquiryID, constants.StatusExecuted) if err != nil { logger.ContextLogger(ctx).Error("error when updating inquiry status", zap.Error(err)) } trx, err := s.createTransaction(ctx, order, paymentMethod) if err != nil { logger.ContextLogger(ctx).Error("error when creating transaction", zap.Error(err)) } if order.CustomerID != nil && *order.CustomerID > 0 { err = s.addCustomerPoints(ctx, *order.CustomerID, int(order.Total/1000), fmt.Sprintf("TRX #%s", trx.ID)) if err != nil { logger.ContextLogger(ctx).Error("error when adding points", zap.Error(err)) } } s.sendTransactionReceipt(ctx, order, trx, "CASH") return nil } func (s *orderSvc) createTransaction(ctx mycontext.Context, order *entity.Order, paymentMethod string) (*entity.Transaction, error) { transaction := &entity.Transaction{ ID: constants.GenerateUUID(), OrderID: order.ID, Amount: order.Total, PaymentMethod: paymentMethod, Status: "SUCCESS", CreatedAt: constants.TimeNow(), PartnerID: order.PartnerID, TransactionType: "TRANSACTION", } _, err := s.transaction.Create(ctx, transaction) 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) sendTransactionReceipt(ctx mycontext.Context, order *entity.Order, transaction *entity.Transaction, paymentMethod string) error { if order.CustomerID == nil || *order.CustomerID == 0 { return nil } customer, err := s.customer.GetCustomer(ctx, *order.CustomerID) if err != nil { logger.ContextLogger(ctx).Error("error getting customer details", zap.Error(err)) return err } branchName := "Bakso 343 Rawamangun" var productIDs []int64 productIDMap := make(map[int64]bool) for _, item := range order.OrderItems { if item.ItemID > 0 && !productIDMap[item.ItemID] { productIDs = append(productIDs, item.ItemID) productIDMap[item.ItemID] = true } } productMap := make(map[int64]*entity.Product) if len(productIDs) > 0 { products, err := s.product.GetProductsByIDs(ctx, productIDs, order.PartnerID) if err != nil { logger.ContextLogger(ctx).Error("error fetching products", zap.Error(err)) } else { for _, product := range products { productMap[product.ID] = product } } } var itemsData []map[string]string for _, item := range order.OrderItems { itemName := "Item" if product, exists := productMap[item.ItemID]; exists { itemName = product.Name } itemsData = append(itemsData, map[string]string{ "ItemName": itemName, "Quantity": fmt.Sprintf("%d", item.Quantity), "Price": fmt.Sprintf("Rp %s", formatCurrency(item.Price)), }) } transactionDate := transaction.CreatedAt.Format("02 January 2006 15:04") viewTransactionLink := fmt.Sprintf("https://enaklo.co.id/transaction/%s", transaction.ID) emailData := map[string]interface{}{ "UserName": customer.Name, "PointsName": "ELP", "PointsBalance": int(order.Total / 1000), "NewPoints": int(order.Total / 1000), "RedeemLink": "enaklo.co.id", "BranchName": branchName, "TransactionNumber": order.ID, "TransactionDate": transactionDate, "PaymentMethod": formatPaymentMethod(paymentMethod), "Items": itemsData, "TotalPayment": fmt.Sprintf("Rp %s", formatCurrency(order.Total)), "ViewTransactionLink": viewTransactionLink, "ExpiryDate": order.CreatedAt.Format("02 January 2006"), } return s.notification.SendEmailTransactional(ctx, entity.SendEmailNotificationParam{ Sender: "noreply@enaklo.co.id", Recipient: customer.Email, Subject: "Hore, kamu dapat poin!", TemplateName: "monthly_points", TemplatePath: "/templates/monthly_points.html", Data: emailData, }) } func formatCurrency(amount float64) string { return fmt.Sprintf("%.2f", amount) } func formatPaymentMethod(method string) string { methodMap := map[string]string{ "CASH": "Tunai", "QRIS": "QRIS", "CARD": "Kartu Kredit/Debit", } if displayName, exists := methodMap[method]; exists { return displayName } return method }