entityManager.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import ApiRequestService from "./apiRequestService";
  2. import {AssociativeArray} from "./data";
  3. import {Repository, useRepo} from "pinia-orm";
  4. import UrlBuilder from "~/services/utils/urlBuilder";
  5. import ModelNormalizer from "./serializer/normalizer/modelNormalizer";
  6. import HydraDenormalizer from "./serializer/denormalizer/hydraDenormalizer";
  7. import ApiModel from "~/models/ApiModel";
  8. import {useProfileAccessStore} from "~/store/profile/access";
  9. import ApiResource from "~/models/ApiResource";
  10. import {MyProfile} from "~/models/Access/MyProfile";
  11. import { v4 as uuid4 } from 'uuid';
  12. /**
  13. * Entity manager: make operations on the models defined with the Pinia-Orm library
  14. *
  15. * @see https://pinia-orm.codedredd.de/
  16. */
  17. class EntityManager {
  18. private initialState: Map<string, Map<number, ApiResource>>;
  19. private apiRequestService: ApiRequestService;
  20. public constructor(apiRequestService: ApiRequestService) {
  21. this.apiRequestService = apiRequestService
  22. this.initialState = new Map<string, Map<number, ApiResource>>()
  23. }
  24. /**
  25. * Return the repository for the model
  26. *
  27. * @param model
  28. */
  29. public getRepository(model: typeof ApiResource): Repository<ApiResource> {
  30. return useRepo(model)
  31. }
  32. /**
  33. * Fetch one Entity / ApiResource by its id, save it to the store and returns it
  34. *
  35. * @param model Model of the object to fetch
  36. * @param id Id of the object to fetch
  37. * @param forceRefresh Force a new get request to the api ;
  38. * current object in store will be overwritten if it exists
  39. */
  40. public async fetch(model: typeof ApiResource, id: number, forceRefresh: boolean = false): Promise<ApiResource> {
  41. const repository = this.getRepository(model)
  42. // If the entity is already in the store and forceRefresh is false, return the object in store
  43. if (!forceRefresh) {
  44. const item = repository.find(id)
  45. console.log(model, id, ' => item : ', item)
  46. if (item && typeof item !== 'undefined') {
  47. return item
  48. }
  49. }
  50. // Else, get the object from the API
  51. const url = UrlBuilder.join('api', model.entity, String(id))
  52. const response = await this.apiRequestService.get(url)
  53. // deserialize the response
  54. const attributes = HydraDenormalizer.denormalize(response).data as object
  55. return this.new(model, attributes)
  56. }
  57. public findBy(model: typeof ApiResource, query: AssociativeArray) {
  58. // TODO: implement
  59. }
  60. public fetchAll(model: typeof ApiResource) {
  61. // TODO: implement
  62. }
  63. /**
  64. * Persist the entity as it is in the store into the data source via the API
  65. *
  66. * @param model
  67. * @param entity
  68. */
  69. public async persist(model: typeof ApiModel, entity: ApiModel) {
  70. const repository = this.getRepository(model)
  71. let url = UrlBuilder.join('api', model.entity)
  72. let response
  73. const data = ModelNormalizer.normalize(entity)
  74. if (!entity.isNew()) {
  75. url = UrlBuilder.join(url, String(entity.id))
  76. response = await this.apiRequestService.put(url, data)
  77. } else {
  78. response = await this.apiRequestService.post(url, data)
  79. }
  80. const attributes = await HydraDenormalizer.denormalize(response)
  81. const returnedEntity = this.new(model, attributes)
  82. this.storeInitialState(returnedEntity)
  83. // Save data into the store
  84. repository.save(returnedEntity)
  85. if (['accesses', 'organizations', 'parameters', 'subdomains'].includes(model.entity)) {
  86. await this.refreshProfile()
  87. }
  88. return returnedEntity
  89. }
  90. /**
  91. * Delete the entity from the datasource via the API
  92. *
  93. * @param model
  94. * @param id
  95. */
  96. public async delete(model: typeof ApiModel, id: number) {
  97. const repository = this.getRepository(model)
  98. const entity = repository.find(id) as ApiModel
  99. if (!entity || typeof entity === 'undefined') {
  100. throw new Error(model + ' ' + id + ' does not exists in store')
  101. }
  102. // If object has been persisted to the datasource, send a delete request
  103. if (!entity.isNew()) {
  104. const url = UrlBuilder.join('api', model.entity, String(id))
  105. await this.apiRequestService.delete(url)
  106. }
  107. // update the store
  108. repository.destroy(id)
  109. }
  110. /**
  111. * Create a new instance of the given model
  112. *
  113. * @param model
  114. * @param properties
  115. */
  116. public new(model: typeof ApiResource, properties: object = {}) {
  117. const repository = this.getRepository(model)
  118. const entity = repository.make(properties)
  119. if (!entity.id) {
  120. // Object has no id yet, we give him a temporary one
  121. entity.id = 'tmp' + uuid4()
  122. }
  123. repository.save(entity)
  124. this.storeInitialState(entity)
  125. return entity
  126. }
  127. /**
  128. * Reset the entity to its initial state (i.e. the state it had when it was fetched from the API)
  129. *
  130. * @param entity
  131. */
  132. public reset(entity: ApiResource) {
  133. const state = this.getInitialStateOf(entity)
  134. if (state === null) {
  135. console.log('no initial state recorded for this object - abort', entity)
  136. return
  137. }
  138. const repository = this.getRepository(EntityManager.getModelOf(entity))
  139. repository.save(state)
  140. }
  141. /**
  142. * Re-fetch the user profile and update the store
  143. */
  144. public async refreshProfile() {
  145. const response = await this.apiRequestService.get('api/my_profile')
  146. // deserialize the response
  147. const attributes = await HydraDenormalizer.denormalize(response).data
  148. const profile = this.new(MyProfile, attributes)
  149. const profileAccessStore = useProfileAccessStore()
  150. profileAccessStore.setProfile(profile)
  151. }
  152. /**
  153. * Delete all records in the repository of the model
  154. *
  155. * @param model
  156. */
  157. public async flush(model: typeof ApiModel) {
  158. const repository = this.getRepository(model)
  159. repository.flush()
  160. }
  161. private storeInitialState(entity: ApiResource) {
  162. const model = EntityManager.getModelOf(entity)
  163. if (!this.initialState.has(model.entity)) {
  164. this.initialState.set(model.entity, new Map())
  165. }
  166. // @ts-ignore
  167. this.initialState.get(model.entity).set(entity.id, useCloneDeep(entity))
  168. // TODO: voir si structuredClone est compatible avec les navigateurs supportés, et si ça vaut le coup
  169. // de l'utiliser à la place du clonedeep de lodash
  170. // >> https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
  171. }
  172. private getInitialStateOf(entity: ApiResource): ApiResource | null {
  173. const model = EntityManager.getModelOf(entity)
  174. // @ts-ignore
  175. if (!this.initialState.has(model.entity) || !this.initialState.get(model.entity).has(entity.id)) {
  176. return null
  177. }
  178. // @ts-ignore
  179. return this.initialState.get(model.entity).get(entity.id)
  180. }
  181. /**
  182. * Return the model (=class) of the given entity
  183. *
  184. * @param entity
  185. * @private
  186. */
  187. private static getModelOf(entity: ApiResource): typeof ApiResource {
  188. return Object.getPrototypeOf(entity)
  189. }
  190. }
  191. export default EntityManager