import { defineStore } from 'pinia' import { computed } from 'vue' import type { Ref } from 'vue' import * as _ from 'lodash-es' import type { BaseOrganizationProfile } from '~/types/interfaces' import { LEGAL_STATUS, NETWORK, ORGANIZATION_ID, PRODUCT, } from '~/types/enum/enums' import type OrganizationProfile from '~/models/Organization/OrganizationProfile' export const useOrganizationProfileStore = defineStore( 'organizationProfile', () => { // State const id: Ref = ref(null) const parametersId: Ref = ref(null) const name: Ref = ref(null) const product: Ref = ref(null) const currentActivityYear: Ref = ref(null) const modules: Ref> = ref([]) const hasChildren: Ref = ref(false) const legalStatus: Ref = ref(null) const principalType: Ref = ref(null) const showAdherentList: Ref = ref(false) const networks: Ref> = ref([]) const website: Ref = ref(null) const parents: Ref> = ref([]) const isTrialActive: Ref = ref(false) const trialCountDown: Ref = ref(0) const productBeforeTrial: Ref = ref(null) // Getters /** * L'organization fait-elle partie du réseau CMF ? * * @return {boolean} */ const isCmf = computed((): boolean => { return ( networks.value && networks.value.filter((network: string) => { return network === NETWORK.CMF }).length > 0 ) }) /** * L'organization fait-elle partie du réseau FFEC ? * * @return {boolean} */ const isFfec = computed((): boolean => { return ( networks.value && networks.value.filter((network: string) => { return network === NETWORK.FFEC }).length > 0 ) }) /** * L'organization fait-elle partie d'un réseau ? * * @return {boolean} */ const isInsideNetwork = computed((): boolean => { return isCmf.value || isFfec.value }) /** * L'organization possède t'elle un produit premium * @return {boolean} */ const isArtistProduct = computed((): boolean => { return product.value === PRODUCT.ARTIST }) /** * L'organization possède t'elle un produit artiste premium * @return {boolean} */ const isArtistPremiumProduct = computed((): boolean => { return product.value === PRODUCT.ARTIST_PREMIUM }) /** * L'organization possède-t-elle un produit artiste ou artiste premium * @return {boolean} */ const isArtist = computed((): boolean => { return isArtistProduct.value || isArtistPremiumProduct.value }) /** * L'organization possède-t-elle un produit school? * * @return {boolean} */ const isSchoolProduct = computed((): boolean => { return product.value === PRODUCT.SCHOOL }) /** * L'organization possède-t-elle un produit school-premium? * * @return {boolean} */ const isSchoolPremiumProduct = computed((): boolean => { return product.value === PRODUCT.SCHOOL_PREMIUM }) /** * L'organization possède-t-elle un produit school ou school-premium * @return {boolean} */ const isSchool = computed((): boolean => { return isSchoolProduct.value || isSchoolPremiumProduct.value }) /** * L'organization possède-t-elle un produit manager * @return {boolean} */ const isManagerProduct = computed((): boolean => { return product.value === PRODUCT.MANAGER }) /** * L'organization peut-elle afficher la lister des adhérents avec leurs coordonnées * @return {boolean|null} */ const isShowAdherentList = computed((): boolean => { return showAdherentList.value === true }) /** * L'organization est elle une association ? * @return {boolean|null} */ const isAssociation = computed((): boolean => { return legalStatus.value === LEGAL_STATUS.ASSOCIATION_LAW_1901 }) /** * L'organization est-elle la CMF ? * * @return {boolean} */ const isCMFCentralService = computed((): boolean => { return id.value === ORGANIZATION_ID.CMF }) const getWebsite = computed((): string | null => { return website.value ?? null }) const hasModule = (module: string): boolean => { return modules.value && modules.value.includes(module) } /** * /!\ Server side only * @param profile */ const initiateProfile = (profile: OrganizationProfile) => { setProfile(profile) // >>> Triggers a listener in plugins/ability } // Actions const setProfile = (profile: OrganizationProfile) => { id.value = profile.id as number parametersId.value = profile.parametersId name.value = profile.name product.value = profile.product currentActivityYear.value = profile.currentYear website.value = profile.website modules.value = Array.from(profile.modules) hasChildren.value = profile.hasChildren legalStatus.value = profile.legalStatus principalType.value = profile.principalType showAdherentList.value = profile.showAdherentList isTrialActive.value = profile.trialActive trialCountDown.value = profile.trialCountDown networks.value = Array.from(profile.networks) productBeforeTrial.value = profile.productBeforeTrial _.each(profile.parents, (parent) => { parents.value.push({ id: parent.id, name: parent.name, website: parent.website, }) }) } const refreshProfile = (profile: OrganizationProfile) => { name.value = profile.name currentActivityYear.value = profile.currentYear website.value = profile.website legalStatus.value = profile.legalStatus } return { id, parametersId, name, product, currentActivityYear, modules, hasChildren, legalStatus, principalType, showAdherentList, networks, website, parents, isCmf, isCMFCentralService, isFfec, isInsideNetwork, isArtistProduct, isArtistPremiumProduct, isArtist, isSchoolPremiumProduct, isSchoolProduct, isSchool, isManagerProduct, isShowAdherentList, isAssociation, isTrialActive, trialCountDown, productBeforeTrial, getWebsite, hasModule, setProfile, refreshProfile, initiateProfile, } }, )