137 lines
3.3 KiB
TypeScript
137 lines
3.3 KiB
TypeScript
import Database from "@Common/databases/database";
|
|
import BaseRepository from "@Repositories/BaseRepository";
|
|
import { Service } from "typedi";
|
|
import { DocumentTypes, Documents, EDocumentStatus, Prisma } from "@prisma/client";
|
|
import { Document } from "le-coffre-resources/dist/SuperAdmin";
|
|
|
|
@Service()
|
|
export default class DocumentsRepository extends BaseRepository {
|
|
constructor(private database: Database) {
|
|
super();
|
|
}
|
|
protected get model() {
|
|
return this.database.getClient().documents;
|
|
}
|
|
protected get instanceDb() {
|
|
return this.database.getClient();
|
|
}
|
|
|
|
/**
|
|
* @description : Find many documents
|
|
*/
|
|
public async findMany(query: Prisma.DocumentsFindManyArgs) {
|
|
query.take = Math.min(query.take || this.defaultFetchRows, this.maxFetchRows);
|
|
return this.model.findMany(query);
|
|
}
|
|
|
|
/**
|
|
* @description : Create a document
|
|
*/
|
|
public async create(document: Document): Promise<Documents & {document_type: DocumentTypes}> {
|
|
const createArgs: Prisma.DocumentsCreateArgs = {
|
|
data: {
|
|
folder: {
|
|
connect: {
|
|
uid: document.folder!.uid,
|
|
},
|
|
},
|
|
depositor: {
|
|
connect: {
|
|
uid: document.depositor!.uid,
|
|
},
|
|
},
|
|
document_type: {
|
|
connect: {
|
|
uid: document.document_type!.uid,
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
const documentCreated = await this.model.create({...createArgs, include: {document_type: true}});
|
|
|
|
await this.instanceDb.documentHistory.create({
|
|
data: {
|
|
document: {
|
|
connect: {
|
|
uid: documentCreated.uid,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
return documentCreated;
|
|
}
|
|
|
|
/**
|
|
* @description : Create many documents linked to an office folder
|
|
*/
|
|
public async createMany(documents: Document[]): Promise<Documents[]> {
|
|
const createArgs: Prisma.DocumentsCreateManyArgs = {
|
|
data: documents.map((document) => ({
|
|
folder_uid: document.folder!.uid!,
|
|
depositor_uid: document.depositor!.uid!,
|
|
document_type_uid: document.document_type!.uid!,
|
|
})),
|
|
skipDuplicates: true,
|
|
};
|
|
|
|
const batchPayload = await this.model.createMany(createArgs);
|
|
|
|
const documentsCreated = await this.model.findMany({orderBy: {created_at: 'desc'}, take: batchPayload.count});
|
|
|
|
const createHistoryArgs: Prisma.DocumentHistoryCreateManyArgs = {
|
|
data: documentsCreated.map((document) => ({
|
|
document_uid: document.uid!,
|
|
})),
|
|
skipDuplicates: true,
|
|
};
|
|
await this.instanceDb.documentHistory.createMany(createHistoryArgs);
|
|
|
|
return documentsCreated;
|
|
}
|
|
|
|
/**
|
|
* @description : Update data of a document
|
|
*/
|
|
public async update(uid: string, document: Partial<Document>, refusedReason?: string): Promise<Documents> {
|
|
return this.model.update({
|
|
where: {
|
|
uid: uid,
|
|
},
|
|
data: {
|
|
document_status: EDocumentStatus[document.document_status as keyof typeof EDocumentStatus],
|
|
document_history: {
|
|
create: {
|
|
document_status: EDocumentStatus[document.document_status as keyof typeof EDocumentStatus],
|
|
refused_reason: refusedReason,
|
|
},
|
|
}
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description : Delete a document
|
|
*/
|
|
public async delete(uid: string): Promise<Documents> {
|
|
return this.model.delete({
|
|
where: {
|
|
uid: uid,
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description : Find unique document
|
|
*/
|
|
public async findOneByUid(uid: string, query?: Prisma.DocumentsInclude) {
|
|
return this.model.findUnique({
|
|
where: {
|
|
uid: uid,
|
|
},
|
|
include: query,
|
|
});
|
|
}
|
|
}
|