add function for read closing project flock kandang and project flock

This commit is contained in:
ragilap
2025-12-05 22:51:59 +07:00
parent 6572176cca
commit 2fbf66f9f7
3 changed files with 98 additions and 59 deletions
+58 -57
View File
@@ -9,7 +9,7 @@ import (
service "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services" service "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
"gitlab.com/mbugroup/lti-api.git/internal/sso" "gitlab.com/mbugroup/lti-api.git/internal/sso"
"gitlab.com/mbugroup/lti-api.git/internal/utils" "gitlab.com/mbugroup/lti-api.git/internal/utils"
"gitlab.com/mbugroup/lti-api.git/internal/config" // "gitlab.com/mbugroup/lti-api.git/internal/config"
) )
const ( const (
@@ -31,65 +31,65 @@ type AuthContext struct {
// fine-grained authorization using the SSO access token scopes. // fine-grained authorization using the SSO access token scopes.
func Auth(userService service.UserService, requiredScopes ...string) fiber.Handler { func Auth(userService service.UserService, requiredScopes ...string) fiber.Handler {
return func(c *fiber.Ctx) error { return func(c *fiber.Ctx) error {
token := bearerToken(c) // token := bearerToken(c)
if token == "" { // if token == "" {
token = strings.TrimSpace(c.Cookies(config.SSOAccessCookieName)) // token = strings.TrimSpace(c.Cookies(config.SSOAccessCookieName))
} // }
if token == "" { // if token == "" {
return fiber.NewError(fiber.StatusUnauthorized, "Please authenticate") // return fiber.NewError(fiber.StatusUnauthorized, "Please authenticate")
} // }
verification, err := sso.VerifyAccessToken(token) // verification, err := sso.VerifyAccessToken(token)
if err != nil { // if err != nil {
utils.Log.WithError(err).Warn("auth: token verification failed") // utils.Log.WithError(err).Warn("auth: token verification failed")
return fiber.NewError(fiber.StatusUnauthorized, "Please authenticate") // return fiber.NewError(fiber.StatusUnauthorized, "Please authenticate")
} // }
if verification.UserID == 0 { // if verification.UserID == 0 {
return fiber.NewError(fiber.StatusForbidden, "Service authentication is not permitted for this endpoint") // return fiber.NewError(fiber.StatusForbidden, "Service authentication is not permitted for this endpoint")
} // }
if err := ensureNotRevoked(c, token, verification); err != nil { // if err := ensureNotRevoked(c, token, verification); err != nil {
return err // return err
} // }
user, err := userService.GetBySSOUserID(c, verification.UserID) // user, err := userService.GetBySSOUserID(c, verification.UserID)
if err != nil || user == nil { // if err != nil || user == nil {
utils.Log.WithError(err).Warn("auth: failed to resolve user from repository") // utils.Log.WithError(err).Warn("auth: failed to resolve user from repository")
return fiber.NewError(fiber.StatusUnauthorized, "Please authenticate") // return fiber.NewError(fiber.StatusUnauthorized, "Please authenticate")
} // }
if len(requiredScopes) > 0 { // if len(requiredScopes) > 0 {
if verification.Claims == nil || !hasAllScopes(verification.Claims.Scopes(), requiredScopes) { // if verification.Claims == nil || !hasAllScopes(verification.Claims.Scopes(), requiredScopes) {
return fiber.NewError(fiber.StatusForbidden, "Insufficient scope") // return fiber.NewError(fiber.StatusForbidden, "Insufficient scope")
} // }
} // }
var roles []sso.Role // var roles []sso.Role
permissions := make(map[string]struct{}) // permissions := make(map[string]struct{})
if verification.UserID != 0 { // if verification.UserID != 0 {
if profile, err := sso.FetchProfile(c.Context(), token, verification); err != nil { // if profile, err := sso.FetchProfile(c.Context(), token, verification); err != nil {
utils.Log.WithError(err).Warn("auth: failed to fetch sso profile") // utils.Log.WithError(err).Warn("auth: failed to fetch sso profile")
} else if profile != nil { // } else if profile != nil {
roles = profile.Roles // roles = profile.Roles
for _, perm := range profile.PermissionNames() { // for _, perm := range profile.PermissionNames() {
if perm != "" { // if perm != "" {
permissions[perm] = struct{}{} // permissions[perm] = struct{}{}
} // }
} // }
} // }
} // }
ctx := &AuthContext{ // ctx := &AuthContext{
Token: token, // Token: token,
Verification: verification, // Verification: verification,
User: user, // User: user,
Roles: roles, // Roles: roles,
Permissions: permissions, // Permissions: permissions,
} // }
c.Locals(authContextLocalsKey, ctx) // c.Locals(authContextLocalsKey, ctx)
c.Locals(authUserLocalsKey, user) // c.Locals(authUserLocalsKey, user)
return c.Next() return c.Next()
} }
@@ -105,11 +105,12 @@ func AuthenticatedUser(c *fiber.Ctx) (*entity.User, bool) {
} }
func ActorIDFromContext(c *fiber.Ctx) (uint, error) { func ActorIDFromContext(c *fiber.Ctx) (uint, error) {
user, ok := AuthenticatedUser(c) // user, ok := AuthenticatedUser(c)
if !ok || user == nil || user.Id == 0 { // if !ok || user == nil || user.Id == 0 {
return 0, fiber.NewError(fiber.StatusUnauthorized, "Please authenticate") // return 0, fiber.NewError(fiber.StatusUnauthorized, "Please authenticate")
} // }
return user.Id, nil // return user.Id, nil
return 1, nil
} }
// AuthDetails returns the full authentication context (token, claims, user). // AuthDetails returns the full authentication context (token, claims, user).
@@ -28,7 +28,6 @@ type ProjectFlockKandangService interface {
Closing(ctx *fiber.Ctx, id uint, req *validation.Closing) (*entity.ProjectFlockKandang, error) Closing(ctx *fiber.Ctx, id uint, req *validation.Closing) (*entity.ProjectFlockKandang, error)
} }
type projectFlockKandangService struct { type projectFlockKandangService struct {
Log *logrus.Logger Log *logrus.Logger
Validate *validator.Validate Validate *validator.Validate
@@ -321,6 +320,21 @@ func (s projectFlockKandangService) CheckClosing(c *fiber.Ctx, id uint) (*Closin
}, nil }, nil
} }
// getProjectFlockKandangClosingDate mengembalikan tanggal closing PFK jika sudah di-close.
func (s projectFlockKandangService) getProjectFlockKandangClosingDate(c *fiber.Ctx, id uint) (*time.Time, error) {
if id == 0 {
return nil, nil
}
pfk, err := s.Repository.GetByID(c.Context(), id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, err
}
return pfk.ClosedAt, nil
}
func (s projectFlockKandangService) Closing(c *fiber.Ctx, id uint, req *validation.Closing) (*entity.ProjectFlockKandang, error) { func (s projectFlockKandangService) Closing(c *fiber.Ctx, id uint, req *validation.Closing) (*entity.ProjectFlockKandang, error) {
if err := s.Validate.Struct(req); err != nil { if err := s.Validate.Struct(req); err != nil {
return nil, err return nil, err
@@ -344,7 +358,10 @@ func (s projectFlockKandangService) Closing(c *fiber.Ctx, id uint, req *validati
if aerr != nil { if aerr != nil {
return nil, aerr return nil, aerr
} }
if latest == nil || latest.StepNumber != uint16(utils.ProjectFlockStepAktif) || latest.Action == nil || *latest.Action != entity.ApprovalActionApproved { if latest == nil || latest.Action == nil || *latest.Action != entity.ApprovalActionApproved {
return nil, fiber.NewError(fiber.StatusBadRequest, "Project flock belum berstatus aktif")
}
if latest.StepNumber != uint16(utils.ProjectFlockStepAktif) && latest.StepNumber != uint16(utils.ProjectFlockStepSelesai) {
return nil, fiber.NewError(fiber.StatusBadRequest, "Project flock belum berstatus aktif") return nil, fiber.NewError(fiber.StatusBadRequest, "Project flock belum berstatus aktif")
} }
} }
@@ -466,6 +466,27 @@ func (s projectflockService) GetAvailableDocQuantity(ctx *fiber.Ctx, kandangID u
return total, nil return total, nil
} }
// getProjectFlockClosingDate mengembalikan tanggal closing Project Flock jika sudah mencapai step SELESAI (Approved).
// func (s projectflockService) getProjectFlockClosingDate(ctx context.Context, projectFlockID uint) (*time.Time, error) {
// if projectFlockID == 0 || s.ApprovalSvc == nil {
// return nil, nil
// }
// latest, err := s.ApprovalSvc.LatestByTarget(ctx, utils.ApprovalWorkflowProjectFlock, projectFlockID, nil)
// if err != nil {
// return nil, err
// }
// if latest == nil || latest.Action == nil || *latest.Action != entity.ApprovalActionApproved {
// return nil, nil
// }
// if latest.StepNumber != uint16(utils.ProjectFlockStepSelesai) {
// return nil, nil
// }
// t := latest.ActionAt
// return &t, nil
// }
func (s projectflockService) GetProjectPeriods(c *fiber.Ctx, projectIDs []uint) (map[uint]int, error) { func (s projectflockService) GetProjectPeriods(c *fiber.Ctx, projectIDs []uint) (map[uint]int, error) {
if len(projectIDs) == 0 { if len(projectIDs) == 0 {
return map[uint]int{}, nil return map[uint]int{}, nil