Creating get of anchors

This commit is contained in:
Maxime Lalo 2023-10-03 11:21:25 +02:00
parent 55797019ec
commit ef4cb3f5fa
3 changed files with 85 additions and 1 deletions

View File

@ -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<OfficeFolderAnchor>(
@ -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<OfficeFolderAnchor>(officeFolderAnchorsEntities, {
strategy: "excludeAll",
});
//success
this.httpSuccess(response, officeFolderAnchors);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}