106 lines
2.7 KiB
TypeScript
106 lines
2.7 KiB
TypeScript
import { Contact, Customer } from "le-coffre-resources/dist/Notary";
|
|
|
|
import BaseNotary from "../BaseNotary";
|
|
import { ECivility } from "le-coffre-resources/dist/Customer/Contact";
|
|
|
|
// TODO Type get query params -> Where + inclue + orderby
|
|
export interface IGetCustomersparams {
|
|
where?: {};
|
|
include?: {};
|
|
}
|
|
|
|
// TODO Type getbyuid query params
|
|
|
|
export type IPutCustomersParams = {
|
|
uid?: Customer["uid"];
|
|
contact?: Customer["contact"];
|
|
};
|
|
|
|
export interface IPostCustomersParams {
|
|
first_name: string;
|
|
last_name: string;
|
|
email: string;
|
|
cell_phone_number: string;
|
|
civility: ECivility;
|
|
address?: Contact["address"];
|
|
}
|
|
|
|
export default class Customers extends BaseNotary {
|
|
private static instance: Customers;
|
|
private readonly baseURl = this.namespaceUrl.concat("/customers");
|
|
|
|
private constructor() {
|
|
super();
|
|
}
|
|
|
|
public static getInstance() {
|
|
if (!this.instance) {
|
|
return new this();
|
|
} else {
|
|
return this.instance;
|
|
}
|
|
}
|
|
|
|
public async get(q: IGetCustomersparams): Promise<Customer[]> {
|
|
const url = new URL(this.baseURl);
|
|
const query = { q };
|
|
Object.entries(query).forEach(([key, value]) => url.searchParams.set(key, JSON.stringify(value)));
|
|
try {
|
|
return await this.getRequest<Customer[]>(url);
|
|
} catch (err) {
|
|
this.onError(err);
|
|
return Promise.reject(err);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description : Create a Customer
|
|
*/
|
|
public async post(body: any): Promise<Customer> {
|
|
const url = new URL(this.baseURl);
|
|
try {
|
|
return await this.postRequest<Customer>(url, body);
|
|
} catch (err) {
|
|
this.onError(err);
|
|
return Promise.reject(err);
|
|
}
|
|
}
|
|
|
|
public async getByUid(uid: string, q?: any): Promise<Customer> {
|
|
const url = new URL(this.baseURl.concat(`/${uid}`));
|
|
const query = { q };
|
|
if (q) Object.entries(query).forEach(([key, value]) => url.searchParams.set(key, JSON.stringify(value)));
|
|
try {
|
|
return await this.getRequest<Customer>(url);
|
|
} catch (err) {
|
|
this.onError(err);
|
|
return Promise.reject(err);
|
|
}
|
|
}
|
|
|
|
public async put(uid: string, body: IPutCustomersParams): Promise<Customer> {
|
|
const url = new URL(this.baseURl.concat(`/${uid}`));
|
|
try {
|
|
return await this.putRequest<Customer>(url, body);
|
|
} catch (err) {
|
|
this.onError(err);
|
|
return Promise.reject(err);
|
|
}
|
|
}
|
|
|
|
public async sendReminder(uid: string, documentsUid: string[]): Promise<void> {
|
|
// TODO: review
|
|
const baseBackUrl = 'http://localhost:8080';//variables.BACK_API_PROTOCOL + variables.BACK_API_HOST;
|
|
|
|
const url = new URL(`${baseBackUrl}/api/${uid}/send_reminder`);
|
|
|
|
//const url = new URL(this.baseURl.concat(`/${uid}/send_reminder`));
|
|
try {
|
|
await this.postRequest<void>(url, { email: 'ja.janin.anthony@gmail.com', documentsUid });
|
|
} catch (err) {
|
|
this.onError(err);
|
|
return Promise.reject(err);
|
|
}
|
|
}
|
|
}
|