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'; /** * Entity manager: make operations on the models defined with the Pinia-Orm library * * @see https://pinia-orm.codedredd.de/ */ class EntityManager { private initialState: Map>; private apiRequestService: ApiRequestService; public constructor(apiRequestService: ApiRequestService) { this.apiRequestService = apiRequestService this.initialState = new Map>() } /** * Return the repository for the model * * @param model */ public getRepository(model: typeof ApiResource): Repository { return useRepo(model) } /** * 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) console.log(model, id, ' => item : ', item) 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 { response = await this.apiRequestService.post(url, data) } const attributes = await HydraDenormalizer.denormalize(response) const returnedEntity = this.new(model, attributes) this.storeInitialState(returnedEntity) // Save data into the store repository.save(returnedEntity) if (['accesses', 'organizations', 'parameters', 'subdomains'].includes(model.entity)) { await this.refreshProfile() } return returnedEntity } /** * Delete the entity from the datasource via the API * * @param model * @param id */ public async delete(model: typeof ApiModel, id: number) { const repository = this.getRepository(model) const entity = repository.find(id) as ApiModel if (!entity || typeof entity === 'undefined') { throw new Error(model + ' ' + id + ' does not exists in store') } // If object has been persisted to the datasource, send a delete request if (!entity.isNew()) { const url = UrlBuilder.join('api', model.entity, String(id)) await this.apiRequestService.delete(url) } // update the store repository.destroy(id) } /** * 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) if (!entity.id) { // Object has no id yet, we give him a temporary one entity.id = 'tmp' + uuid4() } repository.save(entity) this.storeInitialState(entity) return entity } /** * Reset the entity to its initial state (i.e. the state it had when it was fetched from the API) * * @param entity */ public reset(entity: ApiResource) { const state = this.getInitialStateOf(entity) if (state === null) { console.log('no initial state recorded for this object - abort', entity) return } const repository = this.getRepository(EntityManager.getModelOf(entity)) repository.save(state) } /** * 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 attributes = await HydraDenormalizer.denormalize(response).data const profile = this.new(MyProfile, attributes) 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() } private storeInitialState(entity: ApiResource) { const model = EntityManager.getModelOf(entity) if (!this.initialState.has(model.entity)) { this.initialState.set(model.entity, new Map()) } // @ts-ignore this.initialState.get(model.entity).set(entity.id, useCloneDeep(entity)) // TODO: voir si structuredClone est compatible avec les navigateurs supportés, et si ça vaut le coup // de l'utiliser à la place du clonedeep de lodash // >> https://developer.mozilla.org/en-US/docs/Web/API/structuredClone } private getInitialStateOf(entity: ApiResource): ApiResource | null { const model = EntityManager.getModelOf(entity) // @ts-ignore if (!this.initialState.has(model.entity) || !this.initialState.get(model.entity).has(entity.id)) { return null } // @ts-ignore return this.initialState.get(model.entity).get(entity.id) } /** * Return the model (=class) of the given entity * * @param entity * @private */ private static getModelOf(entity: ApiResource): typeof ApiResource { return Object.getPrototypeOf(entity) } } export default EntityManager