From 1cef92c449164f1f2d6cfca16712a3c8c2e1566e Mon Sep 17 00:00:00 2001 From: Kheireddine Mehdi Date: Thu, 23 Oct 2025 11:04:53 +0100 Subject: [PATCH] feat: add cron job to resend unsent emails --- .../axelor/apps/message/job/SendMailJob.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 modules/axelor-open-suite/axelor-message/src/main/java/com/axelor/apps/message/job/SendMailJob.java diff --git a/modules/axelor-open-suite/axelor-message/src/main/java/com/axelor/apps/message/job/SendMailJob.java b/modules/axelor-open-suite/axelor-message/src/main/java/com/axelor/apps/message/job/SendMailJob.java new file mode 100644 index 0000000..8124d11 --- /dev/null +++ b/modules/axelor-open-suite/axelor-message/src/main/java/com/axelor/apps/message/job/SendMailJob.java @@ -0,0 +1,81 @@ +/* + * Axelor Business Solutions + * + * Copyright (C) 2019 Axelor (). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package com.axelor.apps.message.job; + +import com.axelor.apps.message.db.Message; +import com.axelor.apps.message.db.repo.MessageRepository; +import com.axelor.apps.message.service.MessageService; +import com.axelor.exception.service.TraceBackService; +import com.google.inject.Inject; +import java.io.IOException; +import java.time.LocalDateTime; +import java.time.LocalDate; +import java.util.List; +import javax.mail.MessagingException; +import com.axelor.exception.AxelorException; +import org.quartz.Job; +import org.quartz.JobExecutionContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** An example {@link Job} class that prints a some messages to the stderr. */ +public class SendEmailJob implements Job { + + private final Logger log = LoggerFactory.getLogger(SendEmailJob.class); + + @Inject private MessageService messageService; + + @Inject private MessageRepository messageRepository; + + @Override + public void execute(JobExecutionContext context) { + + LocalDate today = LocalDate.now(); + LocalDateTime startOfDay = today.atStartOfDay(); + + List draftMessages = messageRepository + .all() + .filter("self.statusSelect = 1 AND self.createdOn >= :startOfDay") + .bind("startOfDay", startOfDay) + .fetch(); + + log.debug("Total of draft messages : {}", draftMessages.size()); + for (Message message : draftMessages) { + try { + Message m = messageService.sendMessage(message); + } catch (AxelorException e) { + TraceBackService.trace(e); + } + } + + List inProgressMessages = messageRepository + .all() + .filter("self.statusSelect = 2 AND self.createdOn >= :startOfDay") + .bind("startOfDay", startOfDay) + .fetch(); + + log.debug("Total of in progress messages : {}", inProgressMessages.size()); + for (Message message : inProgressMessages) { + try { + Message m = messageService.sendMessage(message); + } catch (AxelorException e) { + TraceBackService.trace(e); + } + } + } +} \ No newline at end of file