The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Split the campaign send checks from the send preparation (#28357)
neo773db199f2194d2
diff --git a/packages/twenty-server/src/modules/emailing/services/message-campaign-schedule.service.ts b/packages/twenty-server/src/modules/emailing/services/message-campaign-schedule.service.tsindex df323e53d15..c3c08407509 100644--- a/packages/twenty-server/src/modules/emailing/services/message-campaign-schedule.service.ts+++ b/packages/twenty-server/src/modules/emailing/services/message-campaign-schedule.service.ts@@ -42,13 +42,6 @@ export class MessageCampaignScheduleService {campaignId: string;scheduledAt: Date;}): Promise<SendCampaignResult> {- if (scheduledAt.getTime() <= Date.now()) {- throw new EmailingDomainException(- `Campaign ${campaignId} cannot be scheduled for ${scheduledAt.toISOString()}, which is not in the future`,- EmailingDomainExceptionCode.MESSAGE_CAMPAIGN_SCHEDULE_NOT_IN_FUTURE,- );- }-const {roleId,sendableRecipients,@@ -59,6 +52,7 @@ export class MessageCampaignScheduleService {workspaceId,userWorkspaceId,campaignId,+ requestedScheduledAt: scheduledAt,});const scheduled =diff --git a/packages/twenty-server/src/modules/emailing/services/message-campaign.service.ts b/packages/twenty-server/src/modules/emailing/services/message-campaign.service.tsindex 51247190eb3..a3d7e76d3d8 100644--- a/packages/twenty-server/src/modules/emailing/services/message-campaign.service.ts+++ b/packages/twenty-server/src/modules/emailing/services/message-campaign.service.ts@@ -94,16 +94,74 @@ export class MessageCampaignService {workspaceId,userWorkspaceId,campaignId,+ requestedScheduledAt,}: {workspaceId: string;userWorkspaceId: string;campaignId: string;+ requestedScheduledAt?: Date;}): Promise<PreparedCampaignSend> {const roleId = await this.userRoleService.getRoleIdForUserWorkspace({workspaceId,userWorkspaceId,});+ const {+ fromAddress,+ status,+ scheduledAt: currentScheduledAt,+ emailingDomain,+ sendableRecipients,+ audience,+ } = await this.checkCampaignSendOrThrow({+ workspaceId,+ userWorkspaceId,+ campaignId,+ roleId,+ requestedScheduledAt,+ });++ const messageChannel =+ await this.messageChannelMetadataService.getOrCreateEmailGroupChannel({+ fromAddress,+ userWorkspaceId,+ workspaceId,+ });++ return {+ roleId,+ emailingDomainId: emailingDomain.id,+ messageChannelId: messageChannel.id,+ sendableRecipients,+ audience,+ expectedStatus: status,+ expectedScheduledAt: currentScheduledAt ?? null,+ };+ }++ private async checkCampaignSendOrThrow({+ workspaceId,+ userWorkspaceId,+ campaignId,+ roleId,+ requestedScheduledAt,+ }: {+ workspaceId: string;+ userWorkspaceId: string;+ campaignId: string;+ roleId: string;+ requestedScheduledAt?: Date;+ }) {+ if (+ isDefined(requestedScheduledAt) &&+ requestedScheduledAt.getTime() <= Date.now()+ ) {+ throw new EmailingDomainException(+ `Campaign ${campaignId} cannot be scheduled for ${requestedScheduledAt.toISOString()}, which is not in the future`,+ EmailingDomainExceptionCode.MESSAGE_CAMPAIGN_SCHEDULE_NOT_IN_FUTURE,+ );+ }+const { fromAddress, listId, unsubscribeTopicId, status, scheduledAt } =await this.workspaceOrmManager.executeInWorkspaceContext(async () => {const { fromAddress, listId, unsubscribeTopicId, status, scheduledAt } =@@ -148,21 +206,13 @@ export class MessageCampaignService {});}- const messageChannel =- await this.messageChannelMetadataService.getOrCreateEmailGroupChannel({- fromAddress,- userWorkspaceId,- workspaceId,- });-return {- roleId,- emailingDomainId: emailingDomain.id,- messageChannelId: messageChannel.id,+ fromAddress,+ status,+ scheduledAt,+ emailingDomain,sendableRecipients,audience,- expectedStatus: status,- expectedScheduledAt: scheduledAt ?? null,};}