Skip to content

Commit d0f2846

Browse files
authored
Merge branch 'main' into leaderboard-topics
2 parents bb96eb5 + 69df610 commit d0f2846

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,42 @@ describe("POST /subscription/send-email/single", () => {
254254
expect(mockSESV2Send).toHaveBeenCalledTimes(1);
255255
});
256256
});
257+
258+
describe("GET /subscription/:mailingList", () => {
259+
it("should return the list of subscribers for an existing mailing list", async () => {
260+
const subscribers = [EMAIL_1, EMAIL_2];
261+
await SupabaseDB.SUBSCRIPTIONS.insert({
262+
mailingList: VALID_mailingList,
263+
subscriptions: subscribers,
264+
});
265+
266+
const response = await getAsAdmin(
267+
`/subscription/${VALID_mailingList}`
268+
).expect(StatusCodes.OK);
269+
270+
expect(response.body).toEqual(expect.arrayContaining(subscribers));
271+
expect(response.body.length).toBe(2);
272+
});
273+
274+
it("should return a 404 Not Found for a non-existent mailing list", async () => {
275+
const response = await getAsAdmin(
276+
"/subscription/non-existent-list"
277+
).expect(StatusCodes.NOT_FOUND);
278+
279+
expect(response.body).toEqual({ error: "Mailing list not found." });
280+
});
281+
282+
it("should return an empty array for a list that has no subscribers", async () => {
283+
// Setup: Create a list with an empty subscriptions array
284+
await SupabaseDB.SUBSCRIPTIONS.insert({
285+
mailingList: VALID_mailingList,
286+
subscriptions: [],
287+
});
288+
289+
const response = await getAsAdmin(
290+
`/subscription/${VALID_mailingList}`
291+
).expect(StatusCodes.OK);
292+
293+
expect(response.body).toEqual([]);
294+
});
295+
});

src/services/subscription/subscription-router.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,28 @@ subscriptionRouter.post(
136136
}
137137
);
138138

139+
// Get all the emails in a specific mailing list
140+
// Param: mailingList - the name of the mailing list to retrieve
141+
subscriptionRouter.get(
142+
"/:mailingList",
143+
RoleChecker([Role.Enum.ADMIN]),
144+
async (req, res) => {
145+
const { mailingList } = req.params;
146+
const { data: list } = await SupabaseDB.SUBSCRIPTIONS.select(
147+
"subscriptions"
148+
)
149+
.eq("mailingList", mailingList)
150+
.maybeSingle()
151+
.throwOnError();
152+
153+
if (!list) {
154+
return res
155+
.status(StatusCodes.NOT_FOUND)
156+
.json({ error: "Mailing list not found." });
157+
}
158+
159+
return res.status(StatusCodes.OK).json(list.subscriptions || []);
160+
}
161+
);
162+
139163
export default subscriptionRouter;

0 commit comments

Comments
 (0)