organizationProfile.ts 6.5 KB

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