Customer Order

This commit is contained in:
aditya.siregar
2024-08-04 01:16:25 +07:00
parent 265dd9b6e8
commit bdf930445c
30 changed files with 832 additions and 43 deletions
+49 -5
View File
@@ -2,7 +2,9 @@ package discovery
import (
"context"
"errors"
"furtuna-be/config"
"gorm.io/gorm"
"furtuna-be/internal/entity"
"furtuna-be/internal/repository"
@@ -15,14 +17,16 @@ const (
)
type DiscoveryService struct {
repo repository.SiteRepository
cfg config.Discovery
repo repository.SiteRepository
cfg config.Discovery
product repository.Product
}
func NewDiscoveryService(repo repository.SiteRepository, cfg config.Discovery) *DiscoveryService {
func NewDiscoveryService(repo repository.SiteRepository, cfg config.Discovery, product repository.Product) *DiscoveryService {
return &DiscoveryService{
repo: repo,
cfg: cfg,
repo: repo,
cfg: cfg,
product: product,
}
}
@@ -119,3 +123,43 @@ func (s *DiscoveryService) Search(ctx context.Context, search *entity.DiscoveryS
return response, total, nil
}
func (s *DiscoveryService) GetByID(ctx context.Context, id int64) (*entity.Site, error) {
site, err := s.repo.GetByID(ctx, id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, err
}
if site.Status == "Inactive" {
return nil, nil
}
return site.ToSite(), nil
}
func (s *DiscoveryService) GetProductsByID(ctx context.Context, id int64) ([]*entity.Product, error) {
site, err := s.repo.GetByID(ctx, id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, err
}
if site.Status == "Inactive" {
return nil, nil
}
product, err := s.product.GetProductsBySiteID(ctx, site.ID)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, err
}
return product.ToProductList(), nil
}
+17
View File
@@ -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"
@@ -81,6 +82,7 @@ func (s *OrderService) CreateOrder(ctx mycontext.Context, req *entity.OrderReque
SiteID: ctx.GetSiteID(),
CreatedBy: req.CreatedBy,
OrderItems: []entity.OrderItem{},
Source: req.Source,
}
for _, item := range req.OrderItems {
@@ -191,6 +193,7 @@ func (s *OrderService) processNonCashPayment(ctx context.Context, order *entity.
PaymentReferenceID: generator.GenerateUUIDV4(),
TotalAmount: int64(order.Amount),
OrderItems: order.OrderItems,
PaymentMethod: order.PaymentType,
}
paymentResponse, err := s.midtrans.CreatePayment(paymentRequest)
@@ -358,3 +361,17 @@ func (s OrderService) SumAmount(ctx mycontext.Context, req entity.OrderSearch) (
return data, nil
}
func (s *OrderService) GetByID(ctx mycontext.Context, id int64) (*entity.Order, error) {
order, err := s.repo.FindByID(ctx, id)
if err != nil {
logger.ContextLogger(ctx).Error("error when getting products by IDs", zap.Error(err))
return nil, err
}
if order.CreatedBy != ctx.RequestedBy() {
return nil, errors2.NewError(errors2.ErrorBadRequest.ErrorType(), "order not found")
}
return order, nil
}
+4 -1
View File
@@ -58,7 +58,7 @@ func NewServiceManagerImpl(cfg *config.Config, repo *repository.RepoManagerImpl)
LicenseSvc: service.NewLicenseService(repo.License),
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),
DiscoverService: discovery.NewDiscoveryService(repo.Site, cfg.Discovery, repo.Product),
}
}
@@ -119,6 +119,7 @@ type Order interface {
SumAmount(ctx mycontext.Context, req entity.OrderSearch) (*entity.Order, error)
GetDailySales(ctx mycontext.Context, req entity.OrderSearch) ([]entity.ProductDailySales, error)
GetPaymentDistribution(ctx mycontext.Context, req entity.OrderSearch) ([]entity.PaymentTypeDistribution, error)
GetByID(ctx mycontext.Context, id int64) (*entity.Order, error)
}
type OSSService interface {
@@ -163,4 +164,6 @@ type Balance interface {
type DiscoverService interface {
Home(ctx context.Context, search *entity.DiscoverySearch) (*entity.DiscoverySearchResp, error)
Search(ctx context.Context, search *entity.DiscoverySearch) (*entity.DiscoverySearchResp, int64, error)
GetByID(ctx context.Context, id int64) (*entity.Site, error)
GetProductsByID(ctx context.Context, id int64) ([]*entity.Product, error)
}