import { computed, ComputedRef } from '@nuxtjs/composition-api' import { Item, Model } from '@vuex-orm/core' import { repositoryHelper } from '~/services/store/repository' import { QUERY_TYPE } from '~/types/enums' import { AnyJson, AnyStore, Historical } from '~/types/interfaces' import DataPersister from '~/services/data/dataPersister' import { MyProfile } from '~/models/Access/MyProfile' import { $accessProfile } from '@/services/profile/accessProfile' import { useContext } from '@nuxtjs/composition-api' /** * @category Use/updater * @class UseMyProfileUpdater * Use Classe pour la gestion REST de MyProfile */ export class UseMyProfileUpdater { private store!:AnyStore private $dataPersister!:DataPersister private myProfile!:MyProfile constructor (store:AnyStore, $dataPersister:DataPersister) { this.store = store this.$dataPersister = $dataPersister } /** * Composition function */ public invoke (): AnyJson { const {$ability} = useContext() const currentAccessId = $accessProfile(this.store).getCurrentAccessId() this.myProfile = this.getMyProfileInstance(currentAccessId) as MyProfile const activityYear:ComputedRef = computed(() => this.myProfile.activityYear) const historical:ComputedRef = computed(() => this.myProfile.historical) return { updateMyProfile: () => this.updateMyProfile(), setActivityYear: (activityYear:number) => this.setActivityYear(activityYear), setHistorical: (historicalChoices:Array) => this.setHistorical(historicalChoices), setHistoricalRange: (dates:Array) => this.setHistoricalRange(dates), activityYear, historical } } /** * récupère l'instance MyProfile * @param myProfileId */ private getMyProfileInstance (myProfileId:any): Item { return repositoryHelper.findItemFromModel(MyProfile, parseInt(myProfileId)) } /** * Mets à jour l'activity de my profile * @param activityYear */ private setActivityYear (activityYear:number) { if (activityYear <= 0) { throw new Error('year must be positive') } repositoryHelper.updateStoreFromField(MyProfile, this.myProfile, activityYear, 'activityYear') } /** * Mets à jour l'historical de my profile * @param historicalChoices */ private setHistorical (historicalChoices:Array) { repositoryHelper.updateStoreFromField(MyProfile, this.myProfile, this.getHistoricalEntry(historicalChoices), 'historical') } /** * Mets à jour l'historical de my profile * @param dates */ private setHistoricalRange (dates:Array) { repositoryHelper.updateStoreFromField(MyProfile, this.myProfile, this.getHistoricalRangeEntry(dates), 'historical') } /** * Transform les choix de l'historique en objet JSON reconnaissable coté API * @param historicalChoices */ private getHistoricalEntry (historicalChoices:Array) { const historicalDefault:any = { past: false, future: false, present: false } for (const historicalChoice of historicalChoices) { historicalDefault[historicalChoice] = true } return historicalDefault } /** * Trasnforme le choix des période en Objet JSON reconnaissable coté API * @param dates */ private getHistoricalRangeEntry (dates:Array) { return { past: false, future: false, present: false, dateStart: dates[0], dateEnd: dates[1] } } /** * Effectue la mise à jour (coté API) de MyProfile */ private async updateMyProfile (): Promise { await this.$dataPersister.invoke({ type: QUERY_TYPE.MODEL, model: MyProfile, id: this.myProfile.id }) } } export const $useMyProfileUpdater = (store:AnyStore, $dataPersister:DataPersister) => new UseMyProfileUpdater(store, $dataPersister).invoke()