Skip to content

Commit

Permalink
fix-dublicate-email-notification-error
Browse files Browse the repository at this point in the history
  • Loading branch information
JeromeBu committed Jun 8, 2023
1 parent 675b8e6 commit 80d408b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,10 @@ describe("PgNotificationRepository", () => {
});

it("saves Email notification in a dedicated table, then gets it", async () => {
const email: TemplatedEmail = {
type: "AGENCY_WAS_ACTIVATED",
cc: ["copy@mail.com"],
const { id, emailNotification } = createTemplatedEmailAndNotification({
recipients: ["bob@mail.com", "jane@mail.com"],
params: { agencyName: "My agency", agencyLogoUrl: "https://my-logo.com" },
};
const id = "22222222-2222-4444-2222-222222222222";
const emailNotification: Notification = {
id,
kind: "email",
createdAt: new Date("2023-01-01").toISOString(),
followedIds: { agencyId: "cccccccc-1111-4111-1111-cccccccccccc" },
templatedContent: email,
};
cc: ["copy@mail.com"],
});

await pgNotificationRepository.save(emailNotification);

Expand All @@ -70,20 +60,12 @@ describe("PgNotificationRepository", () => {
});

it("save and eliminates duplicates when they exit", async () => {
const email: TemplatedEmail = {
type: "AGENCY_WAS_ACTIVATED",
recipients: ["bob@mail.com", "jane@mail.com", "bob@mail.com"],
cc: ["copy@mail.com", "jane@mail.com"],
params: { agencyName: "My agency", agencyLogoUrl: "https://my-logo.com" },
};
const id = "22222222-2222-4444-2222-222222222222";
const emailNotification: Notification = {
id,
kind: "email",
createdAt: new Date("2023-01-01").toISOString(),
followedIds: { agencyId: "cccccccc-1111-4111-1111-cccccccccccc" },
templatedContent: email,
};
const recipients = ["bob@mail.com", "jane@mail.com", "bob@mail.com"];
const cc = ["copy@mail.com", "jane@mail.com"];
const { id, emailNotification } = createTemplatedEmailAndNotification({
recipients,
cc,
});

await pgNotificationRepository.save(emailNotification);

Expand All @@ -97,4 +79,50 @@ describe("PgNotificationRepository", () => {
},
});
});

it("save and eliminates duplicates when cc ends up empty after de-duplication", async () => {
const { id, emailNotification } = createTemplatedEmailAndNotification({
recipients: ["bob@mail.com"],
cc: ["bob@mail.com"],
});

await pgNotificationRepository.save(emailNotification);

const response = await pgNotificationRepository.getByIdAndKind(id, "email");
expect(response).toEqual({
...emailNotification,
templatedContent: {
...emailNotification.templatedContent,
recipients: ["bob@mail.com"],
cc: [],
},
});
});
});

const createTemplatedEmailAndNotification = ({
recipients,
cc,
}: {
recipients: string[];
cc?: string[];
}) => {
const email: TemplatedEmail = {
type: "AGENCY_WAS_ACTIVATED",
recipients,
cc,
params: { agencyName: "My agency", agencyLogoUrl: "https://my-logo.com" },
};
const emailNotification: Notification = {
id: "22222222-2222-4444-2222-222222222222",
kind: "email",
createdAt: new Date("2023-01-01").toISOString(),
followedIds: { agencyId: "cccccccc-1111-4111-1111-cccccccccccc" },
templatedContent: email,
};

return {
id: emailNotification.id,
emailNotification,
};
};
2 changes: 1 addition & 1 deletion back/src/adapters/secondary/pg/PgNotificationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class PgNotificationRepository implements NotificationRepository {
const templatedContent = {
...notification.templatedContent,
recipients,
...(cc.length ? { cc } : {}),
cc,
};

return this.saveEmailNotification({
Expand Down

0 comments on commit 80d408b

Please sign in to comment.