diff --git a/server/src/mail/orderConfirmation.ts b/server/src/mail/orderConfirmation.ts index e3c66a4..cb0b9c8 100644 --- a/server/src/mail/orderConfirmation.ts +++ b/server/src/mail/orderConfirmation.ts @@ -16,6 +16,22 @@ Din tid ved ${order.trainer.first_name} ${order.trainer.last_name} i ${order.tra Vi glæder os til at se dig ${formatDate(order.startDate)} ${formatTime(order.startDate)} - ${formatTime(order.endDate)} +Venlig hilsen Fitness World.` + }) + + await transporter.sendMail({ + from: `Fitness World <${smtpAddress}>`, + to: order.trainer.email, + subject: "Ny ordre - Fitness World", + text: + `Hej, ${order.trainer.first_name} ${order.trainer.last_name}. + +Der er blevet bestilt en tid af + +${user.first_name} ${user.last_name} i ${order.trainer.center_name} + +${formatDate(order.startDate)} ${formatTime(order.startDate)} - ${formatTime(order.endDate)} + Venlig hilsen Fitness World.` }) } \ No newline at end of file diff --git a/server/src/routes/createOrder.ts b/server/src/routes/createOrder.ts index cf39d50..ef83bc4 100644 --- a/server/src/routes/createOrder.ts +++ b/server/src/routes/createOrder.ts @@ -11,7 +11,6 @@ import Stripe from 'stripe'; import { stripe } from "../stripe"; import Trainer from "../interfaces/trainer"; import { baseURL } from "../environment"; -import { sendOrderConfirmationEmail } from "../mail/orderConfirmation"; import User from "../interfaces/user"; import { Order } from "../interfaces/order"; import { formatDate, formatTime } from "../utils/dates"; diff --git a/server/src/webhooks/checkoutSessionCompleted.ts b/server/src/webhooks/checkoutSessionCompleted.ts index 5656d4e..6e9efed 100644 --- a/server/src/webhooks/checkoutSessionCompleted.ts +++ b/server/src/webhooks/checkoutSessionCompleted.ts @@ -53,50 +53,44 @@ export default async function checkoutSessionCompleted(event: Stripe.Event) { ]); const orderLookup = await client.query(` - SELECT + SELECT orders.id, order_status, price, - start_time, - end_time, + created_at, + payment_intents.status as payment_intent_status, + start_time as "startDate", + end_time as "endDate", json_build_object( 'id',trainers.id, - 'first_name',users.first_name, - 'last_name',users.last_name, + 'first_name',trainer_user.first_name, + 'last_name',trainer_user.last_name, 'center_id',trainers.center_id, - 'center_name',centers.name - ) as trainer + 'center_name',centers.name, + 'email',trainer_user.email + ) as trainer, + json_build_object( + 'id', order_user.id, + 'first_name', order_user.first_name, + 'last_name', order_user.last_name, + 'email', order_user.email + ) as user FROM orders LEFT JOIN reserved_timeslots ON reserved_timeslots.id = orders.timeslot_id + LEFT JOIN payment_intents ON payment_intents.id = orders.payment_intent LEFT JOIN trainers ON trainers.id = reserved_timeslots.trainer_id LEFT JOIN centers on trainers.center_id = centers.id - LEFT JOIN users on users.id = trainers.user_id + LEFT JOIN users AS trainer_user on trainer_user.id = trainers.user_id + LEFT JOIN users AS order_user on order_user.id = orders.user_id WHERE checkout_session = $1; `, [ session.id ]); + const user: User = orderLookup.rows[0].user; + orderLookup.rows[0].user = undefined; const order: Order = orderLookup.rows[0]; - order.startDate = orderLookup.rows[0].start_time; - order.endDate = orderLookup.rows[0].end_time; - - const userLookup = await client.query(` - SELECT - users.email, - users.id, - users.first_name, - users.last_name - FROM orders - LEFT JOIN users on users.id = orders.user_id - WHERE - checkout_session = $1; - `, [ - session.id - ]); - - const user: User = userLookup.rows[0]; - sendOrderConfirmationEmail(user, order); } \ No newline at end of file