organizationProfile.ts 6.1 KB

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