111 lines
3 KiB
TypeScript
111 lines
3 KiB
TypeScript
import "server-only";
|
|
|
|
import { Prisma } from "@/generated/prisma/client";
|
|
import { RentalCategory } from "@/generated/prisma/enums";
|
|
import { prisma } from "@/lib/prisma";
|
|
|
|
export const RENTAL_CATEGORY_LABEL: Record<RentalCategory, string> = {
|
|
SLEEP: "💤 Couchage",
|
|
NAVIGATION: "🛶 Navigation",
|
|
FISHING: "🎣 Pêche",
|
|
COOKING: "🍳 Cuisine",
|
|
SAFETY: "🦺 Sécurité",
|
|
};
|
|
|
|
export type AdminRentalItemFilters = {
|
|
q?: string;
|
|
category?: RentalCategory;
|
|
providerId?: string;
|
|
active?: "yes" | "no";
|
|
};
|
|
|
|
export type AdminRentalItemListItem = {
|
|
id: string;
|
|
name: string;
|
|
category: RentalCategory;
|
|
providerId: string;
|
|
providerName: string;
|
|
providerIsSystemD: boolean;
|
|
pricePerDay: string;
|
|
pricePerWeek: string | null;
|
|
deposit: string;
|
|
totalQty: number;
|
|
withMotor: boolean;
|
|
fuelIncluded: boolean;
|
|
requiresLicense: boolean;
|
|
active: boolean;
|
|
imageUrl: string | null;
|
|
updatedAt: Date;
|
|
};
|
|
|
|
const CATEGORY_VALUES: RentalCategory[] = [
|
|
RentalCategory.SLEEP,
|
|
RentalCategory.NAVIGATION,
|
|
RentalCategory.FISHING,
|
|
RentalCategory.COOKING,
|
|
RentalCategory.SAFETY,
|
|
];
|
|
|
|
export function isRentalCategory(v: string): v is RentalCategory {
|
|
return (CATEGORY_VALUES as string[]).includes(v);
|
|
}
|
|
|
|
export async function listRentalItemsAdmin(
|
|
filters: AdminRentalItemFilters = {},
|
|
): Promise<AdminRentalItemListItem[]> {
|
|
const where: Prisma.RentalItemWhereInput = {};
|
|
if (filters.q) {
|
|
where.OR = [
|
|
{ name: { contains: filters.q, mode: "insensitive" } },
|
|
{ description: { contains: filters.q, mode: "insensitive" } },
|
|
];
|
|
}
|
|
if (filters.category) where.category = filters.category;
|
|
if (filters.providerId) where.providerId = filters.providerId;
|
|
if (filters.active === "yes") where.active = true;
|
|
if (filters.active === "no") where.active = false;
|
|
|
|
const rows = await prisma.rentalItem.findMany({
|
|
where,
|
|
orderBy: [{ category: "asc" }, { name: "asc" }],
|
|
take: 300,
|
|
include: {
|
|
provider: { select: { name: true, isSystemD: true } },
|
|
},
|
|
});
|
|
return rows.map((r) => ({
|
|
id: r.id,
|
|
name: r.name,
|
|
category: r.category,
|
|
providerId: r.providerId,
|
|
providerName: r.provider.name,
|
|
providerIsSystemD: r.provider.isSystemD,
|
|
pricePerDay: r.pricePerDay.toString(),
|
|
pricePerWeek: r.pricePerWeek?.toString() ?? null,
|
|
deposit: r.deposit.toString(),
|
|
totalQty: r.totalQty,
|
|
withMotor: r.withMotor,
|
|
fuelIncluded: r.fuelIncluded,
|
|
requiresLicense: r.requiresLicense,
|
|
active: r.active,
|
|
imageUrl: r.imageUrl,
|
|
updatedAt: r.updatedAt,
|
|
}));
|
|
}
|
|
|
|
export async function getRentalItemForAdmin(id: string) {
|
|
return prisma.rentalItem.findUnique({
|
|
where: { id },
|
|
include: {
|
|
provider: { select: { id: true, name: true, isSystemD: true } },
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function listProvidersForSelect() {
|
|
return prisma.rentalProvider.findMany({
|
|
where: { active: true },
|
|
orderBy: [{ isSystemD: "desc" }, { name: "asc" }],
|
|
select: { id: true, name: true, isSystemD: true, approved: true },
|
|
});
|
|
}
|