organizationProfile.ts 6.6 KB

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