Implemented database actions to store bookings in database
This commit is contained in:
parent
bc7ac136cc
commit
e32caa68eb
1 changed files with 43 additions and 7 deletions
|
@ -2,6 +2,7 @@ import type { NextApiRequest, NextApiResponse } from 'next';
|
|||
import prisma from '../../../lib/prisma';
|
||||
import {createEvent, CalendarEvent} from '../../../lib/calendarClient';
|
||||
import createConfirmBookedEmail from "../../../lib/emails/confirm-booked";
|
||||
import sha256 from "../../../lib/sha256";
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const {user} = req.query;
|
||||
|
@ -11,6 +12,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||
username: user,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
credentials: true,
|
||||
timeZone: true,
|
||||
email: true,
|
||||
|
@ -31,8 +33,42 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||
]
|
||||
};
|
||||
|
||||
const eventType = await prisma.eventType.findFirst({
|
||||
where: {
|
||||
userId: currentUser.id,
|
||||
title: evt.type
|
||||
},
|
||||
select: {
|
||||
id: true
|
||||
}
|
||||
});
|
||||
|
||||
const result = await createEvent(currentUser.credentials[0], evt);
|
||||
|
||||
const hashUID = sha256(JSON.stringify(evt));
|
||||
|
||||
await prisma.booking.create({
|
||||
data: {
|
||||
uid: hashUID,
|
||||
userId: currentUser.id,
|
||||
references: {
|
||||
create: [
|
||||
//TODO Create references
|
||||
]
|
||||
},
|
||||
eventTypeId: eventType.id,
|
||||
|
||||
title: evt.title,
|
||||
description: evt.description,
|
||||
startTime: evt.startTime,
|
||||
endTime: evt.endTime,
|
||||
|
||||
attendees: {
|
||||
create: evt.attendees
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (!result.disableConfirmationEmail) {
|
||||
createConfirmBookedEmail(
|
||||
evt
|
||||
|
|
Loading…
Reference in a new issue