Delete files on refusing documents

This commit is contained in:
Maxime Lalo 2023-10-09 10:05:25 +02:00
parent b61da5f831
commit 4860a95209
7 changed files with 227 additions and 29 deletions

View File

@ -3,7 +3,7 @@ import { Controller, Delete, Get, Post, Put } from "@ControllerPattern/index";
import ApiController from "@Common/system/controller-pattern/ApiController"; import ApiController from "@Common/system/controller-pattern/ApiController";
import { Service } from "typedi"; import { Service } from "typedi";
import DocumentsService from "@Services/admin/DocumentsService/DocumentsService"; import DocumentsService from "@Services/admin/DocumentsService/DocumentsService";
import { Documents, Prisma } from "@prisma/client"; import { Documents, EDocumentStatus, Prisma } from "@prisma/client";
import { Document } from "le-coffre-resources/dist/Admin"; import { Document } from "le-coffre-resources/dist/Admin";
import { validateOrReject } from "class-validator"; import { validateOrReject } from "class-validator";
import authHandler from "@App/middlewares/AuthHandler"; import authHandler from "@App/middlewares/AuthHandler";
@ -31,8 +31,8 @@ export default class DocumentsController extends ApiController {
query = JSON.parse(req.query["q"] as string); query = JSON.parse(req.query["q"] as string);
} }
const officeId: string = req.body.user.office_Id; const officeId: string = req.body.user.office_Id;
const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId } ; const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId };
if(!query.where) query.where = { document_type : {office: officeWhereInput}}; if (!query.where) query.where = { document_type: { office: officeWhereInput } };
query.where.document_type!.office = officeWhereInput; query.where.document_type!.office = officeWhereInput;
//call service to get prisma entity //call service to get prisma entity
@ -104,7 +104,55 @@ export default class DocumentsController extends ApiController {
await validateOrReject(documentEntity, { groups: ["updateDocument"] }); await validateOrReject(documentEntity, { groups: ["updateDocument"] });
//call service to get prisma entity //call service to get prisma entity
const documentEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity, req.body.refused_reason); const documentEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity);
//Hydrate ressource with prisma entity
const document = Document.hydrate<Document>(documentEntityUpdated, { strategy: "excludeAll" });
//success
this.httpSuccess(response, document);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Update a specific document
*/
@Put("/api/v1/notary/documents/:uid/refuse", [authHandler, ruleHandler, documentHandler])
protected async refuseDocument(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
this.httpBadRequest(response, "No uid provided");
return;
}
const documentFound = await this.documentsService.getByUid(uid, {
files: true,
});
if (!documentFound) {
this.httpNotFoundRequest(response, "document not found");
return;
}
//init Document resource with request body values
const documentEntity = Document.hydrate<Document>(documentFound);
// Status to refuse
documentEntity.document_status = EDocumentStatus.REFUSED;
//validate document
await validateOrReject(documentEntity, { groups: ["updateDocument"] });
//call service to get prisma entity
const documentEntityUpdated: Documents = await this.documentsService.refuse(uid, documentEntity, req.body.refused_reason);
//create email for asked document
// this.emailBuilder.sendDocumentEmails(documentEntityUpdated);
// this.notificationBuilder.sendDocumentAnchoredNotificatiom(documentEntityUpdated);
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
const document = Document.hydrate<Document>(documentEntityUpdated, { strategy: "excludeAll" }); const document = Document.hydrate<Document>(documentEntityUpdated, { strategy: "excludeAll" });

View File

