update
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"enaklo-pos-be/internal/common/errors"
|
||||
"enaklo-pos-be/internal/common/logger"
|
||||
"enaklo-pos-be/internal/common/mycontext"
|
||||
"enaklo-pos-be/internal/entity"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
FindByEmail(ctx mycontext.Context, email string) (*entity.Customer, error)
|
||||
}
|
||||
|
||||
type CryptoSvc interface {
|
||||
GenerateJWTCustomer(user *entity.Customer) (string, error)
|
||||
CompareHashAndPassword(hash string, password string) bool
|
||||
}
|
||||
|
||||
type Service interface {
|
||||
AuthCustomer(ctx mycontext.Context, email, password string) (*entity.AuthenticateUser, error)
|
||||
}
|
||||
|
||||
type authSvc struct {
|
||||
repo Repository
|
||||
crypt CryptoSvc
|
||||
}
|
||||
|
||||
func New(repo Repository, cryptSvc CryptoSvc) Service {
|
||||
return &authSvc{
|
||||
repo: repo,
|
||||
crypt: cryptSvc,
|
||||
}
|
||||
}
|
||||
|
||||
func (a authSvc) AuthCustomer(ctx mycontext.Context, email, password string) (*entity.AuthenticateUser, error) {
|
||||
user, err := a.repo.FindByEmail(ctx, email)
|
||||
if err != nil {
|
||||
logger.ContextLogger(ctx).Error("error when get user", zap.Error(err))
|
||||
return nil, errors.ErrorInternalServer
|
||||
}
|
||||
|
||||
if user == nil {
|
||||
return nil, errors.ErrorUserIsNotFound
|
||||
}
|
||||
|
||||
if ok := a.crypt.CompareHashAndPassword(user.Password, password); !ok {
|
||||
return nil, errors.ErrorUserInvalidLogin
|
||||
}
|
||||
|
||||
signedToken, err := a.crypt.GenerateJWTCustomer(user)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return user.ToUserAuthenticate(signedToken), nil
|
||||
}
|
||||
@@ -13,11 +13,13 @@ import (
|
||||
type InProgressOrderService interface {
|
||||
Save(ctx mycontext.Context, order *entity.OrderRequest) (*entity.Order, error)
|
||||
GetOrdersByPartnerID(ctx mycontext.Context, partnerID int64, limit, offset int) ([]*entity.Order, error)
|
||||
GetOrderByOrderAndPartnerID(ctx mycontext.Context, partnerID int64, orderID int64) (*entity.Order, error)
|
||||
}
|
||||
|
||||
type OrderRepository interface {
|
||||
CreateOrUpdate(ctx mycontext.Context, order *entity.Order) (*entity.Order, error)
|
||||
GetListByPartnerID(ctx mycontext.Context, partnerID int64, limit, offset int, status string) ([]*entity.Order, error)
|
||||
FindByIDAndPartnerID(ctx mycontext.Context, id int64, partnerID int64) (*entity.Order, error)
|
||||
}
|
||||
|
||||
type OrderCalculator interface {
|
||||
@@ -26,6 +28,7 @@ type OrderCalculator interface {
|
||||
items []entity.OrderItemRequest,
|
||||
productDetails *entity.ProductDetails,
|
||||
source string,
|
||||
partnerID int64,
|
||||
) (*entity.OrderCalculation, error)
|
||||
ValidateOrderItems(ctx mycontext.Context, items []entity.OrderItemRequest) ([]int64, []entity.OrderItemRequest, error)
|
||||
}
|
||||
@@ -57,7 +60,7 @@ func (s *inProgressOrderSvc) Save(ctx mycontext.Context, req *entity.OrderReques
|
||||
return nil, err
|
||||
}
|
||||
|
||||
orderCalculation, err := s.orderCalculator.CalculateOrderTotals(ctx, req.OrderItems, productDetails, req.Source)
|
||||
orderCalculation, err := s.orderCalculator.CalculateOrderTotals(ctx, req.OrderItems, productDetails, req.Source, req.PartnerID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -71,11 +74,12 @@ func (s *inProgressOrderSvc) Save(ctx mycontext.Context, req *entity.OrderReques
|
||||
}
|
||||
|
||||
orderItems[i] = entity.OrderItem{
|
||||
ItemID: item.ProductID,
|
||||
ItemName: productName,
|
||||
Quantity: item.Quantity,
|
||||
Price: product.Price,
|
||||
ItemType: product.Type,
|
||||
ItemID: item.ProductID,
|
||||
ItemName: productName,
|
||||
Quantity: item.Quantity,
|
||||
Price: product.Price,
|
||||
ItemType: product.Type,
|
||||
Description: product.Description,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,3 +123,15 @@ func (s *inProgressOrderSvc) GetOrdersByPartnerID(ctx mycontext.Context, partner
|
||||
|
||||
return orders, nil
|
||||
}
|
||||
|
||||
func (s *inProgressOrderSvc) GetOrderByOrderAndPartnerID(ctx mycontext.Context, partnerID int64, orderID int64) (*entity.Order, error) {
|
||||
orders, err := s.repo.FindByIDAndPartnerID(ctx, orderID, partnerID)
|
||||
if err != nil {
|
||||
logger.ContextLogger(ctx).Error("failed to get in-progress orders by partner ID",
|
||||
zap.Error(err),
|
||||
zap.Int64("partnerID", partnerID))
|
||||
return nil, errors.Wrap(err, "failed to get order")
|
||||
}
|
||||
|
||||
return orders, nil
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ func (s *orderSvc) CreateOrderInquiry(ctx mycontext.Context,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
orderCalculation, err := s.CalculateOrderTotals(ctx, req.OrderItems, productDetails, req.Source)
|
||||
orderCalculation, err := s.CalculateOrderTotals(ctx, req.OrderItems, productDetails, req.Source, req.PartnerID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -104,6 +104,7 @@ func (s *orderSvc) CalculateOrderTotals(
|
||||
items []entity.OrderItemRequest,
|
||||
productDetails *entity.ProductDetails,
|
||||
source string,
|
||||
partnerID int64,
|
||||
) (*entity.OrderCalculation, error) {
|
||||
subtotal := 0.0
|
||||
|
||||
@@ -115,8 +116,7 @@ func (s *orderSvc) CalculateOrderTotals(
|
||||
subtotal += product.Price * float64(item.Quantity)
|
||||
}
|
||||
|
||||
partnerID := ctx.GetPartnerID()
|
||||
setting, err := s.partnerSetting.GetSettings(ctx, *partnerID)
|
||||
setting, err := s.partnerSetting.GetSettings(ctx, partnerID)
|
||||
|
||||
if err != nil {
|
||||
return nil, errors.NewError(errors.ErrorInvalidRequest.ErrorType(), "failed to get partner settings")
|
||||
|
||||
@@ -67,6 +67,7 @@ type Service interface {
|
||||
items []entity.OrderItemRequest,
|
||||
productDetails *entity.ProductDetails,
|
||||
source string,
|
||||
partnerID int64,
|
||||
) (*entity.OrderCalculation, error)
|
||||
ValidateOrderItems(ctx mycontext.Context, items []entity.OrderItemRequest) ([]int64, []entity.OrderItemRequest, error)
|
||||
GetOrderPaymentAnalysis(
|
||||
|
||||
@@ -31,3 +31,13 @@ func (s *productSvc) GetProductsByIDs(ctx mycontext.Context, ids []int64, partne
|
||||
|
||||
return products, nil
|
||||
}
|
||||
|
||||
func (s *productSvc) GetProductsByPartnerID(ctx mycontext.Context, search entity.ProductSearch) ([]*entity.Product, int64, error) {
|
||||
products, total, err := s.repo.GetProductsByPartnerID(ctx, search)
|
||||
|
||||
if err != nil {
|
||||
return nil, 0, errors.Wrap(err, "failed to get products by partner ID")
|
||||
}
|
||||
|
||||
return products, total, nil
|
||||
}
|
||||
|
||||
@@ -8,11 +8,13 @@ import (
|
||||
type Repository interface {
|
||||
GetProductsByIDs(ctx mycontext.Context, ids []int64, partnerID int64) ([]*entity.Product, error)
|
||||
GetProductDetails(ctx mycontext.Context, productIDs []int64, partnerID int64) (*entity.ProductDetails, error)
|
||||
GetProductsByPartnerID(ctx mycontext.Context, req entity.ProductSearch) ([]*entity.Product, int64, error)
|
||||
}
|
||||
|
||||
type Service interface {
|
||||
GetProductsByIDs(ctx mycontext.Context, ids []int64, partnerID int64) ([]*entity.Product, error)
|
||||
GetProductDetails(ctx mycontext.Context, productIDs []int64, partnerID int64) (*entity.ProductDetails, error)
|
||||
GetProductsByPartnerID(ctx mycontext.Context, search entity.ProductSearch) ([]*entity.Product, int64, error)
|
||||
}
|
||||
|
||||
type productSvc struct {
|
||||
|
||||
Reference in New Issue
Block a user