dataDeleter.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { Context } from '@nuxt/types/app'
  2. import { hooks } from '~/services/dataDeleter/hook/_import'
  3. import { DataDeleterArgs } from '~/types/interfaces'
  4. import Connection from '~/services/connection/connection'
  5. import ApiError from '~/services/utils/apiError'
  6. import { repositoryHelper } from '~/services/store/repository'
  7. /**
  8. * Le DataProvider a pour rôle de supprimer des enregistrements via l'API Opentalent
  9. */
  10. class DataDeleter {
  11. private ctx !: Context
  12. private arguments!: DataDeleterArgs
  13. /**
  14. * Initialise le contexte (la connection en particulier)
  15. * @param ctx
  16. */
  17. public initCtx (ctx:Context) {
  18. Connection.initConnector(ctx.$axios)
  19. this.ctx = ctx
  20. }
  21. /**
  22. * Exécute la requête
  23. * @param args
  24. */
  25. public async invoke (args:DataDeleterArgs): Promise<any> {
  26. this.arguments = args
  27. try {
  28. await this.preHook()
  29. // const url = ConstructUrl.invoke(this.arguments)
  30. // const response = await Connection.invoke(HTTP_METHOD.DELETE, url, this.arguments)
  31. if (this.arguments.model) {
  32. repositoryHelper.deleteItem(this.arguments.model, this.arguments.id)
  33. }
  34. } catch (error) {
  35. throw new ApiError(error.response.status, error.response.data.detail)
  36. }
  37. }
  38. /**
  39. * Iterate over the available hooks and return
  40. * the first one that support the given args
  41. */
  42. async preHook () {
  43. for (const Hook of DataDeleter.sortedHooks()) {
  44. if (Hook.support(this.arguments)) {
  45. await new Hook().invoke(this.arguments)
  46. }
  47. }
  48. }
  49. /**
  50. * Sort the available hooks by priority
  51. * @private
  52. */
  53. private static sortedHooks (): Iterable<any> {
  54. return hooks.sort(function (a, b) {
  55. if (a.priority > b.priority) { return 1 }
  56. if (a.priority < b.priority) { return -1 }
  57. return 0
  58. })
  59. }
  60. }
  61. export default DataDeleter