---
# System prepended metadata

title: hydrate an appointment

---

# hydrate an appointment

ideas: add Shops-to-resources table

## create sub-query filter lists
- If customer gids supplied as filter
    - Get filtered list of customers
      - **line 80**
          1:  create array: customer[]
          select from *customersRepo* whereIn(gid === filter gid)

      - **line 88**
          2:  create array: customer_id[] (repo id)

      - **line 91**
          2:  set query param: { customerIds: customer_id[] }
  
- If resource gids supplied as filter
    - Get filtered list of resources
      - **line 95**
          1:  create array: resource[]
          select from *appointmentsToresourcesRepo* whereIn (gid === filter gid)

      - **line 101**
          2:  create array: unique appointment_id[] (repo id)

      - **line 103**
          2:  set query param: { id: appointment_id[] }

## create appointment_id list to query from appointments
- State: line 105  right now dbQuery = { id: appointment_id, customerIds: customer_id[] }

- line 107
    If shop gids supplied as filter

  - Note: this is where having shops-to-resources table would come in.
  - assumption: Resources are stored already as an array on a shop
      - Get list of appointment_ids filtered by relevant resource_id
          - **line 109**
          1:  create array: shops[]
              select from shopsRepo: returns shape of:
              [{ id, shopGid, resources: resource[] }]

          - **line 115**
          1:  with above assumption, we will extract ids from aggregated array
          1b: without above assumption, we will create this list from shops-to-resources table instead
          2:  create array: resource_id[] (repo id)

          - **line 128**
          3:  create array: unique appointment_id[] (repo id)
              select from: appointment-to-resources whereIn(resource_id === [line115])

          - **line 130**
          4:  update query param: { [...dbQuery.id, ...[line128]] }

## Get relevant appointment objects
- Get filtered list of appointments
    - line 133
      - 1:  create array: ???
          select - explain

      - 1b: Is it:
      - 1a: create array: appointment[] filtered by dbQuery.id[]

- Isolate relevant appointment and customer ids based on applied filters
    - line 136
      1:  create arrays: { appointmentIds: appointment_id[](repo id), customerIds: customer_id[](repo id) }

- Create filtered list of appointmentResourceRelationships
    - line 146
      1:  create array: appointmentResourceRelationship[] whose appointment_id exists in [line136] appointmentIds

- Create map from appointmentResourceRelationships for simple look up by appointment_id
    - line 152
      1:  create map: { [appointment_id]: resources[] }

- Create unique list of resource_id
    - line 160
      1:  create set: resource_id[] from [line152].values()

- Create list of resources filtered by list of unique resouce_id
    - line 164
      1:  create array: resource[]
      select from resourceRepo whereIn(resource_id === [line160])

- Create map of resources for simple look up by resource_id
    - line 170
      1:  create map: { [resource_id]: resource }

- Find resource_id(s) and associated resources; assign to appointmentResourceMap[appointment_id]
    - line 179
      1:  create map: { [appointment_id]: resources[] }

- Create list of customers filtered by customer_id
    - line 189
      1:  create array: customer[]
      select from customerRepo

- Create map from customers list for simple look up by customer_id
    - line 195
      1:  create map: { [customer_id]: customer }

- Create hydrated appointment
    - line 204
      1:  create array: [{ ...appointment, customer: customer{}, resources: resource[], type: appointmentType{} }]
