Implemented database actions to store bookings in database

pull/253/head
nicolas 2021-06-06 01:32:24 +02:00
parent bc7ac136cc
commit e32caa68eb
1 changed files with 43 additions and 7 deletions

View File

@ -2,6 +2,7 @@ import type { NextApiRequest, NextApiResponse } from 'next';
import prisma from '../../../lib/prisma'; import prisma from '../../../lib/prisma';
import {createEvent, CalendarEvent} from '../../../lib/calendarClient'; import {createEvent, CalendarEvent} from '../../../lib/calendarClient';
import createConfirmBookedEmail from "../../../lib/emails/confirm-booked"; import createConfirmBookedEmail from "../../../lib/emails/confirm-booked";
import sha256 from "../../../lib/sha256";
export default async function handler(req: NextApiRequest, res: NextApiResponse) { export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const {user} = req.query; const {user} = req.query;
@ -11,6 +12,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
username: user, username: user,
}, },
select: { select: {
id: true,
credentials: true, credentials: true,
timeZone: true, timeZone: true,
email: 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 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) { if (!result.disableConfirmationEmail) {
createConfirmBookedEmail( createConfirmBookedEmail(
evt evt