diff --git a/src/app/api/notary/OfficeFolderAnchorsController.ts b/src/app/api/notary/OfficeFolderAnchorsController.ts index 052dffc8..62c9d3d7 100644 --- a/src/app/api/notary/OfficeFolderAnchorsController.ts +++ b/src/app/api/notary/OfficeFolderAnchorsController.ts @@ -12,6 +12,8 @@ import ruleHandler from "@App/middlewares/RulesHandler"; import folderHandler from "@App/middlewares/OfficeMembershipHandlers/FolderHandler"; import OfficeFolderAnchor from "le-coffre-resources/dist/Notary/OfficeFolderAnchor"; import NotificationBuilder from "@Common/notifications/NotificationBuilder"; +import { Prisma } from "@prisma/client"; +import OfficeFolderAnchorsService from "@Services/notary/OfficeFolderAnchorsService/OfficeFolderAnchorsService"; const hydrateOfficeFolderAnchor = (data: any): OfficeFolderAnchor => OfficeFolderAnchor.hydrate( @@ -37,6 +39,7 @@ export default class OfficeFoldersController extends ApiController { constructor( private secureService: SecureService, private officeFolderAnchorsRepository: OfficeFolderAnchorsRepository, + private officeFolderAnchorsService: OfficeFolderAnchorsService, private officeFoldersService: OfficeFoldersService, private notificationBuilder: NotificationBuilder, ) { @@ -163,7 +166,7 @@ export default class OfficeFoldersController extends ApiController { * @description Verify a folder anchor status */ @Get("/api/v1/notary/anchors/:uid", [authHandler, ruleHandler, folderHandler]) - protected async get(req: Request, response: Response) { + protected async getOneByUid(req: Request, response: Response) { try { const uid = req.params["uid"]; @@ -230,4 +233,38 @@ export default class OfficeFoldersController extends ApiController { return; } } + + /** + * @description Get all folders + */ + @Get("/api/v1/notary/anchors", [authHandler, ruleHandler]) + protected async get(req: Request, response: Response) { + try { + //get query + let query: Prisma.OfficeFolderAnchorsFindManyArgs = {}; + if (req.query["q"]) { + query = JSON.parse(req.query["q"] as string); + } + + query.where = { + ...query.where, + folder: { + office_uid: req.body.user.office_Id as string, + }, + }; + + //call service to get prisma entity + const officeFolderAnchorsEntities: OfficeFolderAnchor[] = await this.officeFolderAnchorsService.get(query); + + //Hydrate ressource with prisma entity + const officeFolderAnchors = OfficeFolderAnchor.hydrateArray(officeFolderAnchorsEntities, { + strategy: "excludeAll", + }); + //success + this.httpSuccess(response, officeFolderAnchors); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } } diff --git a/src/common/repositories/OfficeFolderAnchorsRepository.ts b/src/common/repositories/OfficeFolderAnchorsRepository.ts index c0687017..53cd98ca 100644 --- a/src/common/repositories/OfficeFolderAnchorsRepository.ts +++ b/src/common/repositories/OfficeFolderAnchorsRepository.ts @@ -65,4 +65,24 @@ export default class OfficeFolderAnchorsRepository extends BaseRepository { ...updateArgs, }); } + + /** + * @description : Find one office folder + */ + public async findOneByUid(uid: string, query?: Prisma.OfficeFolderAnchorsInclude) { + return this.model.findUnique({ + where: { + uid: uid, + }, + include: query, + }); + } + + /** + * @description : Find many office folders + */ + public async findMany(query: Prisma.OfficeFolderAnchorsFindManyArgs) { + query.take = Math.min(query.take || this.defaultFetchRows, this.maxFetchRows); + return this.model.findMany(query); + } } diff --git a/src/services/notary/OfficeFolderAnchorsService/OfficeFolderAnchorsService.ts b/src/services/notary/OfficeFolderAnchorsService/OfficeFolderAnchorsService.ts new file mode 100644 index 00000000..ee4c9ebf --- /dev/null +++ b/src/services/notary/OfficeFolderAnchorsService/OfficeFolderAnchorsService.ts @@ -0,0 +1,27 @@ +import BaseService from "@Services/BaseService"; +import { Service } from "typedi"; +import { Prisma } from "@prisma/client"; +import OfficeFolderAnchorsRepository from "@Repositories/OfficeFolderAnchorsRepository"; + +@Service() +export default class OfficeFolderAnchorsService extends BaseService { + constructor(private officeFolderAnchorsRepository: OfficeFolderAnchorsRepository) { + super(); + } + + /** + * @description : Get all folders + * @throws {Error} If folders cannot be get + */ + public async get(query: Prisma.OfficeFolderAnchorsFindManyArgs) { + return this.officeFolderAnchorsRepository.findMany(query); + } + + /** + * @description : Get a folder by uid + * @throws {Error} If folder cannot be get by uid + */ + public async getByUid(uid: string, query?: Prisma.OfficeFolderAnchorsInclude) { + return this.officeFolderAnchorsRepository.findOneByUid(uid, query); + } +}