mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
Feat[BE-261]: creating multiple Approval API, Update API, Delete API and some logic Adjustment
This commit is contained in:
@@ -103,11 +103,23 @@ func (u *ExpenseController) CreateOne(c *fiber.Ctx) error {
|
||||
|
||||
var singleCostPerKandang validation.CostPerKandang
|
||||
if err := json.Unmarshal([]byte(costPerKandangJSON), &singleCostPerKandang); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Invalid cost_per_kandang JSON: %v", err))
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Invalid cost_per_kandangs JSON: %v", err))
|
||||
}
|
||||
|
||||
if singleCostPerKandang.KandangID == 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Field KandangID is required")
|
||||
}
|
||||
|
||||
req.CostPerKandangs = []validation.CostPerKandang{singleCostPerKandang}
|
||||
} else {
|
||||
for i, costPerKandang := range req.CostPerKandangs {
|
||||
if costPerKandang.KandangID == 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Field KandangID is required for cost_per_kandangs[%d]", i))
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Field cost_per_kandangs is required")
|
||||
}
|
||||
|
||||
result, err := u.ExpenseService.CreateOne(c, req)
|
||||
@@ -133,8 +145,30 @@ func (u *ExpenseController) UpdateOne(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
form, err := c.MultipartForm()
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid multipart form")
|
||||
}
|
||||
|
||||
req.Documents = form.File["documents"]
|
||||
if transactionDate := c.FormValue("transaction_date"); transactionDate != "" {
|
||||
req.TransactionDate = &transactionDate
|
||||
}
|
||||
|
||||
costPerKandangJSON := c.FormValue("cost_per_kandang")
|
||||
if costPerKandangJSON != "" {
|
||||
var costPerKandang []validation.CostPerKandang
|
||||
if err := json.Unmarshal([]byte(costPerKandangJSON), &costPerKandang); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Invalid cost_per_kandang JSON: %v", err))
|
||||
}
|
||||
|
||||
for i, costPerKandang := range costPerKandang {
|
||||
if costPerKandang.KandangID == 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Field KandangID is required for cost_per_kandang[%d]", i))
|
||||
}
|
||||
}
|
||||
|
||||
req.CostPerKandang = &costPerKandang
|
||||
}
|
||||
|
||||
result, err := u.ExpenseService.UpdateOne(c, req, uint(id))
|
||||
@@ -171,42 +205,45 @@ func (u *ExpenseController) DeleteOne(c *fiber.Ctx) error {
|
||||
})
|
||||
}
|
||||
|
||||
func (u *ExpenseController) ApproveExpense(c *fiber.Ctx) error {
|
||||
expenseID := c.Params("id")
|
||||
id, err := strconv.Atoi(expenseID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid expense ID")
|
||||
}
|
||||
func (u *ExpenseController) Approval(c *fiber.Ctx) error {
|
||||
req := new(validation.ApprovalRequest)
|
||||
|
||||
// Extract step from URL path (manager or finance)
|
||||
path := c.Path()
|
||||
var step string
|
||||
if strings.Contains(path, "/approvals/manager") {
|
||||
step = "Manager"
|
||||
} else if strings.Contains(path, "/approvals/finance") {
|
||||
step = "Finance"
|
||||
} else {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid approval step")
|
||||
}
|
||||
|
||||
// Parse approval request
|
||||
var req validation.ApprovalRequest
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
// Approve expense
|
||||
expense, err := u.ExpenseService.ApproveExpense(c, uint(id), step, req.Action, req.Notes)
|
||||
path := c.Path()
|
||||
approvalType := ""
|
||||
if strings.Contains(path, "/approvals/manager") {
|
||||
approvalType = "manager"
|
||||
} else if strings.Contains(path, "/approvals/finance") {
|
||||
approvalType = "finance"
|
||||
} else {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid approval path")
|
||||
}
|
||||
|
||||
results, err := u.ExpenseService.Approval(c, req, approvalType)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var (
|
||||
data interface{}
|
||||
message = "Submit expense approval successfully"
|
||||
)
|
||||
if len(results) == 1 {
|
||||
data = results[0]
|
||||
} else {
|
||||
message = "Submit expense approvals successfully"
|
||||
data = results
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Approve expense successfully",
|
||||
Data: expense,
|
||||
Message: message,
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -224,8 +261,8 @@ func (u *ExpenseController) CreateRealization(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid multipart form")
|
||||
}
|
||||
req.Documents = form.File["documents"]
|
||||
req.RealizationDate = c.FormValue("realization_date")
|
||||
|
||||
// Parse realizations JSON
|
||||
realizationsJSON := c.FormValue("realizations")
|
||||
if realizationsJSON != "" {
|
||||
if err := json.Unmarshal([]byte(realizationsJSON), &req.Realizations); err != nil {
|
||||
@@ -255,8 +292,21 @@ func (u *ExpenseController) UpdateRealization(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
var req validation.UpdateRealization
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
|
||||
form, err := c.MultipartForm()
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid multipart form")
|
||||
}
|
||||
|
||||
req.Documents = form.File["documents"]
|
||||
|
||||
req.RealizationDate = c.FormValue("realization_date")
|
||||
|
||||
realizationsJSON := c.FormValue("realizations")
|
||||
if realizationsJSON != "" {
|
||||
if err := json.Unmarshal([]byte(realizationsJSON), &req.Realizations); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Invalid realizations JSON: %v", err))
|
||||
}
|
||||
}
|
||||
|
||||
expense, err := u.ExpenseService.UpdateRealization(c, uint(id), &req)
|
||||
@@ -293,3 +343,49 @@ func (u *ExpenseController) CompleteExpense(c *fiber.Ctx) error {
|
||||
Data: expense,
|
||||
})
|
||||
}
|
||||
|
||||
func (u *ExpenseController) DeleteDocument(c *fiber.Ctx) error {
|
||||
expenseID, err := strconv.Atoi(c.Params("id"))
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid expense ID")
|
||||
}
|
||||
|
||||
documentID, err := strconv.ParseUint(c.Params("documentId"), 10, 64)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid document ID")
|
||||
}
|
||||
|
||||
if err := u.ExpenseService.DeleteDocument(c, uint(expenseID), documentID, false); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Common{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Delete document successfully",
|
||||
})
|
||||
}
|
||||
|
||||
func (u *ExpenseController) DeleteRealizationDocument(c *fiber.Ctx) error {
|
||||
expenseID, err := strconv.Atoi(c.Params("id"))
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid expense ID")
|
||||
}
|
||||
|
||||
documentID, err := strconv.ParseUint(c.Params("documentId"), 10, 64)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid document ID")
|
||||
}
|
||||
|
||||
if err := u.ExpenseService.DeleteDocument(c, uint(expenseID), documentID, true); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Common{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Delete realization document successfully",
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user