@tmlmobilidade/controllers 20260411.1248.30 → 20260418.947.46

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/dist/index.d.ts CHANGED
@@ -1,6 +1,3 @@
1
1
  export * from './agencies/index.js';
2
- export * from './hashed_trips/index.js';
3
- export * from './lines/index.js';
4
2
  export * from './exporter/index.js';
5
3
  export * from './rides/index.js';
6
- export * from './stops/index.js';
package/dist/index.js CHANGED
@@ -1,6 +1,3 @@
1
1
  export * from './agencies/index.js';
2
- export * from './hashed_trips/index.js';
3
- export * from './lines/index.js';
4
2
  export * from './exporter/index.js';
5
3
  export * from './rides/index.js';
6
- export * from './stops/index.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/controllers",
3
- "version": "20260411.1248.30",
3
+ "version": "20260418.947.46",
4
4
  "author": {
5
5
  "email": "iso@tmlmobilidade.pt",
6
6
  "name": "TML-ISO"
@@ -40,13 +40,12 @@
40
40
  "@tmlmobilidade/fastify": "*",
41
41
  "@tmlmobilidade/interfaces": "*",
42
42
  "@tmlmobilidade/normalizers": "*",
43
- "@tmlmobilidade/utils": "*",
44
- "zod": "3.25.76"
43
+ "@tmlmobilidade/utils": "*"
45
44
  },
