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) {
try {
//get query
let query: Prisma.CustomersFindManyArgs = {};
let query: Prisma.DocumentsFindManyArgs = {};
if (req.query["q"]) {
query = JSON.parse(req.query["q"] as string);
}
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;

View File

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

View File

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

View File

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

View File

@ -24,9 +24,9 @@ export default class DocumentsService extends BaseService {
* @throws {Error} If document cannot be created
*/
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_status = "DEPOSITED";