Skip to content

Commit

Permalink
fix validation error when saving campaigns: IsUndefined validator cor…
Browse files Browse the repository at this point in the history
…rectly throwed an error when sending null (#36)

* fix validation error when saving campaigns: IsUndefined validator throwed an correctly throwed an error when sending null

* update scheduledAt logic to remove the previous scheduledAt when sending null and keeping it when sending undefined
  • Loading branch information
raphaelblum authored Feb 8, 2024
1 parent 6e4ceb4 commit f6b4226
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
8 changes: 3 additions & 5 deletions packages/admin/src/emailCampaigns/form/EmailCampaignForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function EmailCampaignForm({ id, EmailCampaignContentBlock, scope, previe

type EmailCampaignState = Omit<GQLEmailCampaignFormFragment, "content" | "targetGroup"> & {
[key in keyof typeof rootBlocks]: BlockState<(typeof rootBlocks)[key]>;
} & { targetGroup: string | null };
} & { targetGroup?: string };

const stackApi = useStackApi();
const stackSwitchApi = useStackSwitchApi();
Expand Down Expand Up @@ -102,23 +102,21 @@ export function EmailCampaignForm({ id, EmailCampaignContentBlock, scope, previe
content: EmailCampaignContentBlock.input2State(emailCampaign.content),
scheduledAt: emailCampaign?.scheduledAt ? new Date(emailCampaign.scheduledAt) : null,
sendingState: emailCampaign?.sendingState,
targetGroup: emailCampaign?.targetGroup?.id ?? null,
targetGroup: emailCampaign?.targetGroup?.id,
};
},
state2Output: (state) => ({
...state,
content: EmailCampaignContentBlock.state2Output(state.content),
targetGroup: state.targetGroup ?? null,
scheduledAt: state.targetGroup ? state.scheduledAt ?? null : null,
sendingState: undefined,
}),
defaultState: {
title: "",
subject: "",
content: EmailCampaignContentBlock.defaultValues(),
scheduledAt: null,
targetGroup: null,
sendingState: "DRAFT",
scheduledAt: undefined,
},
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Block, BlockInputInterface, isBlockInputInterface } from "@comet/blocks-api";
import { IsUndefinable, RootBlockInputScalar } from "@comet/cms-api";
import { IsNullable, IsUndefinable, RootBlockInputScalar } from "@comet/cms-api";
import { Type } from "@nestjs/common";
import { Field, ID, InputType, PartialType } from "@nestjs/graphql";
import { Transform } from "class-transformer";
Expand All @@ -8,7 +8,7 @@ import { IsDate, IsNotEmpty, IsString, IsUUID, MinDate, ValidateNested } from "c
export interface EmailCampaignInputInterface {
title: string;
subject: string;
scheduledAt?: Date;
scheduledAt?: Date | null;
content: BlockInputInterface;
targetGroup?: string;
}
Expand All @@ -31,11 +31,12 @@ export class EmailCampaignInputFactory {
@Field()
subject: string;

@IsUndefinable()
@IsUndefinable() // When sending undefined, the previous scheduledAt value will be kept
@IsNullable() // When sending null, the scheduledAt value will be removed
@IsDate()
@MinDate(new Date())
@Field(() => Date, { nullable: true })
scheduledAt?: Date;
scheduledAt?: Date | null;

@IsUndefinable()
@Field(() => ID, { nullable: true })
Expand Down
5 changes: 2 additions & 3 deletions packages/api/src/email-campaign/email-campaign.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export function createEmailCampaignsResolver({
scope,
targetGroup: targetGroupInput ? Reference.create(await this.targetGroupRepository.findOneOrFail(targetGroupInput)) : undefined,
content: input.content.transformToBlockData(),
scheduledAt: input.scheduledAt ?? undefined,
});

await this.entityManager.flush();
Expand Down Expand Up @@ -128,7 +129,6 @@ export function createEmailCampaignsResolver({
wrap(campaign).assign({
...input,
content: input.content ? input.content.transformToBlockData() : undefined,
scheduledAt: input.scheduledAt ?? null,
});

await this.entityManager.flush();
Expand All @@ -143,8 +143,7 @@ export function createEmailCampaignsResolver({
throw new Error("Cannot update email campaign that has already been sent.");
}

hasScheduleRemoved = input.scheduledAt == null && brevoEmailCampaign.scheduledAt !== null;

hasScheduleRemoved = input.scheduledAt === null && brevoEmailCampaign.scheduledAt !== null;
if (hasScheduleRemoved && !(sendingState === SendingState.DRAFT)) {
await this.campaignsService.suspendEmailCampaign(campaign.brevoId);
}
Expand Down

0 comments on commit f6b4226

Please sign in to comment.