Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/services/subscription/subscription-router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,45 @@ describe("POST /subscription/send-email/single", () => {
});
});

describe("GET /subscription/lists", () => {
it("should return an empty array when no subscriptions exist", async () => {
const response = await getAsAdmin("/subscription/lists").expect(
StatusCodes.OK
);
expect(response.body).toEqual([]);
});

it("should return unique mailing lists", async () => {
await SupabaseDB.SUBSCRIPTIONS.insert([
{ userId: USER_ID_1, mailingList: VALID_mailingList },
{ userId: USER_ID_2, mailingList: VALID_mailingList }, // duplicate mailing list
{ userId: USER_ID_1, mailingList: "newsletter" },
]).throwOnError();

const response = await getAsAdmin("/subscription/lists").expect(
StatusCodes.OK
);

expect(response.body).toHaveLength(2);
expect(response.body).toEqual(
expect.arrayContaining([VALID_mailingList, "newsletter"])
);
});

it("should return a single unique mailing list when all subscriptions are for the same list", async () => {
await SupabaseDB.SUBSCRIPTIONS.insert([
{ userId: USER_ID_1, mailingList: VALID_mailingList },
{ userId: USER_ID_2, mailingList: VALID_mailingList },
]).throwOnError();

const response = await getAsAdmin("/subscription/lists").expect(
StatusCodes.OK
);

expect(response.body).toEqual([VALID_mailingList]);
});
});

describe("GET /subscription/:mailingList", () => {
it("should return the list of subscribers for an existing mailing list", async () => {
const emails = ["[email protected]", "[email protected]"];
Expand Down
15 changes: 15 additions & 0 deletions src/services/subscription/subscription-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ subscriptionRouter.get(
}
);

subscriptionRouter.get(
"/lists",
RoleChecker([Role.Enum.ADMIN]),
async (req, res) => {
const { data: subscriptions } =
await SupabaseDB.SUBSCRIPTIONS.select("mailingList").throwOnError();

const uniqueMailingLists = [
...new Set(subscriptions?.map((sub) => sub.mailingList) || []),
];

return res.status(StatusCodes.OK).json(uniqueMailingLists);
}
);

// Send an email to a mailing list
// API body: {String} mailingList The list to send the email to, {String} subject The subject line of the email, {String} htmlBody The HTML content of the email.
subscriptionRouter.post(
Expand Down