import ApiRequestService from "./apiRequestService"; import {Repository, useRepo} from "pinia-orm"; import Url from "~/services/utils/url"; import ModelNormalizer from "./serializer/normalizer/modelNormalizer"; import HydraDenormalizer from "./serializer/denormalizer/hydraDenormalizer"; import ApiModel from "~/models/ApiModel"; import {useProfileAccessStore} from "~/store/profile/access"; import ApiResource from "~/models/ApiResource"; import {MyProfile} from "~/models/Access/MyProfile"; import { v4 as uuid4 } from 'uuid'; import {AssociativeArray, Collection} from "~/types/data.d"; /** * Entity manager: make operations on the models defined with the Pinia-Orm library * * @see https://pinia-orm.codedredd.de/ */ class EntityManager { private CLONE_PREFIX = '_clone_' private apiRequestService: ApiRequestService; public constructor(apiRequestService: ApiRequestService) { this.apiRequestService = apiRequestService } /** * Return the repository for the model * * @param model */ public getRepository(model: typeof ApiResource): Repository { return useRepo(model) } /** * Create a new instance of the given model * * @param model * @param properties */ public new(model: typeof ApiResource, properties: object = {}) { const repository = this.getRepository(model) const entity = repository.make(properties) // @ts-ignore if (!properties.hasOwnProperty('id') || !properties.id) { // Object has no id yet, we give him a temporary one entity.id = 'tmp' + uuid4() } repository.save(entity) this.saveInitialState(model, entity) return entity } /** * On met à jour directement l'entité par référence ou la liste d'entités, * pour maintenir la réactivité lorsque l'entité ou l'array est déclarée comme réactive * * Attention à ce que le sujet et la nouvelle valeur soient des objets de même type * * @see http://underscorejs.org/#extend * @param subject * @param newValue */ public reactiveUpdate(subject: ApiResource | Array, newValue: ApiResource | Array) { if (typeof subject !== typeof newValue) { // TODO: remplacer par des règles typescript console.log('Error : subject and new value have to share the same type') return } if (Array.isArray(subject)) { this.reactiveUpdateArray(subject as Array, newValue as Array) } else { this.reactiveUpdateItem(subject as ApiResource, newValue as ApiResource) } } private reactiveUpdateItem(entity: ApiResource, newEntity: ApiResource) { useExtend(entity, newEntity) } private reactiveUpdateArray(items: Array, newItems: Array) { items.length = 0 newItems.forEach((f: ApiResource) => { items.push(f) }) } /** * Fetch an Entity / ApiResource by its id, save it to the store and returns it * * @param model Model of the object to fetch * @param id Id of the object to fetch * @param forceRefresh Force a new get request to the api ; * current object in store will be overwritten if it exists */ public async fetch(model: typeof ApiResource, id: number, forceRefresh: boolean = false): Promise { const repository = this.getRepository(model) // If the entity is already in the store and forceRefresh is false, return the object in store if (!forceRefresh) { const item = repository.find(id) if (item && typeof item !== 'undefined') { return item } } // Else, get the object from the API const url = Url.join('api', model.entity, String(id)) const response = await this.apiRequestService.get(url) // deserialize the response const attributes = HydraDenormalizer.denormalize(response).data as object return this.new(model, attributes) } public findBy(model: typeof ApiResource, query: AssociativeArray) { // TODO: implement } public async fetchAll(model: typeof ApiResource, page: number = 1): Promise { let url = Url.join('api', model.entity) if (page !== 1) { url = Url.join(url, '?page=' + page) } console.log(url) const response = await this.apiRequestService.get(url) // deserialize the response const collection = HydraDenormalizer.denormalize(response) const items = collection.data.map((attributes: object) => { return this.new(model, attributes) }) return { items, totalItems: collection.metadata.totalItems, firstPage: collection.metadata.firstPage, lastPage: collection.metadata.lastPage, nextPage: collection.metadata.nextPage, previousPage: collection.metadata.previousPage, } } /** * Persist the entity as it is in the store into the data source via the API * * @param model * @param entity */ public async persist(model: typeof ApiModel, entity: ApiModel) { const repository = this.getRepository(model) let url = Url.join('api', model.entity) let response const data = ModelNormalizer.normalize(entity) if (!entity.isNew()) { url = Url.join(url, String(entity.id)) response = await this.apiRequestService.put(url, data) } else { delete data.id response = await this.apiRequestService.post(url, data) } const hydraResponse = await HydraDenormalizer.denormalize(response) const returnedEntity = this.new(model, hydraResponse.data) this.saveInitialState(model, returnedEntity) // Save data into the store repository.save(returnedEntity) if (['accesses', 'organizations', 'parameters', 'subdomains'].includes(model.entity)) { await this.refreshProfile() } this.reactiveUpdate(entity, returnedEntity) } /** * Delete the entity from the datasource via the API * * @param model * @param entity */ public async delete(model: typeof ApiModel, entity: ApiResource) { const repository = this.getRepository(model) // If object has been persisted to the datasource, send a delete request if (!entity.isNew()) { const url = Url.join('api', model.entity, String(entity.id)) await this.apiRequestService.delete(url) } // reactiveUpdate the store repository.destroy(entity.id) } /** * Reset the entity to its initial state (i.e. the state it had when it was fetched from the API) * * @param model * @param entity */ public reset(model: typeof ApiResource, entity: ApiResource) { const initialEntity = this.getInitialStateOf(model, entity.id) if (initialEntity === null) { throw new Error('no initial state recorded for this object - abort [' + model.entity + '/' + entity.id + ']') } const repository = this.getRepository(model) repository.save(initialEntity) this.reactiveUpdate(entity, initialEntity) } /** * Re-fetch the user profile and reactiveUpdate the store */ public async refreshProfile() { const response = await this.apiRequestService.get('api/my_profile') // deserialize the response const hydraResponse = await HydraDenormalizer.denormalize(response) const profile = this.new(MyProfile, hydraResponse.data) const profileAccessStore = useProfileAccessStore() profileAccessStore.setProfile(profile) } /** * Delete all records in the repository of the model * * @param model */ public async flush(model: typeof ApiModel) { const repository = this.getRepository(model) repository.flush() } /** * Is the entity a new one, or does it already exist in the data source (=API) * * @param model * @param id */ public isNewEntity(model: typeof ApiModel, id: number | string): boolean { const repository = this.getRepository(model) const item = repository.find(id) if (!item || typeof item === 'undefined') { console.error(model.entity + '/' + id, ' does not exist!') return false } return item.isNew() } /** * Save the state of the entity in the store, so this state could be be restored later * * @param model * @param entity * @private */ private saveInitialState(model: typeof ApiResource, entity: ApiResource) { const repository = this.getRepository(model) // Clone and prefix id const clone = useCloneDeep(entity) clone.id = this.CLONE_PREFIX + clone.id repository.save(clone) } /** * Return the saved state of the entity from the store * * @param model * @param id * @private */ private getInitialStateOf(model: typeof ApiResource, id: string | number): ApiResource | null { const repository = this.getRepository(model) // Find the clone by id const entity = repository.find(this.CLONE_PREFIX + id) if (entity === null) { return null } // Restore the initial id entity.id = id return entity } } export default EntityManager