-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCreateTaskService.java
More file actions
96 lines (77 loc) · 4.08 KB
/
CreateTaskService.java
File metadata and controls
96 lines (77 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package clap.server.application.service.task;
import clap.server.adapter.inbound.web.dto.notification.SseRequest;
import clap.server.adapter.inbound.web.dto.task.CreateTaskRequest;
import clap.server.adapter.inbound.web.dto.task.CreateTaskResponse;
import clap.server.adapter.outbound.infrastructure.s3.S3UploadAdapter;
import clap.server.adapter.outbound.persistense.entity.notification.constant.NotificationType;
import clap.server.application.mapper.AttachmentMapper;
import clap.server.application.mapper.TaskMapper;
import clap.server.application.port.inbound.domain.CategoryService;
import clap.server.application.port.inbound.domain.MemberService;
import clap.server.application.port.inbound.task.CreateTaskUsecase;
import clap.server.application.port.outbound.task.CommandAttachmentPort;
import clap.server.application.port.outbound.task.CommandTaskPort;
import clap.server.application.service.notification.SendWebhookService;
import clap.server.common.annotation.architecture.ApplicationService;
import clap.server.domain.model.member.Member;
import clap.server.domain.model.notification.Notification;
import clap.server.domain.model.task.Attachment;
import clap.server.domain.model.task.Category;
import clap.server.common.constants.FilePathConstants;
import clap.server.domain.model.task.Task;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import static clap.server.domain.model.notification.Notification.createTaskNotification;
@ApplicationService
@RequiredArgsConstructor
public class CreateTaskService implements CreateTaskUsecase {
private final MemberService memberService;
private final CategoryService categoryService;
private final CommandTaskPort commandTaskPort;
private final CommandAttachmentPort commandAttachmentPort;
private final S3UploadAdapter s3UploadAdapter;
private final SendWebhookService sendWebhookService;
private final ApplicationEventPublisher applicationEventPublisher;
@Override
@Transactional
public CreateTaskResponse createTask(Long requesterId, CreateTaskRequest createTaskRequest, List<MultipartFile> files) {
Member member = memberService.findActiveMember(requesterId);
Category category = categoryService.findById(createTaskRequest.categoryId());
Task task = Task.createTask(member, category, createTaskRequest.title(), createTaskRequest.description());
Task savedTask = commandTaskPort.save(task);
savedTask.setInitialProcessorOrder();
commandTaskPort.save(savedTask);
if (files != null) {
saveAttachments(files, savedTask);
}
publishNotification(savedTask);
return TaskMapper.toCreateTaskResponse(savedTask);
}
private void saveAttachments(List<MultipartFile> files, Task task) {
List<String> fileUrls = s3UploadAdapter.uploadFiles(FilePathConstants.TASK_IMAGE, files);
List<Attachment> attachments = AttachmentMapper.toTaskAttachments(task, files, fileUrls);
commandAttachmentPort.saveAll(attachments);
}
private void publishNotification(Task task) {
List<Member> reviewers = memberService.findReviewers();
// 검토자들 각각에 대한 알림 생성 후 event 발행
for (Member reviewer : reviewers) {
// 알림 저장
Notification notification = createTaskNotification(task, reviewer, NotificationType.TASK_REQUESTED);
applicationEventPublisher.publishEvent(notification);
// SSE 실시간 알림 전송
SseRequest sseRequest = new SseRequest(
notification.getTask().getTitle(),
notification.getType(),
reviewer.getMemberId(),
null
);
applicationEventPublisher.publishEvent(sseRequest);
sendWebhookService.sendWebhookNotification(reviewer, NotificationType.TASK_REQUESTED,
task, null, null);
}
}
}