diff --git a/internal/contract/chart_of_account_request.go b/internal/contract/chart_of_account_request.go index cb3c461..f9b656e 100644 --- a/internal/contract/chart_of_account_request.go +++ b/internal/contract/chart_of_account_request.go @@ -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"` diff --git a/internal/entities/chart_of_account.go b/internal/entities/chart_of_account.go index f8d021d..26f9d82 100644 --- a/internal/entities/chart_of_account.go +++ b/internal/entities/chart_of_account.go @@ -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"` diff --git a/internal/mappers/chart_of_account_mapper.go b/internal/mappers/chart_of_account_mapper.go index c991742..16ec573 100644 --- a/internal/mappers/chart_of_account_mapper.go +++ b/internal/mappers/chart_of_account_mapper.go @@ -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, diff --git a/internal/mappers/contract_mapper.go b/internal/mappers/contract_mapper.go index 50413e9..b65f9d9 100644 --- a/internal/mappers/contract_mapper.go +++ b/internal/mappers/contract_mapper.go @@ -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, diff --git a/internal/models/chart_of_account.go b/internal/models/chart_of_account.go index 3f5fa9e..3e0abc4 100644 --- a/internal/models/chart_of_account.go +++ b/internal/models/chart_of_account.go @@ -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"` diff --git a/internal/processor/chart_of_account_processor.go b/internal/processor/chart_of_account_processor.go index fc10571..34f32cf 100644 --- a/internal/processor/chart_of_account_processor.go +++ b/internal/processor/chart_of_account_processor.go @@ -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) diff --git a/internal/repository/chart_of_account_repository.go b/internal/repository/chart_of_account_repository.go index 7b21b15..4cff46b 100644 --- a/internal/repository/chart_of_account_repository.go +++ b/internal/repository/chart_of_account_repository.go @@ -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) diff --git a/internal/service/chart_of_account_service.go b/internal/service/chart_of_account_service.go index ade6963..d26a47c 100644 --- a/internal/service/chart_of_account_service.go +++ b/internal/service/chart_of_account_service.go @@ -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 }