refacto document middleware (#96)

This commit is contained in:
Arnaud D. Natali 2023-09-28 16:21:43 +02:00 committed by GitHub
commit f1baa2f1c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 65 deletions

View File

@ -24,13 +24,14 @@ export default class DocumentsController extends ApiController {
protected async get(req: Request, response: Response) { protected async get(req: Request, response: Response) {
try { try {
//get query //get query
let query: Prisma.CustomersFindManyArgs = {}; let query: Prisma.DocumentsFindManyArgs = {};
if (req.query["q"]) { if (req.query["q"]) {
query = JSON.parse(req.query["q"] as string); query = JSON.parse(req.query["q"] as string);
} }
const customerId: string = req.body.user.customerId; const customerId: string = req.body.user.customerId;
const customerWhereInput: Prisma.DocumentsWhereInput ={ depositor: { uid: customerId } }; if(query.where?.depositor) delete query.where.depositor;
if(query.where?.depositor_uid) delete query.where.depositor_uid;
const customerWhereInput: Prisma.DocumentsWhereInput = { ...query.where, depositor: { uid: customerId } };
query.where = customerWhereInput; query.where = customerWhereInput;

View File

@ -45,12 +45,12 @@ export default async function documentHandler(req: Request, response: Response,
const document = await documentService.getByUidWithOffice(uid!); const document = await documentService.getByUidWithOffice(uid!);
if (!document) { if (!document) {
response.sendStatus(HttpCodes.NOT_FOUND); response.sendStatus(HttpCodes.NOT_FOUND).send("Document not found");
return; return;
} }
if (document.document_type.office.uid != officeId) { if (document.folder.office.uid != officeId) {
response.sendStatus(HttpCodes.UNAUTHORIZED); response.sendStatus(HttpCodes.UNAUTHORIZED).send("Unauthorized with this office");
return; return;
} }
} }

View File

@ -171,15 +171,15 @@ export default async function main() {
const contacts: Contact[] = [ const contacts: Contact[] = [
{ {
address: addresses[0], address: addresses[0],
first_name: "Arnaud", first_name: "Angela",
last_name: "Daubernatali", last_name: "Dubois",
email: "arnaud.daubernatali@smart-chain.fr", email: "angela.dubois@gmail.com",
phone_number: "06 12 34 56 78", phone_number: "06 12 34 56 78",
cell_phone_number: "06 12 34 56 78", cell_phone_number: "06 12 34 56 78",
birthdate: null, birthdate: null,
created_at: new Date(), created_at: new Date(),
updated_at: new Date(), updated_at: new Date(),
civility: ECivility.MALE, civility: ECivility.FEMALE,
}, },
{ {
address: addresses[1], address: addresses[1],
@ -1039,10 +1039,10 @@ export default async function main() {
}, },
{ {
archived_at: null, archived_at: null,
name: "Other", name: "Autres documents",
office: offices[0], office: offices[0],
private_description: "Other", private_description: "Document ajouté à l'initiative du client",
public_description: "Other", public_description: "Documents annexes pour compléter le dossier",
created_at: new Date(), created_at: new Date(),
updated_at: new Date(), updated_at: new Date(),
}, },

View File

@ -33,7 +33,7 @@ interface IRattachementData {
name: string; name: string;
}; };
locationsUrl: string; locationsUrl: string;
} };
personne: { personne: {
numeroTelephonePro: string; numeroTelephonePro: string;
prenom: string; prenom: string;
@ -67,7 +67,7 @@ interface IOfficeData {
}; };
typeEntite: { typeEntite: {
name: string; name: string;
} };
} }
interface IOfficeLocation { interface IOfficeLocation {
@ -152,19 +152,19 @@ export default class IdNotService extends BaseService {
public async getOfficeRole(roleName: string, officeUid: string) { public async getOfficeRole(roleName: string, officeUid: string) {
switch (roleName) { switch (roleName) {
case EIdnotRole.NOTAIRE_TITULAIRE: case EIdnotRole.NOTAIRE_TITULAIRE:
return (await this.officeRolesService.get({ where: {AND:[{ name: "Notaire" }, {office_uid: officeUid}]}}))[0]!; return (await this.officeRolesService.get({ where: { AND: [{ name: "Notaire" }, { office_uid: officeUid }] } }))[0]!;
case EIdnotRole.NOTAIRE_ASSOCIE: case EIdnotRole.NOTAIRE_ASSOCIE:
return (await this.officeRolesService.get({ where: {AND:[{ name: "Notaire" }, {office_uid: officeUid}]}}))[0]!; return (await this.officeRolesService.get({ where: { AND: [{ name: "Notaire" }, { office_uid: officeUid }] } }))[0]!;
case EIdnotRole.NOTAIRE_SALARIE: case EIdnotRole.NOTAIRE_SALARIE:
return (await this.officeRolesService.get({ where: {AND:[{ name: "Notaire" }, {office_uid: officeUid}]}}))[0]!; return (await this.officeRolesService.get({ where: { AND: [{ name: "Notaire" }, { office_uid: officeUid }] } }))[0]!;
case EIdnotRole.COLLABORATEUR: case EIdnotRole.COLLABORATEUR:
return (await this.officeRolesService.get({ where: {AND:[{ name: "Collaborateur" }, {office_uid: officeUid}]}}))[0]!; return (await this.officeRolesService.get({ where: { AND: [{ name: "Collaborateur" }, { office_uid: officeUid }] } }))[0]!;
case EIdnotRole.SUPPLEANT: case EIdnotRole.SUPPLEANT:
return (await this.officeRolesService.get({ where: {AND:[{ name: "Collaborateur" }, {office_uid: officeUid}]}}))[0]!; return (await this.officeRolesService.get({ where: { AND: [{ name: "Collaborateur" }, { office_uid: officeUid }] } }))[0]!;
case EIdnotRole.ADMINISTRATEUR: case EIdnotRole.ADMINISTRATEUR:
return (await this.officeRolesService.get({ where: {AND:[{ name: "Collaborateur" }, {office_uid: officeUid}]}}))[0]!; return (await this.officeRolesService.get({ where: { AND: [{ name: "Collaborateur" }, { office_uid: officeUid }] } }))[0]!;
case EIdnotRole.CURATEUR: case EIdnotRole.CURATEUR:
return (await this.officeRolesService.get({ where: {AND:[{ name: "Collaborateur" }, {office_uid: officeUid}]}}))[0]!; return (await this.officeRolesService.get({ where: { AND: [{ name: "Collaborateur" }, { office_uid: officeUid }] } }))[0]!;
default: default:
return; return;
} }
@ -193,39 +193,37 @@ export default class IdNotService extends BaseService {
const searchParams = new URLSearchParams({ const searchParams = new URLSearchParams({
key: this.variables.IDNOT_API_KEY, key: this.variables.IDNOT_API_KEY,
}); });
let userData = await (await fetch( let userData = (await (
await fetch(
`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/rattachements/${user.idNot}_${user.office_membership!.idNot}?` + `${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/rattachements/${user.idNot}_${user.office_membership!.idNot}?` +
searchParams, searchParams,
{ {
method: "GET", method: "GET",
}, },
)).json() as IRattachementData; )
).json()) as IRattachementData;
if (!userData.statutDuRattachement) { if (!userData.statutDuRattachement) {
const rattachements = await (await fetch( const rattachements = (await (
`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/personnes/${user.idNot}/rattachements?` + await fetch(`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/personnes/${user.idNot}/rattachements?` + searchParams, {
searchParams,
{
method: "GET", method: "GET",
}, })
)).json() as any; ).json()) as any;
if (rattachements.totalResultCount === 0) { if (rattachements.totalResultCount === 0) {
await this.userService.updateCheckedAt(user.uid!); await this.userService.updateCheckedAt(user.uid!);
//await this.userService.delete(user.uid!); //await this.userService.delete(user.uid!);
return; return;
} }
const rattachementsResults = rattachements.result as IRattachementData[]; const rattachementsResults = rattachements.result as IRattachementData[];
if(!rattachementsResults) return; if (!rattachementsResults) return;
rattachementsResults.forEach(async (rattachement) => { rattachementsResults.forEach(async (rattachement) => {
if (rattachement.statutDuRattachement) { if (rattachement.statutDuRattachement) {
const officeData = await (await fetch( const officeData = (await (
`${this.variables.IDNOT_API_BASE_URL + rattachement.entiteUrl}?` + await fetch(`${this.variables.IDNOT_API_BASE_URL + rattachement.entiteUrl}?` + searchParams, {
searchParams,
{
method: "GET", method: "GET",
}, })
)).json() as IOfficeData; ).json()) as IOfficeData;
if(officeData.typeEntite.name === "office") { if (officeData.typeEntite.name === "office") {
userData = rattachement; userData = rattachement;
} }
} }
@ -236,7 +234,7 @@ export default class IdNotService extends BaseService {
if (user.office_membership!.idNot !== userData.entite.ou) { if (user.office_membership!.idNot !== userData.entite.ou) {
updates++; updates++;
let officeData = (await this.officeService.get({ where: { idNot:userData.entite.ou } }))[0]; let officeData = (await this.officeService.get({ where: { idNot: userData.entite.ou } }))[0];
if (!officeData) { if (!officeData) {
const officeLocationData = (await ( const officeLocationData = (await (
await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entite.locationsUrl}?` + searchParams, { method: "GET" }) await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entite.locationsUrl}?` + searchParams, { method: "GET" })
@ -279,13 +277,9 @@ export default class IdNotService extends BaseService {
const searchParams = new URLSearchParams({ const searchParams = new URLSearchParams({
key: this.variables.IDNOT_API_KEY, key: this.variables.IDNOT_API_KEY,
}); });
const officeRawData = await fetch( const officeRawData = await fetch(`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/entities/${office.idNot}?` + searchParams, {
`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/entities/${office.idNot}?` +
searchParams,
{
method: "GET", method: "GET",
}, });
);
if (officeRawData.status === 404) { if (officeRawData.status === 404) {
await this.officeService.updateCheckedAt(office.uid!); await this.officeService.updateCheckedAt(office.uid!);
//await this.officeService.delete(office.uid!); //await this.officeService.delete(office.uid!);
@ -293,15 +287,15 @@ export default class IdNotService extends BaseService {
} }
const officeData = (await officeRawData.json()) as IOfficeData; const officeData = (await officeRawData.json()) as IOfficeData;
let updates = 0; let updates = 0;
if(office.name !== officeData.denominationSociale) { if (office.name !== officeData.denominationSociale) {
updates++; updates++;
office.name = officeData.denominationSociale; office.name = officeData.denominationSociale;
} }
if(office.office_status !== this.getOfficeStatus(officeData.statutEntite.name)) { if (office.office_status !== this.getOfficeStatus(officeData.statutEntite.name)) {
updates++; updates++;
office.office_status = this.getOfficeStatus(officeData.statutEntite.name); office.office_status = this.getOfficeStatus(officeData.statutEntite.name);
} }
if(updates != 0) await this.officeService.update(office.uid!, office); if (updates != 0) await this.officeService.update(office.uid!, office);
await this.officeService.updateCheckedAt(office.uid!); await this.officeService.updateCheckedAt(office.uid!);
} }
@ -318,8 +312,7 @@ export default class IdNotService extends BaseService {
}) })
).json()) as IRattachementData; ).json()) as IRattachementData;
if (!userData.statutDuRattachement || userData.entite.typeEntite.name !== "office") {
if(!userData.statutDuRattachement || userData.entite.typeEntite.name !== "office") {
return null; return null;
} }
@ -366,9 +359,15 @@ export default class IdNotService extends BaseService {
const user = await this.userService.create(userHydrated); const user = await this.userService.create(userHydrated);
userHydrated = User.hydrate<User>(user); userHydrated = User.hydrate<User>(user);
const officeRoles = await this.officeRolesService.get({ where: { office: { idNot: "0000" }}, include: { office: true, rules: true } }); const officeRoles = await this.officeRolesService.get({
const documentTypes = await this.documentTypesService.get({ where: { office: { idNot: "0000" }}, include: { office: true } }); where: { office: { idNot: "0000" } },
const deedTypes = await this.deedTypesService.get({ where:{ office: { idNot: "0000" }}, include: { office: true, document_types: true } }); include: { office: true, rules: true },
});
const documentTypes = await this.documentTypesService.get({ where: { office: { idNot: "0000" } }, include: { office: true } });
const deedTypes = await this.deedTypesService.get({
where: { office: { idNot: "0000" } },
include: { office: true, document_types: { include: { office: true } } },
});
const officeRolesHydrated = OfficeRole.hydrateArray<OfficeRole>(officeRoles); const officeRolesHydrated = OfficeRole.hydrateArray<OfficeRole>(officeRoles);
const documentTypesHydrated = DocumentType.hydrateArray<DocumentType>(documentTypes); const documentTypesHydrated = DocumentType.hydrateArray<DocumentType>(documentTypes);
@ -384,12 +383,15 @@ export default class IdNotService extends BaseService {
}); });
deedTypesHydrated.forEach(async (deedType) => { deedTypesHydrated.forEach(async (deedType) => {
deedType.office!.uid = user.office_uid; deedType.office!.uid = user.office_uid;
deedType.document_types!.forEach((documentType) => {
documentType.office!.uid = user.office_uid;
});
await this.deedTypesService.create(deedType); await this.deedTypesService.create(deedType);
}); });
const officeRole = await this.getOfficeRole(userData.typeLien.name, user.office_uid); const officeRole = await this.getOfficeRole(userData.typeLien.name, user.office_uid);
if(officeRole) { if (officeRole) {
const officeRoleHydrated = OfficeRole.hydrate<OfficeRole>(officeRole!); const officeRoleHydrated = OfficeRole.hydrate<OfficeRole>(officeRole!);
userHydrated.office_role = officeRoleHydrated; userHydrated.office_role = officeRoleHydrated;
await this.userService.update(user.uid, userHydrated); await this.userService.update(user.uid, userHydrated);
@ -405,7 +407,6 @@ export default class IdNotService extends BaseService {
const users = User.hydrateArray<User>(usersReq); const users = User.hydrateArray<User>(usersReq);
users.forEach(async (user) => { users.forEach(async (user) => {
await this.updateUser(user.uid!); await this.updateUser(user.uid!);
}); });
} }

View File

@ -24,9 +24,9 @@ export default class DocumentsService extends BaseService {
* @throws {Error} If document cannot be created * @throws {Error} If document cannot be created
*/ */
public async create(document: Document): Promise<Documents> { public async create(document: Document): Promise<Documents> {
const otherDocumentType = await this.documentTypeService.get({ where: { name: "Other" } }); const otherDocumentType = await this.documentTypeService.get({ where: { name: "Autres documents" } });
if(otherDocumentType.length < 1) throw new Error("Other document type not found"); if(otherDocumentType.length < 1) throw new Error("Autres documents document type not found");
document.document_type = otherDocumentType[0]; document.document_type = otherDocumentType[0];
document.document_status = "DEPOSITED"; document.document_status = "DEPOSITED";