diff --git a/src/app/_api/data/nominations.ts b/src/app/_api/data/nominations.ts index 10599004..28105cab 100644 --- a/src/app/_api/data/nominations.ts +++ b/src/app/_api/data/nominations.ts @@ -460,9 +460,108 @@ export const getNominationsWeeklyStatsForUser = async ( pointsEarned: (nominationsSent?.build_points_sent ?? 0) + (nominationsReceived?.build_points_received ?? 0), + user: { + id: user.id, + username: user.username, + farcasterId: user.farcaster_id, + }, }; }, [`nominations_weekly_stats_${user.id}`] as CacheKey[], { revalidate: CACHE_5_MINUTES }, )(); }; + +export const getNominationsWeeklyStats = async () => { + return unstable_cache( + async () => { + const allUsers = await supabase + .from("users") + .select( + ` + *, + wallets ( + * + ), + boss_leaderboard ( + * + ) + `, + ) + .throwOnError() + .then((res) => res.data); + console.log("[BG] allUsers number:", allUsers?.length); + console.log("[BG] example user:", allUsers?.[0]); + if (!allUsers) return []; + const allStats = await Promise.all( + allUsers.map((u) => getNominationsWeeklyStatsForUser(u)), + ); + return allStats; + }, + [`nominations_weekly_stats_${Date.now()}`] as CacheKey[], + { revalidate: CACHE_5_MINUTES }, + )(); +}; + +export const getNominationsWeeklyReminder = async () => { + return unstable_cache( + async () => { + const filterDate = DateTime.local().minus({ days: 7 }).toISO(); + + const latestNominations = await supabase + .from("boss_nominations") + .select( + ` + origin_user_id, + created_at, + valid + `, + ) + .eq("valid", true) + .gt("created_at", filterDate) + .throwOnError() + .then((res) => res.data); + + const usersWhoHaveNominated = new Set(); + if (latestNominations) { + for (const nomination of latestNominations) { + usersWhoHaveNominated.add(nomination.origin_user_id); + } + } + + console.log("[BG] goodUsers number:", usersWhoHaveNominated.size); + + const filterString = `(${Array.from(usersWhoHaveNominated).join(",")})`; + + const users = await supabase + .from("users") + .select( + ` + id, + username, + boss_budget, + nominations_made, + farcaster_id, + last_wallet, + last_budget_calculation + `, + ) + .not("id", "in", filterString) + .throwOnError() + .then((res) => res.data); + + console.log("[BG] users to ping", users?.length); + return ( + users?.map((u) => ({ + nominationsBudget: u.boss_budget, + nominationsSent: u.nominations_made ?? 0, + farcasterId: u.farcaster_id, + username: u.username, + wallet: u.last_wallet, + })) ?? [] + ); + }, + [`nominations_reminder_${Date.now()}`] as CacheKey[], + { revalidate: CACHE_5_MINUTES }, + )(); +}; diff --git a/src/app/_api/helpers/cache-keys.ts b/src/app/_api/helpers/cache-keys.ts index 3e4b02aa..d07435ca 100644 --- a/src/app/_api/helpers/cache-keys.ts +++ b/src/app/_api/helpers/cache-keys.ts @@ -19,7 +19,8 @@ export type CacheKey = | `api_nominations_received_${string}` | `count_nominations_received_${string}` | `nominations_weekly_stats_${string}` - | `eligibility_${string}`; + | `eligibility_${string}` + | `nominations_reminder_${string}`; export const CACHE_1_MINUTE = 60; export const CACHE_5_MINUTES = 60 * 5; diff --git a/src/app/api/reminders/route.ts b/src/app/api/reminders/route.ts new file mode 100644 index 00000000..14ebd8a3 --- /dev/null +++ b/src/app/api/reminders/route.ts @@ -0,0 +1,37 @@ +import { + // getNominationsUserSent, + getNominationsWeeklyReminder, // getNominationsWeeklyStatsForUser, +} from "@/app/_api/data/nominations"; +// import { getUserFromWallet } from "@/app/_api/data/users"; +import { restApiHandler } from "@/app/_api/helpers/rest-api-handler"; + +// import { BadRequestError, NotFoundError } from "@/shared/utils/error"; + +// const statsParamsSchema = z.object({ +// wallet: z.string().optional(), +// }); + +export const GET = restApiHandler(async (request, params) => { + // const nominationsMadeParams = statsParamsSchema.parse({ + // wallet: params?.wallet, + // }); + + // if ( + // !nominationsMadeParams.wallet || + // nominationsMadeParams.wallet.length !== 42 || + // !nominationsMadeParams.wallet.startsWith("0x") + // ) { + // throw new BadRequestError("Invalid wallet address"); + // } + + // const user = await getUserFromWallet( + // nominationsMadeParams.wallet.toLowerCase(), + // ); + // if (!user) { + // throw new NotFoundError("User not found"); + // } + + const reminders = await getNominationsWeeklyReminder(); + + return reminders; +}); diff --git a/src/app/api/weekly-stats/route.ts b/src/app/api/weekly-stats/[wallet]/route.ts similarity index 100% rename from src/app/api/weekly-stats/route.ts rename to src/app/api/weekly-stats/[wallet]/route.ts diff --git a/src/app/api/weekly-stats/route.tsx b/src/app/api/weekly-stats/route.tsx new file mode 100644 index 00000000..eecb6c86 --- /dev/null +++ b/src/app/api/weekly-stats/route.tsx @@ -0,0 +1,7 @@ +import { getNominationsWeeklyStats } from "@/app/_api/data/nominations"; +import { restApiHandler } from "@/app/_api/helpers/rest-api-handler"; + +export const GET = restApiHandler(async (request, params) => { + const nominationsWeeklyStats = await getNominationsWeeklyStats(); + return nominationsWeeklyStats; +});