@tmlmobilidade/interfaces 20250325.1729.19
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 +33 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/src/interfaces/agencies/agencies.interface.d.ts +18 -0
- package/dist/src/interfaces/agencies/agencies.interface.js +46 -0
- package/dist/src/interfaces/alerts/alerts.interface.d.ts +15 -0
- package/dist/src/interfaces/alerts/alerts.interface.js +44 -0
- package/dist/src/interfaces/apex-t11/apex-t11.interface.d.ts +13 -0
- package/dist/src/interfaces/apex-t11/apex-t11.interface.js +37 -0
- package/dist/src/interfaces/apex-t19/apex-t19.interface.d.ts +13 -0
- package/dist/src/interfaces/apex-t19/apex-t19.interface.js +36 -0
- package/dist/src/interfaces/auth/roles.interface.d.ts +24 -0
- package/dist/src/interfaces/auth/roles.interface.js +44 -0
- package/dist/src/interfaces/auth/sessions.interface.d.ts +17 -0
- package/dist/src/interfaces/auth/sessions.interface.js +37 -0
- package/dist/src/interfaces/auth/users.interface.d.ts +97 -0
- package/dist/src/interfaces/auth/users.interface.js +112 -0
- package/dist/src/interfaces/auth/verification-tokens.interface.d.ts +24 -0
- package/dist/src/interfaces/auth/verification-tokens.interface.js +45 -0
- package/dist/src/interfaces/files/files.interface.d.ts +41 -0
- package/dist/src/interfaces/files/files.interface.js +129 -0
- package/dist/src/interfaces/hashed-shapes/hashed-shapes.interface.d.ts +13 -0
- package/dist/src/interfaces/hashed-shapes/hashed-shapes.interface.js +31 -0
- package/dist/src/interfaces/hashed-trips/hashed-trips.interface.d.ts +13 -0
- package/dist/src/interfaces/hashed-trips/hashed-trips.interface.js +32 -0
- package/dist/src/interfaces/index.d.ts +19 -0
- package/dist/src/interfaces/index.js +20 -0
- package/dist/src/interfaces/localities/localities.interface.d.ts +28 -0
- package/dist/src/interfaces/localities/localities.interface.js +49 -0
- package/dist/src/interfaces/municipalities/municipalities.interface.d.ts +28 -0
- package/dist/src/interfaces/municipalities/municipalities.interface.js +49 -0
- package/dist/src/interfaces/organizations/organizations.interface.d.ts +52 -0
- package/dist/src/interfaces/organizations/organizations.interface.js +54 -0
- package/dist/src/interfaces/plans/plans.interface.d.ts +20 -0
- package/dist/src/interfaces/plans/plans.interface.js +38 -0
- package/dist/src/interfaces/rides/rides.interface.d.ts +55 -0
- package/dist/src/interfaces/rides/rides.interface.js +102 -0
- package/dist/src/interfaces/stops/stops.interface.d.ts +30 -0
- package/dist/src/interfaces/stops/stops.interface.js +59 -0
- package/dist/src/interfaces/vehicle-events/vehicle-events.interface.d.ts +13 -0
- package/dist/src/interfaces/vehicle-events/vehicle-events.interface.js +37 -0
- package/dist/src/interfaces/zones/zones.interface.d.ts +35 -0
- package/dist/src/interfaces/zones/zones.interface.js +59 -0
- package/dist/src/providers/auth/auth.d.ts +47 -0
- package/dist/src/providers/auth/auth.js +132 -0
- package/dist/src/providers/email/email.d.ts +21 -0
- package/dist/src/providers/email/email.js +75 -0
- package/dist/src/providers/index.d.ts +5 -0
- package/dist/src/providers/index.js +6 -0
- package/dist/src/providers/storage/s3-storage.d.ts +58 -0
- package/dist/src/providers/storage/s3-storage.js +161 -0
- package/dist/src/providers/storage/storage.factory.d.ts +20 -0
- package/dist/src/providers/storage/storage.factory.js +22 -0
- package/dist/src/providers/storage/storage.interface.d.ts +9 -0
- package/dist/src/providers/storage/storage.interface.js +2 -0
- package/package.json +55 -0
package/README.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
## @tmlmobilidade/interfaces
|
|
2
|
+
|
|
3
|
+
This package provides SDK-style connectors for interacting with databases (e.g., stops, plans, rides, alerts) and external providers (e.g., authentication, storage).
|
|
4
|
+
It simplifies data access and integration across projects.
|
|
5
|
+
|
|
6
|
+
### Purpose
|
|
7
|
+
- Unified interface for accessing databases and external services
|
|
8
|
+
- Reduces boilerplate by handling common queries and operations
|
|
9
|
+
- Ensures consistency across different modules
|
|
10
|
+
|
|
11
|
+
### Installation
|
|
12
|
+
```
|
|
13
|
+
npm install @tmlmobilidade/interfaces
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
### Local development
|
|
17
|
+
|
|
18
|
+
Each interface has a given fixed port number to connect to the corresponding database instance. Use these
|
|
19
|
+
|
|
20
|
+
| Interface Name | Port | Env Value |
|
|
21
|
+
|----------------|:-----:|-------------------------------------|
|
|
22
|
+
| auth | 37000 | mongodb://root:root@localhost:37000 |
|
|
23
|
+
| files | 37001 | mongodb://root:root@localhost:37001 |
|
|
24
|
+
| stops | 37002 | mongodb://root:root@localhost:37002 |
|
|
25
|
+
| agencies | 37003 | mongodb://root:root@localhost:37003 |
|
|
26
|
+
| plans | 37004 | mongodb://root:root@localhost:37004 |
|
|
27
|
+
| alerts | 37005 | mongodb://root:root@localhost:37005 |
|
|
28
|
+
| rides | 37006 | mongodb://root:root@localhost:37006 |
|
|
29
|
+
| vehicle_events | 37007 | mongodb://root:root@localhost:37007 |
|
|
30
|
+
| hashed_shapes | 37008 | mongodb://root:root@localhost:37008 |
|
|
31
|
+
| hashed_trips | 37009 | mongodb://root:root@localhost:37009 |
|
|
32
|
+
| apex_t11 | 37010 | mongodb://root:root@localhost:37010 |
|
|
33
|
+
| apex_t19 | 37011 | mongodb://root:root@localhost:37011 |
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { MongoCollectionClass } from '@tmlmobilidade/lib';
|
|
2
|
+
import { Agency, CreateAgencyDto, UpdateAgencyDto } from '@tmlmobilidade/types';
|
|
3
|
+
import { IndexDescription } from 'mongodb';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
declare class AgenciesClass extends MongoCollectionClass<Agency, CreateAgencyDto, UpdateAgencyDto> {
|
|
6
|
+
private static _instance;
|
|
7
|
+
private constructor();
|
|
8
|
+
static getInstance(): Promise<AgenciesClass>;
|
|
9
|
+
findByCode(code: string): Promise<import("mongodb").WithId<Agency> | null>;
|
|
10
|
+
updateByCode(code: string, fields: Partial<Agency>): Promise<import("mongodb").UpdateResult<Agency>>;
|
|
11
|
+
protected getCollectionIndexes(): IndexDescription[];
|
|
12
|
+
protected getCollectionName(): string;
|
|
13
|
+
protected getCreateSchema(): z.ZodSchema;
|
|
14
|
+
protected getEnvName(): string;
|
|
15
|
+
protected getUpdateSchema(): z.ZodSchema;
|
|
16
|
+
}
|
|
17
|
+
export declare const agencies: AgenciesClass;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/* * */
|
|
2
|
+
import { MongoCollectionClass } from '@tmlmobilidade/lib';
|
|
3
|
+
import { AgencySchema, UpdateAgencySchema } from '@tmlmobilidade/types';
|
|
4
|
+
import { AsyncSingletonProxy } from '@tmlmobilidade/utils';
|
|
5
|
+
/* * */
|
|
6
|
+
class AgenciesClass extends MongoCollectionClass {
|
|
7
|
+
static _instance;
|
|
8
|
+
constructor() {
|
|
9
|
+
super();
|
|
10
|
+
}
|
|
11
|
+
static async getInstance() {
|
|
12
|
+
if (!AgenciesClass._instance) {
|
|
13
|
+
const instance = new AgenciesClass();
|
|
14
|
+
await instance.connect();
|
|
15
|
+
AgenciesClass._instance = instance;
|
|
16
|
+
}
|
|
17
|
+
return AgenciesClass._instance;
|
|
18
|
+
}
|
|
19
|
+
async findByCode(code) {
|
|
20
|
+
return this.mongoCollection.findOne({ code });
|
|
21
|
+
}
|
|
22
|
+
async updateByCode(code, fields) {
|
|
23
|
+
return this.mongoCollection.updateOne({ code }, { $set: fields });
|
|
24
|
+
}
|
|
25
|
+
getCollectionIndexes() {
|
|
26
|
+
return [
|
|
27
|
+
{ background: true, key: { name: 1 }, unique: true },
|
|
28
|
+
{ background: true, key: { code: 1 }, unique: true },
|
|
29
|
+
{ background: true, key: { email: 1 }, unique: true },
|
|
30
|
+
];
|
|
31
|
+
}
|
|
32
|
+
getCollectionName() {
|
|
33
|
+
return 'agencies';
|
|
34
|
+
}
|
|
35
|
+
getCreateSchema() {
|
|
36
|
+
return AgencySchema;
|
|
37
|
+
}
|
|
38
|
+
getEnvName() {
|
|
39
|
+
return 'TML_INTERFACE_AGENCIES';
|
|
40
|
+
}
|
|
41
|
+
getUpdateSchema() {
|
|
42
|
+
return UpdateAgencySchema;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/* * */
|
|
46
|
+
export const agencies = AsyncSingletonProxy(AgenciesClass);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { MongoCollectionClass } from '@tmlmobilidade/lib';
|
|
2
|
+
import { Alert, CreateAlertDto, UpdateAlertDto } from '@tmlmobilidade/types';
|
|
3
|
+
import { IndexDescription } from 'mongodb';
|
|
4
|
+
declare class AlertsClass extends MongoCollectionClass<Alert, CreateAlertDto, UpdateAlertDto> {
|
|
5
|
+
private static _instance;
|
|
6
|
+
private constructor();
|
|
7
|
+
static getInstance(): Promise<AlertsClass>;
|
|
8
|
+
findByMunicipalityId(municipality_id: string): Promise<import("mongodb").WithId<Alert>[]>;
|
|
9
|
+
findByTitle(title: string): Promise<import("mongodb").WithId<Alert> | null>;
|
|
10
|
+
protected getCollectionIndexes(): IndexDescription[];
|
|
11
|
+
protected getCollectionName(): string;
|
|
12
|
+
protected getEnvName(): string;
|
|
13
|
+
}
|
|
14
|
+
export declare const alerts: AlertsClass;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/* * */
|
|
2
|
+
import { MongoCollectionClass } from '@tmlmobilidade/lib';
|
|
3
|
+
import { AsyncSingletonProxy } from '@tmlmobilidade/utils';
|
|
4
|
+
/* * */
|
|
5
|
+
class AlertsClass extends MongoCollectionClass {
|
|
6
|
+
static _instance;
|
|
7
|
+
constructor() {
|
|
8
|
+
super();
|
|
9
|
+
}
|
|
10
|
+
static async getInstance() {
|
|
11
|
+
if (!AlertsClass._instance) {
|
|
12
|
+
const instance = new AlertsClass();
|
|
13
|
+
await instance.connect();
|
|
14
|
+
AlertsClass._instance = instance;
|
|
15
|
+
}
|
|
16
|
+
return AlertsClass._instance;
|
|
17
|
+
}
|
|
18
|
+
async findByMunicipalityId(municipality_id) {
|
|
19
|
+
return this.mongoCollection.find({ municipality_ids: { $in: [municipality_id] } }).toArray();
|
|
20
|
+
}
|
|
21
|
+
async findByTitle(title) {
|
|
22
|
+
return this.mongoCollection.findOne({ title });
|
|
23
|
+
}
|
|
24
|
+
getCollectionIndexes() {
|
|
25
|
+
return [
|
|
26
|
+
{ background: true, key: { agency_ids: 1 } },
|
|
27
|
+
{ background: true, key: { line_ids: 1 } },
|
|
28
|
+
{ background: true, key: { municipality_ids: 1 } },
|
|
29
|
+
{ background: true, key: { route_ids: 1 } },
|
|
30
|
+
{ background: true, key: { stop_ids: 1 } },
|
|
31
|
+
{ background: true, key: { title: 1 } },
|
|
32
|
+
{ background: true, key: { active_period_end_date: -1, active_period_start_date: -1 } },
|
|
33
|
+
{ background: true, key: { publish_end_date: -1, publish_start_date: -1 } },
|
|
34
|
+
];
|
|
35
|
+
}
|
|
36
|
+
getCollectionName() {
|
|
37
|
+
return 'alerts';
|
|
38
|
+
}
|
|
39
|
+
getEnvName() {
|
|
40
|
+
return 'TML_INTERFACE_ALERTS';
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/* * */
|
|
44
|
+
export const alerts = AsyncSingletonProxy(AlertsClass);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { MongoCollectionClass } from '@tmlmobilidade/lib';
|
|
2
|
+
import { ApexT11 } from '@tmlmobilidade/types';
|
|
3
|
+
import { IndexDescription } from 'mongodb';
|
|
4
|
+
declare class ApexT11Class extends MongoCollectionClass<ApexT11, ApexT11, ApexT11> {
|
|
5
|
+
private static _instance;
|
|
6
|
+
private constructor();
|
|
7
|
+
static getInstance(): Promise<ApexT11Class>;
|
|
8
|
+
protected getCollectionIndexes(): IndexDescription[];
|
|
9
|
+
protected getCollectionName(): string;
|
|
10
|
+
protected getEnvName(): string;
|
|
11
|
+
}
|
|
12
|
+
export declare const apexT11: ApexT11Class;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/* * */
|
|
2
|
+
import { MongoCollectionClass } from '@tmlmobilidade/lib';
|
|
3
|
+
import { AsyncSingletonProxy } from '@tmlmobilidade/utils';
|
|
4
|
+
/* * */
|
|
5
|
+
class ApexT11Class extends MongoCollectionClass {
|
|
6
|
+
static _instance;
|
|
7
|
+
constructor() {
|
|
8
|
+
super();
|
|
9
|
+
}
|
|
10
|
+
static async getInstance() {
|
|
11
|
+
if (!ApexT11Class._instance) {
|
|
12
|
+
const instance = new ApexT11Class();
|
|
13
|
+
await instance.connect();
|
|
14
|
+
ApexT11Class._instance = instance;
|
|
15
|
+
}
|
|
16
|
+
return ApexT11Class._instance;
|
|
17
|
+
}
|
|
18
|
+
getCollectionIndexes() {
|
|
19
|
+
return [
|
|
20
|
+
{ background: true, key: { created_at: 1 } },
|
|
21
|
+
{ background: true, key: { received_at: 1 } },
|
|
22
|
+
{ background: true, key: { card_serial_number: 1 } },
|
|
23
|
+
{ background: true, key: { agency_id: 1 } },
|
|
24
|
+
// eslint-disable-next-line perfectionist/sort-objects
|
|
25
|
+
{ background: true, key: { trip_id: 1, created_at: 1 } },
|
|
26
|
+
{ background: true, key: { agency_id: 1, created_at: 1 } },
|
|
27
|
+
];
|
|
28
|
+
}
|
|
29
|
+
getCollectionName() {
|
|
30
|
+
return 'apex_t11';
|
|
31
|
+
}
|
|
32
|
+
getEnvName() {
|
|
33
|
+
return 'TML_INTERFACE_APEX_T11';
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/* * */
|
|
37
|
+
export const apexT11 = AsyncSingletonProxy(ApexT11Class);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { MongoCollectionClass } from '@tmlmobilidade/lib';
|
|
2
|
+
import { ApexT19 } from '@tmlmobilidade/types';
|
|
3
|
+
import { IndexDescription } from 'mongodb';
|
|
4
|
+
declare class ApexT19Class extends MongoCollectionClass<ApexT19, ApexT19, ApexT19> {
|
|
5
|
+
private static _instance;
|
|
6
|
+
private constructor();
|
|
7
|
+
static getInstance(): Promise<ApexT19Class>;
|
|
8
|
+
protected getCollectionIndexes(): IndexDescription[];
|
|
9
|
+
protected getCollectionName(): string;
|
|
10
|
+
protected getEnvName(): string;
|
|
11
|
+
}
|
|
12
|
+
export declare const apexT19: ApexT19Class;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/* * */
|
|
2
|
+
import { MongoCollectionClass } from '@tmlmobilidade/lib';
|
|
3
|
+
import { AsyncSingletonProxy } from '@tmlmobilidade/utils';
|
|
4
|
+
/* * */
|
|
5
|
+
class ApexT19Class extends MongoCollectionClass {
|
|
6
|
+
static _instance;
|
|
7
|
+
constructor() {
|
|
8
|
+
super();
|
|
9
|
+
}
|
|
10
|
+
static async getInstance() {
|
|
11
|
+
if (!ApexT19Class._instance) {
|
|
12
|
+
const instance = new ApexT19Class();
|
|
13
|
+
await instance.connect();
|
|
14
|
+
ApexT19Class._instance = instance;
|
|
15
|
+
}
|
|
16
|
+
return ApexT19Class._instance;
|
|
17
|
+
}
|
|
18
|
+
getCollectionIndexes() {
|
|
19
|
+
return [
|
|
20
|
+
{ background: true, key: { created_at: 1 } },
|
|
21
|
+
{ background: true, key: { received_at: 1 } },
|
|
22
|
+
{ background: true, key: { agency_id: 1 } },
|
|
23
|
+
// eslint-disable-next-line perfectionist/sort-objects
|
|
24
|
+
{ background: true, key: { trip_id: 1, created_at: 1 } },
|
|
25
|
+
{ background: true, key: { agency_id: 1, created_at: 1 } },
|
|
26
|
+
];
|
|
27
|
+
}
|
|
28
|
+
getCollectionName() {
|
|
29
|
+
return 'apex_t19';
|
|
30
|
+
}
|
|
31
|
+
getEnvName() {
|
|
32
|
+
return 'TML_INTERFACE_APEX_T19';
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/* * */
|
|
36
|
+
export const apexT19 = AsyncSingletonProxy(ApexT19Class);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { MongoCollectionClass } from '@tmlmobilidade/lib';
|
|
2
|
+
import { CreateRoleDto, Role, UpdateRoleDto } from '@tmlmobilidade/types';
|
|
3
|
+
import { IndexDescription, UpdateResult } from 'mongodb';
|
|
4
|
+
declare class RolesClass extends MongoCollectionClass<Role, CreateRoleDto, UpdateRoleDto> {
|
|
5
|
+
private static _instance;
|
|
6
|
+
private constructor();
|
|
7
|
+
static getInstance(): Promise<RolesClass>;
|
|
8
|
+
/**
|
|
9
|
+
* Finds a role by its name
|
|
10
|
+
*
|
|
11
|
+
* @param name - The name of the role to find
|
|
12
|
+
* @returns A promise that resolves to the matching role document or null if not found
|
|
13
|
+
*/
|
|
14
|
+
findByName(name: string): Promise<import("mongodb").WithId<Role> | null>;
|
|
15
|
+
/**
|
|
16
|
+
* Disable Update Many
|
|
17
|
+
*/
|
|
18
|
+
updateMany(): Promise<UpdateResult<Role>>;
|
|
19
|
+
protected getCollectionIndexes(): IndexDescription[];
|
|
20
|
+
protected getCollectionName(): string;
|
|
21
|
+
protected getEnvName(): string;
|
|
22
|
+
}
|
|
23
|
+
export declare const roles: RolesClass;
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { HttpException, HttpStatus } from '@tmlmobilidade/lib';
|
|
2
|
+
import { MongoCollectionClass } from '@tmlmobilidade/lib';
|
|
3
|
+
import { AsyncSingletonProxy } from '@tmlmobilidade/utils';
|
|
4
|
+
class RolesClass extends MongoCollectionClass {
|
|
5
|
+
static _instance;
|
|
6
|
+
constructor() {
|
|
7
|
+
super();
|
|
8
|
+
}
|
|
9
|
+
static async getInstance() {
|
|
10
|
+
if (!RolesClass._instance) {
|
|
11
|
+
const instance = new RolesClass();
|
|
12
|
+
await instance.connect();
|
|
13
|
+
RolesClass._instance = instance;
|
|
14
|
+
}
|
|
15
|
+
return RolesClass._instance;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Finds a role by its name
|
|
19
|
+
*
|
|
20
|
+
* @param name - The name of the role to find
|
|
21
|
+
* @returns A promise that resolves to the matching role document or null if not found
|
|
22
|
+
*/
|
|
23
|
+
async findByName(name) {
|
|
24
|
+
return this.mongoCollection.findOne({ name });
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Disable Update Many
|
|
28
|
+
*/
|
|
29
|
+
async updateMany() {
|
|
30
|
+
throw new HttpException(HttpStatus.METHOD_NOT_ALLOWED, 'Method not allowed for roles');
|
|
31
|
+
}
|
|
32
|
+
getCollectionIndexes() {
|
|
33
|
+
return [
|
|
34
|
+
{ background: true, key: { name: 1 }, unique: true },
|
|
35
|
+
];
|
|
36
|
+
}
|
|
37
|
+
getCollectionName() {
|
|
38
|
+
return 'roles';
|
|
39
|
+
}
|
|
40
|
+
getEnvName() {
|
|
41
|
+
return 'TML_INTERFACE_AUTH';
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export const roles = AsyncSingletonProxy(RolesClass);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { MongoCollectionClass } from '@tmlmobilidade/lib';
|
|
2
|
+
import { CreateSessionDto, Session, UpdateSessionDto } from '@tmlmobilidade/types';
|
|
3
|
+
import { IndexDescription, UpdateResult } from 'mongodb';
|
|
4
|
+
declare class SessionsClass extends MongoCollectionClass<Session, CreateSessionDto, UpdateSessionDto> {
|
|
5
|
+
private static _instance;
|
|
6
|
+
private constructor();
|
|
7
|
+
static getInstance(): Promise<SessionsClass>;
|
|
8
|
+
/**
|
|
9
|
+
* Disable Update Many
|
|
10
|
+
*/
|
|
11
|
+
updateMany(): Promise<UpdateResult<Session>>;
|
|
12
|
+
protected getCollectionIndexes(): IndexDescription[];
|
|
13
|
+
protected getCollectionName(): string;
|
|
14
|
+
protected getEnvName(): string;
|
|
15
|
+
}
|
|
16
|
+
export declare const sessions: SessionsClass;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { MongoCollectionClass } from '@tmlmobilidade/lib';
|
|
2
|
+
import { HttpException, HttpStatus } from '@tmlmobilidade/lib';
|
|
3
|
+
import { AsyncSingletonProxy } from '@tmlmobilidade/utils';
|
|
4
|
+
class SessionsClass extends MongoCollectionClass {
|
|
5
|
+
static _instance;
|
|
6
|
+
constructor() {
|
|
7
|
+
super();
|
|
8
|
+
}
|
|
9
|
+
static async getInstance() {
|
|
10
|
+
if (!SessionsClass._instance) {
|
|
11
|
+
const instance = new SessionsClass();
|
|
12
|
+
await instance.connect();
|
|
13
|
+
SessionsClass._instance = instance;
|
|
14
|
+
}
|
|
15
|
+
return SessionsClass._instance;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Disable Update Many
|
|
19
|
+
*/
|
|
20
|
+
async updateMany() {
|
|
21
|
+
throw new HttpException(HttpStatus.METHOD_NOT_ALLOWED, 'Method not allowed for sessions');
|
|
22
|
+
}
|
|
23
|
+
getCollectionIndexes() {
|
|
24
|
+
return [
|
|
25
|
+
{ background: true, key: { user_id: 1 } },
|
|
26
|
+
{ background: true, key: { expires: 1 } },
|
|
27
|
+
{ background: true, key: { token: 1 }, unique: true },
|
|
28
|
+
];
|
|
29
|
+
}
|
|
30
|
+
getCollectionName() {
|
|
31
|
+
return 'sessions';
|
|
32
|
+
}
|
|
33
|
+
getEnvName() {
|
|
34
|
+
return 'TML_INTERFACE_AUTH';
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export const sessions = AsyncSingletonProxy(SessionsClass);
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { MongoCollectionClass } from '@tmlmobilidade/lib';
|
|
2
|
+
import { CreateUserDto, UpdateUserDto, User } from '@tmlmobilidade/types';
|
|
3
|
+
import { Filter, IndexDescription, Sort, WithId } from 'mongodb';
|
|
4
|
+
type NewType = string;
|
|
5
|
+
declare class UsersClass extends MongoCollectionClass<User, CreateUserDto, UpdateUserDto> {
|
|
6
|
+
private static _instance;
|
|
7
|
+
private constructor();
|
|
8
|
+
static getInstance(): Promise<UsersClass>;
|
|
9
|
+
/**
|
|
10
|
+
* Finds a user document by its email.
|
|
11
|
+
*
|
|
12
|
+
* @param email - The email of the user to find
|
|
13
|
+
* @param includePasswordHash - Whether to include the password hash in the result
|
|
14
|
+
* @returns A promise that resolves to the matching user document or null if not found
|
|
15
|
+
*/
|
|
16
|
+
findByEmail(email: string, includePasswordHash?: boolean): Promise<null | WithId<User>>;
|
|
17
|
+
/**
|
|
18
|
+
* Finds a document by its ID.
|
|
19
|
+
*
|
|
20
|
+
* @param id - The ID of the document to find
|
|
21
|
+
* @param includePasswordHash - Whether to include the password hash in the result
|
|
22
|
+
* @returns A promise that resolves to the matching document or null if not found
|
|
23
|
+
*/
|
|
24
|
+
findById(id: string, includePasswordHash?: boolean): Promise<WithId<User> | null>;
|
|
25
|
+
/**
|
|
26
|
+
* Finds users by their organization code
|
|
27
|
+
*
|
|
28
|
+
* @param code - The code of the organization to find users for
|
|
29
|
+
* @param includePasswordHash - Whether to include the password hash in the result
|
|
30
|
+
* @returns A promise that resolves to the matching user documents or null if not found
|
|
31
|
+
*/
|
|
32
|
+
findByOrganization(id: NewType, includePasswordHash?: boolean): Promise<{
|
|
33
|
+
created_at: import("@tmlmobilidade/types").UnixTimestamp;
|
|
34
|
+
updated_at: import("@tmlmobilidade/types").UnixTimestamp;
|
|
35
|
+
email: string;
|
|
36
|
+
phone?: string | null | undefined | undefined;
|
|
37
|
+
permissions: {
|
|
38
|
+
action: string;
|
|
39
|
+
scope: string;
|
|
40
|
+
resource?: Record<string, any> | null | undefined;
|
|
41
|
+
}[];
|
|
42
|
+
email_verified?: (null | import("@tmlmobilidade/types").UnixTimestamp) | undefined;
|
|
43
|
+
first_name: string;
|
|
44
|
+
last_name: string;
|
|
45
|
+
organization_ids: string[];
|
|
46
|
+
role_ids: string[];
|
|
47
|
+
session_ids: string[];
|
|
48
|
+
verification_token_ids: string[];
|
|
49
|
+
avatar?: string | null | undefined | undefined;
|
|
50
|
+
bio?: string | null | undefined | undefined;
|
|
51
|
+
_id: string;
|
|
52
|
+
}[]>;
|
|
53
|
+
/**
|
|
54
|
+
* Finds a user by their role
|
|
55
|
+
*
|
|
56
|
+
* @param role - The role of the user to find
|
|
57
|
+
* @returns A promise that resolves to the matching user document or null if not found
|
|
58
|
+
*/
|
|
59
|
+
findByRole(role: string, includePasswordHash?: boolean): Promise<{
|
|
60
|
+
created_at: import("@tmlmobilidade/types").UnixTimestamp;
|
|
61
|
+
updated_at: import("@tmlmobilidade/types").UnixTimestamp;
|
|
62
|
+
email: string;
|
|
63
|
+
phone?: string | null | undefined | undefined;
|
|
64
|
+
permissions: {
|
|
65
|
+
action: string;
|
|
66
|
+
scope: string;
|
|
67
|
+
resource?: Record<string, any> | null | undefined;
|
|
68
|
+
}[];
|
|
69
|
+
email_verified?: (null | import("@tmlmobilidade/types").UnixTimestamp) | undefined;
|
|
70
|
+
first_name: string;
|
|
71
|
+
last_name: string;
|
|
72
|
+
organization_ids: string[];
|
|
73
|
+
role_ids: string[];
|
|
74
|
+
session_ids: string[];
|
|
75
|
+
verification_token_ids: string[];
|
|
76
|
+
avatar?: string | null | undefined | undefined;
|
|
77
|
+
bio?: string | null | undefined | undefined;
|
|
78
|
+
_id: string;
|
|
79
|
+
}[]>;
|
|
80
|
+
/**
|
|
81
|
+
* Finds multiple documents matching the filter criteria with optional pagination and sorting.
|
|
82
|
+
*
|
|
83
|
+
* @param filter - (Optional) filter criteria to match documents
|
|
84
|
+
* @param perPage - (Optional) number of documents per page for pagination
|
|
85
|
+
* @param page - (Optional) page number for pagination
|
|
86
|
+
* @param sort - (Optional) sort specification
|
|
87
|
+
* @returns A promise that resolves to an array of matching documents
|
|
88
|
+
*/
|
|
89
|
+
findMany(filter?: Filter<User>, perPage?: number, page?: number, sort?: Sort): Promise<WithId<User>[]>;
|
|
90
|
+
findOne(filter: Filter<User>): Promise<WithId<User> | null>;
|
|
91
|
+
protected getCollectionIndexes(): IndexDescription[];
|
|
92
|
+
protected getCollectionName(): string;
|
|
93
|
+
protected getEnvName(): string;
|
|
94
|
+
private deletePasswordHash;
|
|
95
|
+
}
|
|
96
|
+
export declare const users: UsersClass;
|
|
97
|
+
export {};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { MongoCollectionClass } from '@tmlmobilidade/lib';
|
|
2
|
+
import { AsyncSingletonProxy } from '@tmlmobilidade/utils';
|
|
3
|
+
class UsersClass extends MongoCollectionClass {
|
|
4
|
+
static _instance;
|
|
5
|
+
constructor() {
|
|
6
|
+
super();
|
|
7
|
+
}
|
|
8
|
+
static async getInstance() {
|
|
9
|
+
if (!UsersClass._instance) {
|
|
10
|
+
const instance = new UsersClass();
|
|
11
|
+
await instance.connect();
|
|
12
|
+
UsersClass._instance = instance;
|
|
13
|
+
}
|
|
14
|
+
return UsersClass._instance;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Finds a user document by its email.
|
|
18
|
+
*
|
|
19
|
+
* @param email - The email of the user to find
|
|
20
|
+
* @param includePasswordHash - Whether to include the password hash in the result
|
|
21
|
+
* @returns A promise that resolves to the matching user document or null if not found
|
|
22
|
+
*/
|
|
23
|
+
async findByEmail(email, includePasswordHash = false) {
|
|
24
|
+
const user = await this.mongoCollection.findOne({ email });
|
|
25
|
+
if (!user) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
return includePasswordHash ? user : this.deletePasswordHash(user);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Finds a document by its ID.
|
|
32
|
+
*
|
|
33
|
+
* @param id - The ID of the document to find
|
|
34
|
+
* @param includePasswordHash - Whether to include the password hash in the result
|
|
35
|
+
* @returns A promise that resolves to the matching document or null if not found
|
|
36
|
+
*/
|
|
37
|
+
async findById(id, includePasswordHash = false) {
|
|
38
|
+
const user = await this.mongoCollection.findOne({ _id: id });
|
|
39
|
+
if (!user) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
return includePasswordHash ? user : this.deletePasswordHash(user);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Finds users by their organization code
|
|
46
|
+
*
|
|
47
|
+
* @param code - The code of the organization to find users for
|
|
48
|
+
* @param includePasswordHash - Whether to include the password hash in the result
|
|
49
|
+
* @returns A promise that resolves to the matching user documents or null if not found
|
|
50
|
+
*/
|
|
51
|
+
async findByOrganization(id, includePasswordHash = false) {
|
|
52
|
+
const users = await this.mongoCollection.find({ organization_ids: { $in: [id] } }).toArray();
|
|
53
|
+
return includePasswordHash ? users : users.map(user => this.deletePasswordHash(user));
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Finds a user by their role
|
|
57
|
+
*
|
|
58
|
+
* @param role - The role of the user to find
|
|
59
|
+
* @returns A promise that resolves to the matching user document or null if not found
|
|
60
|
+
*/
|
|
61
|
+
async findByRole(role, includePasswordHash = false) {
|
|
62
|
+
const users = await this.mongoCollection.find({ role_ids: { $in: [role] } }).toArray();
|
|
63
|
+
return includePasswordHash ? users : users.map(user => this.deletePasswordHash(user));
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Finds multiple documents matching the filter criteria with optional pagination and sorting.
|
|
67
|
+
*
|
|
68
|
+
* @param filter - (Optional) filter criteria to match documents
|
|
69
|
+
* @param perPage - (Optional) number of documents per page for pagination
|
|
70
|
+
* @param page - (Optional) page number for pagination
|
|
71
|
+
* @param sort - (Optional) sort specification
|
|
72
|
+
* @returns A promise that resolves to an array of matching documents
|
|
73
|
+
*/
|
|
74
|
+
async findMany(filter, perPage, page, sort) {
|
|
75
|
+
const query = this.mongoCollection.find(filter ?? {});
|
|
76
|
+
if (perPage)
|
|
77
|
+
query.limit(perPage);
|
|
78
|
+
if (page && perPage)
|
|
79
|
+
query.skip(perPage * (page - 1));
|
|
80
|
+
if (sort)
|
|
81
|
+
query.sort(sort);
|
|
82
|
+
const users = await query.toArray();
|
|
83
|
+
return users.map(user => this.deletePasswordHash(user));
|
|
84
|
+
}
|
|
85
|
+
async findOne(filter) {
|
|
86
|
+
const user = await this.mongoCollection.findOne(filter);
|
|
87
|
+
if (!user) {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
return this.deletePasswordHash(user);
|
|
91
|
+
}
|
|
92
|
+
getCollectionIndexes() {
|
|
93
|
+
return [
|
|
94
|
+
{ background: true, key: { email: 1 }, unique: true },
|
|
95
|
+
{ background: true, key: { 'profile.first_name': 1, 'profile.last_name': 1 } },
|
|
96
|
+
{ background: true, key: { session_ids: 1 } },
|
|
97
|
+
{ background: true, key: { role_ids: 1 } },
|
|
98
|
+
];
|
|
99
|
+
}
|
|
100
|
+
getCollectionName() {
|
|
101
|
+
return 'users';
|
|
102
|
+
}
|
|
103
|
+
getEnvName() {
|
|
104
|
+
return 'TML_INTERFACE_AUTH';
|
|
105
|
+
}
|
|
106
|
+
deletePasswordHash(user) {
|
|
107
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
108
|
+
const { password_hash, ...userWithoutPassword } = user;
|
|
109
|
+
return userWithoutPassword;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
export const users = AsyncSingletonProxy(UsersClass);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { MongoCollectionClass } from '@tmlmobilidade/lib';
|
|
2
|
+
import { CreateVerificationTokenDto, UpdateVerificationTokenDto, VerificationToken } from '@tmlmobilidade/types';
|
|
3
|
+
import { IndexDescription, UpdateResult } from 'mongodb';
|
|
4
|
+
declare class VerificationTokensClass extends MongoCollectionClass<VerificationToken, CreateVerificationTokenDto, UpdateVerificationTokenDto> {
|
|
5
|
+
private static _instance;
|
|
6
|
+
private constructor();
|
|
7
|
+
static getInstance(): Promise<VerificationTokensClass>;
|
|
8
|
+
/**
|
|
9
|
+
* Finds a verification token by its token.
|
|
10
|
+
*
|
|
11
|
+
* @param token - The token to find
|
|
12
|
+
* @returns The verification token or null if not found
|
|
13
|
+
*/
|
|
14
|
+
findByToken(token: string): Promise<import("mongodb").WithId<VerificationToken> | null>;
|
|
15
|
+
/**
|
|
16
|
+
* Disable Update Many
|
|
17
|
+
*/
|
|
18
|
+
updateMany(): Promise<UpdateResult<VerificationToken>>;
|
|
19
|
+
protected getCollectionIndexes(): IndexDescription[];
|
|
20
|
+
protected getCollectionName(): string;
|
|
21
|
+
protected getEnvName(): string;
|
|
22
|
+
}
|
|
23
|
+
export declare const verificationTokens: VerificationTokensClass;
|
|
24
|
+
export {};
|