import { DocumentsNotary, EDocumentNotaryStatus, Prisma } from "@prisma/client"; import { Document, DocumentNotary } from "le-coffre-resources/dist/Notary"; import DocumentsNotaryRepository from "@Repositories/DocumentsNotaryRepository"; import BaseService from "@Services/BaseService"; import { Service } from "typedi"; @Service() export default class DocumentsNotaryService extends BaseService { constructor(private documentsNotaryRepository: DocumentsNotaryRepository) { super(); } /** * @description : Get all documents * @throws {Error} If documents cannot be get */ public async get(query: Prisma.DocumentsNotaryFindManyArgs) { return this.documentsNotaryRepository.findMany(query); } /** * @description : Create a new document * @throws {Error} If document cannot be created */ public async create(document: DocumentNotary): Promise { return this.documentsNotaryRepository.create(document); } /** * @description : Delete a document * @throws {Error} If document cannot be deleted */ public async delete(uid: string): Promise { const documentEntity = await this.documentsNotaryRepository.findOneByUid(uid, { files: true }); if (!documentEntity) throw new Error("document not found"); const document = Document.hydrate(documentEntity, { strategy: "excludeAll" }); const isDocumentEmpty = document.files && !document!.files.find((file) => file.archived_at === null); if (!isDocumentEmpty && document.document_status !== "REFUSED") { throw new Error("Can't delete a document with file"); } return this.documentsNotaryRepository.delete(uid); } /** * @description : Get a document by uid * @throws {Error} If document cannot be get by uid */ public async getByUid(uid: string, query?: Prisma.DocumentsNotaryInclude): Promise { return this.documentsNotaryRepository.findOneByUid(uid, query); } /** * @description : Get a document by uid * @throws {Error} If document cannot be get by uid */ public async getByUidWithOffice(uid: string) { return this.documentsNotaryRepository.findOneByUidWithOffice(uid); } public changeStatus(uid: string, status: EDocumentNotaryStatus) { return this.documentsNotaryRepository.changeStatus(uid, status); } }