61 lines
1.9 KiB
TypeScript
61 lines
1.9 KiB
TypeScript
import { Documents } from "@prisma/client";
|
|
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, private documentTypeService: DocumentTypesService) {
|
|
super();
|
|
}
|
|
|
|
/**
|
|
* @description : Get all documents
|
|
* @throws {Error} If documents cannot be get
|
|
*/
|
|
public async get(query: any): Promise<Documents[]> {
|
|
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: "Autres documents" } });
|
|
|
|
if(otherDocumentType.length < 1) throw new Error("Autres documents document type not found");
|
|
|
|
document.document_type = otherDocumentType[0];
|
|
document.document_status = "DEPOSITED";
|
|
|
|
return this.documentsRepository.create(document);
|
|
}
|
|
|
|
/**
|
|
* @description : Delete a document
|
|
* @throws {Error} If document cannot be created
|
|
*/
|
|
public async delete(uid: string): Promise<Documents> {
|
|
return this.documentsRepository.delete(uid);
|
|
}
|
|
|
|
/**
|
|
* @description : Modify a document
|
|
* @throws {Error} If document cannot be modified
|
|
*/
|
|
public async update(uid: string, document: Document): Promise<Documents> {
|
|
return this.documentsRepository.update(uid, document);
|
|
}
|
|
|
|
/**
|
|
* @description : Get a document by uid
|
|
* @throws {Error} If document cannot be get by uid
|
|
*/
|
|
public async getByUid(uid: string, query?: any): Promise<Documents | null> {
|
|
return this.documentsRepository.findOneByUid(uid, query);
|
|
}
|
|
}
|