Skip to content

Commit 066a8dd

Browse files
committed
2 parents 789c425 + af4e60b commit 066a8dd

3 files changed

Lines changed: 16 additions & 4 deletions

File tree

src/controllers/documentRoots.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const findManyFor: RequestHandler<
2525
{ id: string /** userId */ },
2626
any,
2727
any,
28-
{ ignoreMissingRoots?: boolean; ids: string[] }
28+
{ ignoreMissingRoots?: boolean; type?: string; ids: string[] }
2929
> = async (req, res, next) => {
3030
if (!req.params.id) {
3131
throw new HTTP400Error('Missing user id');
@@ -38,7 +38,10 @@ export const findManyFor: RequestHandler<
3838
if (ids.length === 0 || !req.query.ids) {
3939
return res.json([]);
4040
}
41-
const documents = await DocumentRoot.findManyModels(req.params.id, ids, !!req.query.ignoreMissingRoots);
41+
const documents = await DocumentRoot.findManyModels(req.params.id, ids, {
42+
ignoreMissingRoots: !!req.query.ignoreMissingRoots,
43+
documentType: req.query.type && (req.query.type as string | undefined)
44+
});
4245
res.json(documents);
4346
};
4447

src/models/DocumentRoot.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ function DocumentRoot(db: PrismaClient['documentRoot']) {
9696
async findManyModels(
9797
actorId: string,
9898
ids: string[],
99-
ignoreMissingRoots: boolean = false
99+
config: {
100+
ignoreMissingRoots?: boolean;
101+
documentType?: string;
102+
} = {}
100103
): Promise<ApiDocumentRoot[] | null> {
101104
const documentRoots = (await prisma.view_UsersDocuments.findMany({
102105
where: { id: { in: ids }, userId: actorId },
@@ -106,6 +109,11 @@ function DocumentRoot(db: PrismaClient['documentRoot']) {
106109
.filter((docRoot) => docRoot !== null)
107110
.map((docRoot) => {
108111
delete (docRoot as any).userId;
112+
if (config.documentType) {
113+
docRoot.documents = docRoot.documents.filter(
114+
(doc) => doc.type === config.documentType
115+
);
116+
}
109117
return docRoot;
110118
});
111119
const missingDocumentRoots = ids.filter(
@@ -115,7 +123,7 @@ function DocumentRoot(db: PrismaClient['documentRoot']) {
115123
* Possibly the user does not have documents in the requested document roots (and thus no docRoot is returned above).
116124
* In this case, we have to load these documentRoots too.
117125
*/
118-
if (missingDocumentRoots.length > 0 && !ignoreMissingRoots) {
126+
if (missingDocumentRoots.length > 0 && !config.ignoreMissingRoots) {
119127
const docRoots = await db.findMany({
120128
where: { id: { in: missingDocumentRoots } },
121129
include: {

src/routes/router.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ router.get('/users/:id', findUser);
6161
router.put('/users/:id', updateUser);
6262
/**
6363
* @optional ?ignoreMissingRoots: boolean
64+
* @optional ?type: string -> filter included documents by provided type
6465
* @requires ?ids: string[]
6566
*/
6667
router.get('/users/:id/documentRoots', findManyDocumentRootsFor);

0 commit comments

Comments
 (0)