api.js 896 B

123456789101112131415161718192021222324
  1. const app = require('../app')
  2. const _ = require('lodash')
  3. app.run(function(api) {
  4. api.statistics = () => api.get('/api/statistics')
  5. let locations = null
  6. api.locations = () => locations || (locations = api.get('/api/locations'))
  7. let locationsDictionary = null
  8. api.locationsDictionary = () => locationsDictionary || (locationsDictionary = api.locations().then(locations =>
  9. _.fromPairs(locations.map(loc => [loc.key, loc]))
  10. ))
  11. api.location = (key) => api.locations().then(async locations =>
  12. (await api.locationsDictionary())[key]
  13. )
  14. let staffMembers = null
  15. api.staffMembers = () => staffMembers || (staffMembers = api.get('/api/staff-members'))
  16. let staffMemberDictionary = null
  17. api.staffMemberDictionary = () => staffMemberDictionary || (staffMemberDictionary = api.staffMembers().then(staffMembers =>
  18. _.fromPairs(staffMembers.map(sm => [sm.id, sm]))
  19. ))
  20. })