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) {
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 {
@ -193,22 +193,22 @@ 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!);
@ -218,13 +218,11 @@ export default class IdNotService extends BaseService {
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;
})
).json()) as IOfficeData;
if (officeData.typeEntite.name === "office") {
userData = rattachement;
}
@ -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!);
@ -318,7 +312,6 @@ export default class IdNotService extends BaseService {
})
).json()) as IRattachementData;
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 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 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,6 +383,9 @@ 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);
});
@ -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";