|
| 1 | +import type { Event } from '@prisma/client'; |
| 2 | + |
| 3 | +import { prisma } from '../prisma.js'; |
| 4 | +import { NotFoundError } from '../utils/AppError.js'; |
| 5 | +import { getAllEateriesData } from '../utils/cache.js'; |
| 6 | + |
| 7 | +export const getAllEateries = async (days: number = 0) => { |
| 8 | + // Calculate date range for filtering events |
| 9 | + // days=0 means today, days=1 means tomorrow, etc. |
| 10 | + const now = new Date(); |
| 11 | + const targetDay = new Date(now); |
| 12 | + targetDay.setDate(now.getDate() + days); |
| 13 | + |
| 14 | + const startOfDay = new Date(targetDay); |
| 15 | + startOfDay.setHours(0, 0, 0, 0); |
| 16 | + |
| 17 | + const endOfDay = new Date(targetDay); |
| 18 | + endOfDay.setHours(23, 59, 59, 999); |
| 19 | + |
| 20 | + // Try to get from cache first |
| 21 | + try { |
| 22 | + const cachedEateries = getAllEateriesData(); |
| 23 | + |
| 24 | + // Filter events to only include those on the specified day |
| 25 | + const filteredEateries = cachedEateries.map((eatery) => ({ |
| 26 | + ...eatery, |
| 27 | + events: eatery.events.filter((event: Event) => { |
| 28 | + const eventStart = new Date(event.startTimestamp); |
| 29 | + const eventEnd = new Date(event.endTimestamp); |
| 30 | + |
| 31 | + // Include event if it overlaps with the target day |
| 32 | + return eventStart <= endOfDay && eventEnd >= startOfDay; |
| 33 | + }), |
| 34 | + })); |
| 35 | + |
| 36 | + return filteredEateries; |
| 37 | + } catch { |
| 38 | + // If cache is cold (should never happen), fall back to database |
| 39 | + const eateries = await prisma.eatery.findMany({ |
| 40 | + include: { |
| 41 | + events: { |
| 42 | + where: { |
| 43 | + startTimestamp: { |
| 44 | + lte: endOfDay, |
| 45 | + }, |
| 46 | + endTimestamp: { |
| 47 | + gte: startOfDay, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }); |
| 53 | + |
| 54 | + return eateries; |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +export const getEateryById = async (eateryId: number) => { |
| 59 | + // Try to get eatery from cache first |
| 60 | + try { |
| 61 | + const cachedEateries = getAllEateriesData(); |
| 62 | + const eatery = cachedEateries.find((e) => e.id === eateryId); |
| 63 | + |
| 64 | + if (!eatery) { |
| 65 | + throw new NotFoundError('Eatery not found'); |
| 66 | + } |
| 67 | + |
| 68 | + return eatery; |
| 69 | + } catch (error) { |
| 70 | + // If cache is cold or eatery not found in cache, fall back to database |
| 71 | + if (error instanceof NotFoundError) { |
| 72 | + throw error; |
| 73 | + } |
| 74 | + |
| 75 | + const eatery = await prisma.eatery.findUnique({ |
| 76 | + where: { id: eateryId }, |
| 77 | + include: { |
| 78 | + events: { |
| 79 | + include: { |
| 80 | + menu: { |
| 81 | + include: { |
| 82 | + items: { |
| 83 | + include: { |
| 84 | + dietaryPreferences: true, |
| 85 | + allergens: true, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + userEventVotes: true, |
| 91 | + }, |
| 92 | + orderBy: { |
| 93 | + startTimestamp: 'asc', |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }); |
| 98 | + |
| 99 | + if (!eatery) { |
| 100 | + throw new NotFoundError('Eatery not found'); |
| 101 | + } |
| 102 | + |
| 103 | + return eatery; |
| 104 | + } |
| 105 | +}; |
0 commit comments