import ApiRequestService from "./apiRequestService"; import {AssociativeArray} from "./data"; import {Repository, useRepo} from "pinia-orm"; import UrlBuilder from "~/services/utils/urlBuilder"; 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 {useOrmStore} from "~/store/orm"; /** * Entity manager: make operations on the models defined with the Pinia-Orm library * * @see https://pinia-orm.codedredd.de/ */ class EntityManager { 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) EntityManager.saveInitialState(model, entity) return entity } private update(entity: ApiResource, newEntity: ApiResource) { // On met à jour l'entité par référence, pour maintenir la réactivité lorsque l'entité est réactive // @see http://underscorejs.org/#extend useExtend(entity, newEntity) } /** * Fetch one 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 = UrlBuilder.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 fetchAll(model: typeof ApiResource) { // TODO: implement } /** * 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 = UrlBuilder.join('api', model.entity) let response const data = ModelNormalizer.normalize(entity) if (!entity.isNew()) { url = UrlBuilder.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) EntityManager.saveInitialState(model, returnedEntity) // Save data into the store repository.save(returnedEntity) if (['accesses', 'organizations', 'parameters', 'subdomains'].includes(model.entity)) { await this.refreshProfile() } this.update(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 = UrlBuilder.join('api', model.entity, String(entity.id)) await this.apiRequestService.delete(url) } // update 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 = EntityManager.getInitialStateOf(model, entity.id) if (initialEntity === null) { throw new Error('no initial state recorded for this object - abort [' + model.entity + '/' + entity.id + ']') } EntityManager.saveInitialState(model, initialEntity) const repository = this.getRepository(model) repository.save(initialEntity) this.update(entity, initialEntity) } /** * Re-fetch the user profile and update 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 static saveInitialState(model: typeof ApiResource, entity: ApiResource) { useOrmStore().storeInitialValue(model, entity) } /** * Return the saved state of the entity from the store * * @param model * @param id * @private */ private static getInitialStateOf(model: typeof ApiResource, id: number): ApiResource | null { const ormStore = useOrmStore() //@ts-ignore if (!ormStore.initialValues.has(model.entity) || !ormStore.initialValues.get(model.entity).has(id)) { return null } //@ts-ignore return ormStore.initialValues.get(model.entity).get(id) } } export default EntityManager