arocapi 1.1.0 → 1.2.2
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/LICENSE.md +5 -0
- package/README.md +459 -36
- package/dist/app.d.ts +19 -1
- package/dist/app.js +37 -29
- package/dist/app.js.map +1 -1
- package/dist/app.test.js +124 -0
- package/dist/app.test.js.map +1 -0
- package/dist/index.dev.js +146 -0
- package/dist/index.dev.js.map +1 -0
- package/dist/routes/crate.d.ts +7 -0
- package/dist/routes/crate.js +101 -0
- package/dist/routes/crate.js.map +1 -0
- package/dist/routes/crate.test.d.ts +1 -0
- package/dist/routes/crate.test.js +360 -0
- package/dist/routes/crate.test.js.map +1 -0
- package/dist/routes/entities.d.ts +6 -1
- package/dist/routes/entities.js +52 -39
- package/dist/routes/entities.js.map +1 -1
- package/dist/routes/entities.test.d.ts +1 -0
- package/dist/routes/entities.test.js +245 -0
- package/dist/routes/entities.test.js.map +1 -0
- package/dist/routes/entity.d.ts +6 -1
- package/dist/routes/entity.js +30 -8
- package/dist/routes/entity.js.map +1 -1
- package/dist/routes/entity.test.d.ts +1 -0
- package/dist/routes/entity.test.js +109 -0
- package/dist/routes/entity.test.js.map +1 -0
- package/dist/routes/file.d.ts +7 -0
- package/dist/routes/file.js +113 -0
- package/dist/routes/file.js.map +1 -0
- package/dist/routes/file.test.d.ts +1 -0
- package/dist/routes/file.test.js +319 -0
- package/dist/routes/file.test.js.map +1 -0
- package/dist/routes/files.d.ts +8 -0
- package/dist/routes/files.js +62 -0
- package/dist/routes/files.js.map +1 -0
- package/dist/routes/files.test.d.ts +1 -0
- package/dist/routes/files.test.js +244 -0
- package/dist/routes/files.test.js.map +1 -0
- package/dist/routes/search.d.ts +6 -1
- package/dist/routes/search.js +44 -19
- package/dist/routes/search.js.map +1 -1
- package/dist/routes/search.test.d.ts +1 -0
- package/dist/routes/search.test.js +698 -0
- package/dist/routes/search.test.js.map +1 -0
- package/dist/test/helpers/fastify.d.ts +9 -0
- package/dist/test/helpers/fastify.js +24 -0
- package/dist/test/helpers/fastify.js.map +1 -0
- package/dist/test/integration.setup.d.ts +58 -0
- package/dist/test/integration.setup.js +181 -0
- package/dist/test/integration.setup.js.map +1 -0
- package/dist/test/integration.test.d.ts +1 -0
- package/dist/test/integration.test.js +337 -0
- package/dist/test/integration.test.js.map +1 -0
- package/dist/transformers/default.d.ts +45 -0
- package/dist/transformers/default.js +46 -0
- package/dist/transformers/default.js.map +1 -0
- package/dist/transformers/default.test.d.ts +1 -0
- package/dist/transformers/default.test.js +177 -0
- package/dist/transformers/default.test.js.map +1 -0
- package/dist/transformers/transformer.test.d.ts +1 -0
- package/dist/transformers/transformer.test.js +176 -0
- package/dist/transformers/transformer.test.js.map +1 -0
- package/dist/types/fileHandlers.d.ts +41 -0
- package/dist/types/fileHandlers.js +2 -0
- package/dist/types/fileHandlers.js.map +1 -0
- package/dist/types/transformers.d.ts +10 -0
- package/dist/types/transformers.js +2 -0
- package/dist/types/transformers.js.map +1 -0
- package/dist/utils/errors.d.ts +23 -0
- package/dist/utils/errors.js +21 -0
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/errors.test.d.ts +1 -0
- package/dist/utils/errors.test.js +66 -0
- package/dist/utils/errors.test.js.map +1 -0
- package/package.json +35 -28
- package/prisma/migrations/20250827033956_record_type_scalar/migration.sql +10 -0
- package/prisma/migrations/20250923032753_api_refactor/migration.sql +39 -0
- package/prisma/migrations/20250923223119_entity_type_to_string/migration.sql +8 -0
- package/prisma/migrations/20251020140329_add_meta_column/migration.sql +2 -0
- package/prisma/migrations/20251027010713_add_file_table/migration.sql +17 -0
- package/prisma/migrations/20251027221138_add_file_id_to_entity/migration.sql +2 -0
- package/prisma/models/entity.prisma +16 -12
- package/prisma/models/file.prisma +20 -0
- package/prisma.config.ts +6 -1
- package/src/__snapshots__/app.test.ts.snap +8 -0
- package/src/app.test.ts +158 -0
- package/src/app.ts +99 -36
- package/src/index.dev.ts +196 -0
- package/src/reset.d.ts +1 -0
- package/src/routes/__snapshots__/entities.test.ts.snap +68 -0
- package/src/routes/__snapshots__/entity.test.ts.snap +57 -0
- package/src/routes/__snapshots__/search.test.ts.snap +139 -0
- package/src/routes/crate.test.ts +441 -0
- package/src/routes/crate.ts +145 -0
- package/src/routes/entities.test.ts +284 -0
- package/src/routes/entities.ts +69 -48
- package/src/routes/entity.test.ts +132 -0
- package/src/routes/entity.ts +59 -22
- package/src/routes/file.test.ts +399 -0
- package/src/routes/file.ts +157 -0
- package/src/routes/files.test.ts +303 -0
- package/src/routes/files.ts +89 -0
- package/src/routes/search.test.ts +807 -0
- package/src/routes/search.ts +67 -25
- package/src/test/__snapshots__/integration.test.ts.snap +18 -0
- package/src/test/helpers/fastify.ts +39 -0
- package/src/test/integration.setup.ts +200 -0
- package/src/test/integration.test.ts +411 -0
- package/src/transformers/default.test.ts +199 -0
- package/src/transformers/default.ts +165 -0
- package/src/transformers/transformer.test.ts +233 -0
- package/src/types/fileHandlers.ts +65 -0
- package/src/types/transformers.ts +52 -0
- package/src/utils/errors.test.ts +75 -0
- package/src/utils/errors.ts +45 -0
- package/dist/index.js +0 -25
- package/dist/index.js.map +0 -1
- package/dist/scripts/loadEntities.js +0 -170
- package/dist/scripts/loadEntities.js.map +0 -1
- package/dist/type.d.ts +0 -6
- package/dist/type.js +0 -2
- package/dist/type.js.map +0 -1
- package/src/index.ts +0 -35
- package/src/scripts/loadEntities.ts +0 -223
- package/src/type.ts +0 -7
- /package/dist/{scripts/loadEntities.d.ts → app.test.d.ts} +0 -0
- /package/dist/{index.d.ts → index.dev.d.ts} +0 -0
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
2
|
+
import type { FileAccessTransformer, FileTransformer } from '../app.js';
|
|
3
|
+
import { fastify, fastifyAfter, fastifyBefore, prisma } from '../test/helpers/fastify.js';
|
|
4
|
+
import { AllPublicFileAccessTransformer } from '../transformers/default.js';
|
|
5
|
+
import type { StandardErrorResponse } from '../utils/errors.js';
|
|
6
|
+
import filesRoute from './files.js';
|
|
7
|
+
|
|
8
|
+
describe('Files Route', () => {
|
|
9
|
+
beforeEach(async () => {
|
|
10
|
+
await fastifyBefore();
|
|
11
|
+
await fastify.register(filesRoute, { fileAccessTransformer: AllPublicFileAccessTransformer });
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
afterEach(async () => {
|
|
15
|
+
await fastifyAfter();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const mockFile1 = {
|
|
19
|
+
id: 1,
|
|
20
|
+
fileId: 'http://example.com/file1.wav',
|
|
21
|
+
filename: 'file1.wav',
|
|
22
|
+
mediaType: 'audio/wav',
|
|
23
|
+
size: BigInt(1024),
|
|
24
|
+
memberOf: 'http://example.com/collection/1',
|
|
25
|
+
rootCollection: 'http://example.com/collection/1',
|
|
26
|
+
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
27
|
+
meta: {},
|
|
28
|
+
createdAt: new Date('2025-01-01'),
|
|
29
|
+
updatedAt: new Date('2025-01-01'),
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const mockFile2 = {
|
|
33
|
+
id: 2,
|
|
34
|
+
fileId: 'http://example.com/file2.txt',
|
|
35
|
+
filename: 'file2.txt',
|
|
36
|
+
mediaType: 'text/plain',
|
|
37
|
+
size: BigInt(512),
|
|
38
|
+
memberOf: 'http://example.com/collection/1',
|
|
39
|
+
rootCollection: 'http://example.com/collection/1',
|
|
40
|
+
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
41
|
+
meta: {},
|
|
42
|
+
createdAt: new Date('2025-01-02'),
|
|
43
|
+
updatedAt: new Date('2025-01-02'),
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
describe('GET /files', () => {
|
|
47
|
+
it('should list all files successfully', async () => {
|
|
48
|
+
prisma.file.findMany.mockResolvedValue([mockFile1, mockFile2]);
|
|
49
|
+
prisma.file.count.mockResolvedValue(2);
|
|
50
|
+
|
|
51
|
+
const response = await fastify.inject({
|
|
52
|
+
method: 'GET',
|
|
53
|
+
url: '/files',
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
expect(response.statusCode).toBe(200);
|
|
57
|
+
const body = JSON.parse(response.body) as { total: number; files: unknown[] };
|
|
58
|
+
expect(body.total).toBe(2);
|
|
59
|
+
expect(body.files).toHaveLength(2);
|
|
60
|
+
expect(body.files[0]).toMatchObject({
|
|
61
|
+
id: 'http://example.com/file1.wav',
|
|
62
|
+
filename: 'file1.wav',
|
|
63
|
+
mediaType: 'audio/wav',
|
|
64
|
+
size: 1024,
|
|
65
|
+
memberOf: 'http://example.com/collection/1',
|
|
66
|
+
rootCollection: 'http://example.com/collection/1',
|
|
67
|
+
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
68
|
+
access: {
|
|
69
|
+
content: true,
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
expect(body.files[0]).not.toHaveProperty('metadataLicenseId');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should filter files by memberOf', async () => {
|
|
76
|
+
prisma.file.findMany.mockResolvedValue([mockFile1]);
|
|
77
|
+
prisma.file.count.mockResolvedValue(1);
|
|
78
|
+
|
|
79
|
+
const response = await fastify.inject({
|
|
80
|
+
method: 'GET',
|
|
81
|
+
url: '/files?memberOf=http://example.com/collection/1',
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
expect(response.statusCode).toBe(200);
|
|
85
|
+
const body = JSON.parse(response.body) as { total: number };
|
|
86
|
+
expect(body.total).toBe(1);
|
|
87
|
+
expect(prisma.file.findMany).toHaveBeenCalledWith(
|
|
88
|
+
expect.objectContaining({
|
|
89
|
+
where: { memberOf: 'http://example.com/collection/1' },
|
|
90
|
+
}),
|
|
91
|
+
);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('should support pagination with limit and offset', async () => {
|
|
95
|
+
prisma.file.findMany.mockResolvedValue([mockFile2]);
|
|
96
|
+
prisma.file.count.mockResolvedValue(2);
|
|
97
|
+
|
|
98
|
+
const response = await fastify.inject({
|
|
99
|
+
method: 'GET',
|
|
100
|
+
url: '/files?limit=1&offset=1',
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
expect(response.statusCode).toBe(200);
|
|
104
|
+
const body = JSON.parse(response.body) as { total: number; files: unknown[] };
|
|
105
|
+
expect(body.total).toBe(2);
|
|
106
|
+
expect(body.files).toHaveLength(1);
|
|
107
|
+
expect(prisma.file.findMany).toHaveBeenCalledWith(
|
|
108
|
+
expect.objectContaining({
|
|
109
|
+
skip: 1,
|
|
110
|
+
take: 1,
|
|
111
|
+
}),
|
|
112
|
+
);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('should support sorting by filename ascending', async () => {
|
|
116
|
+
prisma.file.findMany.mockResolvedValue([mockFile1, mockFile2]);
|
|
117
|
+
prisma.file.count.mockResolvedValue(2);
|
|
118
|
+
|
|
119
|
+
const response = await fastify.inject({
|
|
120
|
+
method: 'GET',
|
|
121
|
+
url: '/files?sort=filename&order=asc',
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
expect(response.statusCode).toBe(200);
|
|
125
|
+
expect(prisma.file.findMany).toHaveBeenCalledWith(
|
|
126
|
+
expect.objectContaining({
|
|
127
|
+
orderBy: { filename: 'asc' },
|
|
128
|
+
}),
|
|
129
|
+
);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('should support sorting by id (maps to fileId)', async () => {
|
|
133
|
+
prisma.file.findMany.mockResolvedValue([mockFile1, mockFile2]);
|
|
134
|
+
prisma.file.count.mockResolvedValue(2);
|
|
135
|
+
|
|
136
|
+
const response = await fastify.inject({
|
|
137
|
+
method: 'GET',
|
|
138
|
+
url: '/files?sort=id&order=desc',
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
expect(response.statusCode).toBe(200);
|
|
142
|
+
expect(prisma.file.findMany).toHaveBeenCalledWith(
|
|
143
|
+
expect.objectContaining({
|
|
144
|
+
orderBy: { fileId: 'desc' },
|
|
145
|
+
}),
|
|
146
|
+
);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('should return empty list when no files found', async () => {
|
|
150
|
+
prisma.file.findMany.mockResolvedValue([]);
|
|
151
|
+
prisma.file.count.mockResolvedValue(0);
|
|
152
|
+
|
|
153
|
+
const response = await fastify.inject({
|
|
154
|
+
method: 'GET',
|
|
155
|
+
url: '/files',
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
expect(response.statusCode).toBe(200);
|
|
159
|
+
const body = JSON.parse(response.body) as { total: number; files: unknown[] };
|
|
160
|
+
expect(body.total).toBe(0);
|
|
161
|
+
expect(body.files).toHaveLength(0);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('should not include metadataLicenseId in response', async () => {
|
|
165
|
+
prisma.file.findMany.mockResolvedValue([mockFile1]);
|
|
166
|
+
prisma.file.count.mockResolvedValue(1);
|
|
167
|
+
|
|
168
|
+
const response = await fastify.inject({
|
|
169
|
+
method: 'GET',
|
|
170
|
+
url: '/files',
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
expect(response.statusCode).toBe(200);
|
|
174
|
+
const body = JSON.parse(response.body) as { files: Array<{ memberOf: string }> };
|
|
175
|
+
expect(body.files[0]).toMatchObject({
|
|
176
|
+
id: 'http://example.com/file1.wav',
|
|
177
|
+
filename: 'file1.wav',
|
|
178
|
+
memberOf: 'http://example.com/collection/1',
|
|
179
|
+
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
180
|
+
});
|
|
181
|
+
expect(body.files[0]).not.toHaveProperty('metadataLicenseId');
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('should return 500 when database error occurs', async () => {
|
|
185
|
+
prisma.file.findMany.mockRejectedValue(new Error('Database connection failed'));
|
|
186
|
+
|
|
187
|
+
const response = await fastify.inject({
|
|
188
|
+
method: 'GET',
|
|
189
|
+
url: '/files',
|
|
190
|
+
});
|
|
191
|
+
const body = JSON.parse(response.body) as { error: { code: string } };
|
|
192
|
+
|
|
193
|
+
expect(response.statusCode).toBe(500);
|
|
194
|
+
expect(body.error.code).toBe('INTERNAL_ERROR');
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('should validate limit parameter', async () => {
|
|
198
|
+
const response = await fastify.inject({
|
|
199
|
+
method: 'GET',
|
|
200
|
+
url: '/files?limit=2000',
|
|
201
|
+
});
|
|
202
|
+
const body = JSON.parse(response.body) as StandardErrorResponse;
|
|
203
|
+
|
|
204
|
+
expect(response.statusCode).toBe(400);
|
|
205
|
+
expect(body.error).toBe('Bad Request');
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it('should validate memberOf parameter format', async () => {
|
|
209
|
+
const response = await fastify.inject({
|
|
210
|
+
method: 'GET',
|
|
211
|
+
url: '/files?memberOf=invalid-url',
|
|
212
|
+
});
|
|
213
|
+
const body = JSON.parse(response.body) as StandardErrorResponse;
|
|
214
|
+
|
|
215
|
+
expect(response.statusCode).toBe(400);
|
|
216
|
+
expect(body.error).toBe('Bad Request');
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('should validate sort parameter', async () => {
|
|
220
|
+
const response = await fastify.inject({
|
|
221
|
+
method: 'GET',
|
|
222
|
+
url: '/files?sort=invalid',
|
|
223
|
+
});
|
|
224
|
+
const body = JSON.parse(response.body) as StandardErrorResponse;
|
|
225
|
+
|
|
226
|
+
expect(response.statusCode).toBe(400);
|
|
227
|
+
expect(body.error).toBe('Bad Request');
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
describe('Custom File Transformers', () => {
|
|
232
|
+
it('should apply custom file transformers', async () => {
|
|
233
|
+
await fastifyBefore();
|
|
234
|
+
|
|
235
|
+
// Custom transformer that adds extra fields
|
|
236
|
+
const customTransformer: FileTransformer = (file) => ({
|
|
237
|
+
...file,
|
|
238
|
+
customField: 'test-value',
|
|
239
|
+
uppercaseFilename: file.filename.toUpperCase(),
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
await fastify.register(filesRoute, {
|
|
243
|
+
fileAccessTransformer: AllPublicFileAccessTransformer,
|
|
244
|
+
fileTransformers: [customTransformer],
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
prisma.file.findMany.mockResolvedValue([mockFile1]);
|
|
248
|
+
prisma.file.count.mockResolvedValue(1);
|
|
249
|
+
|
|
250
|
+
const response = await fastify.inject({
|
|
251
|
+
method: 'GET',
|
|
252
|
+
url: '/files',
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
expect(response.statusCode).toBe(200);
|
|
256
|
+
const body = JSON.parse(response.body) as { files: unknown[] };
|
|
257
|
+
expect(body.files[0]).toMatchObject({
|
|
258
|
+
id: 'http://example.com/file1.wav',
|
|
259
|
+
filename: 'file1.wav',
|
|
260
|
+
customField: 'test-value',
|
|
261
|
+
uppercaseFilename: 'FILE1.WAV',
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
await fastifyAfter();
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it('should apply custom file access transformer', async () => {
|
|
268
|
+
await fastifyBefore();
|
|
269
|
+
|
|
270
|
+
// Custom access transformer that restricts content access
|
|
271
|
+
const customFileAccessTransformer: FileAccessTransformer = (file) => ({
|
|
272
|
+
...file,
|
|
273
|
+
access: {
|
|
274
|
+
content: false,
|
|
275
|
+
contentAuthorizationUrl: 'https://example.com/request-access',
|
|
276
|
+
},
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
await fastify.register(filesRoute, {
|
|
280
|
+
fileAccessTransformer: customFileAccessTransformer,
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
prisma.file.findMany.mockResolvedValue([mockFile1]);
|
|
284
|
+
prisma.file.count.mockResolvedValue(1);
|
|
285
|
+
|
|
286
|
+
const response = await fastify.inject({
|
|
287
|
+
method: 'GET',
|
|
288
|
+
url: '/files',
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
expect(response.statusCode).toBe(200);
|
|
292
|
+
const body = JSON.parse(response.body) as { files: unknown[] };
|
|
293
|
+
expect(body.files[0]).toMatchObject({
|
|
294
|
+
access: {
|
|
295
|
+
content: false,
|
|
296
|
+
contentAuthorizationUrl: 'https://example.com/request-access',
|
|
297
|
+
},
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
await fastifyAfter();
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
});
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { FastifyPluginAsync } from 'fastify';
|
|
2
|
+
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
|
|
3
|
+
import { z } from 'zod/v4';
|
|
4
|
+
import { baseFileTransformer } from '../transformers/default.js';
|
|
5
|
+
import type { FileAccessTransformer, FileTransformer } from '../types/transformers.js';
|
|
6
|
+
import { createInternalError } from '../utils/errors.js';
|
|
7
|
+
|
|
8
|
+
const querySchema = z.object({
|
|
9
|
+
memberOf: z.url().optional(),
|
|
10
|
+
limit: z.coerce.number().int().min(1).max(1000).default(100),
|
|
11
|
+
offset: z.coerce.number().int().min(0).default(0),
|
|
12
|
+
sort: z.enum(['id', 'filename', 'createdAt', 'updatedAt']).default('id'),
|
|
13
|
+
order: z.enum(['asc', 'desc']).default('asc'),
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
type FilesRouteOptions = {
|
|
17
|
+
fileAccessTransformer: FileAccessTransformer;
|
|
18
|
+
fileTransformers?: FileTransformer[];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const files: FastifyPluginAsync<FilesRouteOptions> = async (fastify, opts) => {
|
|
22
|
+
const { fileAccessTransformer, fileTransformers = [] } = opts;
|
|
23
|
+
|
|
24
|
+
fastify.withTypeProvider<ZodTypeProvider>().get(
|
|
25
|
+
'/files',
|
|
26
|
+
{
|
|
27
|
+
schema: {
|
|
28
|
+
querystring: querySchema,
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
async (request, reply) => {
|
|
32
|
+
const { memberOf, limit, offset, sort, order } = request.query;
|
|
33
|
+
|
|
34
|
+
try {
|
|
35
|
+
const where: NonNullable<Parameters<typeof fastify.prisma.file.findMany>[0]>['where'] = {};
|
|
36
|
+
|
|
37
|
+
if (memberOf) {
|
|
38
|
+
where.memberOf = memberOf;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const sortField = sort === 'id' ? 'fileId' : sort;
|
|
42
|
+
|
|
43
|
+
const dbFiles = await fastify.prisma.file.findMany({
|
|
44
|
+
where,
|
|
45
|
+
orderBy: {
|
|
46
|
+
[sortField]: order,
|
|
47
|
+
},
|
|
48
|
+
skip: offset,
|
|
49
|
+
take: limit,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const total = await fastify.prisma.file.count({ where });
|
|
53
|
+
|
|
54
|
+
// Apply transformers to each entity: base -> access -> additional
|
|
55
|
+
const filesWithAccess = await Promise.all(
|
|
56
|
+
dbFiles.map(async (dbFile) => {
|
|
57
|
+
const standardFile = baseFileTransformer(dbFile);
|
|
58
|
+
const authorisedFile = await fileAccessTransformer(standardFile, {
|
|
59
|
+
request,
|
|
60
|
+
fastify,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
let result = authorisedFile;
|
|
64
|
+
for (const transformer of fileTransformers) {
|
|
65
|
+
result = await transformer(result, {
|
|
66
|
+
request,
|
|
67
|
+
fastify,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return result;
|
|
72
|
+
}),
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
total,
|
|
77
|
+
files: filesWithAccess,
|
|
78
|
+
};
|
|
79
|
+
} catch (error) {
|
|
80
|
+
const err = error as Error;
|
|
81
|
+
fastify.log.error(`Database error: ${err.message}`);
|
|
82
|
+
|
|
83
|
+
return reply.code(500).send(createInternalError());
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export default files;
|