@tmlmobilidade/controllers 20260408.1128.43 → 20260410.1049.29

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.
@@ -0,0 +1,28 @@
1
+ import { type FastifyReply, type FastifyRequest } from '@tmlmobilidade/fastify';
2
+ import { type CreateFileExportDto, type FileExport } from '@tmlmobilidade/types';
3
+ export declare class ExporterSharedController {
4
+ /**
5
+ * Returns an Agency by ID.
6
+ * @param request The request object
7
+ * @param reply The reply object
8
+ */
9
+ static create(request: FastifyRequest<{
10
+ Body: CreateFileExportDto<any>;
11
+ }>, reply: FastifyReply<FileExport>): Promise<never>;
12
+ /**
13
+ * Downloads a FileExport by ID.
14
+ * @param request The request object
15
+ * @param reply The reply object
16
+ */
17
+ static download(request: FastifyRequest<{
18
+ Params: {
19
+ id: string;
20
+ };
21
+ }>, reply: FastifyReply<string>): Promise<never>;
22
+ /**
23
+ * Returns all FileExport sorted by ID.
24
+ * @param request The request object
25
+ * @param reply The reply object
26
+ */
27
+ static getAll(request: FastifyRequest, reply: FastifyReply<FileExport[]>): Promise<never>;
28
+ }
@@ -0,0 +1,57 @@
1
+ /* * */
2
+ import { HTTP_STATUS, HttpException } from '@tmlmobilidade/consts';
3
+ import { fileExports, files } from '@tmlmobilidade/interfaces';
4
+ /* * */
5
+ export class ExporterSharedController {
6
+ //
7
+ /**
8
+ * Returns an Agency by ID.
9
+ * @param request The request object
10
+ * @param reply The reply object
11
+ */
12
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
13
+ static async create(request, reply) {
14
+ const fileExportData = await fileExports.insertOne({ ...request.body, created_by: request.me._id, updated_by: request.me._id });
15
+ return reply.send({ data: fileExportData, error: null, statusCode: HTTP_STATUS.CREATED });
16
+ }
17
+ /**
18
+ * Downloads a FileExport by ID.
19
+ * @param request The request object
20
+ * @param reply The reply object
21
+ */
22
+ static async download(request, reply) {
23
+ const { id } = request.params;
24
+ const fileExport = await fileExports.findById(id);
25
+ if (!fileExport)
26
+ throw new HttpException(HTTP_STATUS.NOT_FOUND, 'File export not found');
27
+ // Retrieve file data from database
28
+ const foundFileData = await files.findById(fileExport.file_id);
29
+ if (!foundFileData)
30
+ throw new HttpException(HTTP_STATUS.NOT_FOUND, 'File not found');
31
+ // Stream the file in the given URL to the client
32
+ const storageServiceResponse = await fetch(foundFileData.url);
33
+ if (!storageServiceResponse.ok || !storageServiceResponse.body)
34
+ return reply.code(500).send('Could not fetch file.');
35
+ // Set headers and pipe the response body to the client
36
+ reply.header('Content-Disposition', `attachment; filename="${foundFileData.name}"`);
37
+ reply.header('Content-Type', foundFileData.type);
38
+ // Set content length if available
39
+ const contentLength = storageServiceResponse.headers.get('Content-Length');
40
+ if (contentLength)
41
+ reply.header('Content-Length', contentLength);
42
+ // Pipe the response body to the client
43
+ return reply.send(storageServiceResponse.body);
44
+ }
45
+ /**
46
+ * Returns all FileExport sorted by ID.
47
+ * @param request The request object
48
+ * @param reply The reply object
49
+ */
50
+ static async getAll(request, reply) {
51
+ const filters = {
52
+ created_by: request.me._id,
53
+ };
54
+ const allFileExport = await fileExports.findMany(filters, { sort: { created_at: 1 } });
55
+ return reply.send({ data: allFileExport, error: null, statusCode: HTTP_STATUS.OK });
56
+ }
57
+ }
@@ -0,0 +1 @@
1
+ export * from './exporter.js';
@@ -0,0 +1 @@
1
+ export * from './exporter.js';
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './agencies/index.js';
2
+ export * from './exporter/index.js';
2
3
  export * from './rides/index.js';
package/dist/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './agencies/index.js';
2
+ export * from './exporter/index.js';
2
3
  export * from './rides/index.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/controllers",
3
- "version": "20260408.1128.43",
3
+ "version": "20260410.1049.29",
4
4
  "author": {
5
5
  "email": "iso@tmlmobilidade.pt",
6
6
  "name": "TML-ISO"