refacto document middleware

This commit is contained in:
OxSaitama 2023-09-28 16:20:43 +02:00
parent 3c32eaf15d
commit e2707cb21b
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 {
@ -193,22 +193,22 @@ 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!);
@ -218,13 +218,11 @@ export default class IdNotService extends BaseService {
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;
} }
@ -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!);
@ -318,7 +312,6 @@ 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({
where: { office: { idNot: "0000" } },
include: { office: true, rules: true },
});
const documentTypes = await this.documentTypesService.get({ where: { office: { idNot: "0000" } }, include: { office: 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: 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,6 +383,9 @@ 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);
}); });
@ -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";