import { type OfficeFolder } from "le-coffre-resources/dist/Notary"; import BaseNotary from "../BaseNotary"; import EFolderStatus from "le-coffre-resources/dist/Customer/EFolderStatus"; // TODO Type get query params -> Where + inclue + orderby export interface IGetFoldersParams { q?: { select?: {}; where?: {}; include?: {}; }; } export default class Folders extends BaseNotary { private static instance: Folders; private readonly baseURl = this.namespaceUrl.concat("/folders"); private constructor() { super(); } public static getInstance() { if (!this.instance) { return new this(); } else { return this.instance; } } /** * @description : Get all folders */ public async get(q: IGetFoldersParams): Promise { const url = new URL(this.baseURl); Object.entries(q).forEach(([key, value]) => url.searchParams.set(key, JSON.stringify(value))); try { return await this.getRequest(url); } catch (err) { this.onError(err); return Promise.reject(err); } } /** * @description : Get a folder by uid */ public async getByUid(uid: string, q?: any): Promise { const url = new URL(this.baseURl.concat(`/${uid}`)); if (q) Object.entries(q).forEach(([key, value]) => url.searchParams.set(key, JSON.stringify(value))); try { return await this.getRequest(url); } catch (err) { this.onError(err); return Promise.reject(err); } } /** * @description : Create a folder */ public async post(officeFolder: Partial): Promise { const url = new URL(this.baseURl); try { return await this.postRequest(url, officeFolder); } catch (err) { this.onError(err); return Promise.reject(err); } } /** * @description : Update the folder description */ public async put(uid: string, body: Partial): Promise { const url = new URL(this.baseURl.concat(`/${uid}`)); try { return await this.putRequest(url, body); } catch (err) { this.onError(err); return Promise.reject(err); } } /** * @description : Delete a folder only if the folder don't contains customers */ public async delete(uid: string): Promise { const url = new URL(this.baseURl.concat(`/${uid}`)); const targetedFolder = await this.getByUid(uid); if (targetedFolder.customers) return Promise.reject(`The folder ${uid} contains customers`); try { return await this.deleteRequest(url); } catch (err) { this.onError(err); return Promise.reject(err); } } public async archive(uid: string, body: Partial): Promise { body.status = EFolderStatus.ARCHIVED; try { return await this.put(uid, body); } catch (err) { this.onError(err); return Promise.reject(err); } } public async restore(uid: string, body: Partial): Promise { body.status = EFolderStatus.LIVE; try { return await this.put(uid, body); } catch (err) { this.onError(err); return Promise.reject(err); } } }