@ -3,7 +3,7 @@ import { Controller, Delete, Get, Post, Put } from "@ControllerPattern/index";
import ApiController from "@Common/system/controller-pattern/ApiController"; import ApiController from "@Common/system/controller-pattern/ApiController";
import { Service } from "typedi"; import { Service } from "typedi";
import DocumentsService from "@Services/notary/DocumentsService/DocumentsService"; import DocumentsService from "@Services/notary/DocumentsService/DocumentsService";
import { Documents, Prisma } from "@prisma/client"; import { Documents, EDocumentStatus, Prisma } from "@prisma/client";
import { Document, OfficeFolder } from "le-coffre-resources/dist/Notary"; import { Document, OfficeFolder } from "le-coffre-resources/dist/Notary";
import { validateOrReject } from "class-validator"; import { validateOrReject } from "class-validator";
import authHandler from "@App/middlewares/AuthHandler"; import authHandler from "@App/middlewares/AuthHandler";
@ -127,7 +127,55 @@ export default class DocumentsController extends ApiController {
await validateOrReject(documentEntity, { groups: ["updateDocument"] }); await validateOrReject(documentEntity, { groups: ["updateDocument"] });
//call service to get prisma entity //call service to get prisma entity
const documentEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity, req.body.refused_reason); const documentEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity);
//create email for asked document
// this.emailBuilder.sendDocumentEmails(documentEntityUpdated);
// this.notificationBuilder.sendDocumentAnchoredNotificatiom(documentEntityUpdated);
//Hydrate ressource with prisma entity
const document = Document.hydrate<Document>(documentEntityUpdated, { strategy: "excludeAll" });
//success
this.httpSuccess(response, document);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Update a specific document
*/
@Put("/api/v1/notary/documents/:uid/refuse", [authHandler, ruleHandler, documentHandler])
protected async refuseDocument(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
this.httpBadRequest(response, "No uid provided");
return;
}
const documentFound = await this.documentsService.getByUid(uid, {
files: true,
});
if (!documentFound) {
this.httpNotFoundRequest(response, "document not found");
return;
}
//init Document resource with request body values
const documentEntity = Document.hydrate<Document>(documentFound);
// Status to refuse
documentEntity.document_status = EDocumentStatus.REFUSED;
//validate document
await validateOrReject(documentEntity, { groups: ["updateDocument"] });
//call service to get prisma entity
const documentEntityUpdated: Documents = await this.documentsService.refuse(uid, documentEntity, req.body.refused_reason);
//create email for asked document //create email for asked document
// this.emailBuilder.sendDocumentEmails(documentEntityUpdated); // this.emailBuilder.sendDocumentEmails(documentEntityUpdated);

View File

@ -4,7 +4,7 @@ import roleHandler from "@App/middlewares/RolesHandler";
import ruleHandler from "@App/middlewares/RulesHandler"; import ruleHandler from "@App/middlewares/RulesHandler";
import ApiController from "@Common/system/controller-pattern/ApiController"; import ApiController from "@Common/system/controller-pattern/ApiController";
import { Controller, Delete, Get, Post, Put } from "@ControllerPattern/index"; import { Controller, Delete, Get, Post, Put } from "@ControllerPattern/index";
import { Documents, Prisma } from "@prisma/client"; import { Documents, EDocumentStatus, Prisma } from "@prisma/client";
import DocumentsService from "@Services/super-admin/DocumentsService/DocumentsService"; import DocumentsService from "@Services/super-admin/DocumentsService/DocumentsService";
import { validateOrReject } from "class-validator"; import { validateOrReject } from "class-validator";
import { Request, Response } from "express"; import { Request, Response } from "express";
@ -32,9 +32,9 @@ export default class DocumentsController extends ApiController {
} }
const officeId: string = req.body.user.office_Id; const officeId: string = req.body.user.office_Id;
const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId } ; const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId };
if(!query.where) query.where = { document_type : {office: officeWhereInput}}; if (!query.where) query.where = { document_type: { office: officeWhereInput } };
// query.where.document_type!.office = officeWhereInput; // query.where.document_type!.office = officeWhereInput;
@ -108,7 +108,55 @@ export default class DocumentsController extends ApiController {
await validateOrReject(documentEntity, { groups: ["updateDocument"] }); await validateOrReject(documentEntity, { groups: ["updateDocument"] });
//call service to get prisma entity //call service to get prisma entity
const documentEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity, req.body.refused_reason); const documentEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity);
//Hydrate ressource with prisma entity
const document = Document.hydrate<Document>(documentEntityUpdated, { strategy: "excludeAll" });
//success
this.httpSuccess(response, document);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Update a specific document
*/
@Put("/api/v1/notary/documents/:uid/refuse", [authHandler, ruleHandler, documentHandler])
protected async refuseDocument(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
this.httpBadRequest(response, "No uid provided");
return;
}
const documentFound = await this.documentsService.getByUid(uid, {
files: true,
});
if (!documentFound) {
this.httpNotFoundRequest(response, "document not found");
return;
}
//init Document resource with request body values
const documentEntity = Document.hydrate<Document>(documentFound);
// Status to refuse
documentEntity.document_status = EDocumentStatus.REFUSED;
//validate document
await validateOrReject(documentEntity, { groups: ["updateDocument"] });
//call service to get prisma entity
const documentEntityUpdated: Documents = await this.documentsService.refuse(uid, documentEntity, req.body.refused_reason);
//create email for asked document
// this.emailBuilder.sendDocumentEmails(documentEntityUpdated);
// this.notificationBuilder.sendDocumentAnchoredNotificatiom(documentEntityUpdated);
//Hydrate ressource with prisma entity //Hydrate ressource with prisma entity
const document = Document.hydrate<Document>(documentEntityUpdated, { strategy: "excludeAll" }); const document = Document.hydrate<Document>(documentEntityUpdated, { strategy: "excludeAll" });

