Skip to content

Commit

Permalink
Refactor to store session dates in session metadata (#518)
Browse files Browse the repository at this point in the history
* add check

* update minutes ago

* display date properly

* fix newlines

* improve docs

* make date persist properly

* improve docs

* regen code

* be clear that dates refer to nights
  • Loading branch information
choden-dev authored Jun 26, 2024
1 parent d8d8ca1 commit 4d85c52
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
10 changes: 10 additions & 0 deletions client/src/models/__generated__/schema.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion server/src/business-layer/utils/StripeSessionMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,20 @@ export enum CheckoutTypeValues {
*
* @example '["ef40TsK5emACG5K08j2n", "Zx0hVyCax6YhTl9PRcTq"]'
*/
export const BOOKING_SLOTS_KEY = "booking_slot"
export const BOOKING_SLOTS_KEY = "booking_slot" as const

/**
* Metadata used for better UX when user has an existing session
* Should retrieve this from the session metadata
*
* **Start Date** the last date (inclusive) for the booking
*/
export const START_DATE = "start_date" as const

/**
* Metadata used for better UX when user has an existing session
* Should retrieve this from the session metadata
*
* **End Date** the last date (inclusive) for the booking
*/
export const END_DATE = "end date" as const
1 change: 1 addition & 0 deletions server/src/middleware/__generated__/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions server/src/service-layer/controllers/PaymentController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { AuthServiceClaims } from "business-layer/utils/AuthServiceClaims"
import {
BOOKING_SLOTS_KEY,
CHECKOUT_TYPE_KEY,
CheckoutTypeValues
CheckoutTypeValues,
END_DATE,
START_DATE
} from "business-layer/utils/StripeSessionMetadata"
import {
MembershipTypeValues,
Expand Down Expand Up @@ -244,6 +246,11 @@ export class PaymentController extends Controller {
}
}

/**
* Creates a new booking session for the date ranges passed in,
* will return any existing sessions if they have been started in
* the last 30 minutes (the minimum period stripe has to persist a session for)
*/
@SuccessResponse("200", "Created booking checkout session")
@Security("jwt", ["member"])
@Post("booking")
Expand Down Expand Up @@ -309,7 +316,7 @@ export class PaymentController extends Controller {
this.setStatus(200)
return {
stripeClientSecret: activeSession.client_secret,
message: `Existing booking checkout session found for the dates ${BOOKING_START_DATE} to ${BOOKING_END_DATE}, you may start a new one after ${sessionStartTime} (NZST)`
message: `Existing booking checkout session found for the nights ${activeSession.metadata[START_DATE] || ""} to ${activeSession.metadata[END_DATE] || ""}, you may start a new one after ${sessionStartTime} (NZST)`
}
}
}
Expand Down Expand Up @@ -429,7 +436,9 @@ export class PaymentController extends Controller {
[LODGE_PRICING_TYPE_KEY]: requiredBookingType,
[BOOKING_SLOTS_KEY]: JSON.stringify(
bookingSlots.map((slot) => slot.id)
)
),
[START_DATE]: BOOKING_START_DATE,
[END_DATE]: BOOKING_END_DATE
},
stripeCustomerId,
undefined,
Expand All @@ -442,7 +451,7 @@ export class PaymentController extends Controller {
this.setStatus(200)
return {
stripeClientSecret: clientSecret,
message: `You have until ${new Date(Date.now() + THIRTY_MINUTES_MS).toLocaleTimeString("en-NZ")} to pay for the dates ${BOOKING_START_DATE} to ${BOOKING_END_DATE}`
message: `You have until ${new Date(Date.now() + THIRTY_MINUTES_MS).toLocaleTimeString("en-NZ")} to pay for the nights ${BOOKING_START_DATE} to ${BOOKING_END_DATE}`
}
} catch (e) {
this.setStatus(500)
Expand Down

0 comments on commit 4d85c52

Please sign in to comment.