diff --git a/src/services/weather.ts b/src/services/weather.ts index 3c6d804..c379eaf 100644 --- a/src/services/weather.ts +++ b/src/services/weather.ts @@ -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 // ============================================