update reservation

This commit is contained in:
root
2026-05-25 15:56:14 -04:00
parent 914ae839a7
commit 8ed572b3bd
17 changed files with 809 additions and 63 deletions
+53 -11
View File
@@ -41,19 +41,61 @@ export async function findOfferByPromoCode(companyId: string, code: string) {
}
export async function upsertCustomer(companyId: string, data: {
email: string; firstName: string; lastName: string; phone?: string | null
driverLicense?: string | null; dateOfBirth?: string | null; licenseExpiry?: string | null
licenseIssuedAt?: string | null; nationality?: string | null
email: string
firstName: string
lastName: string
phone: string
driverLicense: string
dateOfBirth: string
licenseExpiry: string
licenseIssuedAt: string
nationality: string
identityDocumentNumber: string
fullAddress: string
licenseCountry: string
licenseNumber?: string | null
licenseCategory: string
internationalLicenseNumber?: string | null
}) {
const parsed = {
dateOfBirth: data.dateOfBirth ? new Date(data.dateOfBirth) : null,
licenseExpiry: data.licenseExpiry ? new Date(data.licenseExpiry) : null,
licenseIssuedAt: data.licenseIssuedAt ? new Date(data.licenseIssuedAt) : null,
}
return prisma.customer.upsert({
const existing = await prisma.customer.findUnique({
where: { companyId_email: { companyId, email: data.email } },
create: { companyId, firstName: data.firstName, lastName: data.lastName, email: data.email, phone: data.phone ?? null, driverLicense: data.driverLicense ?? null, nationality: data.nationality ?? null, ...parsed },
update: { firstName: data.firstName, lastName: data.lastName, phone: data.phone ?? null, driverLicense: data.driverLicense ?? null, nationality: data.nationality ?? null, ...parsed },
select: { id: true, address: true },
})
const address = {
...(existing?.address && typeof existing.address === 'object' && !Array.isArray(existing.address)
? existing.address as Record<string, unknown>
: {}),
fullAddress: data.fullAddress,
identityDocumentNumber: data.identityDocumentNumber,
internationalLicenseNumber: data.internationalLicenseNumber ?? null,
}
const payload = {
firstName: data.firstName,
lastName: data.lastName,
email: data.email,
phone: data.phone,
driverLicense: data.driverLicense,
dateOfBirth: new Date(data.dateOfBirth),
nationality: data.nationality,
address,
licenseExpiry: new Date(data.licenseExpiry),
licenseIssuedAt: new Date(data.licenseIssuedAt),
licenseCountry: data.licenseCountry,
licenseNumber: data.licenseNumber ?? data.driverLicense,
licenseCategory: data.licenseCategory,
}
if (!existing) {
return prisma.customer.create({
data: { companyId, ...payload },
})
}
return prisma.customer.update({
where: { id: existing.id },
data: payload,
})
}
+12 -6
View File
@@ -18,12 +18,18 @@ export const bookSchema = z.object({
firstName: z.string().min(1),
lastName: z.string().min(1),
email: z.string().email(),
phone: z.string().optional(),
driverLicense: z.string().optional(),
dateOfBirth: z.string().datetime().optional(),
licenseExpiry: z.string().datetime().optional(),
licenseIssuedAt: z.string().datetime().optional(),
nationality: z.string().optional(),
phone: z.string().min(1).max(30),
driverLicense: z.string().min(1).max(50),
dateOfBirth: z.string().datetime(),
licenseExpiry: z.string().datetime(),
licenseIssuedAt: z.string().datetime(),
nationality: z.string().min(1).max(100),
identityDocumentNumber: z.string().min(1).max(100),
fullAddress: z.string().min(1).max(500),
licenseCountry: z.string().min(1).max(100),
licenseNumber: z.string().min(1).max(50).optional(),
licenseCategory: z.string().min(1).max(20),
internationalLicenseNumber: z.string().trim().max(100).optional(),
offerId: z.string().cuid().optional(),
promoCodeUsed: z.string().optional(),
notes: z.string().optional(),
+18 -5
View File
@@ -93,8 +93,9 @@ export async function validatePromoCode(slug: string, code: string) {
export async function createBooking(slug: string, body: {
vehicleId: string; startDate: string; endDate: string
firstName: string; lastName: string; email: string; phone?: string
driverLicense?: string; dateOfBirth?: string; licenseExpiry?: string; licenseIssuedAt?: string; nationality?: string
firstName: string; lastName: string; email: string; phone: string
driverLicense: string; dateOfBirth: string; licenseExpiry: string; licenseIssuedAt: string; nationality: string
identityDocumentNumber: string; fullAddress: string; licenseCountry: string; licenseNumber?: string; licenseCategory: string; internationalLicenseNumber?: string
offerId?: string; promoCodeUsed?: string; notes?: string; source?: string
selectedInsurancePolicyIds?: string[]; additionalDrivers?: any[]
}) {
@@ -113,9 +114,21 @@ export async function createBooking(slug: string, body: {
}
const customer = await repo.upsertCustomer(company.id, {
email: body.email, firstName: body.firstName, lastName: body.lastName, phone: body.phone,
driverLicense: body.driverLicense, dateOfBirth: body.dateOfBirth,
licenseExpiry: body.licenseExpiry, licenseIssuedAt: body.licenseIssuedAt, nationality: body.nationality,
email: body.email,
firstName: body.firstName,
lastName: body.lastName,
phone: body.phone,
driverLicense: body.driverLicense,
dateOfBirth: body.dateOfBirth,
licenseExpiry: body.licenseExpiry,
licenseIssuedAt: body.licenseIssuedAt,
nationality: body.nationality,
identityDocumentNumber: body.identityDocumentNumber,
fullAddress: body.fullAddress,
licenseCountry: body.licenseCountry,
licenseNumber: body.licenseNumber,
licenseCategory: body.licenseCategory,
internationalLicenseNumber: body.internationalLicenseNumber,
})
const totalDays = Math.max(1, Math.ceil((end.getTime() - start.getTime()) / 86_400_000))