import ApiRequestService from "./apiRequestService" import {Repository, useRepo} from "pinia-orm" import UrlUtils from "~/services/utils/urlUtils" import HydraDenormalizer from "./normalizer/hydraDenormalizer" import ApiModel from "~/models/ApiModel" import ApiResource from "~/models/ApiResource" import MyProfile from "~/models/Access/MyProfile" import {v4 as uuid4} from 'uuid' import {AssociativeArray, Collection} from "~/types/data.d" import models from "~/models/models"; import {useAccessProfileStore} from "~/stores/accessProfile" import _ from "lodash" /** * Entity manager: make operations on the models defined with the Pinia-Orm library * * @see https://pinia-orm.codedredd.de/ */ class EntityManager { protected CLONE_PREFIX = '_clone_' protected apiRequestService: ApiRequestService public constructor( apiRequestService: ApiRequestService ) { this.apiRequestService = apiRequestService } /** * Return the repository for the model * * @param model */ public getRepository(model: typeof ApiResource): Repository { // TODO: voir si possible de passer par une injection de dépendance plutôt que par un use return useRepo(model) } /** * Cast an object as an ApiResource * This in used internally to ensure the object is recognized as an ApiResource * * @param model * @param entity * @protected */ // noinspection JSMethodCanBeStatic protected cast(model: typeof ApiResource, entity: ApiResource): ApiResource { return new model(entity) } /** * Return the model class with the given entity name * * @param entityName */ public getModelFor(entityName: string): typeof ApiResource { return models[entityName] } /** * Return the model class from an Opentalent Api IRI * * @param iri An IRI of the form .../api//... */ public getModelFromIri(iri: string): typeof ApiResource { const matches = iri.match(/^\/api\/(\w+)\/.*/) if (!matches || !matches[1]) { throw new Error('cannot parse the IRI') } return this.getModelFor(matches[1]) } /** * Create a new instance of the given model * * @param model * @param properties */ public newInstance(model: typeof ApiResource, properties: object = {}): ApiResource { const repository = this.getRepository(model) let entity = repository.make(properties) // Keep track of the entity's model entity.setModel(model) // @ts-ignore if (!properties.hasOwnProperty('id') || !properties.id) { // Object has no id yet, we give him a temporary one entity.id = 'tmp' + uuid4() } entity = repository.save(entity) this.saveInitialState(model, entity) return entity } /** * Save the entity into the store * * @param model * @param entity */ public save(model: typeof ApiResource, entity: ApiResource): ApiResource { return this.getRepository(model).save(entity) } /** * Find the entity into the store * TODO: comment réagit la fonction si l'id n'existe pas? * * @param model * @param id */ // @ts-ignore public find(model: typeof T, id: number): T { const repository = this.getRepository(model) return repository.find(id) as T } /** * 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 { // If the entity is already in the store and forceRefresh is false, return the object in store if (!forceRefresh) { const item = this.find(model, id) if (item && typeof item !== 'undefined') { return item } } // Else, get the object from the API const url = UrlUtils.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.newInstance(model, attributes) } /** * Fetch a collection of entity * The content of `query` is converted into a query-string in the request URL * * @param model * @param query * @param parent */ public async fetchCollection(model: typeof ApiResource, parent: ApiResource | null, query: AssociativeArray = []): Promise { let url if (parent !== null) { url = UrlUtils.join('api', parent.entity, '' + parent.id, model.entity) } else { url = UrlUtils.join('api', model.entity) } const response = await this.apiRequestService.get(url, query) // deserialize the response const apiCollection = HydraDenormalizer.denormalize(response) const items = apiCollection.data.map((attributes: object) => { return this.newInstance(model, attributes) }) return { items, totalItems: apiCollection.metadata.totalItems, pagination: { first: apiCollection.metadata.firstPage || 1, last: apiCollection.metadata.lastPage || 1, next: apiCollection.metadata.nextPage || undefined, previous: apiCollection.metadata.previousPage || undefined, } } } /** * Créé une entité à partir d'une réponse de l'api au format Hydra, l'enregistre * dans le store et la retourne * * @param model * @param response * @protected */ protected async saveResponseAsEntity(model: typeof ApiModel, response: Response) { const repository = this.getRepository(model) const hydraResponse = await HydraDenormalizer.denormalize(response) const returnedEntity = this.newInstance(model, hydraResponse.data) this.saveInitialState(model, returnedEntity) // Save data into the store repository.save(returnedEntity) return returnedEntity } /** * 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) { // Recast in case class definition has been "lost" entity = this.cast(model, entity) let url = UrlUtils.join('api', model.entity) let response const data: any = entity.$toJson() if (!entity.isNew()) { url = UrlUtils.join(url, String(entity.id)) response = await this.apiRequestService.put(url, data) } else { delete data.id response = await this.apiRequestService.post(url, data) } const createdEntity = this.saveResponseAsEntity(model, response) if (entity.isNew()) { this.removeTempAfterPersist(model, entity.id) } return createdEntity } /** * Send an update request (PUT) to the API with the given data on an existing entity * * @param model * @param id * @param data */ public async patch(model: typeof ApiModel, id: number, data: AssociativeArray) { let url = UrlUtils.join('api', model.entity, ''+id) const body = JSON.stringify(data) const response = await this.apiRequestService.put(url, body) return this.saveResponseAsEntity(model, response) } /** * 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 = UrlUtils.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) return initialEntity } /** * @todo: à déplacer dans le store directement * @Deprecated : a priori ce n'est pas le bon service pour mettre à jour le profil, on devrait voir ça * depuis un service dédié, un composable, ou directement dans le store ==> oui ! * * Re-fetch the user profile and update the store */ public async refreshProfile(accessId: number) { const profile = await this.fetch(MyProfile, accessId) // On met à jour le store accessProfile const accessProfile = useAccessProfileStore() accessProfile.setProfile(profile) } /** * Delete all records in the repository of the model * * @param model */ public 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) * * This is a convenient way of testing an entity you did not already fetch, else prefer the use of the * isNew() method of ApiResource * * @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') { // TODO: est-ce qu'il ne faudrait pas lever une erreur ici plutôt? 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 */ protected saveInitialState(model: typeof ApiResource, entity: ApiResource) { const repository = this.getRepository(model) // Clone and prefix id const clone = _.cloneDeep(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 */ protected 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 } /** * Delete the temporary entity from the repo after it was persisted via the api, replaced by the entity * that has been returned by the api with is definitive id. * * @param model * @param tempEntityId * @private */ protected removeTempAfterPersist(model: typeof ApiResource, tempEntityId: number | string) { const repository = this.getRepository(model) const entity = repository.find(tempEntityId) if (!entity || typeof entity === 'undefined') { // TODO: il vaudrait peut-être mieux lever une erreur ici? console.error(model.entity + '/' + tempEntityId + ' does not exist!') return } if (!entity.isNew()) { throw new Error('Error: Can not remove a non-temporary entity') } repository.destroy(tempEntityId) repository.destroy(this.CLONE_PREFIX + tempEntityId) } } export default EntityManager