arocapi 5.0.0-beta.2 → 5.0.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -824
- package/dist/app.d.ts +20 -7
- package/dist/app.js +42 -28
- package/dist/app.js.map +1 -1
- package/dist/app.test.js +41 -56
- package/dist/app.test.js.map +1 -1
- package/dist/errorHandling.test.d.ts +1 -0
- package/dist/errorHandling.test.js +222 -0
- package/dist/errorHandling.test.js.map +1 -0
- package/dist/index.dev.js +4 -3
- package/dist/index.dev.js.map +1 -1
- package/dist/routes/capabilities.test.js +8 -8
- package/dist/routes/capabilities.test.js.map +1 -1
- package/dist/routes/crate.test.js +67 -19
- package/dist/routes/crate.test.js.map +1 -1
- package/dist/routes/entities.d.ts +5 -2
- package/dist/routes/entities.js +31 -49
- package/dist/routes/entities.js.map +1 -1
- package/dist/routes/entities.test.js +16 -12
- package/dist/routes/entities.test.js.map +1 -1
- package/dist/routes/entity.d.ts +7 -2
- package/dist/routes/entity.js +14 -29
- package/dist/routes/entity.js.map +1 -1
- package/dist/routes/entity.test.js +19 -13
- package/dist/routes/entity.test.js.map +1 -1
- package/dist/routes/file.d.ts +6 -0
- package/dist/routes/file.js +67 -59
- package/dist/routes/file.js.map +1 -1
- package/dist/routes/file.test.js +135 -25
- package/dist/routes/file.test.js.map +1 -1
- package/dist/routes/files.d.ts +5 -2
- package/dist/routes/files.js +24 -39
- package/dist/routes/files.js.map +1 -1
- package/dist/routes/files.test.js +44 -21
- package/dist/routes/files.test.js.map +1 -1
- package/dist/routes/metadata.d.ts +6 -0
- package/dist/routes/metadata.js +49 -54
- package/dist/routes/metadata.js.map +1 -1
- package/dist/routes/search.d.ts +6 -2
- package/dist/routes/search.js +95 -99
- package/dist/routes/search.js.map +1 -1
- package/dist/routes/search.test.js +85 -39
- package/dist/routes/search.test.js.map +1 -1
- package/dist/test/helpers/fastify.js +2 -3
- package/dist/test/helpers/fastify.js.map +1 -1
- package/dist/test/integration.setup.d.ts +18 -0
- package/dist/test/integration.setup.js +15 -7
- package/dist/test/integration.setup.js.map +1 -1
- package/dist/test/integration.test.js +368 -2
- package/dist/test/integration.test.js.map +1 -1
- package/dist/transformers/default.d.ts +10 -18
- package/dist/transformers/default.js +41 -15
- package/dist/transformers/default.js.map +1 -1
- package/dist/transformers/default.test.js +109 -47
- package/dist/transformers/default.test.js.map +1 -1
- package/dist/transformers/transformer.test.js +12 -34
- package/dist/transformers/transformer.test.js.map +1 -1
- package/dist/types/access.d.ts +9 -0
- package/dist/types/access.js +2 -0
- package/dist/types/access.js.map +1 -0
- package/dist/types/context.d.ts +6 -0
- package/dist/types/context.js +2 -0
- package/dist/types/context.js.map +1 -0
- package/dist/types/fileHandlers.d.ts +5 -9
- package/dist/types/transformers.d.ts +4 -10
- package/dist/types/visibility.d.ts +5 -0
- package/dist/types/visibility.js +2 -0
- package/dist/types/visibility.js.map +1 -0
- package/dist/utils/access.d.ts +17 -0
- package/dist/utils/access.js +41 -0
- package/dist/utils/access.js.map +1 -0
- package/dist/utils/errors.d.ts +13 -4
- package/dist/utils/errors.js +44 -8
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/errors.test.js +14 -1
- package/dist/utils/errors.test.js.map +1 -1
- package/dist/utils/validation.d.ts +2 -0
- package/dist/utils/validation.js +8 -0
- package/dist/utils/validation.js.map +1 -0
- package/dist/utils/visibility.d.ts +18 -0
- package/dist/utils/visibility.js +16 -0
- package/dist/utils/visibility.js.map +1 -0
- package/package.json +6 -4
- package/src/app.test.ts +45 -66
- package/src/app.ts +85 -54
- package/src/errorHandling.test.ts +288 -0
- package/src/index.dev.ts +6 -4
- package/src/routes/__snapshots__/search.test.ts.snap +2 -2
- package/src/routes/capabilities.test.ts +8 -8
- package/src/routes/crate.test.ts +80 -19
- package/src/routes/entities.test.ts +16 -12
- package/src/routes/entities.ts +40 -59
- package/src/routes/entity.test.ts +19 -13
- package/src/routes/entity.ts +22 -35
- package/src/routes/file.test.ts +156 -25
- package/src/routes/file.ts +84 -62
- package/src/routes/files.test.ts +52 -26
- package/src/routes/files.ts +31 -47
- package/src/routes/metadata.ts +64 -58
- package/src/routes/search.test.ts +88 -39
- package/src/routes/search.ts +123 -119
- package/src/test/helpers/fastify.ts +2 -3
- package/src/test/integration.setup.ts +18 -7
- package/src/test/integration.test.ts +472 -1
- package/src/transformers/default.test.ts +139 -47
- package/src/transformers/default.ts +103 -83
- package/src/transformers/transformer.test.ts +18 -44
- package/src/types/access.ts +20 -0
- package/src/types/context.ts +15 -0
- package/src/types/fileHandlers.ts +5 -13
- package/src/types/transformers.ts +4 -32
- package/src/types/visibility.ts +18 -0
- package/src/utils/access.ts +80 -0
- package/src/utils/errors.test.ts +16 -1
- package/src/utils/errors.ts +67 -10
- package/src/utils/validation.ts +11 -0
- package/src/utils/visibility.ts +56 -0
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
2
2
|
|
|
3
3
|
import { fastify, fastifyAfter, fastifyBefore, prisma } from '../test/helpers/fastify.js';
|
|
4
|
-
import {
|
|
4
|
+
import { AllPublicAccess } from '../utils/access.js';
|
|
5
|
+
import { AllVisibleEntityFilter } from '../utils/visibility.js';
|
|
5
6
|
import entitiesRoute from './entities.js';
|
|
6
7
|
|
|
7
8
|
describe('Entities Route', () => {
|
|
8
9
|
beforeEach(async () => {
|
|
9
10
|
await fastifyBefore();
|
|
10
|
-
await fastify.register(entitiesRoute, {
|
|
11
|
+
await fastify.register(entitiesRoute, {
|
|
12
|
+
entityVisibilityFilter: AllVisibleEntityFilter,
|
|
13
|
+
prisma,
|
|
14
|
+
access: AllPublicAccess,
|
|
15
|
+
});
|
|
11
16
|
});
|
|
12
17
|
|
|
13
18
|
afterEach(async () => {
|
|
@@ -59,7 +64,7 @@ describe('Entities Route', () => {
|
|
|
59
64
|
expect(response.statusCode).toBe(200);
|
|
60
65
|
expect(body).toMatchSnapshot();
|
|
61
66
|
expect(prisma.entity.findMany).toHaveBeenCalledWith({
|
|
62
|
-
where: {},
|
|
67
|
+
where: { AND: [{}, {}] },
|
|
63
68
|
include: { file: { select: { id: true } } },
|
|
64
69
|
orderBy: { id: 'asc' },
|
|
65
70
|
skip: 0,
|
|
@@ -81,7 +86,7 @@ describe('Entities Route', () => {
|
|
|
81
86
|
|
|
82
87
|
expect(response.statusCode).toBe(200);
|
|
83
88
|
expect(prisma.entity.findMany).toHaveBeenCalledWith({
|
|
84
|
-
where: { memberOf: 'http://example.com/collection/1' },
|
|
89
|
+
where: { AND: [{ memberOf: 'http://example.com/collection/1' }, {}] },
|
|
85
90
|
include: { file: { select: { id: true } } },
|
|
86
91
|
orderBy: { id: 'asc' },
|
|
87
92
|
skip: 0,
|
|
@@ -103,7 +108,7 @@ describe('Entities Route', () => {
|
|
|
103
108
|
|
|
104
109
|
expect(response.statusCode).toBe(200);
|
|
105
110
|
expect(prisma.entity.findMany).toHaveBeenCalledWith({
|
|
106
|
-
where: { entityType: { in: ['http://pcdm.org/models#Collection'] } },
|
|
111
|
+
where: { AND: [{ entityType: { in: ['http://pcdm.org/models#Collection'] } }, {}] },
|
|
107
112
|
include: { file: { select: { id: true } } },
|
|
108
113
|
orderBy: { id: 'asc' },
|
|
109
114
|
skip: 0,
|
|
@@ -126,9 +131,7 @@ describe('Entities Route', () => {
|
|
|
126
131
|
expect(response.statusCode).toBe(200);
|
|
127
132
|
expect(prisma.entity.findMany).toHaveBeenCalledWith({
|
|
128
133
|
where: {
|
|
129
|
-
entityType: {
|
|
130
|
-
in: ['http://pcdm.org/models#Collection', 'http://pcdm.org/models#Object'],
|
|
131
|
-
},
|
|
134
|
+
AND: [{ entityType: { in: ['http://pcdm.org/models#Collection', 'http://pcdm.org/models#Object'] } }, {}],
|
|
132
135
|
},
|
|
133
136
|
include: { file: { select: { id: true } } },
|
|
134
137
|
orderBy: { id: 'asc' },
|
|
@@ -152,7 +155,7 @@ describe('Entities Route', () => {
|
|
|
152
155
|
|
|
153
156
|
expect(response.statusCode).toBe(200);
|
|
154
157
|
expect(prisma.entity.findMany).toHaveBeenCalledWith({
|
|
155
|
-
where: {},
|
|
158
|
+
where: { AND: [{}, {}] },
|
|
156
159
|
include: { file: { select: { id: true } } },
|
|
157
160
|
orderBy: { id: 'asc' },
|
|
158
161
|
skip: 10,
|
|
@@ -175,7 +178,7 @@ describe('Entities Route', () => {
|
|
|
175
178
|
|
|
176
179
|
expect(response.statusCode).toBe(200);
|
|
177
180
|
expect(prisma.entity.findMany).toHaveBeenCalledWith({
|
|
178
|
-
where: {},
|
|
181
|
+
where: { AND: [{}, {}] },
|
|
179
182
|
include: { file: { select: { id: true } } },
|
|
180
183
|
orderBy: { name: 'desc' },
|
|
181
184
|
skip: 0,
|
|
@@ -197,7 +200,7 @@ describe('Entities Route', () => {
|
|
|
197
200
|
|
|
198
201
|
expect(response.statusCode).toBe(200);
|
|
199
202
|
expect(prisma.entity.findMany).toHaveBeenCalledWith({
|
|
200
|
-
where: {},
|
|
203
|
+
where: { AND: [{}, {}] },
|
|
201
204
|
include: { file: { select: { id: true } } },
|
|
202
205
|
orderBy: { id: 'asc' },
|
|
203
206
|
skip: 0,
|
|
@@ -251,8 +254,9 @@ describe('Entities Route', () => {
|
|
|
251
254
|
|
|
252
255
|
await fastifyBefore();
|
|
253
256
|
await fastify.register(entitiesRoute, {
|
|
257
|
+
entityVisibilityFilter: AllVisibleEntityFilter,
|
|
254
258
|
prisma,
|
|
255
|
-
|
|
259
|
+
access: AllPublicAccess,
|
|
256
260
|
entityTransformers: [customTransformer],
|
|
257
261
|
});
|
|
258
262
|
|
package/src/routes/entities.ts
CHANGED
|
@@ -2,9 +2,10 @@ import type { FastifyPluginAsync } from 'fastify';
|
|
|
2
2
|
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
import type { PrismaClient } from '../generated/prisma/client.js';
|
|
5
|
-
import {
|
|
6
|
-
import type {
|
|
7
|
-
import {
|
|
5
|
+
import { applyTransformers, authoriseEntities } from '../transformers/default.js';
|
|
6
|
+
import type { AccessFunction } from '../types/access.js';
|
|
7
|
+
import type { EntityTransformer } from '../types/transformers.js';
|
|
8
|
+
import type { EntityVisibilityFilter } from '../types/visibility.js';
|
|
8
9
|
|
|
9
10
|
const querySchema = z.object({
|
|
10
11
|
memberOf: z.url().optional(),
|
|
@@ -25,12 +26,13 @@ const querySchema = z.object({
|
|
|
25
26
|
|
|
26
27
|
type EntitiesRouteOptions = {
|
|
27
28
|
prisma: PrismaClient;
|
|
28
|
-
|
|
29
|
+
access: AccessFunction;
|
|
29
30
|
entityTransformers?: EntityTransformer[];
|
|
31
|
+
entityVisibilityFilter: EntityVisibilityFilter;
|
|
30
32
|
};
|
|
31
33
|
|
|
32
34
|
const entities: FastifyPluginAsync<EntitiesRouteOptions> = async (fastify, opts) => {
|
|
33
|
-
const { prisma,
|
|
35
|
+
const { prisma, access, entityTransformers = [], entityVisibilityFilter } = opts;
|
|
34
36
|
fastify.withTypeProvider<ZodTypeProvider>().get(
|
|
35
37
|
'/entities',
|
|
36
38
|
{
|
|
@@ -38,68 +40,47 @@ const entities: FastifyPluginAsync<EntitiesRouteOptions> = async (fastify, opts)
|
|
|
38
40
|
querystring: querySchema,
|
|
39
41
|
},
|
|
40
42
|
},
|
|
41
|
-
async (request
|
|
43
|
+
async (request) => {
|
|
42
44
|
const { memberOf, entityType, limit, offset, sort, order } = request.query;
|
|
43
45
|
|
|
44
|
-
|
|
45
|
-
const where: NonNullable<Parameters<typeof prisma.entity.findMany>[0]>['where'] = {};
|
|
46
|
+
const where: NonNullable<Parameters<typeof prisma.entity.findMany>[0]>['where'] = {};
|
|
46
47
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (entityType && entityType.length > 0) {
|
|
52
|
-
where.entityType = {
|
|
53
|
-
in: entityType,
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const [dbEntities, total] = await Promise.all([
|
|
58
|
-
prisma.entity.findMany({
|
|
59
|
-
where,
|
|
60
|
-
include: { file: { select: { id: true } } },
|
|
61
|
-
orderBy: {
|
|
62
|
-
[sort]: order,
|
|
63
|
-
},
|
|
64
|
-
skip: offset,
|
|
65
|
-
take: limit,
|
|
66
|
-
}),
|
|
67
|
-
prisma.entity.count({ where }),
|
|
68
|
-
]);
|
|
69
|
-
|
|
70
|
-
// Resolve memberOf and rootCollection references
|
|
71
|
-
const refMap = await resolveEntityReferences(dbEntities, prisma);
|
|
48
|
+
if (memberOf) {
|
|
49
|
+
where.memberOf = memberOf;
|
|
50
|
+
}
|
|
72
51
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
...base,
|
|
79
|
-
memberOf: base.memberOf ? (refMap.get(base.memberOf) ?? null) : null,
|
|
80
|
-
rootCollection: base.rootCollection ? (refMap.get(base.rootCollection) ?? null) : null,
|
|
81
|
-
};
|
|
82
|
-
const authorisedEntity = await accessTransformer(standardEntity, { request, fastify });
|
|
52
|
+
if (entityType && entityType.length > 0) {
|
|
53
|
+
where.entityType = {
|
|
54
|
+
in: entityType,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
83
57
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
58
|
+
const context = { request, fastify };
|
|
59
|
+
const visibilityWhere = await entityVisibilityFilter(context);
|
|
60
|
+
const visibleWhere = { AND: [where, visibilityWhere] };
|
|
88
61
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
62
|
+
const [dbEntities, total] = await Promise.all([
|
|
63
|
+
prisma.entity.findMany({
|
|
64
|
+
where: visibleWhere,
|
|
65
|
+
include: { file: { select: { id: true } } },
|
|
66
|
+
orderBy: {
|
|
67
|
+
[sort]: order,
|
|
68
|
+
},
|
|
69
|
+
skip: offset,
|
|
70
|
+
take: limit,
|
|
71
|
+
}),
|
|
72
|
+
prisma.entity.count({ where: visibleWhere }),
|
|
73
|
+
]);
|
|
92
74
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
} catch (error) {
|
|
98
|
-
const err = error as Error;
|
|
99
|
-
fastify.log.error(`Database error: ${err.message}`);
|
|
75
|
+
const authorisedEntities = await authoriseEntities(dbEntities, prisma, access, context);
|
|
76
|
+
const entities = await Promise.all(
|
|
77
|
+
authorisedEntities.map((entity) => applyTransformers(entity, entityTransformers, context)),
|
|
78
|
+
);
|
|
100
79
|
|
|
101
|
-
|
|
102
|
-
|
|
80
|
+
return {
|
|
81
|
+
total,
|
|
82
|
+
entities,
|
|
83
|
+
};
|
|
103
84
|
},
|
|
104
85
|
);
|
|
105
86
|
};
|
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
2
2
|
|
|
3
3
|
import { fastify, fastifyAfter, fastifyBefore, prisma } from '../test/helpers/fastify.js';
|
|
4
|
-
import {
|
|
4
|
+
import { AllPublicAccess } from '../utils/access.js';
|
|
5
5
|
import type { StandardErrorResponse } from '../utils/errors.js';
|
|
6
|
+
import { AllVisibleEntityFilter } from '../utils/visibility.js';
|
|
6
7
|
import entityRoute from './entity.js';
|
|
7
8
|
|
|
8
9
|
describe('Entity Route', () => {
|
|
9
10
|
beforeEach(async () => {
|
|
10
11
|
await fastifyBefore();
|
|
11
|
-
await fastify.register(entityRoute, {
|
|
12
|
+
await fastify.register(entityRoute, {
|
|
13
|
+
entityVisibilityFilter: AllVisibleEntityFilter,
|
|
14
|
+
refusal: { promptLoginForHidden: false },
|
|
15
|
+
prisma,
|
|
16
|
+
access: AllPublicAccess,
|
|
17
|
+
});
|
|
12
18
|
});
|
|
13
19
|
|
|
14
20
|
afterEach(async () => {
|
|
@@ -32,7 +38,7 @@ describe('Entity Route', () => {
|
|
|
32
38
|
meta: {},
|
|
33
39
|
};
|
|
34
40
|
|
|
35
|
-
prisma.entity.
|
|
41
|
+
prisma.entity.findFirst.mockResolvedValue(mockEntity);
|
|
36
42
|
|
|
37
43
|
const response = await fastify.inject({
|
|
38
44
|
method: 'GET',
|
|
@@ -42,16 +48,14 @@ describe('Entity Route', () => {
|
|
|
42
48
|
|
|
43
49
|
expect(response.statusCode).toBe(200);
|
|
44
50
|
expect(body).toMatchSnapshot();
|
|
45
|
-
expect(prisma.entity.
|
|
46
|
-
where: {
|
|
47
|
-
id: 'http://example.com/entity/123',
|
|
48
|
-
},
|
|
51
|
+
expect(prisma.entity.findFirst).toHaveBeenCalledWith({
|
|
52
|
+
where: { AND: [{ id: 'http://example.com/entity/123' }, {}] },
|
|
49
53
|
include: { file: { select: { id: true } } },
|
|
50
54
|
});
|
|
51
55
|
});
|
|
52
56
|
|
|
53
57
|
it('should return 404 when entity not found', async () => {
|
|
54
|
-
prisma.entity.
|
|
58
|
+
prisma.entity.findFirst.mockResolvedValue(null);
|
|
55
59
|
|
|
56
60
|
const response = await fastify.inject({
|
|
57
61
|
method: 'GET',
|
|
@@ -64,7 +68,7 @@ describe('Entity Route', () => {
|
|
|
64
68
|
});
|
|
65
69
|
|
|
66
70
|
it('should return 500 when database error occurs', async () => {
|
|
67
|
-
prisma.entity.
|
|
71
|
+
prisma.entity.findFirst.mockRejectedValue(new Error('Database connection failed'));
|
|
68
72
|
|
|
69
73
|
const response = await fastify.inject({
|
|
70
74
|
method: 'GET',
|
|
@@ -84,7 +88,7 @@ describe('Entity Route', () => {
|
|
|
84
88
|
const body = JSON.parse(response.body) as StandardErrorResponse;
|
|
85
89
|
|
|
86
90
|
expect(response.statusCode).toBe(400);
|
|
87
|
-
expect(body.error).toBe('
|
|
91
|
+
expect(body.error.code).toBe('VALIDATION_ERROR');
|
|
88
92
|
});
|
|
89
93
|
|
|
90
94
|
it('should apply custom entity transformers', async () => {
|
|
@@ -96,8 +100,10 @@ describe('Entity Route', () => {
|
|
|
96
100
|
|
|
97
101
|
await fastifyBefore();
|
|
98
102
|
await fastify.register(entityRoute, {
|
|
103
|
+
entityVisibilityFilter: AllVisibleEntityFilter,
|
|
104
|
+
refusal: { promptLoginForHidden: false },
|
|
99
105
|
prisma,
|
|
100
|
-
|
|
106
|
+
access: AllPublicAccess,
|
|
101
107
|
entityTransformers: [customTransformer],
|
|
102
108
|
});
|
|
103
109
|
|
|
@@ -115,7 +121,7 @@ describe('Entity Route', () => {
|
|
|
115
121
|
};
|
|
116
122
|
|
|
117
123
|
// @ts-expect-error TS is looking at the wronf function signature
|
|
118
|
-
prisma.entity.
|
|
124
|
+
prisma.entity.findFirst.mockResolvedValue(mockEntity);
|
|
119
125
|
|
|
120
126
|
const response = await fastify.inject({
|
|
121
127
|
method: 'GET',
|
|
@@ -144,7 +150,7 @@ describe('Entity Route', () => {
|
|
|
144
150
|
};
|
|
145
151
|
|
|
146
152
|
// First call returns the entity, second call (for reference resolution) returns empty
|
|
147
|
-
prisma.entity.
|
|
153
|
+
prisma.entity.findFirst.mockResolvedValue(mockEntity);
|
|
148
154
|
prisma.entity.findMany.mockResolvedValue([]);
|
|
149
155
|
|
|
150
156
|
const response = await fastify.inject({
|
package/src/routes/entity.ts
CHANGED
|
@@ -2,9 +2,11 @@ import type { FastifyPluginAsync } from 'fastify';
|
|
|
2
2
|
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
import type { PrismaClient } from '../generated/prisma/client.js';
|
|
5
|
-
import {
|
|
6
|
-
import type {
|
|
7
|
-
import {
|
|
5
|
+
import { applyTransformers, authoriseEntities } from '../transformers/default.js';
|
|
6
|
+
import type { AccessFunction } from '../types/access.js';
|
|
7
|
+
import type { EntityTransformer } from '../types/transformers.js';
|
|
8
|
+
import type { EntityVisibilityFilter } from '../types/visibility.js';
|
|
9
|
+
import { createEntityNotVisibleResponder, type RefusalOptions } from '../utils/visibility.js';
|
|
8
10
|
|
|
9
11
|
const paramsSchema = z.object({
|
|
10
12
|
id: z.url(),
|
|
@@ -12,12 +14,16 @@ const paramsSchema = z.object({
|
|
|
12
14
|
|
|
13
15
|
type EntityRouteOptions = {
|
|
14
16
|
prisma: PrismaClient;
|
|
15
|
-
|
|
17
|
+
access: AccessFunction;
|
|
16
18
|
entityTransformers?: EntityTransformer[];
|
|
19
|
+
entityVisibilityFilter: EntityVisibilityFilter;
|
|
20
|
+
refusal: RefusalOptions;
|
|
17
21
|
};
|
|
18
22
|
|
|
19
23
|
const entity: FastifyPluginAsync<EntityRouteOptions> = async (fastify, opts) => {
|
|
20
|
-
const { prisma,
|
|
24
|
+
const { prisma, access, entityTransformers = [], entityVisibilityFilter, refusal } = opts;
|
|
25
|
+
const sendEntityNotVisible = createEntityNotVisibleResponder(prisma, refusal);
|
|
26
|
+
|
|
21
27
|
fastify.withTypeProvider<ZodTypeProvider>().get(
|
|
22
28
|
'/entity/:id',
|
|
23
29
|
{
|
|
@@ -28,39 +34,20 @@ const entity: FastifyPluginAsync<EntityRouteOptions> = async (fastify, opts) =>
|
|
|
28
34
|
async (request, reply) => {
|
|
29
35
|
const { id } = request.params;
|
|
30
36
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (!entity) {
|
|
38
|
-
return reply.code(404).send(createNotFoundError('The requested entity was not found', id));
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// Resolve memberOf and rootCollection references
|
|
42
|
-
const refMap = await resolveEntityReferences([entity], prisma);
|
|
37
|
+
const context = { request, fastify };
|
|
38
|
+
const visibilityWhere = await entityVisibilityFilter(context);
|
|
39
|
+
const entity = await prisma.entity.findFirst({
|
|
40
|
+
where: { AND: [{ id }, visibilityWhere] },
|
|
41
|
+
include: { file: { select: { id: true } } },
|
|
42
|
+
});
|
|
43
43
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
memberOf: base.memberOf ? (refMap.get(base.memberOf) ?? null) : null,
|
|
48
|
-
rootCollection: base.rootCollection ? (refMap.get(base.rootCollection) ?? null) : null,
|
|
49
|
-
};
|
|
50
|
-
const authorisedEntity = await accessTransformer(standardEntity, { request, fastify });
|
|
51
|
-
|
|
52
|
-
let result = authorisedEntity;
|
|
53
|
-
for (const transformer of entityTransformers) {
|
|
54
|
-
result = await transformer(result, { request, fastify });
|
|
55
|
-
}
|
|
44
|
+
if (!entity) {
|
|
45
|
+
return sendEntityNotVisible(reply, id, context);
|
|
46
|
+
}
|
|
56
47
|
|
|
57
|
-
|
|
58
|
-
} catch (error) {
|
|
59
|
-
const err = error as Error;
|
|
60
|
-
fastify.log.error(`Database error: ${err.message}`);
|
|
48
|
+
const [authorisedEntity] = await authoriseEntities([entity], prisma, access, context);
|
|
61
49
|
|
|
62
|
-
|
|
63
|
-
}
|
|
50
|
+
return applyTransformers(authorisedEntity, entityTransformers, context);
|
|
64
51
|
},
|
|
65
52
|
);
|
|
66
53
|
};
|