import { Context } from '@nuxt/types/app' import { hooks } from '~/services/dataDeleter/hook/_import' import { DataDeleterArgs } from '~/types/interfaces' import Connection from '~/services/connection/connection' import ApiError from '~/services/utils/apiError' import { repositoryHelper } from '~/services/store/repository' /** * Le DataProvider a pour rôle de supprimer des enregistrements via l'API Opentalent */ class DataDeleter { private ctx !: Context private arguments!: DataDeleterArgs /** * Initialise le contexte (la connection en particulier) * @param ctx */ public initCtx (ctx:Context) { Connection.initConnector(ctx.$axios) this.ctx = ctx } /** * Exécute la requête * @param args */ public async invoke (args:DataDeleterArgs): Promise { this.arguments = args try { await this.preHook() // const url = ConstructUrl.invoke(this.arguments) // const response = await Connection.invoke(HTTP_METHOD.DELETE, url, this.arguments) if (this.arguments.model) { repositoryHelper.deleteItem(this.arguments.model, this.arguments.id) } } catch (error) { throw new ApiError(error.response.status, error.response.data.detail) } } /** * Iterate over the available hooks and return * the first one that support the given args */ async preHook () { for (const Hook of DataDeleter.sortedHooks()) { if (Hook.support(this.arguments)) { await new Hook().invoke(this.arguments) } } } /** * Sort the available hooks by priority * @private */ private static sortedHooks (): Iterable { return hooks.sort(function (a, b) { if (a.priority > b.priority) { return 1 } if (a.priority < b.priority) { return -1 } return 0 }) } } export default DataDeleter