mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-23 14:55:42 +00:00
feat(BE-47,48,49,50): implement inventory adjustment system
- Extend DB schema with product_warehouses and stock_logs tables - Implement stock adjustment API (increase/decrease operations) - Add comprehensive validation for all adjustment operations - Implement audit log system for each adjustment with history tracking - Include transaction handling, DTOs, seeders, and proper error handling - Add adjustment history API with pagination and filtering TODO: Integration testing pending
This commit is contained in:
@@ -2,7 +2,6 @@ package controller
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strconv"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/adjustments/dto"
|
||||
service "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/adjustments/services"
|
||||
@@ -22,119 +21,61 @@ func NewAdjustmentController(adjustmentService service.AdjustmentService) *Adjus
|
||||
}
|
||||
}
|
||||
|
||||
func (u *AdjustmentController) GetAll(c *fiber.Ctx) error {
|
||||
query := &validation.Query{
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
Search: c.Query("search", ""),
|
||||
}
|
||||
|
||||
result, totalResults, err := u.AdjustmentService.GetAll(c, query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.SuccessWithPaginate[dto.AdjustmentListDTO]{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Get all adjustments successfully",
|
||||
Meta: response.Meta{
|
||||
Page: query.Page,
|
||||
Limit: query.Limit,
|
||||
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
||||
TotalResults: totalResults,
|
||||
},
|
||||
Data: dto.ToAdjustmentListDTOs(result),
|
||||
})
|
||||
}
|
||||
|
||||
func (u *AdjustmentController) GetOne(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
|
||||
id, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
|
||||
result, err := u.AdjustmentService.GetOne(c, uint(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Get adjustment successfully",
|
||||
Data: dto.ToAdjustmentListDTO(*result),
|
||||
})
|
||||
}
|
||||
|
||||
func (u *AdjustmentController) CreateOne(c *fiber.Ctx) error {
|
||||
func (u *AdjustmentController) Adjustment(c *fiber.Ctx) error {
|
||||
req := new(validation.Create)
|
||||
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
result, err := u.AdjustmentService.CreateOne(c, req)
|
||||
stockLog, err := u.AdjustmentService.Adjustment(c, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
adjustmentDTO := dto.ToAdjustmentDetailDTO(stockLog)
|
||||
|
||||
return c.Status(fiber.StatusCreated).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusCreated,
|
||||
Status: "success",
|
||||
Message: "Create adjustment successfully",
|
||||
Data: dto.ToAdjustmentListDTO(*result),
|
||||
Data: adjustmentDTO,
|
||||
})
|
||||
}
|
||||
|
||||
func (u *AdjustmentController) UpdateOne(c *fiber.Ctx) error {
|
||||
req := new(validation.Update)
|
||||
param := c.Params("id")
|
||||
|
||||
id, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
func (u *AdjustmentController) AdjustmentHistory(c *fiber.Ctx) error {
|
||||
query := &validation.Query{
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
Search: c.Query("search", ""),
|
||||
ProductID: c.QueryInt("product_id", 0),
|
||||
WarehouseID: c.QueryInt("warehouse_id", 0),
|
||||
TransactionType: c.Query("transaction_type", ""),
|
||||
}
|
||||
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
result, err := u.AdjustmentService.UpdateOne(c, req, uint(id))
|
||||
result, totalResults, err := u.AdjustmentService.AdjustmentHistory(c, query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Update adjustment successfully",
|
||||
Data: dto.ToAdjustmentListDTO(*result),
|
||||
})
|
||||
}
|
||||
|
||||
func (u *AdjustmentController) DeleteOne(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
|
||||
id, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
|
||||
if err := u.AdjustmentService.DeleteOne(c, uint(id)); err != nil {
|
||||
return err
|
||||
// Convert to DTOs
|
||||
adjustmentDTOs := make([]dto.AdjustmentDetailDTO, len(result))
|
||||
for i, stockLog := range result {
|
||||
adjustmentDTOs[i] = dto.ToAdjustmentDetailDTO(stockLog)
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Common{
|
||||
JSON(response.SuccessWithPaginate[dto.AdjustmentDetailDTO]{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Delete adjustment successfully",
|
||||
Message: "Get adjustment history successfully",
|
||||
Meta: response.Meta{
|
||||
Page: query.Page,
|
||||
Limit: query.Limit,
|
||||
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
||||
TotalResults: totalResults,
|
||||
},
|
||||
Data: adjustmentDTOs,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user