Refactor[BE]: refactor expense category handling to use constants for BOP and NON-BOP

This commit is contained in:
aguhh18
2025-12-18 15:03:37 +07:00
parent 9e0b4be4dd
commit c95f90f0b9
2 changed files with 27 additions and 8 deletions
@@ -213,7 +213,7 @@ func (s *expenseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*expen
var projectFlockKandangId *uint64
if req.Category == "BOP" {
if req.Category == string(utils.ExpenseCategoryBOP) {
projectFlockKandang, err := projectFlockKandangRepoTx.GetActiveByKandangID(c.Context(), uint(expenseNonstock.KandangID))
if err != nil {
@@ -230,10 +230,10 @@ func (s *expenseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*expen
nonstockId := costItem.NonstockID
var kandangId *uint64
if req.Category == "NON-BOP" {
if req.Category == string(utils.ExpenseCategoryNonBOP) {
id := uint64(expenseNonstock.KandangID)
kandangId = &id
} else if req.Category == "BOP" {
} else if req.Category == string(utils.ExpenseCategoryBOP) {
if projectFlockKandangId != nil {
kandangId = &expenseNonstock.KandangID
}
@@ -385,7 +385,7 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
}
if categoryChanged {
if currentExpense.Category == "BOP" && newCategory == "NON-BOP" {
if currentExpense.Category == string(utils.ExpenseCategoryBOP) && newCategory == string(utils.ExpenseCategoryNonBOP) {
var existingExpenseNonstocks []entity.ExpenseNonstock
if err := tx.Where("expense_id = ?", id).Find(&existingExpenseNonstocks).Error; err != nil {
@@ -400,7 +400,7 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update project flock kandang id to null")
}
}
} else if currentExpense.Category == "NON-BOP" && newCategory == "BOP" {
} else if currentExpense.Category == string(utils.ExpenseCategoryNonBOP) && newCategory == string(utils.ExpenseCategoryBOP) {
var existingExpenseNonstocks []entity.ExpenseNonstock
if err := tx.Where("expense_id = ?", id).Find(&existingExpenseNonstocks).Error; err != nil {
@@ -457,7 +457,7 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
for _, expenseNonstock := range *req.ExpenseNonstocks {
var projectFlockKandangId *uint64
if updatedExpense.Category == "BOP" {
if updatedExpense.Category == string(utils.ExpenseCategoryBOP) {
projectFlockKandangRepoTx := projectFlockKandangRepo.NewProjectFlockKandangRepository(tx)
projectFlockKandang, err := projectFlockKandangRepoTx.GetActiveByKandangID(c.Context(), uint(expenseNonstock.KandangID))
if err != nil {
@@ -480,10 +480,10 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
}
var kandangId *uint64
if updatedExpense.Category == "NON-BOP" {
if updatedExpense.Category == string(utils.ExpenseCategoryNonBOP) {
id := uint64(expenseNonstock.KandangID)
kandangId = &id
} else if updatedExpense.Category == "BOP" {
} else if updatedExpense.Category == string(utils.ExpenseCategoryBOP) {
if projectFlockKandangId != nil {
kandangId = &expenseNonstock.KandangID
}
+19
View File
@@ -135,6 +135,17 @@ const (
SupplierCategorySapronak SupplierCategory = "SAPRONAK"
)
// -------------------------------------------------------------------
// ExpenseCategory
// -------------------------------------------------------------------
type ExpenseCategory string
const (
ExpenseCategoryBOP ExpenseCategory = "BOP"
ExpenseCategoryNonBOP ExpenseCategory = "NON-BOP"
)
// -------------------------------------------------------------------
// Kandang Status
// -------------------------------------------------------------------
@@ -429,6 +440,14 @@ func IsValidSupplierCategory(v string) bool {
return false
}
func IsValidExpenseCategory(v string) bool {
switch ExpenseCategory(v) {
case ExpenseCategoryBOP, ExpenseCategoryNonBOP:
return true
}
return false
}
// example use
// Recording helper