2023-10-19 17:04:19 +02:00

95 lines
3.1 KiB
TypeScript

import { Documents, Files, Prisma } from "@prisma/client";
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, private filesRepository: FilesRepository) {
super();
}
/**
* @description : Get all documents
* @throws {Error} If documents cannot be get
*/
public async get(query: Prisma.DocumentsFindManyArgs) {
return this.documentsRepository.findMany(query);
}
/**
* @description : Create a new document
* @throws {Error} If document cannot be created
*/
public async create(document: Document): Promise<Documents> {
return this.documentsRepository.create(document);
}
/**
* @description : Create new documents
* @throws {Error} If documents or one of them cannot be created
*/
public async createMany(documents: Document[]): Promise<Documents[]> {
return this.documentsRepository.createMany(documents);
}
/**
* @description : Modify a document
* @throws {Error} If document cannot be modified
*/
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++) {
await this.filesRepository.deleteKeyAndArchive(document.files[i]?.uid as string);
}
}
return this.documentsRepository.refuse(uid, refused_reason);
}
/**
* @description : Delete a document
* @throws {Error} If document cannot be deleted
*/
public async delete(uid: string): Promise<Documents> {
const documentEntity = await this.documentsRepository.findOneByUid(uid, { files: true });
if (!documentEntity) throw new Error("document not found");
const document = Document.hydrate<Document>(documentEntity, { strategy: "excludeAll" });
if (document.files && document.files.length !== 0) {
throw new Error("Can't delete a document with file");
}
return this.documentsRepository.delete(uid);
}
/**
* @description : Get a document by uid
* @throws {Error} If document cannot be get by uid
*/
public async getByUid(uid: string, query?: Prisma.DocumentsInclude): Promise<Documents | null> {
return this.documentsRepository.findOneByUid(uid, query);
}
/**
* @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> {
return this.documentsRepository.findOneByUidWithFiles(uid);
}
/**
* @description : Get a document by uid
* @throws {Error} If document cannot be get by uid
*/
public async getByUidWithOffice(uid: string) {
return this.documentsRepository.findOneByUidWithOffice(uid);
}
}