arocapi 5.0.0-beta.1 → 5.0.0-beta.3
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 -802
- package/dist/app.d.ts +22 -9
- package/dist/app.js +46 -30
- 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.d.ts +7 -0
- package/dist/routes/capabilities.js +13 -0
- package/dist/routes/capabilities.js.map +1 -0
- package/dist/routes/capabilities.test.d.ts +1 -0
- package/dist/routes/capabilities.test.js +81 -0
- package/dist/routes/capabilities.test.js.map +1 -0
- 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 +99 -22
- 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 +8 -5
- package/dist/routes/search.js +95 -101
- package/dist/routes/search.js.map +1 -1
- package/dist/routes/search.test.js +92 -39
- package/dist/routes/search.test.js.map +1 -1
- package/dist/test/helpers/fastify.d.ts +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 +19 -1
- 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 +19 -10
- 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/queryBuilder.d.ts +12 -0
- package/dist/utils/queryBuilder.js +9 -0
- package/dist/utils/queryBuilder.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 +34 -33
- package/src/app.test.ts +45 -66
- package/src/app.ts +92 -58
- package/src/errorHandling.test.ts +288 -0
- package/src/index.dev.ts +6 -4
- package/src/routes/__snapshots__/capabilities.test.ts.snap +34 -0
- package/src/routes/__snapshots__/search.test.ts.snap +2 -2
- package/src/routes/capabilities.test.ts +102 -0
- package/src/routes/capabilities.ts +22 -0
- 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 +117 -22
- 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 +95 -39
- package/src/routes/search.ts +120 -125
- 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 +68 -11
- package/src/utils/queryBuilder.ts +25 -0
- package/src/utils/validation.ts +11 -0
- package/src/utils/visibility.ts +56 -0
|
@@ -1,14 +1,23 @@
|
|
|
1
1
|
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
2
2
|
|
|
3
3
|
import { fastify, fastifyAfter, fastifyBefore, opensearch, 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 { OpensearchQueryBuilder } from '../utils/queryBuilder.js';
|
|
7
|
+
import { AllVisibleEntityFilter, AllVisibleSearchFilter } from '../utils/visibility.js';
|
|
6
8
|
import searchRoute from './search.js';
|
|
7
9
|
|
|
8
10
|
describe('Search Route', () => {
|
|
9
11
|
beforeEach(async () => {
|
|
10
12
|
await fastifyBefore();
|
|
11
|
-
await fastify.register(searchRoute, {
|
|
13
|
+
await fastify.register(searchRoute, {
|
|
14
|
+
entityVisibilityFilter: AllVisibleEntityFilter,
|
|
15
|
+
searchVisibilityFilter: AllVisibleSearchFilter,
|
|
16
|
+
prisma,
|
|
17
|
+
opensearch,
|
|
18
|
+
access: AllPublicAccess,
|
|
19
|
+
queryBuilder: new OpensearchQueryBuilder(),
|
|
20
|
+
});
|
|
12
21
|
});
|
|
13
22
|
|
|
14
23
|
afterEach(async () => {
|
|
@@ -102,9 +111,7 @@ describe('Search Route', () => {
|
|
|
102
111
|
// Verify database was queried with correct ids
|
|
103
112
|
expect(prisma.entity.findMany).toHaveBeenCalledWith({
|
|
104
113
|
where: {
|
|
105
|
-
id: {
|
|
106
|
-
in: ['http://example.com/entity/1', 'http://example.com/entity/2'],
|
|
107
|
-
},
|
|
114
|
+
AND: [{ id: { in: ['http://example.com/entity/1', 'http://example.com/entity/2'] } }, {}],
|
|
108
115
|
},
|
|
109
116
|
include: { file: { select: { id: true } } },
|
|
110
117
|
});
|
|
@@ -114,18 +121,25 @@ describe('Search Route', () => {
|
|
|
114
121
|
body: {
|
|
115
122
|
query: {
|
|
116
123
|
bool: {
|
|
124
|
+
filter: [{ match_all: {} }],
|
|
117
125
|
must: [
|
|
118
126
|
{
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
127
|
+
bool: {
|
|
128
|
+
must: [
|
|
129
|
+
{
|
|
130
|
+
multi_match: {
|
|
131
|
+
query: 'test',
|
|
132
|
+
fields: ['name^2', 'description'],
|
|
133
|
+
type: 'best_fields',
|
|
134
|
+
fuzziness: 'AUTO',
|
|
135
|
+
zero_terms_query: 'all',
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
filter: [],
|
|
125
140
|
},
|
|
126
141
|
},
|
|
127
142
|
],
|
|
128
|
-
filter: [],
|
|
129
143
|
},
|
|
130
144
|
},
|
|
131
145
|
aggs: {
|
|
@@ -178,16 +192,23 @@ describe('Search Route', () => {
|
|
|
178
192
|
body: {
|
|
179
193
|
query: {
|
|
180
194
|
bool: {
|
|
195
|
+
filter: [{ match_all: {} }],
|
|
181
196
|
must: [
|
|
182
197
|
{
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
198
|
+
bool: {
|
|
199
|
+
must: [
|
|
200
|
+
{
|
|
201
|
+
query_string: {
|
|
202
|
+
query: 'name:test AND description:entity',
|
|
203
|
+
fields: ['name^2', 'description'],
|
|
204
|
+
default_operator: 'AND',
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
],
|
|
208
|
+
filter: [],
|
|
187
209
|
},
|
|
188
210
|
},
|
|
189
211
|
],
|
|
190
|
-
filter: [],
|
|
191
212
|
},
|
|
192
213
|
},
|
|
193
214
|
aggs: {
|
|
@@ -254,11 +275,18 @@ describe('Search Route', () => {
|
|
|
254
275
|
expect(opensearch.search).toHaveBeenCalledWith(
|
|
255
276
|
expect.objectContaining({
|
|
256
277
|
body: expect.objectContaining({
|
|
257
|
-
query:
|
|
258
|
-
bool:
|
|
259
|
-
filter:
|
|
260
|
-
|
|
261
|
-
|
|
278
|
+
query: {
|
|
279
|
+
bool: {
|
|
280
|
+
filter: [{ match_all: {} }],
|
|
281
|
+
must: [
|
|
282
|
+
expect.objectContaining({
|
|
283
|
+
bool: expect.objectContaining({
|
|
284
|
+
filter: expectedFilters,
|
|
285
|
+
}),
|
|
286
|
+
}),
|
|
287
|
+
],
|
|
288
|
+
},
|
|
289
|
+
},
|
|
262
290
|
}),
|
|
263
291
|
}),
|
|
264
292
|
);
|
|
@@ -310,20 +338,27 @@ describe('Search Route', () => {
|
|
|
310
338
|
expect(opensearch.search).toHaveBeenCalledWith(
|
|
311
339
|
expect.objectContaining({
|
|
312
340
|
body: expect.objectContaining({
|
|
313
|
-
query:
|
|
314
|
-
bool:
|
|
315
|
-
filter: [
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
341
|
+
query: {
|
|
342
|
+
bool: {
|
|
343
|
+
filter: [{ match_all: {} }],
|
|
344
|
+
must: [
|
|
345
|
+
expect.objectContaining({
|
|
346
|
+
bool: expect.objectContaining({
|
|
347
|
+
filter: [
|
|
348
|
+
{
|
|
349
|
+
geo_bounding_box: {
|
|
350
|
+
location: {
|
|
351
|
+
top_left: { lat: 51.5, lon: 0.0 },
|
|
352
|
+
bottom_right: { lat: 51.4, lon: 0.1 },
|
|
353
|
+
},
|
|
354
|
+
},
|
|
355
|
+
},
|
|
356
|
+
],
|
|
357
|
+
}),
|
|
358
|
+
}),
|
|
324
359
|
],
|
|
325
|
-
}
|
|
326
|
-
}
|
|
360
|
+
},
|
|
361
|
+
},
|
|
327
362
|
aggs: expect.objectContaining({
|
|
328
363
|
geohash_grid: {
|
|
329
364
|
geohash_grid: {
|
|
@@ -396,7 +431,7 @@ describe('Search Route', () => {
|
|
|
396
431
|
|
|
397
432
|
expect(response.statusCode).toBe(422);
|
|
398
433
|
const body = JSON.parse(response.body) as StandardErrorResponse;
|
|
399
|
-
expect(body.error.code).toBe('
|
|
434
|
+
expect(body.error.code).toBe('VALIDATION_ERROR');
|
|
400
435
|
});
|
|
401
436
|
|
|
402
437
|
it('should handle opensearch errors', async () => {
|
|
@@ -415,6 +450,24 @@ describe('Search Route', () => {
|
|
|
415
450
|
expect(body).toMatchSnapshot();
|
|
416
451
|
});
|
|
417
452
|
|
|
453
|
+
it('should hide opensearch errors with other client statuses behind a generic 500', async () => {
|
|
454
|
+
const opensearchError = Object.assign(new Error('index_not_found_exception: no such index [entities]'), {
|
|
455
|
+
statusCode: 404,
|
|
456
|
+
});
|
|
457
|
+
opensearch.search.mockRejectedValue(opensearchError);
|
|
458
|
+
|
|
459
|
+
const response = await fastify.inject({
|
|
460
|
+
method: 'POST',
|
|
461
|
+
url: '/search',
|
|
462
|
+
payload: {
|
|
463
|
+
query: 'test',
|
|
464
|
+
},
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
expect(response.statusCode).toBe(500);
|
|
468
|
+
expect(response.json()).toEqual({ error: { code: 'INTERNAL_ERROR', message: 'Internal server error' } });
|
|
469
|
+
});
|
|
470
|
+
|
|
418
471
|
it('should return 422 for malformed opensearch query', async () => {
|
|
419
472
|
const error = new Error('parsing_exception') as Error & { statusCode: number };
|
|
420
473
|
error.statusCode = 400;
|
|
@@ -430,7 +483,7 @@ describe('Search Route', () => {
|
|
|
430
483
|
|
|
431
484
|
expect(response.statusCode).toBe(422);
|
|
432
485
|
const body = JSON.parse(response.body) as StandardErrorResponse;
|
|
433
|
-
expect(body.error.code).toBe('
|
|
486
|
+
expect(body.error.code).toBe('VALIDATION_ERROR');
|
|
434
487
|
expect(body.error.message).toBe('parsing_exception');
|
|
435
488
|
});
|
|
436
489
|
|
|
@@ -599,7 +652,7 @@ describe('Search Route', () => {
|
|
|
599
652
|
expect(response.statusCode).toBe(500);
|
|
600
653
|
const body = JSON.parse(response.body) as { error: { code: string; message: string } };
|
|
601
654
|
expect(body.error.code).toBe('INTERNAL_ERROR');
|
|
602
|
-
expect(body.error.message).toBe('
|
|
655
|
+
expect(body.error.message).toBe('Internal server error');
|
|
603
656
|
});
|
|
604
657
|
|
|
605
658
|
it('should apply custom entity transformers', async () => {
|
|
@@ -611,10 +664,13 @@ describe('Search Route', () => {
|
|
|
611
664
|
|
|
612
665
|
await fastifyBefore();
|
|
613
666
|
await fastify.register(searchRoute, {
|
|
667
|
+
entityVisibilityFilter: AllVisibleEntityFilter,
|
|
668
|
+
searchVisibilityFilter: AllVisibleSearchFilter,
|
|
614
669
|
prisma,
|
|
615
670
|
opensearch,
|
|
616
|
-
|
|
671
|
+
access: AllPublicAccess,
|
|
617
672
|
entityTransformers: [customTransformer],
|
|
673
|
+
queryBuilder: new OpensearchQueryBuilder(),
|
|
618
674
|
});
|
|
619
675
|
|
|
620
676
|
const mockEntities = [
|
package/src/routes/search.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import type { Client } from '@opensearch-project/opensearch';
|
|
2
2
|
import type { MultiBucketAggregateBaseFiltersBucket } from '@opensearch-project/opensearch/api/_types/_common.aggregations.js';
|
|
3
|
-
import type { Search_Request } from '@opensearch-project/opensearch/api/index.js';
|
|
3
|
+
import type { Search_Request, Search_Response } from '@opensearch-project/opensearch/api/index.js';
|
|
4
4
|
import type { FastifyPluginAsync } from 'fastify';
|
|
5
5
|
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
|
|
6
6
|
import { z } from 'zod';
|
|
7
7
|
import type { PrismaClient } from '../generated/prisma/client.js';
|
|
8
|
-
import {
|
|
9
|
-
import type {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
8
|
+
import { applyTransformers, authoriseEntities } from '../transformers/default.js';
|
|
9
|
+
import type { AccessFunction } from '../types/access.js';
|
|
10
|
+
import type { EntityTransformer } from '../types/transformers.js';
|
|
11
|
+
import type { EntityVisibilityFilter, SearchVisibilityFilter } from '../types/visibility.js';
|
|
12
|
+
import { createError, ERROR_CODES } from '../utils/errors.js';
|
|
13
|
+
import type { OpensearchQueryBuilder } from '../utils/queryBuilder.js';
|
|
12
14
|
|
|
13
15
|
const boundingBoxSchema = z.object({
|
|
14
16
|
topRight: z.object({
|
|
@@ -36,22 +38,23 @@ const searchParamsSchema = z.object({
|
|
|
36
38
|
type SearchRouteOptions = {
|
|
37
39
|
prisma: PrismaClient;
|
|
38
40
|
opensearch: Client;
|
|
39
|
-
|
|
41
|
+
access: AccessFunction;
|
|
40
42
|
entityTransformers?: EntityTransformer[];
|
|
41
|
-
|
|
42
|
-
|
|
43
|
+
queryBuilder: OpensearchQueryBuilder;
|
|
44
|
+
entityVisibilityFilter: EntityVisibilityFilter;
|
|
45
|
+
searchVisibilityFilter: SearchVisibilityFilter;
|
|
43
46
|
};
|
|
44
47
|
|
|
45
48
|
const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) => {
|
|
46
49
|
const {
|
|
47
50
|
prisma,
|
|
48
51
|
opensearch,
|
|
49
|
-
|
|
52
|
+
access,
|
|
50
53
|
entityTransformers = [],
|
|
51
|
-
|
|
52
|
-
|
|
54
|
+
queryBuilder,
|
|
55
|
+
entityVisibilityFilter,
|
|
56
|
+
searchVisibilityFilter,
|
|
53
57
|
} = opts;
|
|
54
|
-
const queryBuilder = new queryBuilderClass(queryBuilderOptions);
|
|
55
58
|
fastify.withTypeProvider<ZodTypeProvider>().post(
|
|
56
59
|
'/search',
|
|
57
60
|
{
|
|
@@ -62,138 +65,130 @@ const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) =>
|
|
|
62
65
|
async (request, reply) => {
|
|
63
66
|
const { searchType, query, filters, boundingBox, geohashPrecision, limit, offset, sort, order } = request.body;
|
|
64
67
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
68
|
+
const context = { request, fastify };
|
|
69
|
+
const [visibilityWhere, visibilityQuery] = await Promise.all([
|
|
70
|
+
entityVisibilityFilter(context),
|
|
71
|
+
searchVisibilityFilter(context),
|
|
72
|
+
]);
|
|
73
|
+
|
|
74
|
+
const opensearchQuery: Search_Request = {
|
|
75
|
+
index: 'entities',
|
|
76
|
+
body: {
|
|
77
|
+
query: {
|
|
78
|
+
bool: {
|
|
79
|
+
must: [queryBuilder.buildQuery(searchType, query, filters, boundingBox)],
|
|
80
|
+
filter: [visibilityQuery],
|
|
76
81
|
},
|
|
77
|
-
sort: queryBuilder.buildSort(sort, order),
|
|
78
|
-
from: offset,
|
|
79
|
-
size: limit,
|
|
80
82
|
},
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
throw new Error('Invalid search response: missing hits data');
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const entityIds = response.body.hits.hits.map((hit) => hit._source?.id as string | undefined).filter(Boolean);
|
|
90
|
-
|
|
91
|
-
const dbEntities = await prisma.entity.findMany({
|
|
92
|
-
where: {
|
|
93
|
-
id: {
|
|
94
|
-
in: entityIds,
|
|
83
|
+
aggs: queryBuilder.buildAggregations(geohashPrecision, boundingBox),
|
|
84
|
+
highlight: {
|
|
85
|
+
fields: {
|
|
86
|
+
name: {},
|
|
87
|
+
description: {},
|
|
95
88
|
},
|
|
96
89
|
},
|
|
97
|
-
|
|
98
|
-
|
|
90
|
+
sort: queryBuilder.buildSort(sort, order),
|
|
91
|
+
from: offset,
|
|
92
|
+
size: limit,
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
fastify.log.debug(opensearchQuery);
|
|
96
|
+
let response: Search_Response;
|
|
97
|
+
try {
|
|
98
|
+
response = await opensearch.search(opensearchQuery);
|
|
99
|
+
} catch (error) {
|
|
100
|
+
const { statusCode, message } = error as Error & { statusCode?: number };
|
|
101
|
+
// OpenSearch returns 400 for malformed queries (e.g. invalid query_string syntax)
|
|
102
|
+
if (statusCode !== 400) {
|
|
103
|
+
throw error;
|
|
104
|
+
}
|
|
99
105
|
|
|
100
|
-
|
|
106
|
+
fastify.log.warn(`Invalid search query: ${message}`);
|
|
101
107
|
|
|
102
|
-
|
|
103
|
-
|
|
108
|
+
return reply.code(422).send(createError(ERROR_CODES.VALIDATION_ERROR, message));
|
|
109
|
+
}
|
|
104
110
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
throw new Error('Missing id in search hit');
|
|
109
|
-
}
|
|
111
|
+
if (!response.body?.hits?.hits) {
|
|
112
|
+
throw new Error('Invalid search response: missing hits data');
|
|
113
|
+
}
|
|
110
114
|
|
|
111
|
-
|
|
112
|
-
if (!dbEntity) {
|
|
113
|
-
fastify.log.warn(`Entity ${hit._source.id} found in OpenSearch but not in database`);
|
|
115
|
+
const entityIds = response.body.hits.hits.map((hit) => hit._source?.id as string | undefined).filter(Boolean);
|
|
114
116
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
+
const dbEntities = await prisma.entity.findMany({
|
|
118
|
+
where: {
|
|
119
|
+
AND: [{ id: { in: entityIds } }, visibilityWhere],
|
|
120
|
+
},
|
|
121
|
+
include: { file: { select: { id: true } } },
|
|
122
|
+
});
|
|
117
123
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
...base,
|
|
121
|
-
memberOf: base.memberOf ? (refMap.get(base.memberOf) ?? null) : null,
|
|
122
|
-
rootCollection: base.rootCollection ? (refMap.get(base.rootCollection) ?? null) : null,
|
|
123
|
-
};
|
|
124
|
-
const authorisedEntity = await accessTransformer(standardEntity, { request, fastify });
|
|
125
|
-
|
|
126
|
-
let result = authorisedEntity;
|
|
127
|
-
for (const transformer of entityTransformers) {
|
|
128
|
-
result = await transformer(result, { request, fastify });
|
|
129
|
-
}
|
|
124
|
+
const authorisedEntities = await authoriseEntities(dbEntities, prisma, access, context);
|
|
125
|
+
const entityMap = new Map(authorisedEntities.map((entity) => [entity.id, entity]));
|
|
130
126
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
).then((results) => results.filter(Boolean));
|
|
141
|
-
|
|
142
|
-
const facets: Record<string, Array<{ name: string; count: number }>> = {};
|
|
143
|
-
if (response.body.aggregations) {
|
|
144
|
-
Object.keys(response.body.aggregations).forEach((key) => {
|
|
145
|
-
if (key !== 'geohash_grid') {
|
|
146
|
-
const agg = response.body.aggregations?.[key] as MultiBucketAggregateBaseFiltersBucket;
|
|
147
|
-
if (agg?.buckets && Array.isArray(agg.buckets)) {
|
|
148
|
-
facets[key] = agg.buckets.map((bucket) => ({
|
|
149
|
-
name: bucket.key,
|
|
150
|
-
count: bucket.doc_count,
|
|
151
|
-
}));
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
});
|
|
155
|
-
}
|
|
127
|
+
const entities = await Promise.all(
|
|
128
|
+
response.body.hits.hits.map(async (hit) => {
|
|
129
|
+
if (!hit._source?.id) {
|
|
130
|
+
throw new Error('Missing id in search hit');
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const authorisedEntity = entityMap.get(hit._source.id);
|
|
134
|
+
if (!authorisedEntity) {
|
|
135
|
+
fastify.log.warn(`Entity ${hit._source.id} found in OpenSearch but missing or not visible in database`);
|
|
156
136
|
|
|
157
|
-
|
|
158
|
-
if (response.body.aggregations?.geohash_grid) {
|
|
159
|
-
const geohashAgg = response.body.aggregations.geohash_grid as MultiBucketAggregateBaseFiltersBucket;
|
|
160
|
-
if (geohashAgg?.buckets && Array.isArray(geohashAgg.buckets)) {
|
|
161
|
-
geohashAgg.buckets.forEach((bucket) => {
|
|
162
|
-
geohashGrid ||= {};
|
|
163
|
-
geohashGrid[bucket.key] = bucket.doc_count;
|
|
164
|
-
});
|
|
137
|
+
return null;
|
|
165
138
|
}
|
|
166
|
-
}
|
|
167
139
|
|
|
168
|
-
|
|
169
|
-
const total =
|
|
170
|
-
typeof response.body.hits.total === 'number'
|
|
171
|
-
? response.body.hits.total
|
|
172
|
-
: response.body.hits.total?.value || 0;
|
|
173
|
-
|
|
174
|
-
const result = {
|
|
175
|
-
total,
|
|
176
|
-
searchTime: response.body.took,
|
|
177
|
-
entities,
|
|
178
|
-
facets: Object.keys(facets).length > 0 ? facets : undefined,
|
|
179
|
-
geohashGrid,
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
return result;
|
|
183
|
-
} catch (error) {
|
|
184
|
-
const err = error as Error;
|
|
140
|
+
const result = await applyTransformers(authorisedEntity, entityTransformers, context);
|
|
185
141
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
142
|
+
// Add search-specific metadata
|
|
143
|
+
return {
|
|
144
|
+
...(result as Record<string, unknown>),
|
|
145
|
+
searchExtra: {
|
|
146
|
+
score: hit._score,
|
|
147
|
+
highlight: hit.highlight,
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
}),
|
|
151
|
+
).then((results) => results.filter(Boolean));
|
|
152
|
+
|
|
153
|
+
const facets: Record<string, Array<{ name: string; count: number }>> = {};
|
|
154
|
+
if (response.body.aggregations) {
|
|
155
|
+
Object.keys(response.body.aggregations).forEach((key) => {
|
|
156
|
+
if (key !== 'geohash_grid') {
|
|
157
|
+
const agg = response.body.aggregations?.[key] as MultiBucketAggregateBaseFiltersBucket;
|
|
158
|
+
if (agg?.buckets && Array.isArray(agg.buckets)) {
|
|
159
|
+
facets[key] = agg.buckets.map((bucket) => ({
|
|
160
|
+
name: bucket.key,
|
|
161
|
+
count: bucket.doc_count,
|
|
162
|
+
}));
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
}
|
|
189
167
|
|
|
190
|
-
|
|
168
|
+
let geohashGrid: Record<string, number> | undefined;
|
|
169
|
+
if (response.body.aggregations?.geohash_grid) {
|
|
170
|
+
const geohashAgg = response.body.aggregations.geohash_grid as MultiBucketAggregateBaseFiltersBucket;
|
|
171
|
+
if (geohashAgg?.buckets && Array.isArray(geohashAgg.buckets)) {
|
|
172
|
+
geohashAgg.buckets.forEach((bucket) => {
|
|
173
|
+
geohashGrid ||= {};
|
|
174
|
+
geohashGrid[bucket.key] = bucket.doc_count;
|
|
175
|
+
});
|
|
191
176
|
}
|
|
177
|
+
}
|
|
192
178
|
|
|
193
|
-
|
|
179
|
+
/* v8 ignore next 2 -- Not sure how to force opensearch to hit the other path -- @preserve */
|
|
180
|
+
const total =
|
|
181
|
+
typeof response.body.hits.total === 'number' ? response.body.hits.total : response.body.hits.total?.value || 0;
|
|
194
182
|
|
|
195
|
-
|
|
196
|
-
|
|
183
|
+
const result = {
|
|
184
|
+
total,
|
|
185
|
+
searchTime: response.body.took,
|
|
186
|
+
entities,
|
|
187
|
+
facets: Object.keys(facets).length > 0 ? facets : undefined,
|
|
188
|
+
geohashGrid,
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
return result;
|
|
197
192
|
},
|
|
198
193
|
);
|
|
199
194
|
};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { Client } from '@opensearch-project/opensearch';
|
|
2
2
|
import type { FastifyInstance } from 'fastify';
|
|
3
3
|
import Fastify from 'fastify';
|
|
4
|
-
import { serializerCompiler, validatorCompiler } from 'fastify-type-provider-zod';
|
|
5
4
|
import { mockDeep, mockReset } from 'vitest-mock-extended';
|
|
6
5
|
|
|
7
6
|
import type { PrismaClient } from '../../generated/prisma/client.js';
|
|
7
|
+
import { setupValidation } from '../../utils/validation.js';
|
|
8
8
|
|
|
9
9
|
export let fastify: FastifyInstance;
|
|
10
10
|
export const prisma = mockDeep<PrismaClient>();
|
|
@@ -16,8 +16,7 @@ export const fastifyBefore = async () => {
|
|
|
16
16
|
|
|
17
17
|
fastify = Fastify({ logger: false });
|
|
18
18
|
|
|
19
|
-
fastify
|
|
20
|
-
fastify.setSerializerCompiler(serializerCompiler);
|
|
19
|
+
setupValidation(fastify);
|
|
21
20
|
|
|
22
21
|
return fastify;
|
|
23
22
|
};
|
|
@@ -2,7 +2,8 @@ import { Client } from '@opensearch-project/opensearch';
|
|
|
2
2
|
import { PrismaMariaDb } from '@prisma/adapter-mariadb';
|
|
3
3
|
import type { FastifyInstance } from 'fastify';
|
|
4
4
|
import Fastify from 'fastify';
|
|
5
|
-
import
|
|
5
|
+
import type { Options } from '../app.js';
|
|
6
|
+
import app, { AllPublicAccess, AllVisibleEntityFilter, AllVisibleSearchFilter } from '../app.js';
|
|
6
7
|
import { PrismaClient } from '../generated/prisma/client.js';
|
|
7
8
|
|
|
8
9
|
let fastify: FastifyInstance;
|
|
@@ -32,13 +33,18 @@ export async function setupIntegrationTests() {
|
|
|
32
33
|
node: process.env.OPENSEARCH_URL,
|
|
33
34
|
});
|
|
34
35
|
|
|
35
|
-
fastify =
|
|
36
|
+
fastify = await buildTestApp();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function buildTestApp(overrides: Partial<Options> = {}) {
|
|
40
|
+
const instance = Fastify({ logger: false });
|
|
36
41
|
|
|
37
|
-
await
|
|
42
|
+
await instance.register(app, {
|
|
38
43
|
prisma,
|
|
39
44
|
opensearch,
|
|
40
|
-
|
|
41
|
-
|
|
45
|
+
access: AllPublicAccess,
|
|
46
|
+
entityVisibilityFilter: AllVisibleEntityFilter,
|
|
47
|
+
searchVisibilityFilter: AllVisibleSearchFilter,
|
|
42
48
|
fileHandler: {
|
|
43
49
|
get: async () => {
|
|
44
50
|
throw new Error('File handler not implemented in integration tests');
|
|
@@ -55,9 +61,12 @@ export async function setupIntegrationTests() {
|
|
|
55
61
|
throw new Error('RO-Crate head handler not implemented in integration tests');
|
|
56
62
|
},
|
|
57
63
|
},
|
|
64
|
+
...overrides,
|
|
58
65
|
});
|
|
59
66
|
|
|
60
|
-
await
|
|
67
|
+
await instance.ready();
|
|
68
|
+
|
|
69
|
+
return instance;
|
|
61
70
|
}
|
|
62
71
|
|
|
63
72
|
export async function teardownIntegrationTests() {
|
|
@@ -103,6 +112,7 @@ export async function seedTestData() {
|
|
|
103
112
|
rootCollection: 'http://example.com/entity/1',
|
|
104
113
|
metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
105
114
|
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
115
|
+
meta: { draft: true },
|
|
106
116
|
createdAt: new Date(),
|
|
107
117
|
updatedAt: new Date(),
|
|
108
118
|
},
|
|
@@ -126,7 +136,7 @@ export async function seedTestData() {
|
|
|
126
136
|
memberOf: 'http://example.com/entity/2',
|
|
127
137
|
rootCollection: 'http://example.com/entity/1',
|
|
128
138
|
metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
129
|
-
contentLicenseId: 'https://
|
|
139
|
+
contentLicenseId: 'https://example.com/licences/restricted',
|
|
130
140
|
createdAt: new Date(),
|
|
131
141
|
updatedAt: new Date(),
|
|
132
142
|
},
|
|
@@ -139,6 +149,7 @@ export async function seedTestData() {
|
|
|
139
149
|
rootCollection: 'http://example.com/entity/1',
|
|
140
150
|
metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
141
151
|
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
152
|
+
meta: { draft: true },
|
|
142
153
|
createdAt: new Date(),
|
|
143
154
|
updatedAt: new Date(),
|
|
144
155
|
},
|