🚧 wip api integration

This commit is contained in:
Hugo Lextrait 2023-05-02 10:52:32 +02:00
parent 227e5ee4d9
commit 003dced387
4 changed files with 266 additions and 30 deletions

View File

@ -0,0 +1,130 @@
import { Service } from "typedi";
import { 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?: {
where?: {};
include?: {};
};
}
// TODO Type getbyuid query params
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"];
};
@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: IPostFoldersParams): 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);
}
}
}

View File

@ -1,11 +1,27 @@
import { Service } from "typedi";
import User from "le-coffre-resources/dist/Notary";
import User from "le-coffre-resources/dist/SuperAdmin";
import BaseSuperAdmin from "../BaseSuperAdmin";
// TODO Type get query params -> Where + inclue + orderby
export interface IGetUsersparams {
q?: {};
}
// TODO Type getbyuid query params
export type IPutUsersParams = {
uid?: User["uid"];
idNot?: User["idNot"];
contact?: User["contact"];
office_membership?: User["office_membership"];
office_folder_has_stakeholders?: User["office_folder_has_stakeholders"];
documents?: User["documents"];
};
@Service()
export default class Users extends BaseSuperAdmin {
private static instance: Users;
private readonly baseURl = this.namespaceUrl.concat("/Users");
private readonly baseURl = this.namespaceUrl.concat("/users");
private constructor() {
super();
@ -19,8 +35,12 @@ export default class Users extends BaseSuperAdmin {
}
}
public async get(): Promise<User[]> {
/**
* @description : Get all Users
*/
public async get(q: IGetUsersparams): Promise<User[]> {
const url = new URL(this.baseURl);
Object.entries(q).forEach(([key, value]) => url.searchParams.set(key, JSON.stringify(value)));
try {
return await this.getRequest<User[]>(url);
} catch (err) {
@ -29,8 +49,12 @@ export default class Users extends BaseSuperAdmin {
}
}
public async getByUid(uid: string): Promise<User> {
const url = new URL(this.baseURl.concat("/").concat(uid));
/**
* @description : Get a folder by uid
*/
public async getByUid(uid: string, q?: any): Promise<User> {
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<User>(url);
} catch (err) {
@ -38,4 +62,30 @@ export default class Users extends BaseSuperAdmin {
return Promise.reject(err);
}
}
/**
* @description : Create a User
*/
// public async post(body: IPostDeedsParams): 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: IPutUsersParams): Promise<User> {
const url = new URL(this.baseURl.concat(`/${uid}`));
try {
return await this.putRequest<User>(url, body);
} catch (err) {
this.onError(err);
return Promise.reject(err);
}
}
}

View File

@ -1,3 +1,4 @@
import "reflect-metadata";
import Header from "@Front/Components/DesignSystem/Header";
import Version from "@Front/Components/DesignSystem/Version";
import classNames from "classnames";

View File

@ -1,6 +1,6 @@
import RightImage from "@Front/Assets/images/create-folder/right-image.png";
import Button from "@Front/Components/DesignSystem/Button";
import Form, { IApiFormErrors } from "@Front/Components/DesignSystem/Form";
import Form from "@Front/Components/DesignSystem/Form";
import InputField from "@Front/Components/DesignSystem/Form/Elements/InputField";
import MultiSelect from "@Front/Components/DesignSystem/MultiSelect";
import RadioBox from "@Front/Components/DesignSystem/RadioBox";
@ -13,11 +13,15 @@ import { ActionMeta, MultiValue } from "react-select";
import BasePage from "../../Base";
import classes from "./classes.module.scss";
import { Deed, DeedType, OfficeFolder, OfficeFolderHasCustomer, OfficeFolderHasStakeholder } from "le-coffre-resources/dist/Notary";
import DeedTypes from "@Front/Api/LeCoffreApi/SuperAdmin/DeedTypes/DeedTypes";
import Users from "@Front/Api/LeCoffreApi/SuperAdmin/Users/Users";
import User from "le-coffre-resources/dist/Notary";
type IFormValues = {
folder_number: number;
entitled: string;
act_type: IOption | null;
act_typ: IOption | null;
personal_note: string;
collaborators: MultiValue<IOption>;
};
@ -26,23 +30,12 @@ type IProps = {};
type IState = {
folder_access: string;
formValues: IFormValues;
deedTypes: DeedType[];
deedTypesOptions: IOption[];
collaborators: User[];
collaboratorsOptions: IOption[];
};
export default class CreateFolder extends BasePage<IProps, IState> {
private collaboratorsOptions: IOption[] = [
{ label: "John Doe", value: "john_doe" },
{ label: "Éric Dupont", value: "eric_dupont" },
{ label: "Julien Doe", value: "julien_doe" },
{ label: "Nathalie Costa", value: "nathalie_costa" },
{ label: "Max Durant", value: "max_durant" },
{ label: "Jane Doe", value: "jane_doe" },
];
private actsOptions: IOption[] = [
{ label: "Divorce", value: "divorce" },
{ label: "Succession", value: "succession" },
{ label: "Vente immobilière", value: "vente_immobiliere" },
];
public constructor(props: IProps) {
super(props);
@ -51,10 +44,14 @@ export default class CreateFolder extends BasePage<IProps, IState> {
formValues: {
folder_number: NaN,
entitled: "",
act_type: null,
act_typ: null,
personal_note: "",
collaborators: [],
},
deedTypes: [],
deedTypesOptions: [],
collaborators: [],
collaboratorsOptions: [],
};
this.radioOnChange = this.radioOnChange.bind(this);
@ -64,6 +61,7 @@ export default class CreateFolder extends BasePage<IProps, IState> {
this.onPersonalNoteChange = this.onPersonalNoteChange.bind(this);
this.onCollaboratorsChange = this.onCollaboratorsChange.bind(this);
this.isFormSubmittable = this.isFormSubmittable.bind(this);
this.onFormSubmit = this.onFormSubmit.bind(this);
}
public override render(): JSX.Element {
@ -85,10 +83,15 @@ export default class CreateFolder extends BasePage<IProps, IState> {
type="number"
onChange={this.onFolderNumberChange}
/>
<<<<<<< Updated upstream
<InputField name="entitled" fakeplaceholder="Intitulé" onChange={this.onEntitleChange} />
<Select options={this.actsOptions} placeholder={"Type dacte"} onChange={this.onActTypeChange} />
=======
<InputField name="name" fakeplaceholder="Intitulé" onChange={this.onEntitleChange} />
<Select options={this.state.deedTypesOptions} placeholder={"Type dacte"} onChange={this.onActTypeChange} />
>>>>>>> Stashed changes
<InputField
name="personal_note"
name="description"
fakeplaceholder="Note du dossier"
textarea
onChange={this.onPersonalNoteChange}
@ -109,7 +112,7 @@ export default class CreateFolder extends BasePage<IProps, IState> {
{this.state.folder_access === "select_collaborators" && (
<div className={classes["collaborators-container"]}>
<MultiSelect
options={this.collaboratorsOptions}
options={this.state.collaboratorsOptions}
placeholder="Sélectionner les collaborateurs"
onChange={this.onCollaboratorsChange}
defaultValue={this.state.formValues.collaborators ?? []}
@ -128,6 +131,34 @@ export default class CreateFolder extends BasePage<IProps, IState> {
);
}
public override async componentDidMount() {
const deedTypes = await DeedTypes.getInstance().get({ q: {} });
// TODO SETUP userStore and get the user's office membership -> Replace IwJ70M471c by the user's office membership uid
const collaborators = await Users.getInstance().get({
q: { where: { office_membership: { uid: "IwJ70M471c" } }, include: { contact: true } },
});
this.setState({
deedTypes,
deedTypesOptions: this.mapDeedOptions(deedTypes),
collaborators,
collaboratorsOptions: this.mapUsersOptions(collaborators),
});
}
private mapDeedOptions(deedTypes: DeedType[]) {
return deedTypes.map((deedType) => ({
label: deedType.name,
value: deedType.uid,
})) as IOption[];
}
private mapUsersOptions(collaborators: User[]) {
return collaborators.map((collaborator) => ({
label: collaborator.contact.last_name.concat(" ", collaborator.contact.first_name),
value: collaborator.uid,
})) as IOption[];
}
private onFolderNumberChange(e: React.ChangeEvent<HTMLInputElement>) {
this.setState({
formValues: {
@ -150,7 +181,7 @@ export default class CreateFolder extends BasePage<IProps, IState> {
this.setState({
formValues: {
...this.state.formValues,
act_type: selectedOption,
act_typ: selectedOption,
},
});
}
@ -176,17 +207,41 @@ export default class CreateFolder extends BasePage<IProps, IState> {
private onFormSubmit(
e: React.FormEvent<HTMLFormElement> | null,
values: {
[key: string]: string;
[key: string]: any;
},
onApiErrors: (apiFormErrors: IApiFormErrors | null) => void,
) {}
) {
const selectedDeedType: DeedType | undefined = this.state.deedTypes.find(
(deedType) => deedType.uid === this.state.formValues.act_typ?.value,
);
// const selectedCollaborator: User | undefined = this.state.collaborators?.find(
// (deedType) => deedType.uid === this.state.formValues.act_typ?.value,
// );
let collaborators: User[] = this.state.collaborators;
if (!selectedDeedType) return;
const deed = Deed.hydrate<Deed>({
deed_type: selectedDeedType,
});
const office_folder_has_customers = collaborators.map((collaborator) => {
return OfficeFolderHasStakeholder.hydrate<OfficeFolderHasStakeholder>({
office_folder: new OfficeFolder(),
user_stakeholder: collaborator,
});
});
values["deed"] = deed;
values["office_folder_has_customers"] = office_folder_has_customers;
console.log(values);
}
private isFormSubmittable(): boolean {
if (
this.state.formValues.entitled === "" ||
this.state.formValues.personal_note === "" ||
Number.isNaN(this.state.formValues.folder_number) ||
this.state.formValues.act_type === null
this.state.formValues.act_typ === null
) {
return false;
}