2024-09-09 05:45:17 +02:00

62 lines
2.1 KiB
TypeScript

import { DocumentsNotary, 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 DocumentsService 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<DocumentsNotary> {
return this.documentsNotaryRepository.create(document);
}
/**
* @description : Delete a document
* @throws {Error} If document cannot be deleted
*/
public async delete(uid: string): Promise<DocumentsNotary> {
const documentEntity = await this.documentsNotaryRepository.findOneByUid(uid, { files: true });
if (!documentEntity) throw new Error("document not found");
const document = Document.hydrate<Document>(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<DocumentsNotary | null> {
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);
}
}