add app setting letter out
This commit is contained in:
@@ -2,10 +2,11 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"eslogad-be/internal/appcontext"
|
||||
"eslogad-be/internal/contract"
|
||||
"eslogad-be/internal/entities"
|
||||
"eslogad-be/internal/processor"
|
||||
"eslogad-be/internal/repository"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -38,53 +39,24 @@ 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
|
||||
processor processor.LetterOutgoingProcessor
|
||||
}
|
||||
|
||||
func NewLetterOutgoingService(
|
||||
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,
|
||||
) *LetterOutgoingServiceImpl {
|
||||
func NewLetterOutgoingService(processor processor.LetterOutgoingProcessor) *LetterOutgoingServiceImpl {
|
||||
return &LetterOutgoingServiceImpl{
|
||||
db: db,
|
||||
letterRepo: letterRepo,
|
||||
attachmentRepo: attachmentRepo,
|
||||
recipientRepo: recipientRepo,
|
||||
discussionRepo: discussionRepo,
|
||||
discussionAttachmentRepo: discussionAttachmentRepo,
|
||||
activityLogRepo: activityLogRepo,
|
||||
approvalFlowRepo: approvalFlowRepo,
|
||||
approvalRepo: approvalRepo,
|
||||
txManager: txManager,
|
||||
processor: processor,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *LetterOutgoingServiceImpl) CreateOutgoingLetter(ctx context.Context, req *contract.CreateOutgoingLetterRequest) (*contract.OutgoingLetterResponse, error) {
|
||||
departmentID := getDepartmentIDFromContext(ctx)
|
||||
|
||||
letter := &entities.LetterOutgoing{
|
||||
Subject: req.Subject,
|
||||
Description: req.Description,
|
||||
PriorityID: req.PriorityID,
|
||||
ReceiverInstitutionID: req.ReceiverInstitutionID,
|
||||
IssueDate: req.IssueDate,
|
||||
Status: entities.LetterOutgoingStatusDraft,
|
||||
ApprovalFlowID: req.ApprovalFlowID,
|
||||
CreatedBy: req.UserID,
|
||||
}
|
||||
|
||||
@@ -92,61 +64,25 @@ func (s *LetterOutgoingServiceImpl) CreateOutgoingLetter(ctx context.Context, re
|
||||
letter.ReferenceNumber = req.ReferenceNumber
|
||||
}
|
||||
|
||||
err := s.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
||||
if err := s.letterRepo.Create(txCtx, letter); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(req.Recipients) > 0 {
|
||||
recipients := make([]entities.LetterOutgoingRecipient, len(req.Recipients))
|
||||
for i, r := range req.Recipients {
|
||||
recipients[i] = entities.LetterOutgoingRecipient{
|
||||
LetterID: letter.ID,
|
||||
RecipientName: r.Name,
|
||||
RecipientEmail: r.Email,
|
||||
RecipientPosition: r.Position,
|
||||
RecipientInstitution: r.Institution,
|
||||
IsPrimary: r.IsPrimary,
|
||||
}
|
||||
}
|
||||
if err := s.recipientRepo.CreateBulk(txCtx, recipients); err != nil {
|
||||
return err
|
||||
var attachments []entities.LetterOutgoingAttachment
|
||||
if len(req.Attachments) > 0 {
|
||||
attachments = make([]entities.LetterOutgoingAttachment, len(req.Attachments))
|
||||
for i, a := range req.Attachments {
|
||||
attachments[i] = entities.LetterOutgoingAttachment{
|
||||
FileURL: a.FileURL,
|
||||
FileName: a.FileName,
|
||||
FileType: a.FileType,
|
||||
UploadedBy: &req.UserID,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(req.Attachments) > 0 {
|
||||
attachments := make([]entities.LetterOutgoingAttachment, len(req.Attachments))
|
||||
for i, a := range req.Attachments {
|
||||
attachments[i] = entities.LetterOutgoingAttachment{
|
||||
LetterID: letter.ID,
|
||||
FileURL: a.FileURL,
|
||||
FileName: a.FileName,
|
||||
FileType: a.FileType,
|
||||
UploadedBy: &req.UserID,
|
||||
}
|
||||
}
|
||||
if err := s.attachmentRepo.CreateBulk(txCtx, attachments); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
activityLog := &entities.LetterOutgoingActivityLog{
|
||||
LetterID: letter.ID,
|
||||
ActionType: entities.LetterOutgoingActionCreated,
|
||||
ActorUserID: &req.UserID,
|
||||
}
|
||||
if err := s.activityLogRepo.Create(txCtx, activityLog); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
err := s.processor.CreateOutgoingLetter(ctx, letter, attachments, req.UserID, departmentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result, err := s.letterRepo.Get(ctx, letter.ID)
|
||||
result, err := s.processor.GetOutgoingLetterByID(ctx, letter.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -155,7 +91,7 @@ func (s *LetterOutgoingServiceImpl) CreateOutgoingLetter(ctx context.Context, re
|
||||
}
|
||||
|
||||
func (s *LetterOutgoingServiceImpl) GetOutgoingLetterByID(ctx context.Context, id uuid.UUID) (*contract.OutgoingLetterResponse, error) {
|
||||
letter, err := s.letterRepo.Get(ctx, id)
|
||||
letter, err := s.processor.GetOutgoingLetterByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -173,7 +109,7 @@ func (s *LetterOutgoingServiceImpl) ListOutgoingLetters(ctx context.Context, req
|
||||
ToDate: req.ToDate,
|
||||
}
|
||||
|
||||
letters, total, err := s.letterRepo.List(ctx, filter, req.Limit, req.Offset)
|
||||
letters, total, err := s.processor.ListOutgoingLetters(ctx, filter, req.Limit, req.Offset)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -192,7 +128,7 @@ func (s *LetterOutgoingServiceImpl) ListOutgoingLetters(ctx context.Context, req
|
||||
func (s *LetterOutgoingServiceImpl) UpdateOutgoingLetter(ctx context.Context, id uuid.UUID, req *contract.UpdateOutgoingLetterRequest) (*contract.OutgoingLetterResponse, error) {
|
||||
userID := getUserIDFromContext(ctx)
|
||||
|
||||
letter, err := s.letterRepo.Get(ctx, id)
|
||||
letter, err := s.processor.GetOutgoingLetterByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -220,28 +156,12 @@ func (s *LetterOutgoingServiceImpl) UpdateOutgoingLetter(ctx context.Context, id
|
||||
letter.ReferenceNumber = req.ReferenceNumber
|
||||
}
|
||||
|
||||
err = s.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
||||
if err := s.letterRepo.Update(txCtx, letter); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
activityLog := &entities.LetterOutgoingActivityLog{
|
||||
LetterID: letter.ID,
|
||||
ActionType: entities.LetterOutgoingActionUpdated,
|
||||
ActorUserID: &userID,
|
||||
}
|
||||
if err := s.activityLogRepo.Create(txCtx, activityLog); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
err = s.processor.UpdateOutgoingLetter(ctx, letter, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result, err := s.letterRepo.Get(ctx, id)
|
||||
result, err := s.processor.GetOutgoingLetterByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -252,7 +172,7 @@ func (s *LetterOutgoingServiceImpl) UpdateOutgoingLetter(ctx context.Context, id
|
||||
func (s *LetterOutgoingServiceImpl) DeleteOutgoingLetter(ctx context.Context, id uuid.UUID) error {
|
||||
userID := getUserIDFromContext(ctx)
|
||||
|
||||
letter, err := s.letterRepo.Get(ctx, id)
|
||||
letter, err := s.processor.GetOutgoingLetterByID(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -261,28 +181,13 @@ func (s *LetterOutgoingServiceImpl) DeleteOutgoingLetter(ctx context.Context, id
|
||||
return gorm.ErrInvalidData
|
||||
}
|
||||
|
||||
return s.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
||||
if err := s.letterRepo.SoftDelete(txCtx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
activityLog := &entities.LetterOutgoingActivityLog{
|
||||
LetterID: letter.ID,
|
||||
ActionType: entities.LetterOutgoingActionDeleted,
|
||||
ActorUserID: &userID,
|
||||
}
|
||||
if err := s.activityLogRepo.Create(txCtx, activityLog); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
return s.processor.DeleteOutgoingLetter(ctx, id, userID)
|
||||
}
|
||||
|
||||
func (s *LetterOutgoingServiceImpl) SubmitForApproval(ctx context.Context, letterID uuid.UUID) error {
|
||||
userID := getUserIDFromContext(ctx)
|
||||
|
||||
letter, err := s.letterRepo.Get(ctx, letterID)
|
||||
letter, err := s.processor.GetOutgoingLetterByID(ctx, letterID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -295,48 +200,13 @@ func (s *LetterOutgoingServiceImpl) SubmitForApproval(ctx context.Context, lette
|
||||
return gorm.ErrInvalidData
|
||||
}
|
||||
|
||||
flow, err := s.approvalFlowRepo.Get(ctx, *letter.ApprovalFlowID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
||||
approvals := make([]entities.LetterOutgoingApproval, len(flow.Steps))
|
||||
for i, step := range flow.Steps {
|
||||
approvals[i] = entities.LetterOutgoingApproval{
|
||||
LetterID: letterID,
|
||||
StepID: step.ID,
|
||||
Status: entities.ApprovalStatusPending,
|
||||
}
|
||||
}
|
||||
|
||||
if err := s.approvalRepo.CreateBulk(txCtx, approvals); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.letterRepo.UpdateStatus(txCtx, letterID, entities.LetterOutgoingStatusPendingApproval); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
activityLog := &entities.LetterOutgoingActivityLog{
|
||||
LetterID: letterID,
|
||||
ActionType: entities.LetterOutgoingActionSubmittedApproval,
|
||||
ActorUserID: &userID,
|
||||
FromStatus: ptr(string(entities.LetterOutgoingStatusDraft)),
|
||||
ToStatus: ptr(string(entities.LetterOutgoingStatusPendingApproval)),
|
||||
}
|
||||
if err := s.activityLogRepo.Create(txCtx, activityLog); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
return s.processor.ProcessApprovalSubmission(ctx, letterID, *letter.ApprovalFlowID, userID)
|
||||
}
|
||||
|
||||
func (s *LetterOutgoingServiceImpl) ApproveOutgoingLetter(ctx context.Context, letterID uuid.UUID, req *contract.ApproveLetterRequest) error {
|
||||
userID := getUserIDFromContext(ctx)
|
||||
|
||||
letter, err := s.letterRepo.Get(ctx, letterID)
|
||||
letter, err := s.processor.GetOutgoingLetterByID(ctx, letterID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -345,7 +215,7 @@ func (s *LetterOutgoingServiceImpl) ApproveOutgoingLetter(ctx context.Context, l
|
||||
return gorm.ErrInvalidData
|
||||
}
|
||||
|
||||
approvals, err := s.approvalRepo.ListByLetter(ctx, letterID)
|
||||
approvals, err := s.processor.GetApprovalsByLetter(ctx, letterID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -366,49 +236,23 @@ func (s *LetterOutgoingServiceImpl) ApproveOutgoingLetter(ctx context.Context, l
|
||||
return gorm.ErrInvalidData
|
||||
}
|
||||
|
||||
return s.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
||||
now := time.Now()
|
||||
currentApproval.Status = entities.ApprovalStatusApproved
|
||||
currentApproval.ApproverID = &userID
|
||||
currentApproval.ActedAt = &now
|
||||
currentApproval.Remarks = req.Remarks
|
||||
currentApproval.Remarks = req.Remarks
|
||||
|
||||
if err := s.approvalRepo.Update(txCtx, currentApproval); err != nil {
|
||||
return err
|
||||
allApproved := true
|
||||
for _, approval := range approvals {
|
||||
if approval.ID != currentApproval.ID && approval.Status == entities.ApprovalStatusPending {
|
||||
allApproved = false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
allApproved := true
|
||||
for _, approval := range approvals {
|
||||
if approval.ID != currentApproval.ID && approval.Status == entities.ApprovalStatusPending {
|
||||
allApproved = false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if allApproved {
|
||||
if err := s.letterRepo.UpdateStatus(txCtx, letterID, entities.LetterOutgoingStatusApproved); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
activityLog := &entities.LetterOutgoingActivityLog{
|
||||
LetterID: letterID,
|
||||
ActionType: entities.LetterOutgoingActionApproved,
|
||||
ActorUserID: &userID,
|
||||
TargetID: ¤tApproval.ID,
|
||||
}
|
||||
if err := s.activityLogRepo.Create(txCtx, activityLog); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
return s.processor.ProcessApproval(ctx, letterID, currentApproval, userID, allApproved)
|
||||
}
|
||||
|
||||
func (s *LetterOutgoingServiceImpl) RejectOutgoingLetter(ctx context.Context, letterID uuid.UUID, req *contract.RejectLetterRequest) error {
|
||||
userID := getUserIDFromContext(ctx)
|
||||
|
||||
letter, err := s.letterRepo.Get(ctx, letterID)
|
||||
letter, err := s.processor.GetOutgoingLetterByID(ctx, letterID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -417,7 +261,7 @@ func (s *LetterOutgoingServiceImpl) RejectOutgoingLetter(ctx context.Context, le
|
||||
return gorm.ErrInvalidData
|
||||
}
|
||||
|
||||
approvals, err := s.approvalRepo.ListByLetter(ctx, letterID)
|
||||
approvals, err := s.processor.GetApprovalsByLetter(ctx, letterID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -438,41 +282,15 @@ func (s *LetterOutgoingServiceImpl) RejectOutgoingLetter(ctx context.Context, le
|
||||
return gorm.ErrInvalidData
|
||||
}
|
||||
|
||||
return s.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
||||
now := time.Now()
|
||||
currentApproval.Status = entities.ApprovalStatusRejected
|
||||
currentApproval.ApproverID = &userID
|
||||
currentApproval.ActedAt = &now
|
||||
currentApproval.Remarks = &req.Reason
|
||||
currentApproval.Remarks = &req.Reason
|
||||
|
||||
if err := s.approvalRepo.Update(txCtx, currentApproval); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.letterRepo.UpdateStatus(txCtx, letterID, entities.LetterOutgoingStatusDraft); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
activityLog := &entities.LetterOutgoingActivityLog{
|
||||
LetterID: letterID,
|
||||
ActionType: entities.LetterOutgoingActionRejected,
|
||||
ActorUserID: &userID,
|
||||
TargetID: ¤tApproval.ID,
|
||||
FromStatus: ptr(string(entities.LetterOutgoingStatusPendingApproval)),
|
||||
ToStatus: ptr(string(entities.LetterOutgoingStatusDraft)),
|
||||
}
|
||||
if err := s.activityLogRepo.Create(txCtx, activityLog); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
return s.processor.ProcessRejection(ctx, letterID, currentApproval, userID)
|
||||
}
|
||||
|
||||
func (s *LetterOutgoingServiceImpl) SendOutgoingLetter(ctx context.Context, letterID uuid.UUID) error {
|
||||
userID := getUserIDFromContext(ctx)
|
||||
|
||||
letter, err := s.letterRepo.Get(ctx, letterID)
|
||||
letter, err := s.processor.GetOutgoingLetterByID(ctx, letterID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -481,30 +299,15 @@ func (s *LetterOutgoingServiceImpl) SendOutgoingLetter(ctx context.Context, lett
|
||||
return gorm.ErrInvalidData
|
||||
}
|
||||
|
||||
return s.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
||||
if err := s.letterRepo.UpdateStatus(txCtx, letterID, entities.LetterOutgoingStatusSent); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
activityLog := &entities.LetterOutgoingActivityLog{
|
||||
LetterID: letterID,
|
||||
ActionType: entities.LetterOutgoingActionSent,
|
||||
ActorUserID: &userID,
|
||||
FromStatus: ptr(string(entities.LetterOutgoingStatusApproved)),
|
||||
ToStatus: ptr(string(entities.LetterOutgoingStatusSent)),
|
||||
}
|
||||
if err := s.activityLogRepo.Create(txCtx, activityLog); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
fromStatus := string(entities.LetterOutgoingStatusApproved)
|
||||
toStatus := string(entities.LetterOutgoingStatusSent)
|
||||
return s.processor.UpdateLetterStatus(ctx, letterID, entities.LetterOutgoingStatusSent, userID, &fromStatus, &toStatus)
|
||||
}
|
||||
|
||||
func (s *LetterOutgoingServiceImpl) ArchiveOutgoingLetter(ctx context.Context, letterID uuid.UUID) error {
|
||||
userID := getUserIDFromContext(ctx)
|
||||
|
||||
letter, err := s.letterRepo.Get(ctx, letterID)
|
||||
letter, err := s.processor.GetOutgoingLetterByID(ctx, letterID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -513,30 +316,15 @@ func (s *LetterOutgoingServiceImpl) ArchiveOutgoingLetter(ctx context.Context, l
|
||||
return gorm.ErrInvalidData
|
||||
}
|
||||
|
||||
return s.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
||||
if err := s.letterRepo.UpdateStatus(txCtx, letterID, entities.LetterOutgoingStatusArchived); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
activityLog := &entities.LetterOutgoingActivityLog{
|
||||
LetterID: letterID,
|
||||
ActionType: entities.LetterOutgoingActionArchived,
|
||||
ActorUserID: &userID,
|
||||
FromStatus: ptr(string(entities.LetterOutgoingStatusSent)),
|
||||
ToStatus: ptr(string(entities.LetterOutgoingStatusArchived)),
|
||||
}
|
||||
if err := s.activityLogRepo.Create(txCtx, activityLog); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
fromStatus := string(entities.LetterOutgoingStatusSent)
|
||||
toStatus := string(entities.LetterOutgoingStatusArchived)
|
||||
return s.processor.UpdateLetterStatus(ctx, letterID, entities.LetterOutgoingStatusArchived, userID, &fromStatus, &toStatus)
|
||||
}
|
||||
|
||||
func (s *LetterOutgoingServiceImpl) AddRecipients(ctx context.Context, letterID uuid.UUID, req *contract.AddRecipientsRequest) error {
|
||||
userID := getUserIDFromContext(ctx)
|
||||
|
||||
letter, err := s.letterRepo.Get(ctx, letterID)
|
||||
letter, err := s.processor.GetOutgoingLetterByID(ctx, letterID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -557,26 +345,11 @@ func (s *LetterOutgoingServiceImpl) AddRecipients(ctx context.Context, letterID
|
||||
}
|
||||
}
|
||||
|
||||
return s.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
||||
if err := s.recipientRepo.CreateBulk(txCtx, recipients); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
activityLog := &entities.LetterOutgoingActivityLog{
|
||||
LetterID: letterID,
|
||||
ActionType: entities.LetterOutgoingActionRecipientAdded,
|
||||
ActorUserID: &userID,
|
||||
}
|
||||
if err := s.activityLogRepo.Create(txCtx, activityLog); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
return s.processor.AddRecipients(ctx, letterID, recipients, userID)
|
||||
}
|
||||
|
||||
func (s *LetterOutgoingServiceImpl) UpdateRecipient(ctx context.Context, letterID uuid.UUID, recipientID uuid.UUID, req *contract.UpdateRecipientRequest) error {
|
||||
letter, err := s.letterRepo.Get(ctx, letterID)
|
||||
letter, err := s.processor.GetOutgoingLetterByID(ctx, letterID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -594,13 +367,13 @@ func (s *LetterOutgoingServiceImpl) UpdateRecipient(ctx context.Context, letterI
|
||||
IsPrimary: req.IsPrimary,
|
||||
}
|
||||
|
||||
return s.recipientRepo.Update(ctx, recipient)
|
||||
return s.processor.UpdateRecipient(ctx, recipient)
|
||||
}
|
||||
|
||||
func (s *LetterOutgoingServiceImpl) RemoveRecipient(ctx context.Context, letterID uuid.UUID, recipientID uuid.UUID) error {
|
||||
userID := getUserIDFromContext(ctx)
|
||||
|
||||
letter, err := s.letterRepo.Get(ctx, letterID)
|
||||
letter, err := s.processor.GetOutgoingLetterByID(ctx, letterID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -609,29 +382,13 @@ func (s *LetterOutgoingServiceImpl) RemoveRecipient(ctx context.Context, letterI
|
||||
return gorm.ErrInvalidData
|
||||
}
|
||||
|
||||
return s.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
||||
if err := s.recipientRepo.Delete(txCtx, recipientID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
activityLog := &entities.LetterOutgoingActivityLog{
|
||||
LetterID: letterID,
|
||||
ActionType: entities.LetterOutgoingActionRecipientRemoved,
|
||||
ActorUserID: &userID,
|
||||
TargetID: &recipientID,
|
||||
}
|
||||
if err := s.activityLogRepo.Create(txCtx, activityLog); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
return s.processor.RemoveRecipient(ctx, letterID, recipientID, userID)
|
||||
}
|
||||
|
||||
func (s *LetterOutgoingServiceImpl) AddAttachments(ctx context.Context, letterID uuid.UUID, req *contract.AddAttachmentsRequest) error {
|
||||
userID := getUserIDFromContext(ctx)
|
||||
|
||||
letter, err := s.letterRepo.Get(ctx, letterID)
|
||||
letter, err := s.processor.GetOutgoingLetterByID(ctx, letterID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -651,28 +408,13 @@ func (s *LetterOutgoingServiceImpl) AddAttachments(ctx context.Context, letterID
|
||||
}
|
||||
}
|
||||
|
||||
return s.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
||||
if err := s.attachmentRepo.CreateBulk(txCtx, attachments); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
activityLog := &entities.LetterOutgoingActivityLog{
|
||||
LetterID: letterID,
|
||||
ActionType: entities.LetterOutgoingActionAttachmentAdded,
|
||||
ActorUserID: &userID,
|
||||
}
|
||||
if err := s.activityLogRepo.Create(txCtx, activityLog); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
return s.processor.AddAttachments(ctx, letterID, attachments, userID)
|
||||
}
|
||||
|
||||
func (s *LetterOutgoingServiceImpl) RemoveAttachment(ctx context.Context, letterID uuid.UUID, attachmentID uuid.UUID) error {
|
||||
userID := getUserIDFromContext(ctx)
|
||||
|
||||
letter, err := s.letterRepo.Get(ctx, letterID)
|
||||
letter, err := s.processor.GetOutgoingLetterByID(ctx, letterID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -681,23 +423,7 @@ func (s *LetterOutgoingServiceImpl) RemoveAttachment(ctx context.Context, letter
|
||||
return gorm.ErrInvalidData
|
||||
}
|
||||
|
||||
return s.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
||||
if err := s.attachmentRepo.Delete(txCtx, attachmentID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
activityLog := &entities.LetterOutgoingActivityLog{
|
||||
LetterID: letterID,
|
||||
ActionType: entities.LetterOutgoingActionAttachmentRemoved,
|
||||
ActorUserID: &userID,
|
||||
TargetID: &attachmentID,
|
||||
}
|
||||
if err := s.activityLogRepo.Create(txCtx, activityLog); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
return s.processor.RemoveAttachment(ctx, letterID, attachmentID, userID)
|
||||
}
|
||||
|
||||
func (s *LetterOutgoingServiceImpl) CreateDiscussion(ctx context.Context, letterID uuid.UUID, req *contract.CreateDiscussionRequest) (*contract.DiscussionResponse, error) {
|
||||
@@ -714,45 +440,27 @@ func (s *LetterOutgoingServiceImpl) CreateDiscussion(ctx context.Context, letter
|
||||
discussion.Mentions = req.Mentions
|
||||
}
|
||||
|
||||
err := s.txManager.WithTransaction(ctx, func(txCtx context.Context) error {
|
||||
if err := s.discussionRepo.Create(txCtx, discussion); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(req.Attachments) > 0 {
|
||||
attachments := make([]entities.LetterOutgoingDiscussionAttachment, len(req.Attachments))
|
||||
for i, a := range req.Attachments {
|
||||
attachments[i] = entities.LetterOutgoingDiscussionAttachment{
|
||||
DiscussionID: discussion.ID,
|
||||
FileURL: a.FileURL,
|
||||
FileName: a.FileName,
|
||||
FileType: a.FileType,
|
||||
UploadedBy: &userID,
|
||||
}
|
||||
}
|
||||
if err := s.discussionAttachmentRepo.CreateBulk(txCtx, attachments); err != nil {
|
||||
return err
|
||||
var attachments []entities.LetterOutgoingDiscussionAttachment
|
||||
if len(req.Attachments) > 0 {
|
||||
attachments = make([]entities.LetterOutgoingDiscussionAttachment, len(req.Attachments))
|
||||
for i, a := range req.Attachments {
|
||||
attachments[i] = entities.LetterOutgoingDiscussionAttachment{
|
||||
DiscussionID: discussion.ID,
|
||||
FileURL: a.FileURL,
|
||||
FileName: a.FileName,
|
||||
FileType: a.FileType,
|
||||
UploadedBy: &userID,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
activityLog := &entities.LetterOutgoingActivityLog{
|
||||
LetterID: letterID,
|
||||
ActionType: entities.LetterOutgoingActionDiscussionAdded,
|
||||
ActorUserID: &userID,
|
||||
TargetID: &discussion.ID,
|
||||
}
|
||||
if err := s.activityLogRepo.Create(txCtx, activityLog); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
err := s.processor.CreateDiscussion(ctx, discussion, attachments, userID)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result, err := s.discussionRepo.Get(ctx, discussion.ID)
|
||||
result, err := s.processor.GetDiscussionByID(ctx, discussion.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -761,7 +469,7 @@ func (s *LetterOutgoingServiceImpl) CreateDiscussion(ctx context.Context, letter
|
||||
}
|
||||
|
||||
func (s *LetterOutgoingServiceImpl) UpdateDiscussion(ctx context.Context, discussionID uuid.UUID, req *contract.UpdateDiscussionRequest) error {
|
||||
discussion, err := s.discussionRepo.Get(ctx, discussionID)
|
||||
discussion, err := s.processor.GetDiscussionByID(ctx, discussionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -776,11 +484,11 @@ func (s *LetterOutgoingServiceImpl) UpdateDiscussion(ctx context.Context, discus
|
||||
discussion.Mentions = req.Mentions
|
||||
}
|
||||
|
||||
return s.discussionRepo.Update(ctx, discussion)
|
||||
return s.processor.UpdateDiscussion(ctx, discussion)
|
||||
}
|
||||
|
||||
func (s *LetterOutgoingServiceImpl) DeleteDiscussion(ctx context.Context, discussionID uuid.UUID) error {
|
||||
discussion, err := s.discussionRepo.Get(ctx, discussionID)
|
||||
discussion, err := s.processor.GetDiscussionByID(ctx, discussionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -790,21 +498,29 @@ func (s *LetterOutgoingServiceImpl) DeleteDiscussion(ctx context.Context, discus
|
||||
return gorm.ErrInvalidData
|
||||
}
|
||||
|
||||
return s.discussionRepo.Delete(ctx, discussionID)
|
||||
return s.processor.DeleteDiscussion(ctx, discussionID)
|
||||
}
|
||||
|
||||
func getUserIDFromContext(ctx context.Context) uuid.UUID {
|
||||
appCtx := appcontext.FromGinContext(ctx)
|
||||
if appCtx != nil {
|
||||
return appCtx.UserID
|
||||
}
|
||||
return uuid.New()
|
||||
}
|
||||
|
||||
func getDepartmentIDFromContext(ctx context.Context) uuid.UUID {
|
||||
appCtx := appcontext.FromGinContext(ctx)
|
||||
if appCtx != nil {
|
||||
return appCtx.DepartmentID
|
||||
}
|
||||
return uuid.Nil
|
||||
}
|
||||
|
||||
func userHasRole(ctx context.Context, roleID uuid.UUID) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func ptr(s string) *string {
|
||||
return &s
|
||||
}
|
||||
|
||||
func transformLetterToResponse(letter *entities.LetterOutgoing) *contract.OutgoingLetterResponse {
|
||||
resp := &contract.OutgoingLetterResponse{
|
||||
ID: letter.ID,
|
||||
|
||||
@@ -16,10 +16,11 @@ type MasterServiceImpl struct {
|
||||
priorityRepo *repository.PriorityRepository
|
||||
institutionRepo *repository.InstitutionRepository
|
||||
dispRepo *repository.DispositionActionRepository
|
||||
departmentRepo *repository.DepartmentRepository
|
||||
}
|
||||
|
||||
func NewMasterService(label *repository.LabelRepository, priority *repository.PriorityRepository, institution *repository.InstitutionRepository, disp *repository.DispositionActionRepository) *MasterServiceImpl {
|
||||
return &MasterServiceImpl{labelRepo: label, priorityRepo: priority, institutionRepo: institution, dispRepo: disp}
|
||||
func NewMasterService(label *repository.LabelRepository, priority *repository.PriorityRepository, institution *repository.InstitutionRepository, disp *repository.DispositionActionRepository, department *repository.DepartmentRepository) *MasterServiceImpl {
|
||||
return &MasterServiceImpl{labelRepo: label, priorityRepo: priority, institutionRepo: institution, dispRepo: disp, departmentRepo: department}
|
||||
}
|
||||
|
||||
// Labels
|
||||
@@ -212,3 +213,34 @@ func (s *MasterServiceImpl) ListDispositionActions(ctx context.Context) (*contra
|
||||
}
|
||||
return &contract.ListDispositionActionsResponse{Actions: transformer.DispositionActionsToContract(list)}, nil
|
||||
}
|
||||
|
||||
// Departments
|
||||
func (s *MasterServiceImpl) ListDepartments(ctx context.Context, req *contract.ListDepartmentsRequest) (*contract.ListDepartmentsResponse, error) {
|
||||
// Set default values if not provided
|
||||
page := req.Page
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
|
||||
limit := req.Limit
|
||||
if limit < 1 {
|
||||
limit = 10
|
||||
}
|
||||
if limit > 100 {
|
||||
limit = 100 // Max limit to prevent performance issues
|
||||
}
|
||||
|
||||
offset := (page - 1) * limit
|
||||
|
||||
list, total, err := s.departmentRepo.List(ctx, req.Search, limit, offset)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &contract.ListDepartmentsResponse{
|
||||
Departments: transformer.DepartmentsToContract(list),
|
||||
Total: total,
|
||||
Page: page,
|
||||
Limit: limit,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user