71 lines
2.2 KiB
TypeScript
71 lines
2.2 KiB
TypeScript
import { OfficeFolders } from ".prisma/client";
|
|
import OfficeFoldersRepository from "@Repositories/OfficeFoldersRepository";
|
|
import BaseService from "@Services/BaseService";
|
|
import { OfficeFolder } from "le-coffre-resources/dist/Notary";
|
|
import { Service } from "typedi";
|
|
import { Prisma } from "@prisma/client";
|
|
|
|
@Service()
|
|
export default class OfficeFoldersService extends BaseService {
|
|
constructor(
|
|
private officeFoldersRepository: OfficeFoldersRepository,
|
|
) {
|
|
super();
|
|
}
|
|
|
|
/**
|
|
* @description : Get all folders
|
|
* @throws {Error} If folders cannot be get
|
|
*/
|
|
public async get(query: Prisma.OfficeFoldersFindManyArgs) {
|
|
return this.officeFoldersRepository.findMany(query);
|
|
}
|
|
|
|
/**
|
|
* @description : Create a new folder
|
|
* @throws {Error} If folder cannot be created
|
|
*/
|
|
public async create(officeFolderEntity: OfficeFolder): Promise<OfficeFolders> {
|
|
return this.officeFoldersRepository.create(officeFolderEntity);
|
|
}
|
|
|
|
/**
|
|
* @description : Modify a folder
|
|
* @throws {Error} If folder cannot be modified
|
|
*/
|
|
public async update(officeFolderuid: string, officeFolderEntity: OfficeFolder): Promise<OfficeFolders> {
|
|
return this.officeFoldersRepository.update(officeFolderuid, officeFolderEntity);
|
|
}
|
|
|
|
/**
|
|
* @description : Get a folder by uid
|
|
* @throws {Error} If folder cannot be get by uid
|
|
*/
|
|
public async getByUid(uid: string, query?: Prisma.OfficeFoldersInclude) {
|
|
return this.officeFoldersRepository.findOneByUid(uid, query);
|
|
}
|
|
|
|
/**
|
|
* @description : Get a folder by uid
|
|
* @throws {Error} If folder cannot be get by uid
|
|
*/
|
|
public async getByUidWithOffice(uid: string) {
|
|
return this.officeFoldersRepository.findOneByUidWithOffice(uid);
|
|
}
|
|
|
|
/**
|
|
* @description : Delete a folder
|
|
* @throws {Error} If document cannot be deleted
|
|
*/
|
|
public async delete(uid: string): Promise<OfficeFolders> {
|
|
const officeFolderEntity = await this.officeFoldersRepository.findOneByUid(uid, { customers: true });
|
|
if (!officeFolderEntity) throw new Error("office folder not found");
|
|
const officeFolder = OfficeFolder.hydrate<OfficeFolder>(officeFolderEntity, { strategy: "excludeAll" });
|
|
|
|
if (officeFolder.customers?.length) {
|
|
throw new Error("This folder is used by customers");
|
|
}
|
|
return this.officeFoldersRepository.delete(uid);
|
|
}
|
|
}
|