Skip to content

Commit

Permalink
Merge pull request #109 from ttthanhf/hotfix/feature/task
Browse files Browse the repository at this point in the history
Hotfix/feature/task - Fix sending notification
  • Loading branch information
ttthanhf committed Nov 14, 2023
2 parents ff9acdc + a2c6e44 commit 1222213
Showing 1 changed file with 58 additions and 45 deletions.
103 changes: 58 additions & 45 deletions src/main/java/housemate/services/TaskBuildupService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import static housemate.constants.ServiceConfiguration.*;

import housemate.constants.AccountStatus;
import housemate.constants.Enum.TaskMessType;
import housemate.constants.Enum.TaskReportType;
Expand Down Expand Up @@ -67,7 +64,6 @@
import housemate.repositories.UserRepository;
import housemate.repositories.UserUsageRepository;
import housemate.responses.TaskRes;
import jakarta.annotation.PostConstruct;
import jakarta.transaction.Transactional;

@Component
Expand Down Expand Up @@ -115,7 +111,7 @@ public class TaskBuildupService {
private TaskScheduler taskScheduler;

@Autowired
private static NotificationService notificationService;
private NotificationService notificationService;

private final ZoneId dateTimeZone = ZoneId.of("Asia/Ho_Chi_Minh");

Expand Down Expand Up @@ -144,21 +140,22 @@ public List<Task> createTasksOnUpcomingSchedulesAutoByFixedRate() {
return List.of();
for (Schedule schedule : schedules) {
Task task = this.createTask(schedule);
if (task != null)
taskList.add(this.createTask(schedule));
if (task != null)
taskList.add(task);
}
if (!taskList.isEmpty()) {
if (taskList != null || !taskList.isEmpty()) {
taskList.stream().forEach(x -> {
// TODO: NOTI TO CUSTOMER FOR TASK
TaskBuildupService.createAndSendNotification(
this.createAndSendNotification(
x, // Task
"Chờ tìm nhân viên",
"Đang tìm kiếm nhân viên", // Mess
String.valueOf(x.getSchedule().getCustomerId())); // Receiver
String.valueOf(x.getSchedule().getCustomerId())
); // Receiver

});
// TODO: NOTI NEW TASK LIST FOR STAFF
TaskBuildupService.createAndSendNotification(
this.createAndSendNotification(
null,
"Việc mới",
"Việc mới ngày " + LocalDate.now().getDayOfMonth(),
Expand All @@ -178,17 +175,23 @@ public List<Task> createTaskOnUpComingSchedule(Schedule newSchedule) {
try {
if (schedules.isEmpty())
return List.of();
for (Schedule theSchedule : schedules)
taskList.add(this.createTask(theSchedule));

for (Schedule schedule : schedules) {
Task task = this.createTask(schedule);
if (task != null)
taskList.add(task);
}
if (!taskList.isEmpty()) {
// TODO: NOTI NEW TASK LIST FOR STAFF
TaskBuildupService.createAndSendNotification(
null,
"Việc mới",
"Việc mới ngày " + LocalDate.now().getDayOfMonth(),
Role.STAFF.name());
taskList.stream().forEach(x -> {
// TODO: NOTI TO CUSTOMER FOR TASK
this.createAndSendNotification(
x, // Task
"Chờ tìm nhân viên",
"Đang tìm kiếm nhân viên", // Mess
String.valueOf(x.getSchedule().getCustomerId())
); // Receiver
});
}

} catch (Exception e) {
e.printStackTrace();
return List.of();
Expand Down Expand Up @@ -298,7 +301,7 @@ public Task cancelTaskByRole(Role role, Schedule scheduleHasTaskToBeCancelled, S

//TODO: NOTI CANCEL TO STAFF
if (taskToBeCancelled != null && taskToBeCancelled.getStaff() != null) {
TaskBuildupService.createAndSendNotification(
this.createAndSendNotification(
taskToBeCancelled,
"Hủy lịch",
"Khách hàng hủy lịch làm việc ngày " + dateFormat.format(
Expand All @@ -311,7 +314,7 @@ public Task cancelTaskByRole(Role role, Schedule scheduleHasTaskToBeCancelled, S

// TODO: NOTI CANCEL TO CUSTOMER
if (taskToBeCancelled != null && taskToBeCancelled.getStaff() != null)
TaskBuildupService.createAndSendNotification(
this.createAndSendNotification(
taskToBeCancelled,
"Hủy công việc",
"Nhân viên hủy lịch làm việc, vui lòng chờ nhân viên khác",
Expand Down Expand Up @@ -386,7 +389,7 @@ public TaskRes<Map<String, Task>> updateTaskOnScheduleChangeTime(Schedule schedu

// TODO: NOTI CANCEL TASK FOR CHANGE TIME WORKING TO STAFF
if (oldTask.getStaffId() != null) {
TaskBuildupService.createAndSendNotification(
this.createAndSendNotification(
oldTask,
"Hủy công việc",
"Lich công việc bị hủy do khách hàng đổi giờ làm việc ",
Expand Down Expand Up @@ -425,7 +428,14 @@ public TaskRes<Task> approveQualifiedStaff(Staff staff, Task task) {

//MARKUP CALL EVENTS FOR REPORT TASK
this.createEventForReportTask(task, task.getSchedule().getStartDate(), task.getSchedule().getEndDate());

// TODO: NOTI APPROVE STAFF
this.createAndSendNotification(
task,
"Công việc mới",
"Công việc mới của bạn đã được lên lịch làm việc ",
String.valueOf(task.getStaffId()));
log.info("TASK ĐÂY");

} catch (Exception e) {
e.printStackTrace();
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
Expand Down Expand Up @@ -472,7 +482,7 @@ public TaskRes<TaskReport> reportTask(Task task, TaskReportType taskReport, Task
taskReportResult.setTaskStatus(task.getTaskStatus());

// TODO: NOTI STAFF ARRIVED TO CUSTOMER
TaskBuildupService.createAndSendNotification(
this.createAndSendNotification(
task,
notiTitleForTaskStatus,
"Nhân viên " + task.getStaff().getStaffInfo().getFullName() + "\"Đang đến\"",
Expand All @@ -492,7 +502,7 @@ public TaskRes<TaskReport> reportTask(Task task, TaskReportType taskReport, Task
taskReportResult.setTaskStatus(task.getTaskStatus());

// TODO: NOTI STAFF DOING TO CUSTOMER
TaskBuildupService.createAndSendNotification(
this.createAndSendNotification(
task,
notiTitleForTaskStatus,
"Nhân viên " + task.getStaff().getStaffInfo().getFullName() + "\"Đang làm việc\"",
Expand Down Expand Up @@ -547,7 +557,7 @@ public TaskRes<TaskReport> reportTask(Task task, TaskReportType taskReport, Task


// TODO: NOTI STAFF DONE TO CUSTOMER
TaskBuildupService.createAndSendNotification(
this.createAndSendNotification(
task,
notiTitleForTaskStatus,
"Nhân viên " + task.getStaff().getStaffInfo().getFullName() + "\"Đã hoàn thành công việc\"",
Expand All @@ -571,19 +581,21 @@ public TaskRes<TaskReport> reportTask(Task task, TaskReportType taskReport, Task
}

//MARKUP === SETTING NOTIFICATION===
public static void createAndSendNotification(Task task, String title, String message, String userId) {
public void createAndSendNotification(Task task, String title, String message, String userId) {
// TODO: BUILD MESSAGE
title = task.getSchedule().getService().getTitleName() + " " + title;
int entityId = task.getScheduleId();
notificationService.createNotification(userId, message, title, entityId);
// TODO SEND NOTIFICATION
String serviceName = task.getSchedule() == null ? "" : task.getSchedule().getService().getTitleName();
title = serviceName + " " + title;
int entityId = task.getScheduleId();
notificationService.createNotification(userId, message, title, entityId);
log.info("SEND NOTIFICATION");
}

//MARKUP ===CREATE EVENTS===
private void createEventSendNotiWhenTimeComing(Task theTask, LocalDateTime timeStartTask) {
ZonedDateTime timeStartTaskZone = timeStartTask.atZone(dateTimeZone);
Instant timeSendNotiInstant = timeStartTaskZone.toInstant();

TaskBuildupService taskBuidServ = this;

Runnable runnableTask = new Runnable() {
@Override
public void run() {
Expand All @@ -597,7 +609,7 @@ public void run() {
taskRepo.save(task);

//TODO: NOTI FOR NOT FOUNDED STAFF TO CUSTOMER
TaskBuildupService.createAndSendNotification(
taskBuidServ.createAndSendNotification(
task,
"Hủy lịch",
"Hủy lịch do không tìm thấy nhân viên",
Expand All @@ -608,7 +620,7 @@ public void run() {
if (task.getStaffId() != null) {

//TODO: NOTI FOR STAFF IS COMING TO CUSTOMER
TaskBuildupService.createAndSendNotification(
taskBuidServ.createAndSendNotification(
task,
notiTitleForTaskStatus,
"Nhân viên đang sắp tới phục vụ bạn. Mở cửa cho nhân viên khi tới nhé",
Expand All @@ -632,16 +644,16 @@ public void run() {
private void createEventSendNotiUpcomingTask(Task theTask, LocalDateTime timeStartTask, int periodHourBeforeFrMinutess) {
ZonedDateTime timeStartTaskZone = timeStartTask.atZone(dateTimeZone).minusMinutes(periodHourBeforeFrMinutess);
Instant timeSendNotiInstant = timeStartTaskZone.toInstant();
TaskBuildupService taskBuidServ = this;

Runnable runnableTask = new Runnable() {

@Override
public void run() {
Task task = taskRepo.findById(theTask.getTaskId()).get();

if (task.getStaffId() == null) {
//TODO: NOTI TIME WORKING UPCOMING BUT NOT FOUND STAFF TO CUSTOMER
TaskBuildupService.createAndSendNotification(
taskBuidServ.createAndSendNotification(
task,
notiTitleForTaskStatus,
"Vẫn chưa tìm được nhân viên. Hãy đợi thêm.",
Expand All @@ -658,12 +670,12 @@ public void run() {
taskRepo.save(task);

//TODO: NOTI TIME WORRKING UPCOMING TO STAFF AND CUSTOMER
TaskBuildupService.createAndSendNotification(
taskBuidServ.createAndSendNotification(
task,
notiTitleForTaskStatus,
"Nhân viên sắp đến. Hãy mở cửa nhé !",
String.valueOf(task.getSchedule().getCustomerId()));
TaskBuildupService.createAndSendNotification(
taskBuidServ.createAndSendNotification(
task,
notiTitleForTaskStatus,
"Hãy chuẩn bị đến giờ làm việc tại nhà khách hàng " + task.getSchedule().getCustomerId()
Expand Down Expand Up @@ -702,6 +714,7 @@ private void createEventForReportTask(Task theTask, LocalDateTime timeStartWorki
//Trigger 4
Instant timeCancelTaskForNotReportDone = timeEndWorking.plusMinutes(DURATION_HOURS_SYST_AUTO_DONE_TASK.getNum()).atZone(dateTimeZone).toInstant();

TaskBuildupService taskBuidServ = this;
//Trigger 1
{
Runnable eventMinusScoreForNotReportArrived = new Runnable() {
Expand All @@ -718,7 +731,7 @@ public void run() {
staffRepo.save(staff);

// TODO: NOTI MINUS THE PROFICIENT SCORE FOR REPORT ARRIVED TO STAFF
TaskBuildupService.createAndSendNotification(
taskBuidServ.createAndSendNotification(
task,
notiTitleForTaskStatus,
"Trừ " + MINUS_POINTS_FOR_NOT_COMPLETE_REPORT_TASK + " do trễ hẹn báo cáo trạng thái \"Đã đến\"",
Expand Down Expand Up @@ -765,14 +778,14 @@ public void run() {
scheduleRepo.save(schedule);

// TODO: NOTI CANCEL TASK FOR NOT REPORT DOING TO STAFF
TaskBuildupService.createAndSendNotification(
taskBuidServ.createAndSendNotification(
task,
"Hủy công việc",
"Hủy do không báo cáo tiến trình \"Đang làm việc\" ",
String.valueOf(task.getSchedule().getStaffId()));

// TODO: NOTI CANCEL SCHEDULE WHEN STAFF NOT REPORT DOING TO CUSTOMER
TaskBuildupService.createAndSendNotification(
taskBuidServ.createAndSendNotification(
task,
"Hủy lịch",
"Hủy do nhân viên không báo cáo tiến trình làm việc.",
Expand Down Expand Up @@ -818,13 +831,13 @@ public void run() {
taskRepo.save(task);

// TODO: NOTI CANCEL SCHEDULE WHEN STAFF NOT REPORT DONE TO CUSTOMER
TaskBuildupService.createAndSendNotification(
taskBuidServ.createAndSendNotification(
task,
"Hủy lịch",
"Hủy do nhân viên không báo cáo tiến trình làm việc.",
String.valueOf(task.getSchedule().getCustomerId()));
// TODO: NOTI TO STAFF FOR NOT REPORT STATUS DONE
TaskBuildupService.createAndSendNotification(
taskBuidServ.createAndSendNotification(
task,
"Hủy công việc",
"Hủy do không hoàn thành báo cáo tiến trình \"Hoàn thành\"",
Expand Down

0 comments on commit 1222213

Please sign in to comment.