Skip to content

Commit 4cc5a98

Browse files
committed
deduplicate subscriptions in GET /subscription
1 parent da14ff0 commit 4cc5a98

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

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

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ describe("GET /subscription/", () => {
166166
expect(response.body).toEqual([]);
167167
});
168168

169-
it("should return all subscriptions", async () => {
169+
it("should return aggregated subscriptions by mailing list", async () => {
170170
await SupabaseDB.SUBSCRIPTIONS.insert([
171171
{ userId: USER_ID_1, mailingList: VALID_mailingList },
172172
{ userId: USER_ID_2, mailingList: VALID_mailingList },
@@ -175,14 +175,47 @@ describe("GET /subscription/", () => {
175175
StatusCodes.OK
176176
);
177177

178+
expect(response.body.length).toBe(1);
179+
expect(response.body[0]).toEqual({
180+
mailingList: VALID_mailingList,
181+
userIds: expect.arrayContaining([USER_ID_1, USER_ID_2]),
182+
});
183+
expect(response.body[0].userIds.length).toBe(2);
184+
});
185+
186+
it("should aggregate multiple mailing lists correctly", async () => {
187+
const mailingList2 = "newsletter";
188+
await SupabaseDB.SUBSCRIPTIONS.insert([
189+
{ userId: USER_ID_1, mailingList: VALID_mailingList },
190+
{ userId: USER_ID_2, mailingList: VALID_mailingList },
191+
{ userId: USER_ID_1, mailingList: mailingList2 },
192+
]).throwOnError();
193+
194+
const response = await getAsAdmin("/subscription/").expect(
195+
StatusCodes.OK
196+
);
197+
178198
expect(response.body.length).toBe(2);
179199

180-
expect(response.body).toEqual(
181-
expect.arrayContaining([
182-
{ userId: USER_ID_1, mailingList: VALID_mailingList },
183-
{ userId: USER_ID_2, mailingList: VALID_mailingList },
184-
])
200+
const validMailingListEntry = response.body.find(
201+
(item: { mailingList: string; userIds: string[] }) =>
202+
item.mailingList === VALID_mailingList
203+
);
204+
const newsletterEntry = response.body.find(
205+
(item: { mailingList: string; userIds: string[] }) =>
206+
item.mailingList === mailingList2
185207
);
208+
209+
expect(validMailingListEntry).toEqual({
210+
mailingList: VALID_mailingList,
211+
userIds: expect.arrayContaining([USER_ID_1, USER_ID_2]),
212+
});
213+
expect(validMailingListEntry.userIds.length).toBe(2);
214+
215+
expect(newsletterEntry).toEqual({
216+
mailingList: mailingList2,
217+
userIds: [USER_ID_1],
218+
});
186219
});
187220
});
188221

src/services/subscription/subscription-router.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,26 @@ subscriptionRouter.get(
6464
const { data: subscriptions } =
6565
await SupabaseDB.SUBSCRIPTIONS.select("*").throwOnError();
6666

67-
return res.status(StatusCodes.OK).json(subscriptions);
67+
const aggregated =
68+
subscriptions?.reduce(
69+
(acc, subscription) => {
70+
const existing = acc.find(
71+
(item) => item.mailingList === subscription.mailingList
72+
);
73+
if (existing) {
74+
existing.userIds.push(subscription.userId);
75+
} else {
76+
acc.push({
77+
mailingList: subscription.mailingList,
78+
userIds: [subscription.userId],
79+
});
80+
}
81+
return acc;
82+
},
83+
[] as { mailingList: string; userIds: string[] }[]
84+
) || [];
85+
86+
return res.status(StatusCodes.OK).json(aggregated);
6887
}
6988
);
7089

0 commit comments

Comments
 (0)