| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- 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<string, Map<number, ApiResource>>;
- private apiRequestService: ApiRequestService;
- public constructor(apiRequestService: ApiRequestService) {
- this.apiRequestService = apiRequestService
- this.initialState = new Map<string, Map<number, ApiResource>>()
- }
- /**
- * Return the repository for the model
- *
- * @param model
- */
- public getRepository(model: typeof ApiResource): Repository<ApiResource> {
- 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<ApiResource> {
- 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
|