Skip to content

Commit

Permalink
Merge branch 'feat/ticket-scanning-page' of https://github.com/UoaWDC…
Browse files Browse the repository at this point in the history
…C/auis-portal into feat/ticket-scanning-page
  • Loading branch information
gmat224 committed Dec 9, 2024
2 parents 8228edc + 0cceeee commit e838835
Show file tree
Hide file tree
Showing 9 changed files with 310 additions and 403 deletions.
2 changes: 2 additions & 0 deletions api/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ jspm_packages/
#Migrations
db/migrations/
db/migrations/meta/
schemas/migrations/
schemas/migrations/meta/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
Expand Down
111 changes: 33 additions & 78 deletions api/controller/stripeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,17 @@ const endpointSecret: string = process.env.STRIPE_WEBHOOK_ENDPOINT as string;
export const createCheckout = asyncHandler(
async (req: Request, res: Response) => {
const { priceId } = req.body;

const session = req.session!;

// console.log(session.getUserId());

let email;

if (session) {
const user = await getUser(session.getUserId());
email = user?.emails[0];
} else {
// set to undefined so user manually fills out their email
email = undefined;
}

//prefill user email if they are logged in

// if priceId is undefined, send a 404 back.
if (priceId == undefined || priceId == "") {
return res
Expand All @@ -54,85 +52,42 @@ export const createCheckout = asyncHandler(
reserveTicket(priceId);
}
} else if (isEventTicket === "n") {
if (!email) {
return res.send({
error:
"There are no tickets available for this event. Please come back later to see if more tickets become available.",
});
}
// do nothing
}

// epoch time in seconds, 30mins timeout
let session_expiry = Math.floor(new Date().getTime() / 1000 + 30 * 60);

// Set customer email if they are logged in or don't if they aren't

try {
if (email) {
const session = await stripe.checkout.sessions.create({
//do not change anything below
ui_mode: "embedded",
// Figure out some way to see if user is logged in, and if so then auto enter email pls
customer_email: `${email}`,
// invoice_creation: {
// enabled: true,
// },
expires_at: session_expiry,
line_items: [
{
// Provide the exact Price ID (pr_1234) of the product on sale
price: priceId,
quantity: 1,
},
],
mode: "payment",
payment_method_types: ["card"],
currency: "NZD",
return_url: `${process.env.DOMAIN_FRONTEND}/return?session_id={CHECKOUT_SESSION_ID}`,
allow_promotion_codes: true,

//changeable below:
// use metadata property
metadata: {
priceId: `${priceId}`,
isEventTicket: `${isEventTicket}`,
const session = await stripe.checkout.sessions.create({
//do not change anything below
ui_mode: "embedded",
customer_email: email,
// invoice_creation: {
// enabled: true,
// },
expires_at: session_expiry,
line_items: [
{
// Provide the exact Price ID (pr_1234) of the product on sale
price: priceId,
quantity: 1,
},
});

res.send({ clientSecret: session.client_secret });
} else {
const session = await stripe.checkout.sessions.create({
//do not change anything below
ui_mode: "embedded",
// Figure out some way to see if user is logged in, and if so then auto enter email pls
// customer_email: `${email}`,
// invoice_creation: {
// enabled: true,
// },
expires_at: session_expiry,
line_items: [
{
// Provide the exact Price ID (pr_1234) of the product on sale
price: priceId,
quantity: 1,
},
],
mode: "payment",
payment_method_types: ["card"],
currency: "NZD",
return_url: `${process.env.DOMAIN_FRONTEND}/return?session_id={CHECKOUT_SESSION_ID}`,
allow_promotion_codes: true,

//changeable below:
// use metadata property
metadata: {
priceId: `${priceId}`,
isEventTicket: `${isEventTicket}`,
},
});

res.send({ clientSecret: session.client_secret });
}
],
mode: "payment",
payment_method_types: ["card"],
currency: "NZD",
return_url: `${process.env.DOMAIN_FRONTEND}/return?session_id={CHECKOUT_SESSION_ID}`,
allow_promotion_codes: true,

//changeable below:
metadata: {
priceId: `${priceId}`,
isEventTicket: `${isEventTicket}`,
},
});

res.send({ clientSecret: session.client_secret });
} catch (error) {
res.send({ error }).status(404);
}
Expand Down
5 changes: 1 addition & 4 deletions api/drizzle-schema.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ import { defineConfig } from "drizzle-kit";

export default defineConfig({
schema: "./schemas/*",
out: "./schemas",
out: "./schemas/migrations",
dialect: "postgresql",
dbCredentials: {
url: process.env.DATABASE_URL!,
},
introspect: {
casing: "preserve",
},
verbose: true,
strict: true,
});
2 changes: 1 addition & 1 deletion api/drizzle.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { defineConfig } from "drizzle-kit";

export default defineConfig({
schema: "./schemas/*",
out: "./db/migrations",
out: "./schemas/migrations",
dialect: "postgresql",
dbCredentials: {
url: process.env.DATABASE_URL!,
Expand Down
5 changes: 1 addition & 4 deletions api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,8 @@ app.use(

// Routes
app.use("/api/user", userRoutes);

//StripeJS
app.use("/api/stripe", stripeRoutes);

app.use("/api/event", eventRoutes);
app.use("/api/stripe", stripeRoutes);

// The custom handlers in /middleware need to be below Routes
app.use(notFound);
Expand Down
2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"db:seed": "node -r esbuild-register ./scripts/seed.ts",
"db": "yarn db:generate && yarn db:migrate",
"db:visualise": "drizzle-kit studio",
"db:pull": " drizzle-kit introspect --config=drizzle-schema.config.ts",
"db:pull": " drizzle-kit introspect --config=drizzle.config.ts",
"db:all": "yarn db:generate && yarn db:migrate && yarn db:seed && yarn drizzle-kit studio --config=drizzle.config.ts",
"generate-types": "quicktype ../api/types/types.ts -o ../web/src/types/backend-types.ts --just-types",
"watch-types": "nodemon -e ts -x \"yarn generate-types\""
Expand Down
Loading

0 comments on commit e838835

Please sign in to comment.