Skip to content

Commit

Permalink
partially refactor eventsGateway: add catch statements to db queries,…
Browse files Browse the repository at this point in the history
… reduce event tickets, refactor reserve and release tickets for events and tickets table, refactor isPriceId, remove unused functions
  • Loading branch information
Ratchet7x5 committed Dec 31, 2024
1 parent b8b8e1b commit 67edad3
Showing 1 changed file with 102 additions and 91 deletions.
193 changes: 102 additions & 91 deletions api/gateway/eventsGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,73 @@ import {
events,
peoples,
userTickets,
userTicketsTicketIdLinks,
tickets,
ticketsEventIdLinks,
} from "../schemas/schema";
import Stripe from "stripe";
import { stripe } from "../stripe/stripe";
import { ticketsEventIdLinksRelations } from "../schemas/relations";
import { sendEmail } from "../mailer/mailer";
import { generateQRCode } from "../mailer/qrCode";

/**
* Check if an event ticket is available for sale or not. Returns false if the Events' remaining capacity is greater than 0.
* @param priceId The stripe priceId for the event ticket.
* @returns A boolean representing if tickets available for sale or not.
*/
export async function isTicketAvailableByPriceId(
priceId: string
): Promise<boolean> {
let isTicketAvailable = false;

const remainingTickets = await db.query.events.findFirst({
columns: { eventCapacityRemaining: true },
// Event must be LIVE (true) for reserve and sales to go through.
where: and(
and(eq(events.stripePriceId, priceId), eq(events.isLive, true)),
gt(events.eventCapacityRemaining, 0)
),
});
const ticketsCapacityRemaining = await db
.select()
.from(tickets)
.where(
and(
and(eq(tickets.stripeLink, priceId)),
gt(tickets.numberTicketsLeft, 0)
)
)
.limit(1)
.catch((error) => {
throw new Error(
`isTicketAvailableByPriceId: Ran into error while query tickets table for numberTicketsLeft: ${error}`
);
});

//get eventId by using the ticketsEventIdLinks table.
let eventId = await db
.select()
.from(ticketsEventIdLinks)
.where(eq(ticketsEventIdLinks.eventId, ticketsCapacityRemaining[0].id))
.catch((error) => {
throw new Error(
`isTicketAvailableByPriceId: Ran into error while query ticketsEventIdLinks table: ${error}`
);
});

//check events' capacity
let eventCapacityRemaining = await db
.select()
.from(events)
.where(
and(
eq(events.id, eventId[0].eventId!),
gt(events.eventCapacityRemaining, 0)
)
)
.limit(1)
.catch((error) => {
throw new Error(
`isTicketAvailableByPriceId: Ran into error while query events table: ${error}`
);
});

// Handle the case where remainingTickets or eventCapacityRemaining is undefined or null
if (remainingTickets && remainingTickets.eventCapacityRemaining != null) {
isTicketAvailable = remainingTickets.eventCapacityRemaining > 0;
if (
eventCapacityRemaining &&
eventCapacityRemaining[0].eventCapacityRemaining !== null
) {
isTicketAvailable = eventCapacityRemaining[0].eventCapacityRemaining > 0;
}

return isTicketAvailable;
Expand All @@ -45,11 +86,26 @@ export async function reserveTicket(priceId: string) {
if (canReserveTicket === true) {
// sql for reserving statement
reservedTicket = await db
.update(tickets)
.set({
numberTicketsLeft: sql`${tickets.numberTicketsLeft} - 1`,
})
.where(eq(tickets.stripeLink, priceId))
.returning();

//get eventId by using the ticketsEventIdLinks table. Then reduce the events' ticket capacity too
let eventId = await db
.select()
.from(ticketsEventIdLinks)
.where(eq(ticketsEventIdLinks.eventId, reservedTicket[0].id));

//decrement event ticket capacity
let eventReservedTicket = await db
.update(events)
.set({
eventCapacityRemaining: sql`${events.eventCapacityRemaining} - 1`,
})
.where(eq(events.stripePriceId, priceId))
.where(eq(events.id, eventId[0].id))
.returning();
}

Expand All @@ -62,11 +118,26 @@ export async function releaseReservedTicket(priceId: string) {

// increment event_remaining_ticket by 1
releasedTicket = await db
.update(tickets)
.set({
numberTicketsLeft: sql`${tickets.numberTicketsLeft} + 1`,
})
.where(eq(tickets.stripeLink, priceId))
.returning();

// get eventId by using the ticketsEventIdLinks table.
let eventId = await db
.select()
.from(ticketsEventIdLinks)
.where(eq(ticketsEventIdLinks.eventId, releasedTicket[0].id));

// increment event ticket capacity
let eventReleasedTicket = await db
.update(events)
.set({
eventCapacityRemaining: sql`${events.eventCapacityRemaining} + 1`,
})
.where(eq(events.stripePriceId, priceId))
.where(eq(events.id, eventId[0].id))
.returning();

return releasedTicket;
Expand Down Expand Up @@ -95,12 +166,14 @@ export async function completeTicketPurchase(sessionId: string) {
.where(eq(peoples.email, checkoutSession.customer_details!.email!))
.limit(1);

let event = await db
// retrieve ticket
let ticket = await db
.select()
.from(events)
.where(eq(events.stripePriceId, checkoutSession.metadata!["priceId"]))
.from(tickets)
.where(eq(tickets.stripeLink, checkoutSession.metadata!["priceId"]))
.limit(1);

// update the ticket details: paid, people ticket code
let updatedTicket = await db
.update(userTickets)
.set({
Expand All @@ -118,14 +191,14 @@ export async function completeTicketPurchase(sessionId: string) {
await generateQRCode(updatedTicket[0].peopleTicketCode!),
checkoutSession.customer_details!.email!,
customer[0].name!,
event[0].title!,
ticket[0].name!,
updatedTicket[0].peopleTicketCode!
);
}
}

export async function isPriceIdForEvent(priceId: string) {
let isPriceIdUsedForEventOrMembership = false;
let isEventPriceId = false;

if (priceId === "" || priceId === undefined || priceId === null) {
throw new Error(
Expand All @@ -136,79 +209,17 @@ export async function isPriceIdForEvent(priceId: string) {
// search for this priceId
let isUsedForEvent = await db
.select()
.from(events)
.where(eq(events.stripePriceId, priceId));
.from(tickets)
.where(eq(tickets.stripeLink, priceId));

// if array is 1, true. If 0, set to false.
if (isUsedForEvent.length == 1) {
isPriceIdUsedForEventOrMembership = true;
} else if (isUsedForEvent.length == 0) {
isPriceIdUsedForEventOrMembership = false;
}

return isPriceIdUsedForEventOrMembership;
}

export async function getUserTickets(eventId: number) {
console.log("I WAS CALLED");
console.log(eventId);
if (eventId < 0 || eventId === undefined || eventId === null) {
throw new Error(
"received invalid type for getUserTickets() in eventsGateway" + eventId
);
}

// search for this priceId
let eventTickets = await db
.select({
id: userTickets.id,
userTicketCode: userTickets.peopleTicketCode,
name: userTickets.name,
})
.from(ticketsEventIdLinks)
.where(eq(ticketsEventIdLinks.eventId, eventId))
.leftJoin(
userTicketsTicketIdLinks,
eq(ticketsEventIdLinks.ticketId, userTicketsTicketIdLinks.ticketId)
)
.leftJoin(
userTickets,
eq(userTicketsTicketIdLinks.userTicketId, userTickets.id)
);

console.log(eventTickets);
console.log("isPriceIdForEvent: isUsedForEvent: ", isUsedForEvent);

// if array is 1, true. If 0, set to false.
if (isUsedForEvent.length !== 0) {
isEventPriceId = true;
} else if (isUsedForEvent.length === 0) {
isEventPriceId = false;
}

return eventTickets;
}

export async function updateUserTicket() {
console.log("I WAS CALLED");
// if (eventId < 0 || eventId === undefined || eventId === null) {
// throw new Error(
// "received invalid type for getUserTickets() in eventsGateway" + eventId
// );
// }

let eventTickets = await db
.select({
id: userTickets.id,
userTicketCode: userTickets.peopleTicketCode,
name: userTickets.name,
})
.from(ticketsEventIdLinks)
.where(eq(ticketsEventIdLinks.eventId, 3))
.leftJoin(
userTicketsTicketIdLinks,
eq(ticketsEventIdLinks.ticketId, userTicketsTicketIdLinks.ticketId)
)
.leftJoin(
userTickets,
eq(userTicketsTicketIdLinks.userTicketId, userTickets.id)
);

console.log(eventTickets);

return eventTickets;
return isEventPriceId;
}

0 comments on commit 67edad3

Please sign in to comment.