Files
lti-api/internal/modules/inventory/adjustments/controllers/adjustment.controller.go
T
aguhh18 91b320d489 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
2025-10-10 09:24:17 +07:00

82 lines
2.3 KiB
Go

package controller
import (
"math"
"gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/adjustments/dto"
service "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/adjustments/services"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/adjustments/validations"
"gitlab.com/mbugroup/lti-api.git/internal/response"
"github.com/gofiber/fiber/v2"
)
type AdjustmentController struct {
AdjustmentService service.AdjustmentService
}
func NewAdjustmentController(adjustmentService service.AdjustmentService) *AdjustmentController {
return &AdjustmentController{
AdjustmentService: adjustmentService,
}
}
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")
}
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: adjustmentDTO,
})
}
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", ""),
}
result, totalResults, err := u.AdjustmentService.AdjustmentHistory(c, query)
if 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.SuccessWithPaginate[dto.AdjustmentDetailDTO]{
Code: fiber.StatusOK,
Status: "success",
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,
})
}