mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 15:25:43 +00:00
add api detail daily checklist
This commit is contained in:
@@ -2,6 +2,8 @@ package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -30,6 +32,8 @@ type DailyChecklistService interface {
|
||||
RemoveAssignment(ctx *fiber.Ctx, id uint, employeeID uint) error
|
||||
GetTasks(ctx *fiber.Ctx, checklistID uint) ([]entity.DailyChecklistActivityTask, error)
|
||||
UpdateAssignment(ctx *fiber.Ctx, req *validation.UpdateAssignment) error
|
||||
GetChecklistPhaseIDs(ctx *fiber.Ctx, checklistID uint) ([]uint, error)
|
||||
GetDetail(ctx *fiber.Ctx, id uint) (*DailyChecklistDetail, error)
|
||||
}
|
||||
|
||||
type dailyChecklistService struct {
|
||||
@@ -39,6 +43,15 @@ type dailyChecklistService struct {
|
||||
PhaseRepo phaseRepo.PhasesRepository
|
||||
}
|
||||
|
||||
type DailyChecklistDetail struct {
|
||||
Checklist entity.DailyChecklist
|
||||
Phases []entity.DailyChecklistPhase
|
||||
Tasks []entity.DailyChecklistActivityTask
|
||||
AssignedEmployees []entity.Employee
|
||||
TotalActivities int
|
||||
Progress float64
|
||||
}
|
||||
|
||||
func NewDailyChecklistService(repo repository.DailyChecklistRepository, phaseRepo phaseRepo.PhasesRepository, validate *validator.Validate) DailyChecklistService {
|
||||
return &dailyChecklistService{
|
||||
Log: utils.Log,
|
||||
@@ -49,7 +62,7 @@ func NewDailyChecklistService(repo repository.DailyChecklistRepository, phaseRep
|
||||
}
|
||||
|
||||
func (s dailyChecklistService) withRelations(db *gorm.DB) *gorm.DB {
|
||||
return db
|
||||
return db.Preload("Kandang")
|
||||
}
|
||||
|
||||
func (s dailyChecklistService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.DailyChecklist, int64, error) {
|
||||
@@ -86,6 +99,72 @@ func (s dailyChecklistService) GetOne(c *fiber.Ctx, id uint) (*entity.DailyCheck
|
||||
return dailyChecklist, nil
|
||||
}
|
||||
|
||||
func (s dailyChecklistService) GetDetail(c *fiber.Ctx, id uint) (*DailyChecklistDetail, error) {
|
||||
checklist, err := s.GetOne(c, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
db := s.Repository.DB().WithContext(c.Context())
|
||||
|
||||
var phases []entity.DailyChecklistPhase
|
||||
if err := db.
|
||||
Where("checklist_id = ?", id).
|
||||
Preload("Phase", func(tx *gorm.DB) *gorm.DB {
|
||||
return tx.Preload("Activities")
|
||||
}).
|
||||
Order("created_at ASC").
|
||||
Find(&phases).Error; err != nil {
|
||||
s.Log.Errorf("Failed to get phases for daily checklist %d: %+v", id, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tasks []entity.DailyChecklistActivityTask
|
||||
if err := db.
|
||||
Where("checklist_id = ?", id).
|
||||
Preload("Phase").
|
||||
Preload("PhaseActivity").
|
||||
Preload("Assignments", func(tx *gorm.DB) *gorm.DB {
|
||||
return tx.Preload("Employee")
|
||||
}).
|
||||
Order("created_at ASC").
|
||||
Find(&tasks).Error; err != nil {
|
||||
s.Log.Errorf("Failed to get tasks for daily checklist %d: %+v", id, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
assignedEmployees := collectAssignedEmployees(tasks)
|
||||
|
||||
totalActivities := 0
|
||||
for _, phase := range phases {
|
||||
totalActivities += len(phase.Phase.Activities)
|
||||
}
|
||||
|
||||
var totalAssignments, completedAssignments int
|
||||
for _, task := range tasks {
|
||||
for _, assignment := range task.Assignments {
|
||||
totalAssignments++
|
||||
if assignment.Checked {
|
||||
completedAssignments++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var progress float64
|
||||
if totalAssignments > 0 {
|
||||
progress = math.Round((float64(completedAssignments) / float64(totalAssignments)) * 100)
|
||||
}
|
||||
|
||||
return &DailyChecklistDetail{
|
||||
Checklist: *checklist,
|
||||
Phases: phases,
|
||||
Tasks: tasks,
|
||||
AssignedEmployees: assignedEmployees,
|
||||
TotalActivities: totalActivities,
|
||||
Progress: progress,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *dailyChecklistService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.DailyChecklist, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
@@ -297,6 +376,35 @@ func (s dailyChecklistService) GetTasks(c *fiber.Ctx, checklistID uint) ([]entit
|
||||
return tasks, nil
|
||||
}
|
||||
|
||||
func (s dailyChecklistService) GetChecklistPhaseIDs(c *fiber.Ctx, checklistID uint) ([]uint, error) {
|
||||
if checklistID == 0 {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "checklist_id is required")
|
||||
}
|
||||
|
||||
if _, err := s.Repository.GetByID(c.Context(), checklistID, nil); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "DailyChecklist not found")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var phases []entity.DailyChecklistPhase
|
||||
if err := s.Repository.DB().WithContext(c.Context()).
|
||||
Where("checklist_id = ?", checklistID).
|
||||
Order("created_at ASC").
|
||||
Find(&phases).Error; err != nil {
|
||||
s.Log.Errorf("Failed to get daily checklist phases: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
phaseIDs := make([]uint, len(phases))
|
||||
for i, p := range phases {
|
||||
phaseIDs[i] = p.PhaseId
|
||||
}
|
||||
|
||||
return phaseIDs, nil
|
||||
}
|
||||
|
||||
func (s dailyChecklistService) UpdateAssignment(c *fiber.Ctx, req *validation.UpdateAssignment) error {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return err
|
||||
@@ -392,6 +500,32 @@ func collectTaskIDs(tasks []entity.DailyChecklistActivityTask) []uint {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func collectAssignedEmployees(tasks []entity.DailyChecklistActivityTask) []entity.Employee {
|
||||
employeeMap := make(map[uint]entity.Employee)
|
||||
for _, task := range tasks {
|
||||
for _, assignment := range task.Assignments {
|
||||
if assignment.Employee.Id == 0 {
|
||||
continue
|
||||
}
|
||||
if _, exists := employeeMap[assignment.Employee.Id]; exists {
|
||||
continue
|
||||
}
|
||||
employeeMap[assignment.Employee.Id] = assignment.Employee
|
||||
}
|
||||
}
|
||||
|
||||
employees := make([]entity.Employee, 0, len(employeeMap))
|
||||
for _, emp := range employeeMap {
|
||||
employees = append(employees, emp)
|
||||
}
|
||||
|
||||
sort.Slice(employees, func(i, j int) bool {
|
||||
return employees[i].Id < employees[j].Id
|
||||
})
|
||||
|
||||
return employees
|
||||
}
|
||||
func (s dailyChecklistService) AssignTasks(c *fiber.Ctx, id uint, req *validation.AssignTask) error {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user