✨ Delete files on refusing documents
This commit is contained in:
parent
b61da5f831
commit
4860a95209
@ -3,7 +3,7 @@ import { Controller, Delete, Get, Post, Put } from "@ControllerPattern/index";
|
||||
import ApiController from "@Common/system/controller-pattern/ApiController";
|
||||
import { Service } from "typedi";
|
||||
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 { validateOrReject } from "class-validator";
|
||||
import authHandler from "@App/middlewares/AuthHandler";
|
||||
@ -31,8 +31,8 @@ export default class DocumentsController extends ApiController {
|
||||
query = JSON.parse(req.query["q"] as string);
|
||||
}
|
||||
const officeId: string = req.body.user.office_Id;
|
||||
const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId } ;
|
||||
if(!query.where) query.where = { document_type : {office: officeWhereInput}};
|
||||
const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId };
|
||||
if (!query.where) query.where = { document_type: { office: officeWhereInput } };
|
||||
query.where.document_type!.office = officeWhereInput;
|
||||
|
||||
//call service to get prisma entity
|
||||
@ -104,7 +104,55 @@ export default class DocumentsController extends ApiController {
|
||||
await validateOrReject(documentEntity, { groups: ["updateDocument"] });
|
||||
|
||||
//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
|
||||
const document = Document.hydrate<Document>(documentEntityUpdated, { strategy: "excludeAll" });
|
||||
|
@ -3,7 +3,7 @@ import { Controller, Delete, Get, Post, Put } from "@ControllerPattern/index";
|
||||
import ApiController from "@Common/system/controller-pattern/ApiController";
|
||||
import { Service } from "typedi";
|
||||
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 { validateOrReject } from "class-validator";
|
||||
import authHandler from "@App/middlewares/AuthHandler";
|
||||
@ -127,7 +127,55 @@ export default class DocumentsController extends ApiController {
|
||||
await validateOrReject(documentEntity, { groups: ["updateDocument"] });
|
||||
|
||||
//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
|
||||
// this.emailBuilder.sendDocumentEmails(documentEntityUpdated);
|
||||
|
@ -4,7 +4,7 @@ import roleHandler from "@App/middlewares/RolesHandler";
|
||||
import ruleHandler from "@App/middlewares/RulesHandler";
|
||||
import ApiController from "@Common/system/controller-pattern/ApiController";
|
||||
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 { validateOrReject } from "class-validator";
|
||||
import { Request, Response } from "express";
|
||||
@ -31,16 +31,16 @@ export default class DocumentsController extends ApiController {
|
||||
query = JSON.parse(req.query["q"] as string);
|
||||
}
|
||||
const officeId: string = req.body.user.office_Id;
|
||||
|
||||
const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId } ;
|
||||
|
||||
if(!query.where) query.where = { document_type : {office: officeWhereInput}};
|
||||
|
||||
// query.where.document_type!.office = officeWhereInput;
|
||||
|
||||
const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId };
|
||||
|
||||
if (!query.where) query.where = { document_type: { office: officeWhereInput } };
|
||||
|
||||
// query.where.document_type!.office = officeWhereInput;
|
||||
|
||||
//call service to get prisma entity
|
||||
|
||||
const documentEntities = await this.documentsService.get(query);
|
||||
|
||||
const documentEntities = await this.documentsService.get(query);
|
||||
|
||||
//Hydrate ressource with prisma entity
|
||||
const documents = Document.hydrateArray<Document>(documentEntities, { strategy: "excludeAll" });
|
||||
@ -108,7 +108,55 @@ export default class DocumentsController extends ApiController {
|
||||
await validateOrReject(documentEntity, { groups: ["updateDocument"] });
|
||||
|
||||
//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
|
||||
const document = Document.hydrate<Document>(documentEntityUpdated, { strategy: "excludeAll" });
|
||||
@ -170,7 +218,7 @@ export default class DocumentsController extends ApiController {
|
||||
if (req.query["q"]) {
|
||||
query = JSON.parse(req.query["q"] as string);
|
||||
}
|
||||
|
||||
|
||||
const documentEntity = await this.documentsService.getByUid(uid, query);
|
||||
|
||||
if (!documentEntity) {
|
||||
|
@ -21,7 +21,6 @@ export default class DocumentsRepository extends BaseRepository {
|
||||
* @description : Find many documents
|
||||
*/
|
||||
public async findMany(query: Prisma.DocumentsFindManyArgs) {
|
||||
|
||||
query.take = Math.min(query.take || this.defaultFetchRows, this.maxFetchRows);
|
||||
return this.model.findMany(query);
|
||||
}
|
||||
@ -96,7 +95,26 @@ export default class DocumentsRepository extends BaseRepository {
|
||||
/**
|
||||
* @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({
|
||||
where: {
|
||||
uid: uid,
|
||||
|
@ -3,10 +3,11 @@ import { Document } from "le-coffre-resources/dist/Admin";
|
||||
import DocumentsRepository from "@Repositories/DocumentsRepository";
|
||||
import BaseService from "@Services/BaseService";
|
||||
import { Service } from "typedi";
|
||||
import FilesRepository from "@Repositories/FilesRepository";
|
||||
|
||||
@Service()
|
||||
export default class DocumentsService extends BaseService {
|
||||
constructor(private documentsRepository: DocumentsRepository) {
|
||||
constructor(private documentsRepository: DocumentsRepository, private filesRepository: FilesRepository) {
|
||||
super();
|
||||
}
|
||||
|
||||
@ -38,8 +39,19 @@ export default class DocumentsService extends BaseService {
|
||||
* @description : Modify a document
|
||||
* @throws {Error} If document cannot be modified
|
||||
*/
|
||||
public async update(uid: string, document: Partial<Document>, refused_reason?: string): Promise<Documents> {
|
||||
return this.documentsRepository.update(uid, document, refused_reason);
|
||||
public async update(uid: string, document: Partial<Document>): Promise<Documents> {
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -3,10 +3,11 @@ import { Document } from "le-coffre-resources/dist/Notary";
|
||||
import DocumentsRepository from "@Repositories/DocumentsRepository";
|
||||
import BaseService from "@Services/BaseService";
|
||||
import { Service } from "typedi";
|
||||
import FilesRepository from "@Repositories/FilesRepository";
|
||||
|
||||
@Service()
|
||||
export default class DocumentsService extends BaseService {
|
||||
constructor(private documentsRepository: DocumentsRepository) {
|
||||
constructor(private documentsRepository: DocumentsRepository, private filesRepository: FilesRepository) {
|
||||
super();
|
||||
}
|
||||
|
||||
@ -38,8 +39,19 @@ export default class DocumentsService extends BaseService {
|
||||
* @description : Modify a document
|
||||
* @throws {Error} If document cannot be modified
|
||||
*/
|
||||
public async update(uid: string, document: Partial<Document>, refused_reason?: string): Promise<Documents> {
|
||||
return this.documentsRepository.update(uid, document, refused_reason);
|
||||
public async update(uid: string, document: Partial<Document>): Promise<Documents> {
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -3,10 +3,11 @@ import { Document } from "le-coffre-resources/dist/SuperAdmin";
|
||||
import DocumentsRepository from "@Repositories/DocumentsRepository";
|
||||
import BaseService from "@Services/BaseService";
|
||||
import { Service } from "typedi";
|
||||
import FilesRepository from "@Repositories/FilesRepository";
|
||||
|
||||
@Service()
|
||||
export default class DocumentsService extends BaseService {
|
||||
constructor(private documentsRepository: DocumentsRepository) {
|
||||
constructor(private documentsRepository: DocumentsRepository, private filesRepository: FilesRepository) {
|
||||
super();
|
||||
}
|
||||
|
||||
@ -38,8 +39,19 @@ export default class DocumentsService extends BaseService {
|
||||
* @description : Modify a document
|
||||
* @throws {Error} If document cannot be modified
|
||||
*/
|
||||
public async update(uid: string, document: Partial<Document>, refused_reason?: string): Promise<Documents> {
|
||||
return this.documentsRepository.update(uid, document, refused_reason);
|
||||
public async update(uid: string, document: Partial<Document>): Promise<Documents> {
|
||||
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
|
||||
* @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);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user