|
|
@@ -1,47 +1,54 @@
|
|
|
import ApiRequestService from "./apiRequestService";
|
|
|
import {AssociativeArray} from "./data";
|
|
|
-import {useRepo} from "pinia-orm";
|
|
|
+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 {
|
|
|
+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>>()
|
|
|
}
|
|
|
|
|
|
- public getRepository(model: typeof ApiModel) {
|
|
|
+ /**
|
|
|
+ * Return the repository for the model
|
|
|
+ *
|
|
|
+ * @param model
|
|
|
+ */
|
|
|
+ public getRepository(model: typeof ApiResource): Repository<ApiResource> {
|
|
|
return useRepo(model)
|
|
|
}
|
|
|
|
|
|
- private static getEntityModel(entity: ApiModel): typeof ApiModel{
|
|
|
- return Object.getPrototypeOf(entity)
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
- * Fetch one entity by its id, save it to the store and returns it
|
|
|
+ * 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 ApiModel, id: number, forceRefresh: boolean = false) {
|
|
|
+ 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
|
|
|
}
|
|
|
@@ -53,51 +60,62 @@ class EntityManager {
|
|
|
const response = await this.apiRequestService.get(url)
|
|
|
|
|
|
// deserialize the response
|
|
|
- const entity = await HydraDenormalizer.denormalize(response)
|
|
|
- entity.persisted = true
|
|
|
-
|
|
|
- entity.initialState = structuredClone(entity)
|
|
|
-
|
|
|
- // Save data into the store
|
|
|
- repository.save(entity)
|
|
|
+ const attributes = HydraDenormalizer.denormalize(response).data as object
|
|
|
|
|
|
- return entity
|
|
|
+ return this.new(model, attributes)
|
|
|
}
|
|
|
|
|
|
- public findBy(model: typeof ApiModel, query: AssociativeArray) {
|
|
|
+ public findBy(model: typeof ApiResource, query: AssociativeArray) {
|
|
|
// TODO: implement
|
|
|
}
|
|
|
|
|
|
- public fetchAll(model: typeof ApiModel) {
|
|
|
+ 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 data = ModelNormalizer.normalize(entity)
|
|
|
+
|
|
|
+ const repository = this.getRepository(model)
|
|
|
+
|
|
|
let url = UrlBuilder.join('api', model.entity)
|
|
|
- let response = null
|
|
|
+ let response
|
|
|
+
|
|
|
+ const data = ModelNormalizer.normalize(entity)
|
|
|
|
|
|
- if (entity.persisted) {
|
|
|
+ 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 fetchedEntity = await HydraDenormalizer.denormalize(response)
|
|
|
- fetchedEntity.persisted = true
|
|
|
+ const attributes = await HydraDenormalizer.denormalize(response)
|
|
|
+ const returnedEntity = this.new(model, attributes)
|
|
|
+
|
|
|
+ this.storeInitialState(returnedEntity)
|
|
|
|
|
|
// Save data into the store
|
|
|
- const repository = this.getRepository(model)
|
|
|
- repository.save(fetchedEntity)
|
|
|
+ repository.save(returnedEntity)
|
|
|
|
|
|
if (['accesses', 'organizations', 'parameters', 'subdomains'].includes(model.entity)) {
|
|
|
await this.refreshProfile()
|
|
|
}
|
|
|
|
|
|
- return fetchedEntity
|
|
|
+ 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
|
|
|
@@ -107,7 +125,7 @@ class EntityManager {
|
|
|
}
|
|
|
|
|
|
// If object has been persisted to the datasource, send a delete request
|
|
|
- if (entity.persisted) {
|
|
|
+ if (!entity.isNew()) {
|
|
|
const url = UrlBuilder.join('api', model.entity, String(id))
|
|
|
await this.apiRequestService.delete(url)
|
|
|
}
|
|
|
@@ -116,39 +134,104 @@ class EntityManager {
|
|
|
repository.destroy(id)
|
|
|
}
|
|
|
|
|
|
- public new(model: typeof ApiModel) {
|
|
|
+ /**
|
|
|
+ * 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()
|
|
|
- entity.persisted = false
|
|
|
+ const entity = repository.make(properties)
|
|
|
|
|
|
- // Save data into the store
|
|
|
+ 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
|
|
|
}
|
|
|
|
|
|
- public reset(entity: ApiModel) {
|
|
|
- if (entity.initialState === null) {
|
|
|
- console.log('object has no initial state - abort')
|
|
|
+ /**
|
|
|
+ * 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
|
|
|
}
|
|
|
|
|
|
- entity = entity.initialState as ApiModel
|
|
|
- entity.initialState = structuredClone(entity)
|
|
|
-
|
|
|
- const repository = this.getRepository(EntityManager.getEntityModel(entity))
|
|
|
- repository.save(entity)
|
|
|
+ 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 profile = await HydraDenormalizer.denormalize(response)
|
|
|
+ const attributes = await HydraDenormalizer.denormalize(response).data
|
|
|
+
|
|
|
+ const profile = this.new(MyProfile, attributes)
|
|
|
|
|
|
const profileAccessStore = useProfileAccessStore()
|
|
|
- profileAccessStore.setProfile(profile.data)
|
|
|
+ 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)
|
|
|
}
|
|
|
}
|
|
|
|