organizationProfile.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 possède-t-elle un produit freemium
  121. * @return {boolean}
  122. */
  123. const isFreemiumProduct = computed((): boolean => {
  124. return product.value === PRODUCT.FREEMIUM
  125. })
  126. /**
  127. * L'organization peut-elle afficher la lister des adhérents avec leurs coordonnées
  128. * @return {boolean|null}
  129. */
  130. const isShowAdherentList = computed((): boolean => {
  131. return showAdherentList.value === true
  132. })
  133. /**
  134. * L'organization est elle une association ?
  135. * @return {boolean|null}
  136. */
  137. const isAssociation = computed((): boolean => {
  138. return legalStatus.value === LEGAL_STATUS.ASSOCIATION_LAW_1901
  139. })
  140. /**
  141. * L'organization est-elle la CMF ?
  142. *
  143. * @return {boolean}
  144. */
  145. const isCMFCentralService = computed((): boolean => {
  146. return id.value === ORGANIZATION_ID.CMF
  147. })
  148. const getWebsite = computed((): string | null => {
  149. return website.value ?? null
  150. })
  151. const hasModule = (module: string): boolean => {
  152. return modules.value && modules.value.includes(module)
  153. }
  154. /**
  155. * /!\ Server side only
  156. * @param profile
  157. */
  158. const initiateProfile = (profile: OrganizationProfile) => {
  159. setProfile(profile)
  160. // >>> Triggers a listener in plugins/ability
  161. }
  162. // Actions
  163. const setProfile = (profile: OrganizationProfile) => {
  164. id.value = profile.id as number
  165. parametersId.value = profile.parametersId
  166. name.value = profile.name
  167. product.value = profile.product
  168. currentActivityYear.value = profile.currentYear
  169. website.value = profile.website
  170. modules.value = Array.from(profile.modules)
  171. hasChildren.value = profile.hasChildren
  172. legalStatus.value = profile.legalStatus
  173. principalType.value = profile.principalType
  174. showAdherentList.value = profile.showAdherentList
  175. isTrialActive.value = profile.trialActive
  176. trialCountDown.value = profile.trialCountDown
  177. networks.value = Array.from(profile.networks)
  178. productBeforeTrial.value = profile.productBeforeTrial
  179. _.each(profile.parents, (parent) => {
  180. parents.value.push({
  181. id: parent.id,
  182. name: parent.name,
  183. website: parent.website,
  184. })
  185. })
  186. }
  187. const refreshProfile = (profile: OrganizationProfile) => {
  188. name.value = profile.name
  189. currentActivityYear.value = profile.currentYear
  190. website.value = profile.website
  191. legalStatus.value = profile.legalStatus
  192. }
  193. return {
  194. id,
  195. parametersId,
  196. name,
  197. product,
  198. currentActivityYear,
  199. modules,
  200. hasChildren,
  201. legalStatus,
  202. principalType,
  203. showAdherentList,
  204. networks,
  205. website,
  206. parents,
  207. isCmf,
  208. isCMFCentralService,
  209. isFfec,
  210. isInsideNetwork,
  211. isArtistProduct,
  212. isArtistPremiumProduct,
  213. isArtist,
  214. isSchoolPremiumProduct,
  215. isSchoolProduct,
  216. isSchool,
  217. isManagerProduct,
  218. isFreemiumProduct,
  219. isShowAdherentList,
  220. isAssociation,
  221. isTrialActive,
  222. trialCountDown,
  223. productBeforeTrial,
  224. getWebsite,
  225. hasModule,
  226. setProfile,
  227. refreshProfile,
  228. initiateProfile,
  229. }
  230. },
  231. )