Skip to content

Backend Architecture

Jeffery edited this page Sep 14, 2024 · 9 revisions

We will be using express as the main framework and tsoa as a framework to abstract away some manual processes such as:

  • Route creation (All done in controllers)
  • Authentication middleware
  • Request validation
  • Documentation (Done through annotations)

This means that for the backend we will need to focus on writing:

response-models: Typescript interfaces which describe what response data will be returned

request-models: Typescript interfaces which describe what data is expected in the request

models of DTOs: Typescript interfaces which describe the shape of data

services: Typescript classes which perform operations that are consumed by controllers

controllers: Typescript classes annotated using tsoa to describe the behavior of routes

Note on dates

Please be aware that ALL dates should be stored as UTC midnight - i.e if you want to store the date 27/04/2022 you must first remove the local timezone offset and then set the time to exactly midnight on every level of precision.

Due to the way readonly props are serialized, the nanoseconds and seconds fields in Timestamp are prefixed with an underscore (_) keep this in mind when writing tests etc.

Please also be aware that timestamps converted from dates may not have their helper functions that some Firestore functionalities requires. Timestamp.fromDate is a safe way to ensure that the timestamps are correct.

Booking Logic

Checking availability

We have booking_slots which refer to the date and has a max_bookings. The way we calculate the availability is:

  1. check how many slots there are in the bookings collection - which refers to individual bookings made, no date data is stored in there, instead it has a field that refers to an id of a booking_slot
  2. the available slots for a booking_slot is: the max_bookings minus the amount of bookings pointing to a booking_slot in case of displaying availability to the user we also minus off the amount of checkout sessions that have that booking_slot's id in its metadata
  3. The way we use this with stripe is when we create a checkout session we add an array of dates pointing to the booking_slot id (serialised as a string with JSON.stringify) to the metadata.