37 lines
897 B
TypeScript
37 lines
897 B
TypeScript
import { PrismaClient } from '@prisma/client';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
export class TableService {
|
|
async getByPub(pubId: number) {
|
|
return prisma.table.findMany({
|
|
where: { pubId },
|
|
orderBy: { number: 'asc' },
|
|
});
|
|
}
|
|
|
|
async getAvailability(pubId: number, date: string, time: string) {
|
|
const tables = await prisma.table.findMany({
|
|
where: { pubId },
|
|
orderBy: { number: 'asc' },
|
|
});
|
|
|
|
const reservedTables = await prisma.reservation.findMany({
|
|
where: {
|
|
pubId,
|
|
date: new Date(date),
|
|
time: new Date(`1970-01-01T${time}:00.000Z`),
|
|
status: { in: ['PENDING', 'CONFIRMED'] },
|
|
},
|
|
select: { tableId: true },
|
|
});
|
|
|
|
const reservedIds = new Set(reservedTables.map(r => r.tableId));
|
|
|
|
return tables.map(table => ({
|
|
...table,
|
|
isAvailable: !reservedIds.has(table.id),
|
|
}));
|
|
}
|
|
}
|