Add Checkin
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
errors2 "furtuna-be/internal/common/errors"
|
||||
"furtuna-be/internal/common/logger"
|
||||
"furtuna-be/internal/common/mycontext"
|
||||
order2 "furtuna-be/internal/constants/order"
|
||||
@@ -121,6 +122,77 @@ func (s *OrderService) CreateOrder(ctx mycontext.Context, req *entity.OrderReque
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *OrderService) CheckInInquiry(ctx mycontext.Context, qrCode string, partnerID *int64) (*entity.CheckinResponse, error) {
|
||||
order, err := s.repo.FindByQRCode(ctx, qrCode)
|
||||
if err != nil {
|
||||
logger.ContextLogger(ctx).Error("error when getting order by QR code", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if order.PartnerID != *partnerID {
|
||||
return nil, errors2.ErrorBadRequest
|
||||
}
|
||||
|
||||
if order.TicketStatus == "USED" {
|
||||
return nil, errors2.ErrorTicketInvalidOrAlreadyUsed
|
||||
}
|
||||
|
||||
today := time.Now().Format("2006-01-02")
|
||||
visitDate := order.VisitDate.Format("2006-01-02")
|
||||
|
||||
if visitDate != today {
|
||||
return nil, errors2.ErrorTicketInvalidOrAlreadyUsed
|
||||
}
|
||||
|
||||
token, err := s.crypt.GenerateJWTOrder(order)
|
||||
if err != nil {
|
||||
logger.ContextLogger(ctx).Error("error when generate checkin token", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
orderResponse := &entity.CheckinResponse{
|
||||
Token: token,
|
||||
}
|
||||
|
||||
return orderResponse, nil
|
||||
}
|
||||
|
||||
func (s *OrderService) CheckInExecute(ctx mycontext.Context,
|
||||
token string, partnerID *int64) (*entity.CheckinExecute, error) {
|
||||
pID, orderID, err := s.crypt.ValidateJWTOrder(token)
|
||||
if err != nil {
|
||||
logger.ContextLogger(ctx).Error("error when validating JWT order", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if pID != *partnerID {
|
||||
return nil, errors2.ErrorBadRequest
|
||||
}
|
||||
|
||||
order, err := s.repo.FindByID(ctx, orderID)
|
||||
if err != nil {
|
||||
logger.ContextLogger(ctx).Error("error when getting order by ID", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp := &entity.CheckinExecute{
|
||||
Order: order,
|
||||
}
|
||||
|
||||
if order.Status != "UNUSED" {
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
order.Status = "USED"
|
||||
order, err = s.repo.Update(ctx, order)
|
||||
if err != nil {
|
||||
logger.ContextLogger(ctx).Error("error when updating order status", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *OrderService) Execute(ctx context.Context, req *entity.OrderExecuteRequest) (*entity.ExecuteOrderResponse, error) {
|
||||
partnerID, orderID, err := s.crypt.ValidateJWTOrder(req.Token)
|
||||
if err != nil {
|
||||
@@ -376,7 +448,7 @@ func (s *OrderService) GetAllHistoryOrders(ctx mycontext.Context, req entity.Ord
|
||||
return data, total, nil
|
||||
}
|
||||
|
||||
func (s OrderService) CountSoldOfTicket(ctx mycontext.Context, req entity.OrderSearch) (*entity.TicketSold, error) {
|
||||
func (s *OrderService) CountSoldOfTicket(ctx mycontext.Context, req entity.OrderSearch) (*entity.TicketSold, error) {
|
||||
ticket, err := s.repo.CountSoldOfTicket(ctx, req)
|
||||
|
||||
if err != nil {
|
||||
@@ -389,7 +461,7 @@ func (s OrderService) CountSoldOfTicket(ctx mycontext.Context, req entity.OrderS
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (s OrderService) GetDailySales(ctx mycontext.Context, req entity.OrderSearch) ([]entity.ProductDailySales, error) {
|
||||
func (s *OrderService) GetDailySales(ctx mycontext.Context, req entity.OrderSearch) ([]entity.ProductDailySales, error) {
|
||||
dailySales, err := s.repo.GetDailySalesMetrics(ctx, req)
|
||||
|
||||
if err != nil {
|
||||
@@ -400,7 +472,7 @@ func (s OrderService) GetDailySales(ctx mycontext.Context, req entity.OrderSearc
|
||||
return dailySales, nil
|
||||
}
|
||||
|
||||
func (s OrderService) GetPaymentDistribution(ctx mycontext.Context, req entity.OrderSearch) ([]entity.PaymentTypeDistribution, error) {
|
||||
func (s *OrderService) GetPaymentDistribution(ctx mycontext.Context, req entity.OrderSearch) ([]entity.PaymentTypeDistribution, error) {
|
||||
paymentDistribution, err := s.repo.GetPaymentTypeDistribution(ctx, req)
|
||||
|
||||
if err != nil {
|
||||
@@ -411,7 +483,7 @@ func (s OrderService) GetPaymentDistribution(ctx mycontext.Context, req entity.O
|
||||
return paymentDistribution, nil
|
||||
}
|
||||
|
||||
func (s OrderService) SumAmount(ctx mycontext.Context, req entity.OrderSearch) (*entity.Order, error) {
|
||||
func (s *OrderService) SumAmount(ctx mycontext.Context, req entity.OrderSearch) (*entity.Order, error) {
|
||||
amount, err := s.repo.SumAmount(ctx, req)
|
||||
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user