Skip to content

Commit 9b4daf3

Browse files
committed
2 requests instead of n*2 requests
1 parent 911bfe7 commit 9b4daf3

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/services/leaderboard/leaderboard-utils.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -208,20 +208,19 @@ export async function promoteUsersToNextTier(
208208
}
209209

210210
// Enroll every userId into a Firebase topic if they got promoted today
211-
for (const userId of userIds) {
212-
const { data: userDevice } = await SupabaseDB.NOTIFICATIONS.select(
213-
"deviceId"
214-
)
215-
.eq("userId", userId)
216-
.maybeSingle()
217-
.throwOnError();
218-
if (!userDevice?.deviceId) {
219-
continue; // we can just be done here if they don't have a deviceId
220-
}
211+
212+
const { data: userDevices } = await SupabaseDB.NOTIFICATIONS.select(
213+
"deviceId"
214+
)
215+
.in("userId", userIds)
216+
.throwOnError();
217+
218+
if (userDevices && userDevices.length > 0) {
219+
const deviceTokens = userDevices.map((device) => device.deviceId);
221220
const topicName = `tier-promotion-${day.toLowerCase()}`;
222221
await getFirebaseAdmin()
223222
.messaging()
224-
.subscribeToTopic(userDevice.deviceId, topicName);
223+
.subscribeToTopic(deviceTokens, topicName);
225224
}
226225

227226
return data || 0;

0 commit comments

Comments
 (0)