organizationProfile.ts 6.3 KB

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