Skip to content

Commit 6bc1327

Browse files
committed
add GET /lists route
1 parent 68287ff commit 6bc1327

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/services/subscription/subscription-router.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,45 @@ describe("POST /subscription/send-email/single", () => {
256256
});
257257
});
258258

259+
describe("GET /subscription/lists", () => {
260+
it("should return an empty array when no subscriptions exist", async () => {
261+
const response = await getAsAdmin("/subscription/lists").expect(
262+
StatusCodes.OK
263+
);
264+
expect(response.body).toEqual([]);
265+
});
266+
267+
it("should return unique mailing lists", async () => {
268+
await SupabaseDB.SUBSCRIPTIONS.insert([
269+
{ userId: USER_ID_1, mailingList: VALID_mailingList },
270+
{ userId: USER_ID_2, mailingList: VALID_mailingList }, // duplicate mailing list
271+
{ userId: USER_ID_1, mailingList: "newsletter" },
272+
]).throwOnError();
273+
274+
const response = await getAsAdmin("/subscription/lists").expect(
275+
StatusCodes.OK
276+
);
277+
278+
expect(response.body).toHaveLength(2);
279+
expect(response.body).toEqual(
280+
expect.arrayContaining([VALID_mailingList, "newsletter"])
281+
);
282+
});
283+
284+
it("should return a single unique mailing list when all subscriptions are for the same list", async () => {
285+
await SupabaseDB.SUBSCRIPTIONS.insert([
286+
{ userId: USER_ID_1, mailingList: VALID_mailingList },
287+
{ userId: USER_ID_2, mailingList: VALID_mailingList },
288+
]).throwOnError();
289+
290+
const response = await getAsAdmin("/subscription/lists").expect(
291+
StatusCodes.OK
292+
);
293+
294+
expect(response.body).toEqual([VALID_mailingList]);
295+
});
296+
});
297+
259298
describe("GET /subscription/:mailingList", () => {
260299
it("should return the list of subscribers for an existing mailing list", async () => {
261300
const emails = ["[email protected]", "[email protected]"];

src/services/subscription/subscription-router.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ subscriptionRouter.get(
6868
}
6969
);
7070

71+
subscriptionRouter.get(
72+
"/lists",
73+
RoleChecker([Role.Enum.ADMIN]),
74+
async (req, res) => {
75+
const { data: subscriptions } =
76+
await SupabaseDB.SUBSCRIPTIONS.select("mailingList").throwOnError();
77+
78+
const uniqueMailingLists = [
79+
...new Set(subscriptions?.map((sub) => sub.mailingList) || []),
80+
];
81+
82+
return res.status(StatusCodes.OK).json(uniqueMailingLists);
83+
}
84+
);
85+
7186
// Send an email to a mailing list
7287
// 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.
7388
subscriptionRouter.post(

0 commit comments

Comments
 (0)