From 89493376abba4c97961acb67b174e22b75a7e6f6 Mon Sep 17 00:00:00 2001 From: Eliezer Steinbock <3090527+elie222@users.noreply.github.com> Date: Wed, 25 Dec 2024 01:49:17 +0200 Subject: [PATCH] Prioritize users in watch all --- apps/web/app/api/google/watch/all/route.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/apps/web/app/api/google/watch/all/route.ts b/apps/web/app/api/google/watch/all/route.ts index 7ce905c3..638c4cf5 100644 --- a/apps/web/app/api/google/watch/all/route.ts +++ b/apps/web/app/api/google/watch/all/route.ts @@ -38,9 +38,24 @@ async function watchAllEmails() { }, }); - const users = premiums.flatMap((premium) => - premium.users.map((user) => ({ ...user, premium })), - ); + const users = premiums + .flatMap((premium) => premium.users.map((user) => ({ ...user, premium }))) + .sort((a, b) => { + // Prioritize null dates first + if (!a.watchEmailsExpirationDate && b.watchEmailsExpirationDate) + return -1; + if (a.watchEmailsExpirationDate && !b.watchEmailsExpirationDate) return 1; + + // If both have dates, sort by earliest date first + if (a.watchEmailsExpirationDate && b.watchEmailsExpirationDate) { + return ( + new Date(a.watchEmailsExpirationDate).getTime() - + new Date(b.watchEmailsExpirationDate).getTime() + ); + } + + return 0; + }); console.log(`Watching emails for ${users.length} users`);