diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/entity/DeferredMessage.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/entity/DeferredMessage.java new file mode 100644 index 0000000..5068ed9 --- /dev/null +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/entity/DeferredMessage.java @@ -0,0 +1,26 @@ +package dev.struchkov.bot.gitlab.context.domain.entity; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; + +import java.time.LocalDateTime; + +@Getter +@Setter +@Entity +@Table(name = "deferred_messages") +@EqualsAndHashCode(onlyExplicitlyIncluded = true) +public class DeferredMessage { + @Id + @Column(name = "id") + private int id; + @Column(name = "message") + private String message; + @Column (name = "time") + private LocalDateTime time; +} diff --git a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ConflictMrNotify.java b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ConflictMrNotify.java index b8ea8e2..c129d49 100644 --- a/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ConflictMrNotify.java +++ b/bot-context/src/main/java/dev/struchkov/bot/gitlab/context/domain/notify/mergerequest/ConflictMrNotify.java @@ -1,5 +1,6 @@ package dev.struchkov.bot.gitlab.context.domain.notify.mergerequest; +import dev.struchkov.bot.gitlab.context.domain.notify.Notify; import lombok.Builder; import lombok.Getter; diff --git a/bot-core/pom.xml b/bot-core/pom.xml index 0063051..73a192b 100644 --- a/bot-core/pom.xml +++ b/bot-core/pom.xml @@ -90,6 +90,15 @@ jakarta.el 4.0.2 + + com.google.code.gson + gson + 2.9.0 + + + dev.struchkov.bot.gitlab + bot-data + \ No newline at end of file diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/DelayedNotifyServiceImpl.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/DelayedNotifyServiceImpl.java new file mode 100644 index 0000000..4fd9686 --- /dev/null +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/DelayedNotifyServiceImpl.java @@ -0,0 +1,71 @@ +package dev.struchkov.bot.gitlab.core.service.impl; + +import com.google.gson.Gson; +import dev.struchkov.bot.gitlab.context.domain.entity.DeferredMessage; +import dev.struchkov.bot.gitlab.context.domain.notify.Notify; +import dev.struchkov.bot.gitlab.context.domain.notify.comment.NewCommentNotify; +import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.*; +import dev.struchkov.bot.gitlab.context.domain.notify.pipeline.PipelineNotify; +import dev.struchkov.bot.gitlab.context.domain.notify.project.NewProjectNotify; +import dev.struchkov.bot.gitlab.context.domain.notify.task.DiscussionNewNotify; +import dev.struchkov.bot.gitlab.context.domain.notify.task.TaskCloseNotify; +import dev.struchkov.bot.gitlab.data.jpa.DelayedNotifyJpaRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.time.LocalDateTime; + +@Service +@RequiredArgsConstructor +public class DelayedNotifyServiceImpl { + + private final DelayedNotifyJpaRepository delayedNotifyJpaRepository; + + public void save(T notify) { + + Gson gson = new Gson(); + DeferredMessage deferredMessage = new DeferredMessage(); + String notifyForSave = gson.toJson(notify); + deferredMessage.setMessage(notifyForSave); + deferredMessage.setTime(LocalDateTime.now().plusNanos(0)); + delayedNotifyJpaRepository.save(deferredMessage); + } + + public void getNotifyFromDb(T notify){ + Class newNotify; + + switch (notify.getType()) { + case ConflictMrNotify.TYPE: + newNotify = ConflictMrNotify.class; + break; + case DiscussionNewNotify.TYPE: + newNotify = DiscussionNewNotify.class; + break; + case NewCommentNotify.TYPE: + newNotify = NewCommentNotify.class; + break; + case NewMrForAssignee.TYPE: + newNotify = NewMrForAssignee.class; + break; + case NewMrForReview.TYPE: + newNotify = NewMrForReview.class; + break; + case NewProjectNotify.TYPE: + newNotify = NewProjectNotify.class; + break; + case PipelineNotify.TYPE: + newNotify = PipelineNotify.class; + break; + case StatusMrNotify.TYPE: + newNotify = StatusMrNotify.class; + break; + case TaskCloseNotify.TYPE: + newNotify = TaskCloseNotify.class; + break; + case UpdateMrNotify.TYPE: + newNotify = UpdateMrNotify.class; + break; + } + } + +} diff --git a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/NotifyServiceImpl.java b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/NotifyServiceImpl.java index 336ad0b..8455240 100644 --- a/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/NotifyServiceImpl.java +++ b/bot-core/src/main/java/dev/struchkov/bot/gitlab/core/service/impl/NotifyServiceImpl.java @@ -12,18 +12,21 @@ public class NotifyServiceImpl implements NotifyService { private final MessageSendService messageSendService; private final AppSettingService settingService; + private final DelayedNotifyServiceImpl delayedNotifyService; public NotifyServiceImpl( @Lazy MessageSendService messageSendService, - AppSettingService settingService - ) { + AppSettingService settingService, + DelayedNotifyServiceImpl delayedNotifyService) { this.messageSendService = messageSendService; this.settingService = settingService; + this.delayedNotifyService = delayedNotifyService; } @Override public void send(T notify) { if (settingService.isEnableAllNotify()) { + delayedNotifyService.save(notify); messageSendService.send(notify); } } diff --git a/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/DelayedNotifyJpaRepository.java b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/DelayedNotifyJpaRepository.java new file mode 100644 index 0000000..ab2daf3 --- /dev/null +++ b/bot-data/src/main/java/dev/struchkov/bot/gitlab/data/jpa/DelayedNotifyJpaRepository.java @@ -0,0 +1,7 @@ +package dev.struchkov.bot.gitlab.data.jpa; + +import dev.struchkov.bot.gitlab.context.domain.entity.DeferredMessage; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface DelayedNotifyJpaRepository extends JpaRepository { +}