2022-06-06 16:54:47 +00:00
|
|
|
import { BookingStatus, Prisma } from "@prisma/client";
|
2021-10-28 21:30:42 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
2021-08-18 11:52:25 +00:00
|
|
|
import { getSession } from "@lib/auth";
|
2021-10-28 21:30:42 +00:00
|
|
|
import prisma from "@lib/prisma";
|
2021-03-24 15:03:04 +00:00
|
|
|
|
2021-10-28 21:30:42 +00:00
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2022-06-06 18:24:37 +00:00
|
|
|
if (!["GET", "DELETE"].includes(req.method || "")) {
|
2021-10-28 21:30:42 +00:00
|
|
|
return res.status(405).end();
|
|
|
|
}
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-10-28 21:30:42 +00:00
|
|
|
// Check that user is authenticated
|
|
|
|
const session = await getSession({ req });
|
2022-05-03 23:16:59 +00:00
|
|
|
const userId = session?.user?.id;
|
2021-04-10 12:02:35 +00:00
|
|
|
|
2022-05-03 23:16:59 +00:00
|
|
|
if (!userId) {
|
2021-10-28 21:30:42 +00:00
|
|
|
res.status(401).json({ message: "You must be logged in to do this" });
|
|
|
|
return;
|
|
|
|
}
|
2021-04-10 12:02:35 +00:00
|
|
|
|
2021-10-28 21:30:42 +00:00
|
|
|
if (req.method === "GET") {
|
2021-08-19 12:27:01 +00:00
|
|
|
const credentials = await prisma.credential.findMany({
|
|
|
|
where: {
|
2022-05-03 23:16:59 +00:00
|
|
|
userId,
|
2021-08-19 12:27:01 +00:00
|
|
|
},
|
|
|
|
select: {
|
|
|
|
type: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
res.status(200).json(credentials);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.method == "DELETE") {
|
|
|
|
const id = req.body.id;
|
2022-05-03 23:16:59 +00:00
|
|
|
const data: Prisma.UserUpdateInput = {
|
|
|
|
credentials: {
|
|
|
|
delete: {
|
|
|
|
id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const integration = await prisma.credential.findUnique({
|
2021-08-19 12:27:01 +00:00
|
|
|
where: {
|
2022-05-03 23:16:59 +00:00
|
|
|
id,
|
2021-10-28 21:30:42 +00:00
|
|
|
},
|
2022-05-03 23:16:59 +00:00
|
|
|
});
|
|
|
|
/* If the user deletes a zapier integration, we delete all his api keys as well. */
|
|
|
|
if (integration?.appId === "zapier") {
|
|
|
|
data.apiKeys = {
|
|
|
|
deleteMany: {
|
|
|
|
userId,
|
|
|
|
appId: "zapier",
|
2021-10-28 21:30:42 +00:00
|
|
|
},
|
2022-05-03 23:16:59 +00:00
|
|
|
};
|
|
|
|
/* We also delete all user's zapier wehbooks */
|
|
|
|
data.webhooks = {
|
|
|
|
deleteMany: {
|
|
|
|
userId,
|
|
|
|
appId: "zapier",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
await prisma.user.update({
|
|
|
|
where: {
|
|
|
|
id: userId,
|
2021-08-19 12:27:01 +00:00
|
|
|
},
|
2022-05-03 23:16:59 +00:00
|
|
|
data,
|
2021-08-19 12:27:01 +00:00
|
|
|
});
|
2021-04-10 12:02:35 +00:00
|
|
|
|
2022-05-16 16:27:36 +00:00
|
|
|
if (req.body?.action === "cancel" || req.body?.action === "remove") {
|
|
|
|
try {
|
|
|
|
const bookingIdsWithPayments = await prisma.booking
|
|
|
|
.findMany({
|
|
|
|
where: {
|
|
|
|
userId: session?.user?.id,
|
|
|
|
paid: false,
|
|
|
|
NOT: {
|
|
|
|
payment: {
|
|
|
|
every: {
|
|
|
|
booking: null,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then((bookings) => bookings.map((booking) => booking.id));
|
|
|
|
const deletePayments = prisma.payment.deleteMany({
|
|
|
|
where: {
|
|
|
|
bookingId: {
|
|
|
|
in: bookingIdsWithPayments,
|
|
|
|
},
|
|
|
|
success: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const updateBookings = prisma.booking.updateMany({
|
|
|
|
where: {
|
|
|
|
id: {
|
|
|
|
in: bookingIdsWithPayments,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data: {
|
2022-06-06 16:54:47 +00:00
|
|
|
status: BookingStatus.CANCELLED,
|
2022-05-16 16:27:36 +00:00
|
|
|
rejectionReason: "Payment provider got removed",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const bookingReferences = await prisma.booking
|
|
|
|
.findMany({
|
|
|
|
where: {
|
2022-06-06 16:54:47 +00:00
|
|
|
status: BookingStatus.ACCEPTED,
|
2022-05-16 16:27:36 +00:00
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then((bookings) => bookings.map((booking) => booking.id));
|
|
|
|
|
|
|
|
const deleteBookingReferences = prisma.bookingReference.deleteMany({
|
|
|
|
where: {
|
|
|
|
bookingId: {
|
|
|
|
in: bookingReferences,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (req.body?.action === "cancel") {
|
|
|
|
await prisma.$transaction([deletePayments, updateBookings, deleteBookingReferences]);
|
|
|
|
} else {
|
|
|
|
const updateBookings = prisma.booking.updateMany({
|
|
|
|
where: {
|
|
|
|
id: {
|
|
|
|
in: bookingIdsWithPayments,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
paid: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await prisma.$transaction([deletePayments, updateBookings]);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
res.status(500).json({ message: "Integration could not be deleted" });
|
|
|
|
}
|
|
|
|
}
|
2021-08-19 12:27:01 +00:00
|
|
|
res.status(200).json({ message: "Integration deleted successfully" });
|
|
|
|
}
|
|
|
|
}
|