View File

@ -21,7 +21,6 @@ export default class DocumentsRepository extends BaseRepository {
* @description : Find many documents * @description : Find many documents
*/ */
public async findMany(query: Prisma.DocumentsFindManyArgs) { public async findMany(query: Prisma.DocumentsFindManyArgs) {
query.take = Math.min(query.take || this.defaultFetchRows, this.maxFetchRows); query.take = Math.min(query.take || this.defaultFetchRows, this.maxFetchRows);
return this.model.findMany(query); return this.model.findMany(query);
} }
@ -96,7 +95,26 @@ export default class DocumentsRepository extends BaseRepository {
/** /**
* @description : Update data of a document * @description : Update data of a document
*/ */
public async update(uid: string, document: Partial<DocumentCustomer>, refusedReason?: string): Promise<Documents> { public async update(uid: string, document: Partial<DocumentCustomer>): Promise<Documents> {
return this.model.update({
where: {
uid: uid,
},
data: {
document_status: EDocumentStatus[document.document_status as keyof typeof EDocumentStatus],
document_history: {
create: {
document_status: EDocumentStatus[document.document_status as keyof typeof EDocumentStatus],
},
},
},
});
}
/**
* @description : Update data of a document
*/
public async refuse(uid: string, document: Partial<DocumentCustomer>, refusedReason?: string): Promise<Documents> {
return this.model.update({ return this.model.update({
where: { where: {
uid: uid, uid: uid,

View File

@ -3,10 +3,11 @@ import { Document } from "le-coffre-resources/dist/Admin";
import DocumentsRepository from "@Repositories/DocumentsRepository"; import DocumentsRepository from "@Repositories/DocumentsRepository";
import BaseService from "@Services/BaseService"; import BaseService from "@Services/BaseService";
import { Service } from "typedi"; import { Service } from "typedi";
import FilesRepository from "@Repositories/FilesRepository";
@Service() @Service()
export default class DocumentsService extends BaseService { export default class DocumentsService extends BaseService {
constructor(private documentsRepository: DocumentsRepository) { constructor(private documentsRepository: DocumentsRepository, private filesRepository: FilesRepository) {
super(); super();
} }
@ -38,8 +39,19 @@ export default class DocumentsService extends BaseService {
* @description : Modify a document * @description : Modify a document
* @throws {Error} If document cannot be modified * @throws {Error} If document cannot be modified
*/ */
public async update(uid: string, document: Partial<Document>, refused_reason?: string): Promise<Documents> { public async update(uid: string, document: Partial<Document>): Promise<Documents> {
return this.documentsRepository.update(uid, document, refused_reason); return this.documentsRepository.update(uid, document);
}
public async refuse(uid: string, document: Partial<Document>, refused_reason: string): Promise<Documents> {
if (document.files) {
for (let i = 0; i < document.files.length; i++) {
console.log("archiving file", document.files[i]?.uid);
await this.filesRepository.deleteKeyAndArchive(document.files[i]?.uid as string);
}
}
return this.documentsRepository.refuse(uid, document, refused_reason);
} }
/** /**

View File

@ -3,10 +3,11 @@ import { Document } from "le-coffre-resources/dist/Notary";
import DocumentsRepository from "@Repositories/DocumentsRepository"; import DocumentsRepository from "@Repositories/DocumentsRepository";
import BaseService from "@Services/BaseService"; import BaseService from "@Services/BaseService";
import { Service } from "typedi"; import { Service } from "typedi";
import FilesRepository from "@Repositories/FilesRepository";
@Service() @Service()
export default class DocumentsService extends BaseService { export default class DocumentsService extends BaseService {
constructor(private documentsRepository: DocumentsRepository) { constructor(private documentsRepository: DocumentsRepository, private filesRepository: FilesRepository) {
super(); super();
} }
@ -38,8 +39,19 @@ export default class DocumentsService extends BaseService {
* @description : Modify a document * @description : Modify a document
* @throws {Error} If document cannot be modified * @throws {Error} If document cannot be modified
*/ */
public async update(uid: string, document: Partial<Document>, refused_reason?: string): Promise<Documents> { public async update(uid: string, document: Partial<Document>): Promise<Documents> {
return this.documentsRepository.update(uid, document, refused_reason); return this.documentsRepository.update(uid, document);
}
public async refuse(uid: string, document: Partial<Document>, refused_reason: string): Promise<Documents> {
if (document.files) {
for (let i = 0; i < document.files.length; i++) {
console.log("archiving file", document.files[i]?.uid);
await this.filesRepository.deleteKeyAndArchive(document.files[i]?.uid as string);
}
}
return this.documentsRepository.refuse(uid, document, refused_reason);
} }
/** /**

View File

@ -3,10 +3,11 @@ import { Document } from "le-coffre-resources/dist/SuperAdmin";
import DocumentsRepository from "@Repositories/DocumentsRepository"; import DocumentsRepository from "@Repositories/DocumentsRepository";
import BaseService from "@Services/BaseService"; import BaseService from "@Services/BaseService";
import { Service } from "typedi"; import { Service } from "typedi";
import FilesRepository from "@Repositories/FilesRepository";
@Service() @Service()
export default class DocumentsService extends BaseService { export default class DocumentsService extends BaseService {
constructor(private documentsRepository: DocumentsRepository) { constructor(private documentsRepository: DocumentsRepository, private filesRepository: FilesRepository) {
super(); super();
} }
@ -38,8 +39,19 @@ export default class DocumentsService extends BaseService {
* @description : Modify a document * @description : Modify a document
* @throws {Error} If document cannot be modified * @throws {Error} If document cannot be modified
*/ */
public async update(uid: string, document: Partial<Document>, refused_reason?: string): Promise<Documents> { public async update(uid: string, document: Partial<Document>): Promise<Documents> {
return this.documentsRepository.update(uid, document, refused_reason); return this.documentsRepository.update(uid, document);
}
public async refuse(uid: string, document: Partial<Document>, refused_reason: string): Promise<Documents> {
if (document.files) {
for (let i = 0; i < document.files.length; i++) {
console.log("archiving file", document.files[i]?.uid);
await this.filesRepository.deleteKeyAndArchive(document.files[i]?.uid as string);
}
}
return this.documentsRepository.refuse(uid, document, refused_reason);
} }
/** /**
@ -69,7 +81,7 @@ export default class DocumentsService extends BaseService {
* @description : Get a document by uid * @description : Get a document by uid
* @throws {Error} If document cannot be get by uid * @throws {Error} If document cannot be get by uid
*/ */
public async getByUidWithFiles(uid: string): Promise<Documents & {files: Files[] | null} | null> { public async getByUidWithFiles(uid: string): Promise<(Documents & { files: Files[] | null }) | null> {
return this.documentsRepository.findOneByUidWithFiles(uid); return this.documentsRepository.findOneByUidWithFiles(uid);
} }