StructuresProvider.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 | null): Promise<Array<Structure>> {
  19. let url = '/api/public/federation_structures'
  20. if (parentId !== null) {
  21. url += `?parents=${parentId}`
  22. }
  23. return await this.get(url).then((res) => {
  24. return res["hydra:member"].map((s: any) => { return this.normalize(s) })
  25. })
  26. }
  27. async getById (organizationId: number): Promise<Structure> {
  28. return await this.get(
  29. `/api/public/federation_structures/${organizationId}`
  30. ).then((s) => {
  31. return this.normalize(s)
  32. })
  33. }
  34. }
  35. export default StructuresProvider