Fix
This commit is contained in:
parent
f41daa63da
commit
7d5f061a1b
@ -28,18 +28,19 @@ type CreateOutgoingLetterRequest struct {
|
||||
ReceiverInstitutionID *uuid.UUID `json:"receiver_institution_id,omitempty"`
|
||||
IssueDate time.Time `json:"issue_date" validate:"required"`
|
||||
ApprovalFlowID *uuid.UUID `json:"approval_flow_id,omitempty"`
|
||||
Recipients []CreateOutgoingLetterRecipient `json:"recipients,omitempty"`
|
||||
Recipients []CreateOutgoingLetterRecipient `json:"recipients,omitempty"`
|
||||
Attachments []CreateOutgoingLetterAttachment `json:"attachments,omitempty"`
|
||||
UserID uuid.UUID
|
||||
}
|
||||
|
||||
type OutgoingLetterRecipientResponse struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Email *string `json:"email,omitempty"`
|
||||
Position *string `json:"position,omitempty"`
|
||||
Institution *string `json:"institution,omitempty"`
|
||||
IsPrimary bool `json:"is_primary"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
ID uuid.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Email *string `json:"email,omitempty"`
|
||||
Position *string `json:"position,omitempty"`
|
||||
Institution *string `json:"institution,omitempty"`
|
||||
IsPrimary bool `json:"is_primary"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type OutgoingLetterAttachmentResponse struct {
|
||||
@ -160,10 +161,10 @@ type DiscussionResponse struct {
|
||||
}
|
||||
|
||||
type ApprovalFlowRequest struct {
|
||||
DepartmentID uuid.UUID `json:"department_id" validate:"required"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
Description *string `json:"description,omitempty"`
|
||||
IsActive bool `json:"is_active"`
|
||||
DepartmentID uuid.UUID `json:"department_id" validate:"required"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
Description *string `json:"description,omitempty"`
|
||||
IsActive bool `json:"is_active"`
|
||||
Steps []ApprovalFlowStepRequest `json:"steps" validate:"required,dive"`
|
||||
}
|
||||
|
||||
@ -188,16 +189,16 @@ type ApprovalFlowResponse struct {
|
||||
}
|
||||
|
||||
type ApprovalFlowStepResponse struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
StepOrder int `json:"step_order"`
|
||||
ParallelGroup int `json:"parallel_group"`
|
||||
ApproverRoleID *uuid.UUID `json:"approver_role_id,omitempty"`
|
||||
ApproverRole *RoleResponse `json:"approver_role,omitempty"`
|
||||
ApproverUserID *uuid.UUID `json:"approver_user_id,omitempty"`
|
||||
ApproverUser *UserResponse `json:"approver_user,omitempty"`
|
||||
Required bool `json:"required"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
ID uuid.UUID `json:"id"`
|
||||
StepOrder int `json:"step_order"`
|
||||
ParallelGroup int `json:"parallel_group"`
|
||||
ApproverRoleID *uuid.UUID `json:"approver_role_id,omitempty"`
|
||||
ApproverRole *RoleResponse `json:"approver_role,omitempty"`
|
||||
ApproverUserID *uuid.UUID `json:"approver_user_id,omitempty"`
|
||||
ApproverUser *UserResponse `json:"approver_user,omitempty"`
|
||||
Required bool `json:"required"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type ListApprovalFlowsRequest struct {
|
||||
|
||||
@ -35,13 +35,11 @@ func (h *AdminApprovalFlowHandler) CreateApprovalFlow(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Validate that at least one step is provided
|
||||
if len(req.Steps) == 0 {
|
||||
c.JSON(http.StatusBadRequest, &contract.ErrorResponse{Error: "at least one approval step is required", Code: http.StatusBadRequest})
|
||||
return
|
||||
}
|
||||
|
||||
// Validate each step has either a role or user as approver
|
||||
for i, step := range req.Steps {
|
||||
if step.ApproverRoleID == nil && step.ApproverUserID == nil {
|
||||
c.JSON(http.StatusBadRequest, &contract.ErrorResponse{
|
||||
@ -113,13 +111,11 @@ func (h *AdminApprovalFlowHandler) UpdateApprovalFlow(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Validate that at least one step is provided
|
||||
if len(req.Steps) == 0 {
|
||||
c.JSON(http.StatusBadRequest, &contract.ErrorResponse{Error: "at least one approval step is required", Code: http.StatusBadRequest})
|
||||
return
|
||||
}
|
||||
|
||||
// Validate each step has either a role or user as approver
|
||||
for i, step := range req.Steps {
|
||||
if step.ApproverRoleID == nil && step.ApproverUserID == nil {
|
||||
c.JSON(http.StatusBadRequest, &contract.ErrorResponse{
|
||||
|
||||
@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"eslogad-be/internal/appcontext"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
@ -50,6 +51,8 @@ func (h *LetterOutgoingHandler) CreateOutgoingLetter(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, &contract.ErrorResponse{Error: "invalid body", Code: http.StatusBadRequest})
|
||||
return
|
||||
}
|
||||
ctx := c.Request.Context()
|
||||
req.UserID = appcontext.FromGinContext(ctx).UserID
|
||||
|
||||
resp, err := h.svc.CreateOutgoingLetter(c.Request.Context(), &req)
|
||||
if err != nil {
|
||||
|
||||
@ -94,6 +94,10 @@ func (r *LetterOutgoingRepository) List(ctx context.Context, filter ListOutgoing
|
||||
Preload("Priority").
|
||||
Preload("ReceiverInstitution").
|
||||
Preload("Creator").
|
||||
Preload("Recipients").
|
||||
Preload("Attachments").
|
||||
Preload("Approvals.Step").
|
||||
Preload("Approvals.Approver").
|
||||
Order("created_at DESC").
|
||||
Limit(limit).
|
||||
Offset(offset).
|
||||
|
||||
@ -186,11 +186,10 @@ func (r *Router) addAppRoutes(rg *gin.Engine) {
|
||||
droutes.PUT(":id/active", r.dispRouteHandler.SetActive)
|
||||
}
|
||||
|
||||
admin := v1.Group("/admin")
|
||||
admin := v1.Group("/setting")
|
||||
admin.Use(r.authMiddleware.RequireAuth())
|
||||
{
|
||||
approvalFlows := admin.Group("/approval-flows")
|
||||
approvalFlows.Use(r.authMiddleware.RequirePermissions("admin.approval_flow"))
|
||||
{
|
||||
approvalFlows.POST("", r.adminApprovalFlowHandler.CreateApprovalFlow)
|
||||
approvalFlows.GET("", r.adminApprovalFlowHandler.ListApprovalFlows)
|
||||
|
||||
@ -38,16 +38,16 @@ type LetterOutgoingService interface {
|
||||
}
|
||||
|
||||
type LetterOutgoingServiceImpl struct {
|
||||
db *gorm.DB
|
||||
letterRepo *repository.LetterOutgoingRepository
|
||||
attachmentRepo *repository.LetterOutgoingAttachmentRepository
|
||||
recipientRepo *repository.LetterOutgoingRecipientRepository
|
||||
discussionRepo *repository.LetterOutgoingDiscussionRepository
|
||||
discussionAttachmentRepo *repository.LetterOutgoingDiscussionAttachmentRepository
|
||||
activityLogRepo *repository.LetterOutgoingActivityLogRepository
|
||||
approvalFlowRepo *repository.ApprovalFlowRepository
|
||||
approvalRepo *repository.LetterOutgoingApprovalRepository
|
||||
txManager *repository.TxManager
|
||||
db *gorm.DB
|
||||
letterRepo *repository.LetterOutgoingRepository
|
||||
attachmentRepo *repository.LetterOutgoingAttachmentRepository
|
||||
recipientRepo *repository.LetterOutgoingRecipientRepository
|
||||
discussionRepo *repository.LetterOutgoingDiscussionRepository
|
||||
discussionAttachmentRepo *repository.LetterOutgoingDiscussionAttachmentRepository
|
||||
activityLogRepo *repository.LetterOutgoingActivityLogRepository
|
||||
approvalFlowRepo *repository.ApprovalFlowRepository
|
||||
approvalRepo *repository.LetterOutgoingApprovalRepository
|
||||
txManager *repository.TxManager
|
||||
}
|
||||
|
||||
func NewLetterOutgoingService(
|
||||
@ -77,8 +77,6 @@ func NewLetterOutgoingService(
|
||||
}
|
||||
|
||||
func (s *LetterOutgoingServiceImpl) CreateOutgoingLetter(ctx context.Context, req *contract.CreateOutgoingLetterRequest) (*contract.OutgoingLetterResponse, error) {
|
||||
userID := getUserIDFromContext(ctx)
|
||||
|
||||
letter := &entities.LetterOutgoing{
|
||||
Subject: req.Subject,
|
||||
Description: req.Description,
|
||||
@ -87,7 +85,7 @@ func (s *LetterOutgoingServiceImpl) CreateOutgoingLetter(ctx context.Context, re
|
||||
IssueDate: req.IssueDate,
|
||||
Status: entities.LetterOutgoingStatusDraft,
|
||||
ApprovalFlowID: req.ApprovalFlowID,
|
||||
CreatedBy: userID,
|
||||
CreatedBy: req.UserID,
|
||||
}
|
||||
|
||||
if req.ReferenceNumber != nil {
|
||||
@ -124,7 +122,7 @@ func (s *LetterOutgoingServiceImpl) CreateOutgoingLetter(ctx context.Context, re
|
||||
FileURL: a.FileURL,
|
||||
FileName: a.FileName,
|
||||
FileType: a.FileType,
|
||||
UploadedBy: &userID,
|
||||
UploadedBy: &req.UserID,
|
||||
}
|
||||
}
|
||||
if err := s.attachmentRepo.CreateBulk(txCtx, attachments); err != nil {
|
||||
@ -135,7 +133,7 @@ func (s *LetterOutgoingServiceImpl) CreateOutgoingLetter(ctx context.Context, re
|
||||
activityLog := &entities.LetterOutgoingActivityLog{
|
||||
LetterID: letter.ID,
|
||||
ActionType: entities.LetterOutgoingActionCreated,
|
||||
ActorUserID: &userID,
|
||||
ActorUserID: &req.UserID,
|
||||
}
|
||||
if err := s.activityLogRepo.Create(txCtx, activityLog); err != nil {
|
||||
return err
|
||||
@ -808,7 +806,7 @@ func ptr(s string) *string {
|
||||
}
|
||||
|
||||
func transformLetterToResponse(letter *entities.LetterOutgoing) *contract.OutgoingLetterResponse {
|
||||
return &contract.OutgoingLetterResponse{
|
||||
resp := &contract.OutgoingLetterResponse{
|
||||
ID: letter.ID,
|
||||
LetterNumber: letter.LetterNumber,
|
||||
ReferenceNumber: letter.ReferenceNumber,
|
||||
@ -823,6 +821,82 @@ func transformLetterToResponse(letter *entities.LetterOutgoing) *contract.Outgoi
|
||||
CreatedAt: letter.CreatedAt,
|
||||
UpdatedAt: letter.UpdatedAt,
|
||||
}
|
||||
|
||||
if letter.Priority != nil {
|
||||
resp.Priority = &contract.PriorityResponse{
|
||||
ID: letter.Priority.ID.String(),
|
||||
Name: letter.Priority.Name,
|
||||
Level: letter.Priority.Level,
|
||||
CreatedAt: letter.Priority.CreatedAt,
|
||||
UpdatedAt: letter.Priority.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
if letter.ReceiverInstitution != nil {
|
||||
resp.ReceiverInstitution = &contract.InstitutionResponse{
|
||||
ID: letter.ReceiverInstitution.ID.String(),
|
||||
Name: letter.ReceiverInstitution.Name,
|
||||
Type: string(letter.ReceiverInstitution.Type),
|
||||
Address: letter.ReceiverInstitution.Address,
|
||||
ContactPerson: letter.ReceiverInstitution.ContactPerson,
|
||||
Phone: letter.ReceiverInstitution.Phone,
|
||||
Email: letter.ReceiverInstitution.Email,
|
||||
CreatedAt: letter.ReceiverInstitution.CreatedAt,
|
||||
UpdatedAt: letter.ReceiverInstitution.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
if len(letter.Recipients) > 0 {
|
||||
resp.Recipients = make([]contract.OutgoingLetterRecipientResponse, len(letter.Recipients))
|
||||
for i, recipient := range letter.Recipients {
|
||||
resp.Recipients[i] = contract.OutgoingLetterRecipientResponse{
|
||||
ID: recipient.ID,
|
||||
Name: recipient.RecipientName,
|
||||
Email: recipient.RecipientEmail,
|
||||
Position: recipient.RecipientPosition,
|
||||
Institution: recipient.RecipientInstitution,
|
||||
IsPrimary: recipient.IsPrimary,
|
||||
CreatedAt: recipient.CreatedAt,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(letter.Attachments) > 0 {
|
||||
resp.Attachments = make([]contract.OutgoingLetterAttachmentResponse, len(letter.Attachments))
|
||||
for i, attachment := range letter.Attachments {
|
||||
resp.Attachments[i] = contract.OutgoingLetterAttachmentResponse{
|
||||
ID: attachment.ID,
|
||||
FileURL: attachment.FileURL,
|
||||
FileName: attachment.FileName,
|
||||
FileType: attachment.FileType,
|
||||
UploadedAt: attachment.UploadedAt,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Include Approvals if loaded
|
||||
if len(letter.Approvals) > 0 {
|
||||
resp.Approvals = make([]contract.OutgoingLetterApprovalResponse, len(letter.Approvals))
|
||||
for i, approval := range letter.Approvals {
|
||||
approvalResp := contract.OutgoingLetterApprovalResponse{
|
||||
ID: approval.ID,
|
||||
ApproverID: approval.ApproverID,
|
||||
Status: string(approval.Status),
|
||||
Remarks: approval.Remarks,
|
||||
ActedAt: approval.ActedAt,
|
||||
CreatedAt: approval.CreatedAt,
|
||||
}
|
||||
|
||||
// Include step order if step is loaded
|
||||
if approval.Step != nil {
|
||||
approvalResp.StepOrder = approval.Step.StepOrder
|
||||
}
|
||||
|
||||
resp.Approvals[i] = approvalResp
|
||||
}
|
||||
}
|
||||
|
||||
return resp
|
||||
}
|
||||
|
||||
func transformDiscussionToResponse(discussion *entities.LetterOutgoingDiscussion) *contract.DiscussionResponse {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user