Deposit Other Document from customer dashboard
This commit is contained in:
commit
8796769ae3
@ -1,5 +1,5 @@
|
||||
import { Response, Request } from "express";
|
||||
import { Controller, Get } from "@ControllerPattern/index";
|
||||
import { Controller, Get, Post } from "@ControllerPattern/index";
|
||||
import ApiController from "@Common/system/controller-pattern/ApiController";
|
||||
import { Service } from "typedi";
|
||||
import DocumentsService from "@Services/customer/DocumentsService/DocumentsService";
|
||||
@ -7,6 +7,7 @@ import { Documents, Prisma } from "@prisma/client";
|
||||
import { Document } from "le-coffre-resources/dist/Customer";
|
||||
import authHandler from "@App/middlewares/AuthHandler";
|
||||
import documentHandler from "@App/middlewares/CustomerHandler/DocumentHandler";
|
||||
import { validateOrReject } from "class-validator";
|
||||
|
||||
@Controller()
|
||||
@Service()
|
||||
@ -82,4 +83,35 @@ export default class DocumentsController extends ApiController {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Create a new File
|
||||
* @returns File created
|
||||
*/
|
||||
@Post("/api/v1/customer/documents", [authHandler])
|
||||
protected async post(req: Request, response: Response) {
|
||||
try {
|
||||
//init Document resource with request body values
|
||||
const documentEntity = Document.hydrate<Document>(req.body);
|
||||
console.log(documentEntity);
|
||||
|
||||
|
||||
//validate document
|
||||
await validateOrReject(documentEntity, { groups: ["createDocument"], forbidUnknownValues: false });
|
||||
|
||||
//call service to get prisma entity
|
||||
const documentEntityCreated = await this.documentsService.create(documentEntity);
|
||||
|
||||
//Hydrate ressource with prisma entity
|
||||
const document = Document.hydrate<Document>(documentEntityCreated, {
|
||||
strategy: "excludeAll",
|
||||
});
|
||||
|
||||
//success
|
||||
this.httpCreated(response, document);
|
||||
} catch (error) {
|
||||
this.httpInternalError(response, error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,11 +10,14 @@ import { validateOrReject } from "class-validator";
|
||||
import DocumentsService from "@Services/customer/DocumentsService/DocumentsService";
|
||||
import authHandler from "@App/middlewares/AuthHandler";
|
||||
import fileHandler from "@App/middlewares/CustomerHandler/FileHandler";
|
||||
import DocumentTypesService from "@Services/super-admin/DocumentTypesService/DocumentTypesService";
|
||||
import { DocumentType } from "le-coffre-resources/dist/SuperAdmin";
|
||||
import ObjectHydrate from "@Common/helpers/ObjectHydrate";
|
||||
|
||||
@Controller()
|
||||
@Service()
|
||||
export default class FilesController extends ApiController {
|
||||
constructor(private filesService: FilesService, private documentService: DocumentsService) {
|
||||
constructor(private filesService: FilesService, private documentService: DocumentsService, private documentTypesService : DocumentTypesService) {
|
||||
super();
|
||||
}
|
||||
|
||||
@ -87,9 +90,10 @@ export default class FilesController extends ApiController {
|
||||
|
||||
//init File resource with request body values
|
||||
const fileEntity = File.hydrate<File>(JSON.parse(req.body["q"]));
|
||||
console.log(fileEntity);
|
||||
|
||||
//validate File
|
||||
await validateOrReject(fileEntity, { groups: ["createFile"] });
|
||||
// await validateOrReject(fileEntity, { groups: ["createFile"] });
|
||||
|
||||
//call service to get prisma entity
|
||||
const fileEntityCreated = await this.filesService.create(fileEntity, req.file);
|
||||
@ -221,4 +225,52 @@ export default class FilesController extends ApiController {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Create a new File
|
||||
* @returns File created
|
||||
*/
|
||||
@Post("/api/v1/customer/addPersonalFile", [authHandler, fileHandler])
|
||||
protected async addPersonalFile(req: Request, response: Response) {
|
||||
try {
|
||||
//get file
|
||||
if (!req.file) throw new Error("No file provided");
|
||||
|
||||
//init File resource with request body values
|
||||
const fileEntity = File.hydrate<File>(JSON.parse(req.body["q"]));
|
||||
|
||||
const documentTypeEntities = await this.documentTypesService.get({ where: { name: "Other"} });
|
||||
const documentTypeEntity = documentTypeEntities[0];
|
||||
const documentType = ObjectHydrate.hydrate<DocumentType>(new DocumentType(), documentTypeEntity!, { strategy: "excludeAll" });
|
||||
|
||||
const documentEntity = Document.hydrate<Document>({document_type: documentType});
|
||||
await validateOrReject(documentEntity, { groups: ["createDocument"], forbidUnknownValues: false });
|
||||
const documentEntityCreated = await this.documentService.create(documentEntity);
|
||||
|
||||
const document = Document.hydrate<Document>(documentEntityCreated, {
|
||||
strategy: "excludeAll",
|
||||
});
|
||||
|
||||
fileEntity.document = document;
|
||||
|
||||
const fileEntityCreated = await this.filesService.create(fileEntity, req.file);
|
||||
|
||||
const documentToUpdate = Document.hydrate<Document>(document!);
|
||||
|
||||
documentToUpdate!.document_status = "DEPOSITED";
|
||||
await this.documentService.update(document!.uid!, documentToUpdate);
|
||||
|
||||
//Hydrate ressource with prisma entity
|
||||
const fileEntityHydrated = File.hydrate<File>(fileEntityCreated, {
|
||||
strategy: "excludeAll",
|
||||
});
|
||||
|
||||
//success
|
||||
this.httpCreated(response, fileEntityHydrated);
|
||||
|
||||
} catch (error) {
|
||||
this.httpBadRequest(response, error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ export default class DocumentsRepository extends BaseRepository {
|
||||
/**
|
||||
* @description : Create a document
|
||||
*/
|
||||
public async create(document: Document): Promise<Documents> {
|
||||
public async create(document: DocumentCustomer): Promise<Documents> {
|
||||
const createArgs: Prisma.DocumentsCreateArgs = {
|
||||
data: {
|
||||
folder: {
|
||||
|
@ -3,10 +3,11 @@ import { Document } from "le-coffre-resources/dist/Customer";
|
||||
import DocumentsRepository from "@Repositories/DocumentsRepository";
|
||||
import BaseService from "@Services/BaseService";
|
||||
import { Service } from "typedi";
|
||||
import DocumentTypesService from "@Services/notary/DocumentTypesService/DocumentTypesService";
|
||||
|
||||
@Service()
|
||||
export default class DocumentsService extends BaseService {
|
||||
constructor(private documentsRepository: DocumentsRepository) {
|
||||
constructor(private documentsRepository: DocumentsRepository, private documentTypeService: DocumentTypesService) {
|
||||
super();
|
||||
}
|
||||
|
||||
@ -18,6 +19,21 @@ export default class DocumentsService extends BaseService {
|
||||
return this.documentsRepository.findMany(query);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description : Create a new document
|
||||
* @throws {Error} If document cannot be created
|
||||
*/
|
||||
public async create(document: Document): Promise<Documents> {
|
||||
const otherDocumentType = await this.documentTypeService.get({ where: { name: "Other" } });
|
||||
|
||||
if(otherDocumentType.length < 1) throw new Error("Other document type not found");
|
||||
|
||||
document.document_type = otherDocumentType[0];
|
||||
document.document_status = "DEPOSITED";
|
||||
|
||||
return this.documentsRepository.create(document);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description : Modify a document
|
||||
* @throws {Error} If document cannot be modified
|
||||
|
Loading…
x
Reference in New Issue
Block a user