StructuresProvider.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import BaseProvider from '~/services/data/BaseProvider'
  2. class StructuresProvider extends BaseProvider {
  3. protected normalize (s: any) : Structure {
  4. // Define the on-map address according to the chosen priorities
  5. s.mapAddress = s.addresses.find((a: Address) => { return a.type === 'ADDRESS_PRACTICE' }) ||
  6. s.addresses.find((a: Address) => { return a.type === 'ADDRESS_HEAD_OFFICE' }) ||
  7. s.addresses.find((a: Address) => { return a.type === 'ADDRESS_CONTACT' }) ||
  8. null
  9. // Define the postal address according to the chosen priorities
  10. s.postalAddress = s.addresses.find((a: Address) => { return a.type === 'ADDRESS_CONTACT' }) ||
  11. s.addresses.find((a: Address) => { return a.type === 'ADDRESS_HEAD_OFFICE' }) ||
  12. null
  13. if (s.hasOwnProperty('articles') && s.articles) {
  14. s.articles.sort((a: Article, b: Article) => { return a.date > b.date ? -1 : 1 })
  15. }
  16. return s
  17. }
  18. async getAll (parentId: number): Promise<Array<Structure>> {
  19. return await this.get(
  20. `/api/public/federation_structures?parents=${parentId}`
  21. ).then((res) => {
  22. return res["hydra:member"].map((s: any) => { return this.normalize(s) })
  23. })
  24. }
  25. async getById (organizationId: number): Promise<Structure> {
  26. return await this.get(
  27. `/api/public/federation_structures/${organizationId}`
  28. ).then((s) => {
  29. return this.normalize(s)
  30. })
  31. }
  32. }
  33. export default StructuresProvider