Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions src/app/_api/data/nominations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();
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 },
)();
};
3 changes: 2 additions & 1 deletion src/app/_api/helpers/cache-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
37 changes: 37 additions & 0 deletions src/app/api/reminders/route.ts
Original file line number Diff line number Diff line change
@@ -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;
});
7 changes: 7 additions & 0 deletions src/app/api/weekly-stats/route.tsx
Original file line number Diff line number Diff line change
@@ -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;
});