organizationProfile.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import { defineStore } from 'pinia'
  2. import { computed } from 'vue'
  3. import type { Ref } from 'vue'
  4. import * as _ from 'lodash-es'
  5. import type { BaseOrganizationProfile } from '~/types/interfaces'
  6. import {
  7. LEGAL_STATUS,
  8. NETWORK,
  9. ORGANIZATION_ID,
  10. PRODUCT,
  11. } from '~/types/enum/enums'
  12. import type OrganizationProfile from '~/models/Organization/OrganizationProfile'
  13. export const useOrganizationProfileStore = defineStore(
  14. 'organizationProfile',
  15. () => {
  16. // State
  17. const id: Ref<number | null> = ref(null)
  18. const parametersId: Ref<number | null> = ref(null)
  19. const name: Ref<string | null> = ref(null)
  20. const product: Ref<string | null> = ref(null)
  21. const currentActivityYear: Ref<number | null> = ref(null)
  22. const modules: Ref<Array<string>> = ref([])
  23. const hasChildren: Ref<boolean | null> = ref(false)
  24. const legalStatus: Ref<string | null> = ref(null)
  25. const showAdherentList: Ref<boolean | null> = ref(false)
  26. const networks: Ref<Array<string>> = ref([])
  27. const website: Ref<string | null> = ref(null)
  28. const parents: Ref<Array<BaseOrganizationProfile>> = ref([])
  29. // Getters
  30. /**
  31. * L'organization fait-elle partie du réseau CMF ?
  32. *
  33. * @return {boolean}
  34. */
  35. const isCmf = computed((): boolean => {
  36. return (
  37. networks.value &&
  38. networks.value.filter((network: string) => {
  39. return network === NETWORK.CMF
  40. }).length > 0
  41. )
  42. })
  43. /**
  44. * L'organization fait-elle partie du réseau FFEC ?
  45. *
  46. * @return {boolean}
  47. */
  48. const isFfec = computed((): boolean => {
  49. return (
  50. networks.value &&
  51. networks.value.filter((network: string) => {
  52. return network === NETWORK.FFEC
  53. }).length > 0
  54. )
  55. })
  56. /**
  57. * L'organization fait-elle partie d'un réseau ?
  58. *
  59. * @return {boolean}
  60. */
  61. const isInsideNetwork = computed((): boolean => {
  62. return isCmf.value || isFfec.value
  63. })
  64. /**
  65. * L'organization possède t'elle un produit premium
  66. * @return {boolean}
  67. */
  68. const isArtistProduct = computed((): boolean => {
  69. return product.value === PRODUCT.ARTIST
  70. })
  71. /**
  72. * L'organization possède t'elle un produit artiste premium
  73. * @return {boolean}
  74. */
  75. const isArtistPremiumProduct = computed((): boolean => {
  76. return product.value === PRODUCT.ARTIST_PREMIUM
  77. })
  78. /**
  79. * L'organization possède-t-elle un produit artiste ou artiste premium
  80. * @return {boolean}
  81. */
  82. const isArtist = computed((): boolean => {
  83. return isArtistProduct.value || isArtistPremiumProduct.value
  84. })
  85. /**
  86. * L'organization possède-t-elle un produit school?
  87. *
  88. * @return {boolean}
  89. */
  90. const isSchoolProduct = computed((): boolean => {
  91. return product.value === PRODUCT.SCHOOL
  92. })
  93. /**
  94. * L'organization possède-t-elle un produit school-premium?
  95. *
  96. * @return {boolean}
  97. */
  98. const isSchoolPremiumProduct = computed((): boolean => {
  99. return product.value === PRODUCT.SCHOOL_PREMIUM
  100. })
  101. /**
  102. * L'organization possède-t-elle un produit school ou school-premium
  103. * @return {boolean}
  104. */
  105. const isSchool = computed((): boolean => {
  106. return isSchoolProduct.value || isSchoolPremiumProduct.value
  107. })
  108. /**
  109. * L'organization possède-t-elle un produit manager
  110. * @return {boolean}
  111. */
  112. const isManagerProduct = computed((): boolean => {
  113. return product.value === PRODUCT.MANAGER
  114. })
  115. /**
  116. * L'organization peut-elle afficher la lister des adhérents avec leurs coordonnées
  117. * @return {boolean|null}
  118. */
  119. const isShowAdherentList = computed((): boolean => {
  120. return showAdherentList.value === true
  121. })
  122. /**
  123. * L'organization est elle une association ?
  124. * @return {boolean|null}
  125. */
  126. const isAssociation = computed((): boolean => {
  127. return legalStatus.value === LEGAL_STATUS.ASSOCIATION_LAW_1901
  128. })
  129. /**
  130. * L'organization est-elle la CMF ?
  131. *
  132. * @return {boolean}
  133. */
  134. const isCMFCentralService = computed((): boolean => {
  135. return id.value === ORGANIZATION_ID.CMF
  136. })
  137. const getWebsite = computed((): string | null => {
  138. return website.value ?? null
  139. })
  140. const hasModule = (module: string): boolean => {
  141. return modules.value && modules.value.includes(module)
  142. }
  143. /**
  144. * /!\ Server side only
  145. * @param profile
  146. */
  147. const initiateProfile = (profile: OrganizationProfile) => {
  148. setProfile(profile)
  149. // >>> Triggers a listener in plugins/ability
  150. }
  151. // Actions
  152. const setProfile = (profile: OrganizationProfile) => {
  153. id.value = profile.id as number
  154. parametersId.value = profile.parametersId
  155. name.value = profile.name
  156. product.value = profile.product
  157. currentActivityYear.value = profile.currentYear
  158. website.value = profile.website
  159. modules.value = Array.from(profile.modules)
  160. hasChildren.value = profile.hasChildren
  161. legalStatus.value = profile.legalStatus
  162. showAdherentList.value = profile.showAdherentList
  163. networks.value = Array.from(profile.networks)
  164. _.each(profile.parents, (parent) => {
  165. parents.value.push({
  166. id: parent.id,
  167. name: parent.name,
  168. website: parent.website,
  169. })
  170. })
  171. }
  172. const refreshProfile = (profile: OrganizationProfile) => {
  173. name.value = profile.name
  174. currentActivityYear.value = profile.currentYear
  175. website.value = profile.website
  176. legalStatus.value = profile.legalStatus
  177. }
  178. return {
  179. id,
  180. parametersId,
  181. name,
  182. product,
  183. currentActivityYear,
  184. modules,
  185. hasChildren,
  186. legalStatus,
  187. showAdherentList,
  188. networks,
  189. website,
  190. parents,
  191. isCmf,
  192. isCMFCentralService,
  193. isFfec,
  194. isInsideNetwork,
  195. isArtistProduct,
  196. isArtistPremiumProduct,
  197. isArtist,
  198. isSchoolPremiumProduct,
  199. isSchoolProduct,
  200. isSchool,
  201. isManagerProduct,
  202. isShowAdherentList,
  203. isAssociation,
  204. getWebsite,
  205. hasModule,
  206. setProfile,
  207. refreshProfile,
  208. initiateProfile,
  209. }
  210. },
  211. )