151 lines
3.9 KiB
TypeScript
151 lines
3.9 KiB
TypeScript
import { Service } from "typedi";
|
|
import User, { Customer, DeedType, Office, OfficeFolder } from "le-coffre-resources/dist/Notary";
|
|
import BaseSuperAdmin from "../BaseSuperAdmin";
|
|
import { EFolderStatus } from "le-coffre-resources/dist/Customer/OfficeFolder";
|
|
|
|
// TODO Type get query params -> Where + inclue + orderby
|
|
export interface IGetFoldersParams {
|
|
q?: {
|
|
select?: {};
|
|
where?: {};
|
|
include?: {};
|
|
};
|
|
}
|
|
|
|
export interface IPostFoldersParams {
|
|
folder_number: OfficeFolder["folder_number"];
|
|
name: OfficeFolder["name"];
|
|
description: OfficeFolder["description"];
|
|
deed: {
|
|
deed_type: {
|
|
uid: DeedType["uid"];
|
|
};
|
|
};
|
|
office: {
|
|
uid: Office["uid"];
|
|
};
|
|
office_folder_has_stakeholder: {
|
|
user_stakeholder: {
|
|
uid: User["uid"];
|
|
};
|
|
}[];
|
|
}
|
|
|
|
export type IPutFoldersParams = {
|
|
uid?: OfficeFolder["uid"];
|
|
folder_number?: OfficeFolder["folder_number"];
|
|
name?: OfficeFolder["name"];
|
|
description?: OfficeFolder["description"];
|
|
archived_description?: OfficeFolder["archived_description"];
|
|
status?: OfficeFolder["status"];
|
|
office_folder_has_stakeholder?: OfficeFolder["office_folder_has_stakeholder"];
|
|
OfficeFolderHasCustomer?: { customer: { uid: Customer["uid"] } }[];
|
|
};
|
|
|
|
@Service()
|
|
export default class Folders extends BaseSuperAdmin {
|
|
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<OfficeFolder[]> {
|
|
const url = new URL(this.baseURl);
|
|
Object.entries(q).forEach(([key, value]) => url.searchParams.set(key, JSON.stringify(value)));
|
|
try {
|
|
return await this.getRequest<OfficeFolder[]>(url);
|
|
} catch (err) {
|
|
this.onError(err);
|
|
return Promise.reject(err);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description : Get a folder by uid
|
|
*/
|
|
public async getByUid(uid: string, q?: any): Promise<OfficeFolder> {
|
|
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<OfficeFolder>(url);
|
|
} catch (err) {
|
|
this.onError(err);
|
|
return Promise.reject(err);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description : Create a folder
|
|
*/
|
|
public async post(body: any): Promise<OfficeFolder> {
|
|
const url = new URL(this.baseURl);
|
|
try {
|
|
return await this.postRequest<OfficeFolder>(url, body);
|
|
} catch (err) {
|
|
this.onError(err);
|
|
return Promise.reject(err);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description : Update the folder description
|
|
*/
|
|
public async put(uid: string, body: IPutFoldersParams): Promise<OfficeFolder> {
|
|
const url = new URL(this.baseURl.concat(`/${uid}`));
|
|
try {
|
|
return await this.putRequest<OfficeFolder>(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<OfficeFolder> {
|
|
const url = new URL(this.baseURl.concat(`/${uid}`));
|
|
const targetedFolder = await this.getByUid(uid);
|
|
if (targetedFolder.office_folder_has_customers) return Promise.reject(`The folder ${uid} contains customers`);
|
|
try {
|
|
return await this.deleteRequest<OfficeFolder>(url);
|
|
} catch (err) {
|
|
this.onError(err);
|
|
return Promise.reject(err);
|
|
}
|
|
}
|
|
|
|
public async archive(uid: string, body: IPutFoldersParams): Promise<OfficeFolder> {
|
|
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: IPutFoldersParams): Promise<OfficeFolder> {
|
|
body.status = EFolderStatus.LIVE;
|
|
try {
|
|
return await this.put(uid, body);
|
|
} catch (err) {
|
|
this.onError(err);
|
|
return Promise.reject(err);
|
|
}
|
|
}
|
|
}
|