coa fix
This commit is contained in:
parent
91f51d129e
commit
75ec5274d2
@ -22,26 +22,24 @@ type UpdateChartOfAccountRequest struct {
|
||||
}
|
||||
|
||||
type ChartOfAccountResponse struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
OrganizationID uuid.UUID `json:"organization_id"`
|
||||
OutletID *uuid.UUID `json:"outlet_id"`
|
||||
ChartOfAccountTypeID uuid.UUID `json:"chart_of_account_type_id"`
|
||||
ParentID *uuid.UUID `json:"parent_id"`
|
||||
Name string `json:"name"`
|
||||
Code string `json:"code"`
|
||||
Description *string `json:"description"`
|
||||
IsActive bool `json:"is_active"`
|
||||
IsSystem bool `json:"is_system"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
ID uuid.UUID `json:"id"`
|
||||
OrganizationID uuid.UUID `json:"organization_id"`
|
||||
OutletID *uuid.UUID `json:"outlet_id"`
|
||||
ChartOfAccountTypeID uuid.UUID `json:"chart_of_account_type_id"`
|
||||
ParentID *uuid.UUID `json:"parent_id"`
|
||||
Name string `json:"name"`
|
||||
Code string `json:"code"`
|
||||
Description *string `json:"description"`
|
||||
IsActive bool `json:"is_active"`
|
||||
IsSystem bool `json:"is_system"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
ChartOfAccountType *ChartOfAccountTypeResponse `json:"chart_of_account_type,omitempty"`
|
||||
Parent *ChartOfAccountResponse `json:"parent,omitempty"`
|
||||
Children []ChartOfAccountResponse `json:"children,omitempty"`
|
||||
Parent *ChartOfAccountResponse `json:"parent,omitempty"`
|
||||
Children []ChartOfAccountResponse `json:"children,omitempty"`
|
||||
}
|
||||
|
||||
type ListChartOfAccountsRequest struct {
|
||||
OrganizationID *uuid.UUID `form:"organization_id"`
|
||||
OutletID *uuid.UUID `form:"outlet_id"`
|
||||
ChartOfAccountTypeID *uuid.UUID `form:"chart_of_account_type_id"`
|
||||
ParentID *uuid.UUID `form:"parent_id"`
|
||||
IsActive *bool `form:"is_active"`
|
||||
|
||||
@ -10,7 +10,7 @@ import (
|
||||
type ChartOfAccount struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
|
||||
OrganizationID uuid.UUID `gorm:"type:uuid;not null;index" json:"organization_id" validate:"required"`
|
||||
OutletID *uuid.UUID `gorm:"type:uuid;index" json:"outlet_id"`
|
||||
OutletID uuid.UUID `gorm:"type:uuid;index" json:"outlet_id"`
|
||||
ChartOfAccountTypeID uuid.UUID `gorm:"type:uuid;not null;index" json:"chart_of_account_type_id" validate:"required"`
|
||||
ParentID *uuid.UUID `gorm:"type:uuid;index" json:"parent_id"`
|
||||
Name string `gorm:"not null;size:255" json:"name" validate:"required,min=1,max=255"`
|
||||
|
||||
@ -11,7 +11,7 @@ func ChartOfAccountEntityToResponse(entity *entities.ChartOfAccount) *models.Cha
|
||||
response := &models.ChartOfAccountResponse{
|
||||
ID: entity.ID,
|
||||
OrganizationID: entity.OrganizationID,
|
||||
OutletID: entity.OutletID,
|
||||
OutletID: &entity.OutletID,
|
||||
ChartOfAccountTypeID: entity.ChartOfAccountTypeID,
|
||||
ParentID: entity.ParentID,
|
||||
Name: entity.Name,
|
||||
@ -44,7 +44,7 @@ func ChartOfAccountEntityToResponse(entity *entities.ChartOfAccount) *models.Cha
|
||||
func ChartOfAccountCreateRequestToEntity(req *models.CreateChartOfAccountRequest, organizationID uuid.UUID, outletID *uuid.UUID) *entities.ChartOfAccount {
|
||||
return &entities.ChartOfAccount{
|
||||
OrganizationID: organizationID,
|
||||
OutletID: outletID,
|
||||
OutletID: *outletID,
|
||||
ChartOfAccountTypeID: req.ChartOfAccountTypeID,
|
||||
ParentID: req.ParentID,
|
||||
Name: req.Name,
|
||||
|
||||
@ -3,6 +3,7 @@ package mappers
|
||||
import (
|
||||
"apskel-pos-be/internal/contract"
|
||||
"apskel-pos-be/internal/models"
|
||||
"github.com/google/uuid"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -58,10 +59,10 @@ func ContractToModelUpdateChartOfAccountRequest(req *contract.UpdateChartOfAccou
|
||||
}
|
||||
}
|
||||
|
||||
func ContractToModelListChartOfAccountsRequest(req *contract.ListChartOfAccountsRequest) *models.ListChartOfAccountsRequest {
|
||||
func ContractToModelListChartOfAccountsRequest(req *contract.ListChartOfAccountsRequest, organizationID, outletID uuid.UUID) *models.ListChartOfAccountsRequest {
|
||||
return &models.ListChartOfAccountsRequest{
|
||||
OrganizationID: req.OrganizationID,
|
||||
OutletID: req.OutletID,
|
||||
OrganizationID: organizationID,
|
||||
OutletID: outletID,
|
||||
ChartOfAccountTypeID: req.ChartOfAccountTypeID,
|
||||
ParentID: req.ParentID,
|
||||
IsActive: req.IsActive,
|
||||
|
||||
@ -42,8 +42,8 @@ type UpdateChartOfAccountRequest struct {
|
||||
}
|
||||
|
||||
type ListChartOfAccountsRequest struct {
|
||||
OrganizationID *uuid.UUID `form:"organization_id"`
|
||||
OutletID *uuid.UUID `form:"outlet_id"`
|
||||
OrganizationID uuid.UUID `form:"organization_id"`
|
||||
OutletID uuid.UUID `form:"outlet_id"`
|
||||
ChartOfAccountTypeID *uuid.UUID `form:"chart_of_account_type_id"`
|
||||
ParentID *uuid.UUID `form:"parent_id"`
|
||||
IsActive *bool `form:"is_active"`
|
||||
|
||||
@ -12,7 +12,6 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
|
||||
type ChartOfAccountProcessor interface {
|
||||
CreateChartOfAccount(ctx context.Context, req *models.CreateChartOfAccountRequest) (*models.ChartOfAccountResponse, error)
|
||||
GetChartOfAccountByID(ctx context.Context, id uuid.UUID) (*models.ChartOfAccountResponse, error)
|
||||
@ -91,7 +90,7 @@ func (p *ChartOfAccountProcessorImpl) UpdateChartOfAccount(ctx context.Context,
|
||||
|
||||
// Check if new code already exists (if code is being updated)
|
||||
if req.Code != nil && *req.Code != entity.Code {
|
||||
existing, err := p.chartOfAccountRepo.GetByCode(ctx, entity.OrganizationID, *req.Code, entity.OutletID)
|
||||
existing, err := p.chartOfAccountRepo.GetByCode(ctx, entity.OrganizationID, *req.Code, &entity.OutletID)
|
||||
if err == nil && existing != nil {
|
||||
return nil, fmt.Errorf("chart of account with code %s already exists", *req.Code)
|
||||
}
|
||||
@ -142,19 +141,15 @@ func (p *ChartOfAccountProcessorImpl) DeleteChartOfAccount(ctx context.Context,
|
||||
}
|
||||
|
||||
func (p *ChartOfAccountProcessorImpl) ListChartOfAccounts(ctx context.Context, req *models.ListChartOfAccountsRequest) ([]models.ChartOfAccountResponse, int, error) {
|
||||
// Get organization and outlet from context
|
||||
appCtx := appcontext.FromGinContext(ctx)
|
||||
organizationID := appCtx.OrganizationID
|
||||
var outletID *uuid.UUID
|
||||
if appCtx.OutletID != uuid.Nil {
|
||||
outletID = &appCtx.OutletID
|
||||
filterEntity := &entities.ChartOfAccount{
|
||||
OrganizationID: req.OrganizationID,
|
||||
OutletID: req.OutletID,
|
||||
ParentID: req.ParentID,
|
||||
}
|
||||
|
||||
filterEntity := &entities.ChartOfAccount{
|
||||
OrganizationID: organizationID,
|
||||
OutletID: outletID,
|
||||
ChartOfAccountTypeID: *req.ChartOfAccountTypeID,
|
||||
ParentID: req.ParentID,
|
||||
// Apply optional filter for ChartOfAccountTypeID
|
||||
if req.ChartOfAccountTypeID != nil {
|
||||
filterEntity.ChartOfAccountTypeID = *req.ChartOfAccountTypeID
|
||||
}
|
||||
|
||||
entities, total, err := p.chartOfAccountRepo.List(ctx, filterEntity)
|
||||
|
||||
@ -63,8 +63,8 @@ func (r *ChartOfAccountRepositoryImpl) List(ctx context.Context, req *entities.C
|
||||
if req.OrganizationID != uuid.Nil {
|
||||
query = query.Where("organization_id = ?", req.OrganizationID)
|
||||
}
|
||||
if req.OutletID != nil {
|
||||
query = query.Where("outlet_id = ?", *req.OutletID)
|
||||
if req.OutletID != uuid.Nil {
|
||||
query = query.Where("outlet_id = ?", req.OutletID)
|
||||
}
|
||||
if req.ChartOfAccountTypeID != uuid.Nil {
|
||||
query = query.Where("chart_of_account_type_id = ?", req.ChartOfAccountTypeID)
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"apskel-pos-be/internal/appcontext"
|
||||
"context"
|
||||
|
||||
"apskel-pos-be/internal/contract"
|
||||
@ -55,17 +56,21 @@ func (s *ChartOfAccountServiceImpl) DeleteChartOfAccount(ctx context.Context, id
|
||||
}
|
||||
|
||||
func (s *ChartOfAccountServiceImpl) ListChartOfAccounts(ctx context.Context, req *contract.ListChartOfAccountsRequest) ([]contract.ChartOfAccountResponse, int, error) {
|
||||
modelReq := mappers.ContractToModelListChartOfAccountsRequest(req)
|
||||
appCtx := appcontext.FromGinContext(ctx)
|
||||
organizationID := appCtx.OrganizationID
|
||||
outletID := appCtx.OutletID
|
||||
|
||||
modelReq := mappers.ContractToModelListChartOfAccountsRequest(req, organizationID, outletID)
|
||||
modelResp, total, err := s.processor.ListChartOfAccounts(ctx, modelReq)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
|
||||
contractResp := make([]contract.ChartOfAccountResponse, len(modelResp))
|
||||
for i, resp := range modelResp {
|
||||
contractResp[i] = *mappers.ModelToContractChartOfAccountResponse(&resp)
|
||||
}
|
||||
|
||||
|
||||
return contractResp, total, nil
|
||||
}
|
||||
|
||||
@ -74,12 +79,12 @@ func (s *ChartOfAccountServiceImpl) GetChartOfAccountsByOrganization(ctx context
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
contractResp := make([]contract.ChartOfAccountResponse, len(modelResp))
|
||||
for i, resp := range modelResp {
|
||||
contractResp[i] = *mappers.ModelToContractChartOfAccountResponse(&resp)
|
||||
}
|
||||
|
||||
|
||||
return contractResp, nil
|
||||
}
|
||||
|
||||
@ -88,11 +93,11 @@ func (s *ChartOfAccountServiceImpl) GetChartOfAccountsByType(ctx context.Context
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
contractResp := make([]contract.ChartOfAccountResponse, len(modelResp))
|
||||
for i, resp := range modelResp {
|
||||
contractResp[i] = *mappers.ModelToContractChartOfAccountResponse(&resp)
|
||||
}
|
||||
|
||||
|
||||
return contractResp, nil
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user