@tmlmobilidade/interfaces 20251218.110.37 → 20251222.1537.24

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.
@@ -131,9 +131,9 @@ export declare abstract class MongoCollectionClass<T extends Document, TCreate,
131
131
  }): Promise<InsertManyResult<T>>;
132
132
  /**
133
133
  * Inserts a single document into the collection.
134
- * @param doc - The document to insert
135
- * @param options - The options for the insert operation
136
- * @returns A promise that resolves to the result of the insert operation
134
+ * @param doc The document to insert.
135
+ * @param options The options for the insert operation.
136
+ * @returns A promise that resolves to the result of the insert operation.
137
137
  */
138
138
  insertOne<TReturnDocument extends boolean = true>(doc: TCreate & {
139
139
  _id?: string;
@@ -231,47 +231,57 @@ export class MongoCollectionClass {
231
231
  }
232
232
  /**
233
233
  * Inserts a single document into the collection.
234
- * @param doc - The document to insert
235
- * @param options - The options for the insert operation
236
- * @returns A promise that resolves to the result of the insert operation
234
+ * @param doc The document to insert.
235
+ * @param options The options for the insert operation.
236
+ * @returns A promise that resolves to the result of the insert operation.
237
237
  */
238
238
  async insertOne(doc, { options, unsafe = false } = {}) {
239
- const newDocument = {
240
- ...doc,
241
- _id: doc._id || generateRandomString({ length: 5 }),
242
- created_at: doc.created_at || Dates.now('utc').unix_timestamp,
243
- created_by: doc.created_by || 'system',
244
- updated_at: doc.updated_at || Dates.now('utc').unix_timestamp,
245
- updated_by: doc.updated_by || 'system',
246
- };
247
- if (!doc._id) {
248
- while (await this.findById(newDocument._id)) {
249
- newDocument._id = generateRandomString({ length: 5 });
250
- }
251
- }
252
- let parsedDocument = newDocument;
239
+ // Setup a copy of the document to be inserted
240
+ let parsedDocument = { ...doc };
241
+ // Validate the document against the create schema if unsafe is false
253
242
  if (!unsafe) {
254
243
  try {
255
- if (!this.createSchema) {
244
+ // If no create schema is defined, throw an error.
245
+ // In safe mode, a schema is required to validate the document.
246
+ if (!this.createSchema)
256
247
  throw new Error('No schema defined for insert operation. This is either an internal interface error or you should pass unsafe=true to the insert operation.');
257
- }
258
- parsedDocument = this.createSchema.parse(newDocument);
248
+ // Validate the document against the create schema
249
+ parsedDocument = this.createSchema.parse(parsedDocument);
259
250
  }
260
251
  catch (error) {
261
252
  throw new HttpException(HttpStatus.BAD_REQUEST, error.message, { cause: error });
262
253
  }
263
254
  }
255
+ // Add default fields if they are missing from the original document
256
+ if (!doc.created_at)
257
+ parsedDocument.created_at = Dates.now('utc').unix_timestamp;
258
+ if (!doc.created_by)
259
+ parsedDocument.created_by = 'system';
260
+ if (!doc.updated_at)
261
+ parsedDocument.updated_at = Dates.now('utc').unix_timestamp;
262
+ if (!doc.updated_by)
263
+ parsedDocument.updated_by = 'system';
264
+ // Add the ID if it is missing from the original document
265
+ // If the document is missing any default fields, add them
266
+ if (!doc._id) {
267
+ parsedDocument._id = generateRandomString({ length: 5 });
268
+ while (await this.findById(parsedDocument._id)) {
269
+ parsedDocument._id = generateRandomString({ length: 5 });
270
+ }
271
+ }
272
+ // Attempt to insert the document into the collection
264
273
  const result = await this.mongoCollection.insertOne(parsedDocument, options);
265
- if (!result.acknowledged) {
274
+ // Check if the insert operation was acknowledged
275
+ if (!result.acknowledged)
266
276
  throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR, 'Failed to insert document', result);
267
- }
277
+ // If returnResult is false, return the insert result directly
268
278
  if (options && options.returnResult === false)
269
279
  return result;
270
- const inserted_doc = await this.findOne({ _id: { $eq: result.insertedId } }, options);
271
- if (!inserted_doc) {
280
+ // Otherwise, fetch and return the inserted document
281
+ const insertedDoc = await this.findOne({ _id: { $eq: result.insertedId } }, options);
282
+ if (!insertedDoc)
272
283
  throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR, 'Failed to find inserted document', result);
273
- }
274
- return inserted_doc;
284
+ return insertedDoc;
275
285
  }
