@tmlmobilidade/interfaces 20251217.1540.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.
@@ -1,7 +1,7 @@
1
- import { AggregationPipeline } from './aggregation-pipeline.js';
1
+ import { type AggregationPipeline } from './aggregation-pipeline.js';
2
2
  import { MongoConnector } from '@tmlmobilidade/mongo';
3
3
  import { type UnixTimestamp } from '@tmlmobilidade/types';
4
- import { AggregateOptions, AggregationCursor, Collection, DeleteOptions, DeleteResult, Document, Filter, FindOptions, IndexDescription, InsertManyResult, InsertOneOptions, InsertOneResult, MongoClientOptions, UpdateOptions, UpdateResult, WithId } from 'mongodb';
4
+ import { type AggregateOptions, type AggregationCursor, type Collection, type DeleteOptions, type DeleteResult, type Document, type Filter, type FindOptions, type IndexDescription, type InsertManyResult, type InsertOneOptions, type InsertOneResult, type MongoClientOptions, type UpdateOptions, type UpdateResult, type WithId } from 'mongodb';
5
5
  import { z } from 'zod';
6
6
  export declare abstract class MongoCollectionClass<T extends Document, TCreate, TUpdate> {
7
7
  protected createSchema: null | z.ZodSchema;
@@ -10,9 +10,9 @@ export declare abstract class MongoCollectionClass<T extends Document, TCreate,
10
10
  protected updateSchema: null | z.ZodSchema;
11
11
  /**
12
12
  * Aggregates documents in the collection.
13
- * @param pipeline - The aggregation pipeline to execute
14
- * @param options - The options for the aggregation operation
15
- * @returns A promise that resolves to an array of aggregated documents
13
+ * @param pipeline The aggregation pipeline to execute.
14
+ * @param options The options for the aggregation operation.
15
+ * @returns A promise that resolves to an array of aggregated documents.
16
16
  */
17
17
  aggregate(pipeline: AggregationPipeline<T>, options?: AggregateOptions & {
18
18
  returnResult?: true;
@@ -27,22 +27,24 @@ export declare abstract class MongoCollectionClass<T extends Document, TCreate,
27
27
  all(): Promise<WithId<T>[]>;
28
28
  /**
29
29
  * Establishes a connection to the Mongo database and initializes the collection.
30
- * @param options Optional Mongo client connection options
31
- * @throws {Error} If connection fails
30
+ * @param options Optional Mongo client connection options.
31
+ * @throws Error if the environment variable for the database URI is missing or if the connection fails.
32
32
  */
33
33
  connect(options?: MongoClientOptions): Promise<void>;
34
34
  /**
35
35
  * Counts documents matching the filter criteria.
36
- * @param filter - The filter criteria to match documents
37
- * @returns A promise that resolves to the count of matching documents
36
+ * @param filter The filter criteria to match documents.
37
+ * @returns A promise that resolves to the count of matching documents.
38
38
  */
39
39
  count(filter?: Filter<T>): Promise<number>;
40
40
  /**
41
41
  * Deletes a single document by its ID.
42
- * @param id - The ID of the document to delete
43
- * @returns A promise that resolves to the result of the delete operation
42
+ * @param id The ID of the document to delete.
43
+ * @returns A promise that resolves to the result of the delete operation.
44
44
  */
45
- deleteById(id: string, options?: DeleteOptions): Promise<DeleteResult>;
45
+ deleteById(id: T['_id'], options?: DeleteOptions & {
46
+ forceIfLocked?: boolean;
47
+ }): Promise<DeleteResult>;
46
48
  /**
47
49
  * Deletes multiple documents matching the filter criteria.
48
50
  * @param filter - The filter criteria to match documents to delete
@@ -54,7 +56,9 @@ export declare abstract class MongoCollectionClass<T extends Document, TCreate,
54
56
  * @param filter - The filter criteria to match the document to delete
55
57
  * @returns A promise that resolves to the result of the delete operation
56
58
  */
57
- deleteOne(filter: Filter<T>, options?: DeleteOptions): Promise<DeleteResult>;
59
+ deleteOne(filter: Filter<T>, options?: DeleteOptions & {
60
+ forceIfLocked?: boolean;
61
+ }): Promise<DeleteResult>;
58
62
  /**
59
63
  * Disconnects from the MongoDB database.
60
64
  */
@@ -74,29 +78,29 @@ export declare abstract class MongoCollectionClass<T extends Document, TCreate,
74
78
  exists<K extends keyof T>(key: K, value: T[K]): Promise<boolean>;
75
79
  /**
76
80
  * Checks if a document with the given ID exists in the collection.
77
- * @param _id The ID of the document to check for existence.
81
+ * @param id The ID of the document to check for existence.
78
82
  * @returns A promise that resolves to true if the document exists, false otherwise.
79
83
  */
80
- existsById(_id: T['_id']): Promise<boolean>;
84
+ existsById(id: T['_id']): Promise<boolean>;
81
85
  /**
82
86
  * Finds a document by its ID.
83
- * @param _id The ID of the document to find.
87
+ * @param id The ID of the document to find.
84
88
  * @param options Optional find options.
85
- * @returns A promise that resolves to the matching document or null if not found
89
+ * @returns A promise that resolves to the matching document or null if not found.
86
90
  */
87
- findById(_id: T['_id'], options?: FindOptions): Promise<null | WithId<T>>;
91
+ findById(id: T['_id'], options?: FindOptions): Promise<null | WithId<T>>;
88
92
  /**
89
93
  * Finds multiple documents matching the filter criteria with optional pagination and sorting.
90
- * @param filter - (Optional) filter criteria to match documents
91
- * @param options - (Optional) find options
92
- * @returns A promise that resolves to an array of matching documents
94
+ * @param filter (Optional) filter criteria to match documents.
95
+ * @param options (Optional) find options.
96
+ * @returns A promise that resolves to an array of matching documents.
93
97
  */
94
98
  findMany(filter?: Filter<T>, options?: FindOptions): Promise<WithId<T>[]>;
95
99
  /**
96
100
  * Finds a single document matching the filter criteria.
97
- * @param filter - The filter criteria to match the document
98
- * @param options - (Optional) find options
99
- * @returns A promise that resolves to the matching document or null if not found
101
+ * @param filter The filter criteria to match the document.
102
+ * @param options (Optional) find options.
103
+ * @returns A promise that resolves to the matching document or null if not found.
100
104
  */
101
105
  findOne(filter: Filter<T>, options?: FindOptions): Promise<null | WithId<T>>;
102
106
  /**
@@ -127,9 +131,9 @@ export declare abstract class MongoCollectionClass<T extends Document, TCreate,
127
131
  }): Promise<InsertManyResult<T>>;
128
132
  /**
129
133
  * Inserts a single document into the collection.
130
- * @param doc - The document to insert
131
- * @param options - The options for the insert operation
132
- * @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.
133
137
  */
134
138
  insertOne<TReturnDocument extends boolean = true>(doc: TCreate & {
135
139
  _id?: string;
@@ -143,14 +147,34 @@ export declare abstract class MongoCollectionClass<T extends Document, TCreate,
143
147
  };
144
148
  unsafe?: boolean;
145
149
  }): Promise<TReturnDocument extends true ? WithId<T> : InsertOneResult<T>>;
150
+ /**
151
+ * Checks if a document with the given ID is locked or not.
152
+ * @param id The ID of the document to check.
153
+ * @returns A promise that resolves to the result of the check operation.
154
+ */
155
+ isLocked(filter: Filter<T>): Promise<boolean>;
156
+ /**
157
+ * Checks if a document with the given ID is locked or not.
158
+ * @param id The ID of the document to check.
159
+ * @returns A promise that resolves to the result of the check operation.
160
+ */
161
+ isLockedById(id: T['_id']): Promise<boolean>;
162
+ /**
163
+ * Toogle the lock status of a document by its ID.
164
+ * @param id The ID of the document to toggle lock status.
165
+ * @param forceValue Optional boolean to explicitly set the lock status.
166
+ * @returns A promise that resolves to the result of the update operation.
167
+ */
168
+ toggleLockById(id: T['_id'], forceValue?: boolean): Promise<void>;
146
169
  /**
147
170
  * Updates a document by its ID.
148
- * @param _id The ID of the document to update.
171
+ * @param id The ID of the document to update.
149
172
  * @param updateFields The fields to update in the document.
150
173
  * @param options Optional options for the update operation.
151
174
  * @returns A promise that resolves to the result of the update operation.
152
175
  */
153
- updateById<TReturnDocument extends boolean = true>(_id: T['_id'], updateFields: TUpdate, options?: UpdateOptions & {
176
+ updateById<TReturnDocument extends boolean = true>(id: T['_id'], updateFields: TUpdate, options?: UpdateOptions & {
177
+ forceIfLocked?: boolean;
154
178
  returnResult?: TReturnDocument;
155
179
  }): Promise<TReturnDocument extends true ? WithId<T> : UpdateResult<T>>;
156
180
  /**
@@ -168,12 +192,13 @@ export declare abstract class MongoCollectionClass<T extends Document, TCreate,
168
192
  }): Promise<TReturnDocument extends true ? WithId<T>[] : UpdateResult<T>>;
169
193
  /**
170
194
  * Updates a single document matching the filter criteria.
171
- * @param filter - The filter criteria to match the document to update
172
- * @param updateFields - The fields to update in the document
173
- * @param options - The options for the update operation
174
- * @returns A promise that resolves to the result of the update operation
195
+ * @param filter The filter criteria to match the document to update.
196
+ * @param updateFields The fields to update in the document.
197
+ * @param options The options for the update operation.
198
+ * @returns A promise that resolves to the result of the update operation.
175
199
  */
176
200
  updateOne<TReturnDocument extends boolean = true>(filter: Filter<T>, updateFields: TUpdate, options?: UpdateOptions & {
201
+ forceIfLocked?: boolean;
177
202
  returnResult?: TReturnDocument;
178
203
  }): Promise<TReturnDocument extends true ? WithId<T> : UpdateResult<T>>;
179
204
  protected abstract getCollectionIndexes(): IndexDescription[];
@@ -5,16 +5,19 @@ import { MongoConnector } from '@tmlmobilidade/mongo';
5
5
  import { generateRandomString } from '@tmlmobilidade/strings';
6
6
  /* * */
7
7
  export class MongoCollectionClass {
8
+ //
8
9
  createSchema = null;
9
10
  mongoCollection;
10
11
  mongoConnector;
11
12
  updateSchema = null;
12
13
  async aggregate(pipeline, options) {
13
- const aggregation = this.mongoCollection.aggregate(pipeline, options);
14
- if (options?.returnResult === false) {
15
- return aggregation;
16
- }
17
- return aggregation.toArray();
14
+ // Perform the aggregation pipeline
15
+ const aggregationResult = this.mongoCollection.aggregate(pipeline, options);
16
+ // If returnResult is false, return the cursor directly
17
+ if (options?.returnResult === false)
18
+ return aggregationResult;
19
+ // Otherwise, return the aggregated documents as an array
20
+ return aggregationResult.toArray();
18
21
  }
19
22
  /**
20
23
  * Gets all documents in the collection.
@@ -25,15 +28,15 @@ export class MongoCollectionClass {
25
28
  }
26
29
  /**
27
30
  * Establishes a connection to the Mongo database and initializes the collection.
28
- * @param options Optional Mongo client connection options
29
- * @throws {Error} If connection fails
31
+ * @param options Optional Mongo client connection options.
32
+ * @throws Error if the environment variable for the database URI is missing or if the connection fails.
30
33
  */
31
34
  async connect(options) {
32
- //
35
+ // Extract the database URI from environment variables
33
36
  const dbUri = process.env[this.getEnvName()];
34
- if (!dbUri) {
37
+ if (!dbUri)
35
38
  throw new Error(`Missing ${this.getEnvName()} environment variable`);
36
- }
39
+ // Attempt to connect to the database
37
40
  try {
38
41
  // Connect to the MongoDB database
39
42
  this.mongoConnector = new MongoConnector(dbUri, options);
@@ -52,22 +55,31 @@ export class MongoCollectionClass {
52
55
  }
53
56
  /**
54
57
  * Counts documents matching the filter criteria.
55
- * @param filter - The filter criteria to match documents
56
- * @returns A promise that resolves to the count of matching documents
58
+ * @param filter The filter criteria to match documents.
59
+ * @returns A promise that resolves to the count of matching documents.
57
60
  */
58
61
  async count(filter) {
59
62
  return await this.mongoCollection.countDocuments(filter);
60
63
  }
61
64
  /**
62
65
  * Deletes a single document by its ID.
63
- * @param id - The ID of the document to delete
64
- * @returns A promise that resolves to the result of the delete operation
66
+ * @param id The ID of the document to delete.
67
+ * @returns A promise that resolves to the result of the delete operation.
65
68
  */
66
69
  async deleteById(id, options) {
70
+ // If forceIfLocked is not set then check if the document is locked.
71
+ // If it is locked, then throw an error to prevent the operation.
72
+ if (!options?.forceIfLocked) {
73
+ const isLocked = await this.isLockedById(id);
74
+ if (isLocked)
75
+ throw new HttpException(HttpStatus.FORBIDDEN, 'Document is locked and cannot be deleted');
76
+ }
77
+ // Perform the delete operation
67
78
  const result = await this.deleteOne({ _id: { $eq: id } }, options);
68
- if (!result.acknowledged) {
79
+ // Check if the delete operation was acknowledged
80
+ if (!result.acknowledged)
69
81
  throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR, 'Failed to delete documents', result);
70
- }
82
+ // Return the result of the delete operation
71
83
  return result;
72
84
  }
73
85
  /**
@@ -77,9 +89,8 @@ export class MongoCollectionClass {
77
89
  */
78
90
  async deleteMany(filter) {
79
91
  const result = await this.mongoCollection.deleteMany(filter);
80
- if (!result.acknowledged) {
92
+ if (!result.acknowledged)
81
93
  throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR, 'Failed to delete documents', result);
82
- }
83
94
  return result;
84
95
  }
85
96
  /**
@@ -88,10 +99,17 @@ export class MongoCollectionClass {
88
99
  * @returns A promise that resolves to the result of the delete operation
89
100
  */
90
101
  async deleteOne(filter, options) {
102
+ // If forceIfLocked is not set then check if the document is locked.
103
+ // If it is locked, then throw an error to prevent the operation.
104
+ if (!options?.forceIfLocked) {
105
+ const isLocked = await this.isLocked(filter);
106
+ if (isLocked)
107
+ throw new HttpException(HttpStatus.FORBIDDEN, 'Document is locked and cannot be deleted');
108
+ }
109
+ // Perform the delete operation
91
110
  const result = await this.mongoCollection.deleteOne(filter, options);
92
- if (!result.acknowledged) {
111
+ if (!result.acknowledged)
93
112
  throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR, 'Failed to delete document', result);
94
- }
95
113
  return result;
96
114
  }
97
115
  /**
@@ -121,37 +139,36 @@ export class MongoCollectionClass {
121
139
  }
122
140
  /**
123
141
  * Checks if a document with the given ID exists in the collection.
124
- * @param _id The ID of the document to check for existence.
142
+ * @param id The ID of the document to check for existence.
125
143
  * @returns A promise that resolves to true if the document exists, false otherwise.
126
144
  */
127
- async existsById(_id) {
128
- const doc = await this.mongoCollection.findOne({ _id: _id }, { projection: { _id: 1 } });
129
- return doc !== null;
145
+ async existsById(id) {
146
+ const foundDoc = await this.mongoCollection.findOne({ _id: id }, { projection: { _id: 1 } });
147
+ return foundDoc !== null;
130
148
  }
131
149
  /**
132
150
  * Finds a document by its ID.
133
- * @param _id The ID of the document to find.
151
+ * @param id The ID of the document to find.
134
152
  * @param options Optional find options.
135
- * @returns A promise that resolves to the matching document or null if not found
153
+ * @returns A promise that resolves to the matching document or null if not found.
136
154
  */
137
- async findById(_id, options) {
138
- const filter = { _id: { $eq: _id } };
139
- return this.mongoCollection.findOne(filter, options);
155
+ async findById(id, options) {
156
+ return this.mongoCollection.findOne({ _id: { $eq: id } }, options);
140
157
  }
141
158
  /**
142
159
  * Finds multiple documents matching the filter criteria with optional pagination and sorting.
143
- * @param filter - (Optional) filter criteria to match documents
144
- * @param options - (Optional) find options
145
- * @returns A promise that resolves to an array of matching documents
160
+ * @param filter (Optional) filter criteria to match documents.
161
+ * @param options (Optional) find options.
162
+ * @returns A promise that resolves to an array of matching documents.
146
163
  */
147
164
  async findMany(filter, options) {
148
165
  return await this.mongoCollection.find(filter ?? {}, options).toArray();
149
166
  }
150
167
  /**
151
168
  * Finds a single document matching the filter criteria.
152
- * @param filter - The filter criteria to match the document
153
- * @param options - (Optional) find options
154
- * @returns A promise that resolves to the matching document or null if not found
169
+ * @param filter The filter criteria to match the document.
170
+ * @param options (Optional) find options.
171
+ * @returns A promise that resolves to the matching document or null if not found.
155
172
  */
156
173
  async findOne(filter, options) {
157
174
  return await this.mongoCollection.findOne(filter, options);
@@ -214,58 +231,121 @@ export class MongoCollectionClass {
214
231
  }
215
232
  /**
216
233
  * Inserts a single document into the collection.
217
- * @param doc - The document to insert
218
- * @param options - The options for the insert operation
219
- * @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.
220
237
  */
221
238
  async insertOne(doc, { options, unsafe = false } = {}) {
222
- const newDocument = {
223
- ...doc,
224
- _id: doc._id || generateRandomString({ length: 5 }),
225
- created_at: doc.created_at || Dates.now('utc').unix_timestamp,
226
- created_by: doc.created_by || 'system',
227
- updated_at: doc.updated_at || Dates.now('utc').unix_timestamp,
228
- updated_by: doc.updated_by || 'system',
229
- };
230
- if (!doc._id) {
231
- while (await this.findById(newDocument._id)) {
232
- newDocument._id = generateRandomString({ length: 5 });
233
- }
234
- }
235
- 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
236
242
  if (!unsafe) {
237
243
  try {
238
- 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)
239
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.');
240
- }
241
- parsedDocument = this.createSchema.parse(newDocument);
248
+ // Validate the document against the create schema
249
+ parsedDocument = this.createSchema.parse(parsedDocument);
242
250
  }
243
251
  catch (error) {
244
252
  throw new HttpException(HttpStatus.BAD_REQUEST, error.message, { cause: error });
245
253
  }
246
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
247
273
  const result = await this.mongoCollection.insertOne(parsedDocument, options);
248
- if (!result.acknowledged) {
274
+ // Check if the insert operation was acknowledged
275
+ if (!result.acknowledged)
249
276
  throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR, 'Failed to insert document', result);
250
- }
277
+ // If returnResult is false, return the insert result directly
251
278
  if (options && options.returnResult === false)
252
279
  return result;
253
- const inserted_doc = await this.findOne({ _id: { $eq: result.insertedId } }, options);
254
- 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)
255
283
  throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR, 'Failed to find inserted document', result);
256
- }
257
- return inserted_doc;
284
+ return insertedDoc;
285
+ }
286
+ /**
287
+ * Checks if a document with the given ID is locked or not.
288
+ * @param id The ID of the document to check.
289
+ * @returns A promise that resolves to the result of the check operation.
290
+ */
291
+ async isLocked(filter) {
292
+ // Fetch the document by its ID from the database
293
+ const foundDoc = await this.findOne(filter);
294
+ // If the document has a is_locked field and it resolves to a truthy value,
295
+ // then the document is considered locked.
296
+ if (foundDoc?.is_locked)
297
+ return true;
298
+ // Otherwise, the document is not locked.
299
+ return false;
300
+ }
301
+ /**
302
+ * Checks if a document with the given ID is locked or not.
303
+ * @param id The ID of the document to check.
304
+ * @returns A promise that resolves to the result of the check operation.
305
+ */
306
+ async isLockedById(id) {
307
+ // Fetch the document by its ID from the database
308
+ const foundDoc = await this.findById(id);
309
+ // If the document has a is_locked field and it resolves to a truthy value,
310
+ // then the document is considered locked.
311
+ if (foundDoc?.is_locked)
312
+ return true;
313
+ // Otherwise, the document is not locked.
314
+ return false;
315
+ }
316
+ /**
317
+ * Toogle the lock status of a document by its ID.
318
+ * @param id The ID of the document to toggle lock status.
319
+ * @param forceValue Optional boolean to explicitly set the lock status.
320
+ * @returns A promise that resolves to the result of the update operation.
321
+ */
322
+ async toggleLockById(id, forceValue) {
323
+ // Get the current document from the database
324
+ const foundDoc = await this.findById(id);
325
+ if (!foundDoc)
326
+ throw new Error('Document not found');
327
+ // Determine the new lock status
328
+ const newLockStatus = forceValue !== undefined ? forceValue : !foundDoc.is_locked;
329
+ // Update the document with the new lock status
330
+ await this.mongoCollection.updateOne({ _id: { $eq: id } }, { $set: { is_locked: newLockStatus } });
258
331
  }
259
332
  /**
260
333
  * Updates a document by its ID.
261
- * @param _id The ID of the document to update.
334
+ * @param id The ID of the document to update.
262
335
  * @param updateFields The fields to update in the document.
263
336
  * @param options Optional options for the update operation.
264
337
  * @returns A promise that resolves to the result of the update operation.
265
338
  */
266
- async updateById(_id, updateFields, options) {
267
- const filter = { _id: { $eq: _id } };
268
- return this.updateOne(filter, updateFields, options);
339
+ async updateById(id, updateFields, options) {
340
+ // If forceIfLocked is not set then check if the document is locked.
341
+ // If it is locked, then throw an error to prevent the operation.
342
+ if (!options?.forceIfLocked) {
343
+ const isLocked = await this.isLockedById(id);
344
+ if (isLocked)
345
+ throw new HttpException(HttpStatus.FORBIDDEN, 'Document is locked and cannot be updated');
346
+ }
347
+ // Perform the update operation
348
+ return this.updateOne({ _id: { $eq: id } }, updateFields, options);
269
349
  }
270
350
  /**
271
351
  * Updates multiple documents matching the filter criteria.
@@ -302,12 +382,20 @@ export class MongoCollectionClass {
302
382
  }
303
383
  /**
304
384
  * Updates a single document matching the filter criteria.
305
- * @param filter - The filter criteria to match the document to update
306
- * @param updateFields - The fields to update in the document
307
- * @param options - The options for the update operation
308
- * @returns A promise that resolves to the result of the update operation
385
+ * @param filter The filter criteria to match the document to update.
386
+ * @param updateFields The fields to update in the document.
387
+ * @param options The options for the update operation.
388
+ * @returns A promise that resolves to the result of the update operation.
309
389
  */
310
390
  async updateOne(filter, updateFields, options) {
391
+ // If forceIfLocked is not set then check if the document is locked.
392
+ // If it is locked, then throw an error to prevent the operation.
393
+ if (!options?.forceIfLocked) {
394
+ const isLocked = await this.isLocked(filter);
395
+ if (isLocked)
396
+ throw new HttpException(HttpStatus.FORBIDDEN, 'Document is locked and cannot be updated');
397
+ }
398
+ // Perform the update operation
311
399
  let parsedUpdateFields = updateFields;
312
400
  if (this.updateSchema) {
313
401
  try {
@@ -13,6 +13,7 @@ declare class AgenciesClass extends MongoCollectionClass<Agency, CreateAgencyDto
13
13
  created_at: number & {
14
14
  __brand: "UnixTimestamp";
15
15
  };
16
+ is_locked: boolean;
16
17
  updated_at: number & {
17
18
  __brand: "UnixTimestamp";
18
19
  };
@@ -24,9 +25,7 @@ declare class AgenciesClass extends MongoCollectionClass<Agency, CreateAgencyDto
24
25
  vkm_per_month: number[];
25
26
  };
26
27
  name: string;
27
- operation_start_date: string & {
28
- __brand: "OperationalDate";
29
- };
28
+ operation_start_date: import("@tmlmobilidade/types").OperationalDate | null;
30
29
  phone: string;
31
30
  public_email: string;
32
31
  short_name: string;
@@ -40,6 +39,7 @@ declare class AgenciesClass extends MongoCollectionClass<Agency, CreateAgencyDto
40
39
  created_at: number & {
41
40
  __brand: "UnixTimestamp";
42
41
  };
42
+ is_locked: boolean;
43
43
  updated_at: number & {
44
44
  __brand: "UnixTimestamp";
45
45
  };
@@ -51,9 +51,7 @@ declare class AgenciesClass extends MongoCollectionClass<Agency, CreateAgencyDto
51
51
  vkm_per_month: number[];
52
52
  };
53
53
  name: string;
54
- operation_start_date: string & {
55
- __brand: "OperationalDate";
56
- };
54
+ operation_start_date: import("@tmlmobilidade/types").OperationalDate | null;
57
55
  phone: string;
58
56
  public_email: string;
59
57
  short_name: string;