mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 21:41:55 +00:00
363 lines
10 KiB
Go
363 lines
10 KiB
Go
package controller
|
|
|
|
import (
|
|
"math"
|
|
"strconv"
|
|
|
|
"gitlab.com/mbugroup/lti-api.git/internal/modules/daily-checklists/dto"
|
|
service "gitlab.com/mbugroup/lti-api.git/internal/modules/daily-checklists/services"
|
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/daily-checklists/validations"
|
|
"gitlab.com/mbugroup/lti-api.git/internal/response"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
type DailyChecklistController struct {
|
|
DailyChecklistService service.DailyChecklistService
|
|
}
|
|
|
|
func NewDailyChecklistController(dailyChecklistService service.DailyChecklistService) *DailyChecklistController {
|
|
return &DailyChecklistController{
|
|
DailyChecklistService: dailyChecklistService,
|
|
}
|
|
}
|
|
|
|
func (u *DailyChecklistController) GetAll(c *fiber.Ctx) error {
|
|
query := &validation.Query{
|
|
Page: c.QueryInt("page", 1),
|
|
Limit: c.QueryInt("limit", 10),
|
|
Search: c.Query("search", ""),
|
|
}
|
|
|
|
if query.Page < 1 || query.Limit < 1 {
|
|
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
|
|
}
|
|
|
|
result, totalResults, err := u.DailyChecklistService.GetAll(c, query)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.SuccessWithPaginate[dto.DailyChecklistListDTO]{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Get all dailyChecklists successfully",
|
|
Meta: response.Meta{
|
|
Page: query.Page,
|
|
Limit: query.Limit,
|
|
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
|
TotalResults: totalResults,
|
|
},
|
|
Data: dto.ToDailyChecklistListDTOs(result),
|
|
})
|
|
}
|
|
|
|
func (u *DailyChecklistController) GetSummary(c *fiber.Ctx) error {
|
|
query := &validation.SummaryQuery{
|
|
DateFrom: c.Query("date_from"),
|
|
DateTo: c.Query("date_to"),
|
|
Category: c.Query("category"),
|
|
}
|
|
|
|
if query.DateFrom == "" || query.DateTo == "" {
|
|
return fiber.NewError(fiber.StatusBadRequest, "date_from and date_to are required")
|
|
}
|
|
|
|
if kandangParam := c.Query("kandang_id"); kandangParam != "" {
|
|
kandangID, err := strconv.ParseUint(kandangParam, 10, 64)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid kandang_id")
|
|
}
|
|
value := uint(kandangID)
|
|
query.KandangID = &value
|
|
}
|
|
|
|
result, err := u.DailyChecklistService.GetSummary(c, query)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
type summaryResponse struct {
|
|
PerformanceOverview []dto.DailyChecklistPerformanceOverviewDTO `json:"performance_overview"`
|
|
TrackingABK []dto.DailyChecklistSummaryDTO `json:"tracking_abk"`
|
|
}
|
|
|
|
performanceMap := make(map[uint]*dto.DailyChecklistPerformanceOverviewDTO)
|
|
tracking := make([]dto.DailyChecklistSummaryDTO, len(result))
|
|
|
|
for i, summary := range result {
|
|
tracking[i] = dto.DailyChecklistSummaryDTO{
|
|
EmployeeID: summary.EmployeeID,
|
|
EmployeeName: summary.EmployeeName,
|
|
KandangID: summary.KandangID,
|
|
KandangName: summary.KandangName,
|
|
TotalActivity: summary.TotalActivity,
|
|
ActivityDone: summary.ActivityDone,
|
|
ActivityLeft: summary.ActivityLeft,
|
|
CompletionRate: summary.CompletionRate,
|
|
LastActivity: summary.LastActivity,
|
|
}
|
|
|
|
if _, ok := performanceMap[summary.EmployeeID]; !ok {
|
|
performanceMap[summary.EmployeeID] = &dto.DailyChecklistPerformanceOverviewDTO{
|
|
EmployeeID: summary.EmployeeID,
|
|
EmployeeName: summary.EmployeeName,
|
|
}
|
|
}
|
|
|
|
performanceMap[summary.EmployeeID].TotalActivity += summary.TotalActivity
|
|
performanceMap[summary.EmployeeID].ActivityDone += summary.ActivityDone
|
|
performanceMap[summary.EmployeeID].ActivityLeft += summary.ActivityLeft
|
|
}
|
|
|
|
performance := make([]dto.DailyChecklistPerformanceOverviewDTO, 0, len(performanceMap))
|
|
for _, v := range performanceMap {
|
|
performance = append(performance, *v)
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Get daily checklist summary successfully",
|
|
Data: summaryResponse{
|
|
PerformanceOverview: performance,
|
|
TrackingABK: tracking,
|
|
},
|
|
})
|
|
}
|
|
|
|
func (u *DailyChecklistController) GetOne(c *fiber.Ctx) error {
|
|
param := c.Params("idDailyChecklist")
|
|
|
|
id, err := strconv.Atoi(param)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
|
}
|
|
|
|
detail, err := u.DailyChecklistService.GetDetail(c, uint(id))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Get dailyChecklist successfully",
|
|
Data: dto.ToDailyChecklistDetailDTO(detail.Checklist, detail.Phases, detail.Tasks, detail.AssignedEmployees, detail.TotalActivities, detail.Progress),
|
|
})
|
|
}
|
|
|
|
func (u *DailyChecklistController) CreateOne(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.DailyChecklistService.CreateOne(c, req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusCreated).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusCreated,
|
|
Status: "success",
|
|
Message: "Create dailyChecklist successfully",
|
|
Data: dto.ToDailyChecklistListDTO(*result),
|
|
})
|
|
}
|
|
|
|
func (u *DailyChecklistController) UpdateOne(c *fiber.Ctx) error {
|
|
req := new(validation.Update)
|
|
param := c.Params("idDailyChecklist")
|
|
|
|
id, err := strconv.Atoi(param)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
|
}
|
|
|
|
if err := c.BodyParser(req); err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
|
}
|
|
|
|
result, err := u.DailyChecklistService.UpdateOne(c, req, uint(id))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Update dailyChecklist successfully",
|
|
Data: dto.ToDailyChecklistListDTO(*result),
|
|
})
|
|
}
|
|
|
|
func (u *DailyChecklistController) 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.DailyChecklistService.DeleteOne(c, uint(id)); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Common{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Delete dailyChecklist successfully",
|
|
})
|
|
}
|
|
|
|
func (u *DailyChecklistController) CreateDailyChecklistPhase(c *fiber.Ctx) error {
|
|
param := c.Params("idDailyChecklist")
|
|
id, err := strconv.Atoi(param)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid daily checklist id")
|
|
}
|
|
|
|
req := new(validation.AssignPhases)
|
|
if err := c.BodyParser(req); err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
|
}
|
|
|
|
if err := u.DailyChecklistService.AssignPhases(c, uint(id), req); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusCreated).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusCreated,
|
|
Status: "success",
|
|
Message: "Daily checklist phases saved successfully",
|
|
})
|
|
}
|
|
|
|
func (u *DailyChecklistController) CreateAssignment(c *fiber.Ctx) error {
|
|
param := c.Params("idDailyChecklist")
|
|
id, err := strconv.Atoi(param)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid daily checklist id")
|
|
}
|
|
|
|
req := new(validation.AssignTask)
|
|
if err := c.BodyParser(req); err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
|
}
|
|
|
|
if err := u.DailyChecklistService.AssignTasks(c, uint(id), req); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusCreated).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusCreated,
|
|
Status: "success",
|
|
Message: "Daily checklist assignments saved successfully",
|
|
})
|
|
}
|
|
|
|
func (u *DailyChecklistController) RemoveAssignment(c *fiber.Ctx) error {
|
|
dailyChecklistParam := c.Params("idDailyChecklist")
|
|
employeeParam := c.Params("idEmployee")
|
|
|
|
dailyChecklistID, err := strconv.Atoi(dailyChecklistParam)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid daily checklist id")
|
|
}
|
|
|
|
employeeID, err := strconv.Atoi(employeeParam)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid employee id")
|
|
}
|
|
|
|
if err := u.DailyChecklistService.RemoveAssignment(c, uint(dailyChecklistID), uint(employeeID)); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Common{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Assignment removed successfully",
|
|
})
|
|
}
|
|
|
|
func (u *DailyChecklistController) GetPhaseByIdChecklist(c *fiber.Ctx) error {
|
|
param := c.Params("idDailyChecklist")
|
|
id, err := strconv.Atoi(param)
|
|
if err != nil || id <= 0 {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid daily checklist id")
|
|
}
|
|
|
|
phaseIDs, err := u.DailyChecklistService.GetChecklistPhaseIDs(c, uint(id))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
responseData := make([]map[string]uint, len(phaseIDs))
|
|
for i, phaseID := range phaseIDs {
|
|
responseData[i] = map[string]uint{"phase_id": phaseID}
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Get phases successfully",
|
|
Data: responseData,
|
|
})
|
|
}
|
|
|
|
func (u *DailyChecklistController) GetAllTasks(c *fiber.Ctx) error {
|
|
checklistParam := c.Query("checklist_id", "")
|
|
if checklistParam == "" {
|
|
return fiber.NewError(fiber.StatusBadRequest, "checklist_id is required")
|
|
}
|
|
|
|
checklistID, err := strconv.Atoi(checklistParam)
|
|
if err != nil || checklistID <= 0 {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid checklist_id")
|
|
}
|
|
|
|
result, err := u.DailyChecklistService.GetTasks(c, uint(checklistID))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Get daily checklist tasks successfully",
|
|
Data: result,
|
|
})
|
|
}
|
|
|
|
func (u *DailyChecklistController) UpdateAssignment(c *fiber.Ctx) error {
|
|
req := new(validation.UpdateAssignment)
|
|
if err := c.BodyParser(req); err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
|
}
|
|
|
|
if err := u.DailyChecklistService.UpdateAssignment(c, req); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Assignment updated successfully",
|
|
})
|
|
}
|