276
286
  /**
277
287
  * Checks if a document with the given ID is locked or not.
@@ -1,5 +1,5 @@
1
1
  import { MongoCollectionClass } from '../../common/mongo-collection.js';
2
- import { CreateVerificationTokenDto, UpdateVerificationTokenDto, VerificationToken } from '@tmlmobilidade/types';
2
+ import { type CreateVerificationTokenDto, type UpdateVerificationTokenDto, type VerificationToken } from '@tmlmobilidade/types';
3
3
  import { IndexDescription } from 'mongodb';
4
4
  import { z } from 'zod';
5
5
  declare class VerificationTokensClass extends MongoCollectionClass<VerificationToken, CreateVerificationTokenDto, UpdateVerificationTokenDto> {
@@ -10,9 +10,8 @@ declare class VerificationTokensClass extends MongoCollectionClass<VerificationT
10
10
  static getInstance(): Promise<VerificationTokensClass>;
11
11
  /**
12
12
  * Finds a verification token by its token.
13
- *
14
- * @param token - The token to find
15
- * @returns The verification token or null if not found
13
+ * @param token The token to find.
14
+ * @returns The verification token or null if not found.
16
15
  */
17
16
  findByToken(token: string): Promise<import("mongodb").WithId<{
18
17
  _id: string;
@@ -1,12 +1,12 @@
1
1
  /* * */
2
2
  import { MongoCollectionClass } from '../../common/mongo-collection.js';
3
- import { VerificationTokenSchema } from '@tmlmobilidade/types';
3
+ import { CreateVerificationTokenSchema, UpdateVerificationTokenSchema } from '@tmlmobilidade/types';
4
4
  import { AsyncSingletonProxy } from '@tmlmobilidade/utils';
5
5
  /* * */
6
6
  class VerificationTokensClass extends MongoCollectionClass {
7
7
  static _instance;
8
- createSchema = VerificationTokenSchema;
9
- updateSchema = VerificationTokenSchema;
8
+ createSchema = CreateVerificationTokenSchema;
9
+ updateSchema = UpdateVerificationTokenSchema;
10
10
  constructor() {
11
11
  super();
12
12
  }
@@ -20,9 +20,8 @@ class VerificationTokensClass extends MongoCollectionClass {
20
20
  }
21
21
  /**
22
22
  * Finds a verification token by its token.
23
- *
24
- * @param token - The token to find
25
- * @returns The verification token or null if not found
23
+ * @param token The token to find.
24
+ * @returns The verification token or null if not found.
26
25
  */
27
26
  async findByToken(token) {
28
27
  return this.findOne({ token });
@@ -150,15 +150,13 @@ class AuthProvider {
150
150
  */
151
151
  async register(createUserDto) {
152
152
  // Insert the new user into the database with the provided data
153
- const insertNewUserResult = await users.insertOne({ ...createUserDto });
153
+ const insertNewUserResult = await users.insertOne(createUserDto);
154
154
  // Generate a random token that will be used to verify the user
155
155
  const verificationToken = generateRandomToken();
156
156
  // Insert the verification token into the database
157
157
  await verificationTokens.insertOne({
158
- created_by: 'system',
159
158
  expires_at: Dates.now('utc').plus({ days: 7 }).unix_timestamp,
160
159
  token: verificationToken,
161
- updated_by: 'system',
162
160
  user_id: insertNewUserResult._id,
163
161
  });
164
162
  // Send a welcome email to the user with the verification token
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/interfaces",
3
- "version": "20251218.110.37",
3
+ "version": "20251222.1537.24",
4
4
  "author": {
5
5
  "email": "iso@tmlmobilidade.pt",
6
6
  "name": "TML-ISO"
@@ -46,8 +46,8 @@
46
46
  "@tmlmobilidade/strings": "*",
47
47
  "@tmlmobilidade/utils": "*",
48
48
  "bcryptjs": "3.0.3",
49
- "oci-common": "2.122.1",
50
- "oci-objectstorage": "2.122.1",
49
+ "oci-common": "2.122.2",
50
+ "oci-objectstorage": "2.122.2",
51
51
  "zod": "3.25.76"
52
52
  },
53
53
  "devDependencies": {