Files
setrip/server/repositories/trip.repo.ts
T
2026-04-17 00:16:31 +07:00

74 lines
2.1 KiB
TypeScript

import { prisma } from "@/lib/prisma";
import { Prisma } from "@/app/generated/prisma/client";
export const tripRepo = {
async findAll() {
return prisma.trip.findMany({
include: {
organizer: { select: { id: true, name: true, image: true } },
images: { orderBy: { order: "asc" }, take: 1 },
_count: { select: { participants: true } },
},
orderBy: { date: "asc" },
});
},
async findOpen(filters?: { q?: string; from?: string; to?: string }) {
const where: Prisma.TripWhereInput = {
status: "OPEN",
date: { gte: new Date() },
};
if (filters?.q) {
where.OR = [
{ title: { contains: filters.q, mode: "insensitive" } },
{ mountain: { contains: filters.q, mode: "insensitive" } },
{ location: { contains: filters.q, mode: "insensitive" } },
];
}
if (filters?.from || filters?.to) {
const dateFilter: Prisma.DateTimeFilter = { gte: new Date() };
if (filters.from) {
const fromDate = new Date(filters.from);
if (fromDate > new Date()) dateFilter.gte = fromDate;
}
if (filters.to) {
dateFilter.lte = new Date(filters.to + "T23:59:59.999Z");
}
where.date = dateFilter;
}
return prisma.trip.findMany({
where,
include: {
organizer: { select: { id: true, name: true, image: true } },
images: { orderBy: { order: "asc" }, take: 1 },
_count: { select: { participants: true } },
},
orderBy: { date: "asc" },
});
},
async findById(id: string) {
return prisma.trip.findUnique({
where: { id },
include: {
organizer: { select: { id: true, name: true, email: true, image: true } },
images: { orderBy: { order: "asc" } },
participants: {
include: { user: { select: { id: true, name: true, image: true } } },
},
},
});
},
async create(data: Prisma.TripCreateInput) {
return prisma.trip.create({ data });
},
async updateStatus(id: string, status: "OPEN" | "FULL" | "CLOSED" | "COMPLETED") {
return prisma.trip.update({ where: { id }, data: { status } });
},
};