feat: add getPreviousWeatherEntriesForUsers service function

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-24 16:54:21 +01:00
parent 6b8d3c42f7
commit 220dcf87b9

View File

@@ -177,6 +177,65 @@ export async function deleteWeatherEntry(sessionId: string, userId: string) {
});
}
// Returns the most recent WeatherEntry per userId from any session BEFORE sessionDate,
// excluding the current session. Returned as a map userId → entry.
export async function getPreviousWeatherEntriesForUsers(
excludeSessionId: string,
sessionDate: Date,
userIds: string[]
): Promise<
Map<
string,
{
performanceEmoji: string | null;
moralEmoji: string | null;
fluxEmoji: string | null;
valueCreationEmoji: string | null;
}
>
> {
if (userIds.length === 0) return new Map();
const entries = await prisma.weatherEntry.findMany({
where: {
userId: { in: userIds },
sessionId: { not: excludeSessionId },
session: { date: { lt: sessionDate } },
},
orderBy: [{ userId: 'asc' }, { session: { date: 'desc' } }],
select: {
userId: true,
performanceEmoji: true,
moralEmoji: true,
fluxEmoji: true,
valueCreationEmoji: true,
},
});
// Keep only the most recent entry per user (first occurrence since ordered by date desc)
const map = new Map<
string,
{
performanceEmoji: string | null;
moralEmoji: string | null;
fluxEmoji: string | null;
valueCreationEmoji: string | null;
}
>();
for (const entry of entries) {
if (!map.has(entry.userId)) {
map.set(entry.userId, {
performanceEmoji: entry.performanceEmoji,
moralEmoji: entry.moralEmoji,
fluxEmoji: entry.fluxEmoji,
valueCreationEmoji: entry.valueCreationEmoji,
});
}
}
return map;
}
// ============================================
// Session Sharing
// ============================================