mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
feat(BE): finance (payment, initial_balance, injection). fix(BE): kandang capacity
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/transactions/repositories"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/transactions/validations"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
approvalutils "gitlab.com/mbugroup/lti-api.git/internal/utils/approvals"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type TransactionService interface {
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.Payment, int64, error)
|
||||
GetOne(ctx *fiber.Ctx, id uint) (*entity.Payment, error)
|
||||
DeleteOne(ctx *fiber.Ctx, id uint) error
|
||||
}
|
||||
|
||||
type transactionService struct {
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
Repository repository.TransactionRepository
|
||||
ApprovalSvc commonSvc.ApprovalService
|
||||
approvalWorkflows map[string]approvalutils.ApprovalWorkflowKey
|
||||
}
|
||||
|
||||
func NewTransactionService(
|
||||
repo repository.TransactionRepository,
|
||||
approvalSvc commonSvc.ApprovalService,
|
||||
validate *validator.Validate,
|
||||
) TransactionService {
|
||||
return &transactionService{
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
ApprovalSvc: approvalSvc,
|
||||
approvalWorkflows: map[string]approvalutils.ApprovalWorkflowKey{
|
||||
string(utils.TransactionTypeSaldoAwal): utils.ApprovalWorkflowInitial,
|
||||
string(utils.TransactionTypeInjection): utils.ApprovalWorkflowInjection,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s transactionService) withRelations(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("CreatedUser").
|
||||
Preload("BankWarehouse").
|
||||
Preload("Customer").
|
||||
Preload("Supplier")
|
||||
}
|
||||
|
||||
func (s transactionService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Payment, int64, error) {
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
offset := (params.Page - 1) * params.Limit
|
||||
|
||||
transactions, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
||||
db = s.withRelations(db)
|
||||
if params.Search != "" {
|
||||
like := "%" + strings.ToLower(strings.TrimSpace(params.Search)) + "%"
|
||||
return db.Where(
|
||||
`LOWER(payment_code) LIKE ? OR
|
||||
LOWER(COALESCE(reference_number, '')) LIKE ? OR
|
||||
LOWER(COALESCE(transaction_type, '')) LIKE ? OR
|
||||
LOWER(COALESCE(notes, '')) LIKE ?`,
|
||||
like, like, like, like,
|
||||
)
|
||||
}
|
||||
return db.Order("payment_date DESC").Order("created_at DESC")
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get transactions: %+v", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
s.attachApprovals(c.Context(), transactions)
|
||||
return transactions, total, nil
|
||||
}
|
||||
|
||||
func (s transactionService) GetOne(c *fiber.Ctx, id uint) (*entity.Payment, error) {
|
||||
transaction, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Transaction not found")
|
||||
}
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed get transaction by id: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
if s.ApprovalSvc != nil {
|
||||
approval, err := s.ApprovalSvc.LatestByTarget(
|
||||
c.Context(),
|
||||
s.workflowForTransaction(transaction),
|
||||
id,
|
||||
s.approvalQueryModifier(),
|
||||
)
|
||||
if err != nil {
|
||||
s.Log.Warnf("Unable to load latest approval for transaction %d: %+v", id, err)
|
||||
} else {
|
||||
transaction.LatestApproval = approval
|
||||
}
|
||||
}
|
||||
return transaction, nil
|
||||
}
|
||||
|
||||
func (s transactionService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
if err := s.Repository.DeleteOne(c.Context(), id); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "Transaction not found")
|
||||
}
|
||||
s.Log.Errorf("Failed to delete transaction: %+v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s transactionService) attachApprovals(ctx context.Context, transactions []entity.Payment) {
|
||||
if s.ApprovalSvc == nil || len(transactions) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
workflowIDs := map[approvalutils.ApprovalWorkflowKey][]uint{}
|
||||
for _, transaction := range transactions {
|
||||
workflow := s.workflowForTransaction(&transaction)
|
||||
workflowIDs[workflow] = append(workflowIDs[workflow], transaction.Id)
|
||||
}
|
||||
|
||||
approvalByWorkflow := make(map[approvalutils.ApprovalWorkflowKey]map[uint]*entity.Approval, len(workflowIDs))
|
||||
for workflow, ids := range workflowIDs {
|
||||
if len(ids) == 0 {
|
||||
continue
|
||||
}
|
||||
approvals, err := s.ApprovalSvc.LatestByTargets(ctx, workflow, ids, s.approvalQueryModifier())
|
||||
if err != nil {
|
||||
s.Log.Warnf("Unable to load latest approvals for transactions: %+v", err)
|
||||
continue
|
||||
}
|
||||
approvalByWorkflow[workflow] = approvals
|
||||
}
|
||||
|
||||
for i := range transactions {
|
||||
workflow := s.workflowForTransaction(&transactions[i])
|
||||
if approvals, ok := approvalByWorkflow[workflow]; ok {
|
||||
transactions[i].LatestApproval = approvals[transactions[i].Id]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s transactionService) workflowForTransaction(transaction *entity.Payment) approvalutils.ApprovalWorkflowKey {
|
||||
if transaction == nil {
|
||||
return utils.ApprovalWorkflowPayment
|
||||
}
|
||||
transactionType := strings.TrimSpace(strings.ToUpper(transaction.TransactionType))
|
||||
if transactionType == "" {
|
||||
return utils.ApprovalWorkflowPayment
|
||||
}
|
||||
if workflow, ok := s.approvalWorkflows[transactionType]; ok {
|
||||
return workflow
|
||||
}
|
||||
return utils.ApprovalWorkflowPayment
|
||||
}
|
||||
|
||||
func (s transactionService) approvalQueryModifier() func(*gorm.DB) *gorm.DB {
|
||||
return func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("ActionUser")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user