46
45
  "devDependencies": {
47
46
  "@tmlmobilidade/tsconfig": "*",
48
47
  "@tmlmobilidade/types": "*",
49
- "@types/node": "25.5.2",
48
+ "@types/node": "25.6.0",
50
49
  "@types/ws": "8.18.1",
51
50
  "resolve-tspaths": "0.8.23",
52
51
  "tsc-watch": "7.2.0",
@@ -1,14 +0,0 @@
1
- import { FastifyReply, FastifyRequest } from '@tmlmobilidade/fastify';
2
- import { type HashedTrip } from '@tmlmobilidade/types';
3
- export declare class HashedTripsSharedController {
4
- /**
5
- * Gets a HashedTrip by id.
6
- * @param request The Fastify request object.
7
- * @param reply The Fastify reply object.
8
- */
9
- static getById(request: FastifyRequest<{
10
- Params: {
11
- id: string;
12
- };
13
- }>, reply: FastifyReply<HashedTrip>): Promise<void>;
14
- }
@@ -1,19 +0,0 @@
1
- import { HTTP_STATUS } from '@tmlmobilidade/consts';
2
- import { hashedTrips } from '@tmlmobilidade/interfaces';
3
- export class HashedTripsSharedController {
4
- //
5
- /**
6
- * Gets a HashedTrip by id.
7
- * @param request The Fastify request object.
8
- * @param reply The Fastify reply object.
9
- */
10
- static async getById(request, reply) {
11
- //
12
- // Get a hashed trip by id
13
- const hashedTrip = await hashedTrips.findById(request.params.id);
14
- //
15
- // Send the response
16
- reply.send({ data: hashedTrip, error: null, statusCode: HTTP_STATUS.OK });
17
- //
18
- }
19
- }
@@ -1 +0,0 @@
1
- export * from './hashed_trips.js';
@@ -1 +0,0 @@
1
- export * from './hashed_trips.js';
@@ -1 +0,0 @@
1
- export * from './lines.js';
@@ -1 +0,0 @@
1
- export * from './lines.js';
@@ -1,43 +0,0 @@
1
- import { FastifyReply, FastifyRequest } from '@tmlmobilidade/fastify';
2
- import { type Line } from '@tmlmobilidade/types';
3
- import { z } from 'zod';
4
- export interface LineByHashedTrip {
5
- hashed_trip_ids: string[];
6
- line_id: number;
7
- line_long_name: string;
8
- line_short_name: string;
9
- }
10
- declare const GetLinesByHashedTripQuerySchema: z.ZodObject<{
11
- agency_id: z.ZodOptional<z.ZodString>;
12
- date_end: z.ZodEffects<z.ZodOptional<z.ZodUnion<[z.ZodNumber, z.ZodString]>>, number, string | number>;
13
- date_start: z.ZodEffects<z.ZodOptional<z.ZodUnion<[z.ZodNumber, z.ZodString]>>, number, string | number>;
14
- hashed_trip_ids: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>>;
15
- }, "strip", z.ZodTypeAny, {
16
- agency_id?: string;
17
- date_end?: number;
18
- date_start?: number;
19
- hashed_trip_ids?: string | string[];
20
- }, {
21
- agency_id?: string;
22
- date_end?: string | number;
23
- date_start?: string | number;
24
- hashed_trip_ids?: string | string[];
25
- }>;
26
- export type GetLinesByHashedTripQuery = z.infer<typeof GetLinesByHashedTripQuerySchema>;
27
- export declare class LinesSharedController {
28
- /**
29
- * Gets all Lines.
30
- * @param request The Fastify request object.
31
- * @param reply The Fastify reply object.
32
- */
33
- static getAll(request: FastifyRequest, reply: FastifyReply<Line[]>): Promise<never>;
34
- /**
35
- * Get all lines ids with all hashed_trips used in rides.
36
- * @param request The Fastify request object.
37
- * @param reply The Fastify reply object.
38
- */
39
- static getAllLinesIdsByHashedTrip(request: FastifyRequest<{
40
- Querystring: GetLinesByHashedTripQuery;
41
- }>, reply: FastifyReply<LineByHashedTrip[]>): Promise<never>;
42
- }
43
- export {};
@@ -1,100 +0,0 @@
1
- import { HTTP_STATUS } from '@tmlmobilidade/consts';
2
- import { lines, rides } from '@tmlmobilidade/interfaces';
3
- import { PermissionCatalog } from '@tmlmobilidade/types';
4
- import { z } from 'zod';
5
- const GetLinesByHashedTripQuerySchema = z.object({
6
- agency_id: z.string().optional(),
7
- date_end: z.union([z.number(), z.string()]).optional().transform(value => value !== undefined ? Number(value) : undefined),
8
- date_start: z.union([z.number(), z.string()]).optional().transform(value => value !== undefined ? Number(value) : undefined),
9
- hashed_trip_ids: z.union([z.string(), z.array(z.string())]).optional(),
10
- });
11
- export class LinesSharedController {
12
- //
13
- /**
14
- * Gets all Lines.
15
- * @param request The Fastify request object.
16
- * @param reply The Fastify reply object.
17
- */
18
- static async getAll(request, reply) {
19
- //
20
- // Get all lines
21
- const allLines = await lines.findMany({}, { sort: { created_at: -1 } });
22
- //
23
- // Get the resource permissions for lines for the current user.
24
- const linesPermission = PermissionCatalog.get(request.permissions, PermissionCatalog.all.lines.scope, PermissionCatalog.all.lines.actions.read);
25
- if (!linesPermission)
26
- return reply.send({ data: allLines, error: null, statusCode: HTTP_STATUS.OK });
27
- //
28
- // Send the response
29
- reply.send({ data: allLines, error: null, statusCode: HTTP_STATUS.OK });
30
- //
31
- }
32
- //
33
- /**
34
- * Get all lines ids with all hashed_trips used in rides.
35
- * @param request The Fastify request object.
36
- * @param reply The Fastify reply object.
37
- */
38
- static async getAllLinesIdsByHashedTrip(request, reply) {
39
- //
40
- // Resolve hashed trip ids from query directly or by rides filters (date/agency).
41
- const parsedQuery = GetLinesByHashedTripQuerySchema.parse(request.query);
42
- const parsedDateStart = parsedQuery.date_start;
43
- const parsedDateEnd = parsedQuery.date_end;
44
- const hasDateRangeFilters = Number.isFinite(parsedDateStart) && Number.isFinite(parsedDateEnd);
45
- const hasAgencyFilter = Boolean(parsedQuery.agency_id);
46
- const hashedTripIdsQuery = parsedQuery.hashed_trip_ids
47
- ? (Array.isArray(parsedQuery.hashed_trip_ids) ? parsedQuery.hashed_trip_ids : [parsedQuery.hashed_trip_ids])
48
- : [];
49
- const filteredHashedTripIds = hashedTripIdsQuery.filter(Boolean);
50
- const hasHashedTripIds = filteredHashedTripIds.length > 0;
51
- if (!hasHashedTripIds && !(hasDateRangeFilters && hasAgencyFilter)) {
52
- return reply.send({ data: [], error: null, statusCode: HTTP_STATUS.OK });
53
- }
54
- const ridesMatchStage = {};
55
- if (hasHashedTripIds) {
56
- ridesMatchStage.hashed_trip_id = { $in: filteredHashedTripIds };
57
- }
58
- else if (hasDateRangeFilters && parsedQuery.agency_id) {
59
- ridesMatchStage.start_time_scheduled = { $gte: parsedDateStart, $lte: parsedDateEnd };
60
- ridesMatchStage.agency_id = parsedQuery.agency_id;
61
- }
62
- const aggregatedLines = await rides.aggregate([
63
- { $match: ridesMatchStage },
64
- {
65
- $lookup: {
66
- as: 'hashed_trip',
67
- foreignField: '_id',
68
- from: 'hashed_trips',
69
- localField: 'hashed_trip_id',
70
- },
71
- },
72
- { $unwind: '$hashed_trip' },
73
- { $sort: { hashed_trip_id: 1 } },
74
- {
75
- $group: {
76
- _id: '$hashed_trip.line_id',
77
- hashed_trip_ids: { $addToSet: '$hashed_trip_id' },
78
- line_long_name: { $first: '$hashed_trip.line_long_name' },
79
- line_short_name: { $first: '$hashed_trip.line_short_name' },
80
- },
81
- },
82
- {
83
- $addFields: {
84
- line_id: '$_id',
85
- },
86
- },
87
- {
88
- $project: {
89
- _id: 0,
90
- hashed_trip_ids: 1,
91
- line_id: 1,
92
- line_long_name: 1,
93
- line_short_name: 1,
94
- },
95
- },
96
- { $sort: { line_id: 1 } },
97
- ]);
98
- return reply.send({ data: aggregatedLines, error: null, statusCode: HTTP_STATUS.OK });
99
- }
100
- }
@@ -1 +0,0 @@
1
- export * from './stops.js';
@@ -1 +0,0 @@
1
- export * from './stops.js';
@@ -1,25 +0,0 @@
1
- import { type FastifyReply, type FastifyRequest } from '@tmlmobilidade/fastify';
2
- import { type Stop } from '@tmlmobilidade/types';
3
- export declare class StopsSharedController {
4
- /**
5
- * Gets all stops.
6
- * @param request The Fastify request object.
7
- * @param reply The Fastify reply object.
8
- */
9
- static getAll(request: FastifyRequest, reply: FastifyReply<Stop[]>): Promise<void>;
10
- static getBatch(request: FastifyRequest, reply: FastifyReply<{
11
- label: string;
12
- legacy_id: string;
13
- value: string;
14
- }[]>): Promise<void>;
15
- /**
16
- * Gets a stop by ID.
17
- * @param request The Fastify request object.
18
- * @param reply The Fastify reply object.
19
- */
20
- static getById(request: FastifyRequest<{
21
- Params: {
22
- id: string;
23
- };
24
- }>, reply: FastifyReply<Stop>): Promise<never>;
25
- }
@@ -1,56 +0,0 @@
1
- import { HTTP_STATUS } from '@tmlmobilidade/consts';
2
- import { stops } from '@tmlmobilidade/interfaces';
3
- import { z } from 'zod';
4
- const GetStopsBatchQuerySchema = z.object({
5
- stop_ids: z.union([z.string(), z.array(z.string())]).optional(),
6
- stop_names: z.union([z.string(), z.array(z.string())]).optional(),
7
- });
8
- export class StopsSharedController {
9
- //
10
- /**
11
- * Gets all stops.
12
- * @param request The Fastify request object.
13
- * @param reply The Fastify reply object.
14
- */
15
- static async getAll(request, reply) {
16
- //
17
- // Resolve hashed trip ids from query directly or by rides filters (date/agency).
18
- const allStops = await stops.findMany({}, { sort: { created_at: -1 } });
19
- //
20
- // Send the response
21
- reply.send({ data: allStops, error: null, statusCode: HTTP_STATUS.OK });
22
- //
23
- }
24
- static async getBatch(request, reply) {
25
- const parsedQuery = GetStopsBatchQuerySchema.parse(request.query);
26
- const stopIds = parsedQuery.stop_ids
27
- ? (Array.isArray(parsedQuery.stop_ids) ? parsedQuery.stop_ids : [parsedQuery.stop_ids])
28
- : [];
29
- const stopNames = parsedQuery.stop_names
30
- ? (Array.isArray(parsedQuery.stop_names) ? parsedQuery.stop_names : [parsedQuery.stop_names])
31
- : [];
32
- const matchConditions = [];
33
- if (stopIds.length > 0)
34
- matchConditions.push({ _id: { $in: stopIds } });
35
- if (stopNames.length > 0)
36
- matchConditions.push({ name: { $in: stopNames } });
37
- const stopsData = await stops.aggregate([
38
- ...(matchConditions.length > 0 ? [{ $match: { $or: matchConditions } }] : []),
39
- { $project: { _id: 1, code: 1, name: 1 } },
40
- { $sort: { name: 1 } },
41
- ]);
42
- reply.send({ data: stopsData.map(stop => ({ label: `${stop.legacy_id} | ${stop.name}`, legacy_id: stop.legacy_id, value: stop._id })), error: null, statusCode: HTTP_STATUS.OK });
43
- }
44
- /**
45
- * Gets a stop by ID.
46
- * @param request The Fastify request object.
47
- * @param reply The Fastify reply object.
48
- */
49
- static async getById(request, reply) {
50
- const stop = await stops.findById(request.params.id);
51
- if (!stop) {
52
- return reply.send({ data: null, error: 'Stop not found', statusCode: HTTP_STATUS.NOT_FOUND });
53
- }
54
- reply.send({ data: stop, error: null, statusCode: HTTP_STATUS.OK });
55
- }
56
- }