Fix/unpaid unconfirmed (#2553)

* Fix merge errors

* Errors prettier/prettier

* Update apps/web/pages/api/book/event.ts

Co-authored-by: Miguel Nieto A <[email protected]>

* Update apps/web/pages/api/book/event.ts

Co-authored-by: Miguel Nieto A <[email protected]>

* Update apps/web/pages/api/integrations.ts

Co-authored-by: Miguel Nieto A <[email protected]>

* Fix merge errors

* Errors prettier/prettier

* Update apps/web/pages/api/book/confirm.ts

Co-authored-by: alannnc <[email protected]>

* Modal window before delete stripe integration

* ESLint Report

* Test fixes

Co-authored-by: Miguel Nieto A <[email protected]>
Co-authored-by: alannnc <[email protected]>
Co-authored-by: zomars <[email protected]>
This commit is contained in:
authored and GitHub committed 2022-05-16 10:27:36 -06:00
1 parent 7fd149fd2e
commit f2a6d00348
8 files changed
+316 -20

No files matched your search

+17
View File
@@ -106,6 +106,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
uid: true,
payment: true,
destinationCalendar: true,
paid: true,
recurringEventId: true,
},
});
@@ -122,6 +123,22 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
return res.status(400).json({ message: "booking already confirmed" });
}
/** When a booking that requires payment its being confirmed but doesn't have any payment,
* we shouldn’t save it on DestinationCalendars
*/
if (booking.payment.length > 0 && !booking.paid) {
await prisma.booking.update({
where: {
id: bookingId,
},
data: {
confirmed: true,
},
});
return res.status(204).end();
}
const attendeesListPromises = booking.attendees.map(async (attendee) => {
return {
name: attendee.name,
+44 -19
View File
@@ -518,7 +518,25 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
}
return prisma.booking.create(createBookingObj);
/* Validate if there is any stripe_payment credential for this user */
const stripePaymentCredential = await prisma.credential.findFirst({
where: {
type: "stripe_payment",
userId: users[0].id,
},
select: {
id: true,
},
});
/** eventType doesn’t require payment then we create a booking
* OR
* stripePaymentCredential is found and price is higher than 0 then we create a booking
*/
if (!eventType.price || (stripePaymentCredential && eventType.price > 0)) {
return prisma.booking.create(createBookingObj);
}
// stripePaymentCredential not found and eventType requires payment we return null
return null;
}
let results: EventResult[] = [];
@@ -619,7 +637,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
let booking: Booking | null = null;
try {
booking = await createBooking();
evt.uid = booking.uid;
evt.uid = booking?.uid ?? null;
} catch (_err) {
const err = getErrorFromUnknown(_err);
log.error(`Booking ${eventTypeId} failed`, "Error when saving booking to db", err.message);
@@ -639,7 +657,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
if (originalRescheduledBooking?.uid) {
// Use EventManager to conditionally use all needed integrations.
const updateManager = await eventManager.update(evt, originalRescheduledBooking.uid, booking.id);
const updateManager = await eventManager.update(evt, originalRescheduledBooking.uid, booking?.id);
// This gets overridden when updating the event - to check if notes have been hidden or not. We just reset this back
// to the default description when we are sending the emails.
evt.description = eventType.description;
@@ -732,7 +750,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
);
}
if (typeof eventType.price === "number" && eventType.price > 0 && !originalRescheduledBooking?.paid) {
if (
!Number.isNaN(eventType.price) &&
eventType.price > 0 &&
!originalRescheduledBooking?.paid &&
!!booking
) {
try {
const [firstStripeCredential] = user.credentials.filter((cred) => cred.type == "stripe_payment");
@@ -776,18 +799,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
);
await Promise.all(promises);
// Avoid passing referencesToCreate with id unique constrain values
await prisma.booking.update({
where: {
uid: booking.uid,
},
data: {
references: {
createMany: {
data: referencesToCreate,
},
},
},
});
// refresh hashed link if used
const urlSeed = `${users[0].username}:${dayjs(req.body.start).utc().format()}`;
const hashedUid = translator.fromUUID(uuidv5(urlSeed, uuidv5.URL));
@@ -802,9 +813,23 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
},
});
}
// booking successful
return res.status(201).json(booking);
if (booking) {
await prisma.booking.update({
where: {
uid: booking.uid,
},
data: {
references: {
createMany: {
data: referencesToCreate,
},
},
},
});
// booking successful
return res.status(201).json(booking);
}
return res.status(400).json({ message: "There is not a stripe_payment credential" });
}
export function getLuckyUsers(
+80
View File
@@ -69,6 +69,86 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
data,
});
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: {
status: "CANCELLED",
rejectionReason: "Payment provider got removed",
},
});
const bookingReferences = await prisma.booking
.findMany({
where: {
confirmed: true,
rejected: false,
},
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" });
}
}
res.status(200).json({ message: "Integration deleted successfully" });
}
}