@tmlmobilidade/interfaces 20250924.1708.22 → 20250925.1818.18

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,6 +1,6 @@
1
1
  import { MongoCollectionClass } from '../../mongo-collection.js';
2
2
  import { CreateRideAcceptanceDto, RideAcceptance, UpdateRideAcceptanceDto } from '@tmlmobilidade/types';
3
- import { IndexDescription } from 'mongodb';
3
+ import { IndexDescription, InsertOneOptions, UpdateOptions } from 'mongodb';
4
4
  import { z } from 'zod';
5
5
  declare class RideAcceptanceClass extends MongoCollectionClass<RideAcceptance, CreateRideAcceptanceDto, UpdateRideAcceptanceDto> {
6
6
  private static _instance;
@@ -8,12 +8,16 @@ declare class RideAcceptanceClass extends MongoCollectionClass<RideAcceptance, C
8
8
  protected updateSchema: z.ZodSchema;
9
9
  private constructor();
10
10
  static getInstance(): Promise<RideAcceptanceClass>;
11
- createByRideId(ride_id: string, data: CreateRideAcceptanceDto): Promise<RideAcceptance>;
11
+ createByRideId(ride_id: string, data: CreateRideAcceptanceDto, options?: InsertOneOptions & {
12
+ returnResult?: boolean;
13
+ }): Promise<RideAcceptance>;
12
14
  findByRideId(ride_id: string): Promise<null | RideAcceptance>;
13
- updateByRideId(ride_id: string, data: UpdateRideAcceptanceDto): Promise<RideAcceptance>;
15
+ updateByRideId(ride_id: string, data: UpdateRideAcceptanceDto, options?: UpdateOptions & {
16
+ returnResult?: boolean;
17
+ }): Promise<RideAcceptance>;
14
18
  protected getCollectionIndexes(): IndexDescription[];
15
19
  protected getCollectionName(): string;
16
20
  protected getEnvName(): string;
17
21
  }
18
- export declare const rideAcceptances: Omit<RideAcceptanceClass, 'deleteById' | 'deleteMany' | 'deleteOne' | 'insertOne' | 'updateById' | 'updateMany' | 'updateOne'>;
22
+ export declare const rideAcceptances: Omit<RideAcceptanceClass, 'deleteById' | 'deleteMany' | 'deleteOne' | 'insertOne' | 'updateById' | 'updateOne'>;
19
23
  export {};
@@ -19,7 +19,7 @@ class RideAcceptanceClass extends MongoCollectionClass {
19
19
  }
20
20
  return RideAcceptanceClass._instance;
21
21
  }
22
- async createByRideId(ride_id, data) {
22
+ async createByRideId(ride_id, data, options = {}) {
23
23
  const currentTimestamp = Dates.now('utc').unix_timestamp;
24
24
  const createdBy = data.created_by || 'system';
25
25
  data.comments.push({
@@ -38,12 +38,12 @@ class RideAcceptanceClass extends MongoCollectionClass {
38
38
  type: 'field_changed',
39
39
  updated_at: currentTimestamp,
40
40
  });
41
- return super.insertOne({ ...data, ride_id });
41
+ return super.insertOne({ ...data, ride_id }, { options });
42
42
  }
43
43
  async findByRideId(ride_id) {
44
44
  return super.findOne({ ride_id });
45
45
  }
46
- async updateByRideId(ride_id, data) {
46
+ async updateByRideId(ride_id, data, options = {}) {
47
47
  const prevAcceptance = await this.findByRideId(ride_id);
48
48
  if (!prevAcceptance) {
49
49
  throw new HttpException(HttpStatus.NOT_FOUND, 'Ride acceptance not found');
@@ -65,7 +65,7 @@ class RideAcceptanceClass extends MongoCollectionClass {
65
65
  });
66
66
  }
67
67
  }
68
- return super.updateOne({ ride_id }, data);
68
+ return super.updateOne({ ride_id }, data, options);
69
69
  }
70
70
  getCollectionIndexes() {
71
71
  return [
@@ -1,6 +1,6 @@
1
1
  import { MongoConnector } from '@tmlmobilidade/connectors';
2
2
  import { type UnixTimestamp } from '@tmlmobilidade/types';
3
- import { AggregateOptions, AggregationCursor, Collection, DeleteOptions, DeleteResult, Document, Filter, FindOptions, IndexDescription, InsertOneOptions, InsertOneResult, MongoClientOptions, UpdateOptions, UpdateResult, WithId } from 'mongodb';
3
+ import { AggregateOptions, AggregationCursor, Collection, DeleteOptions, DeleteResult, Document, Filter, FindOptions, IndexDescription, InsertManyResult, InsertOneOptions, InsertOneResult, MongoClientOptions, UpdateOptions, UpdateResult, WithId } from 'mongodb';
4
4
  import { z } from 'zod';
5
5
  import { AggregationPipeline } from './aggregation-pipeline.js';
6
6
  export declare abstract class MongoCollectionClass<T extends Document, TCreate, TUpdate> {
@@ -109,6 +109,22 @@ export declare abstract class MongoCollectionClass<T extends Document, TCreate,
109
109
  * @returns The MongoDB connector instance
110
110
  */
111
111
  getMongoConnector(): MongoConnector;
112
+ /**
113
+ * Inserts multiple documents into the collection.
114
+ * @param docs - The documents to insert
115
+ * @param options - The options for the insert operation
116
+ * @returns A promise that resolves to the result of the insert operation
117
+ */
118
+ insertMany(docs: (TCreate & {
119
+ _id?: string;
120
+ created_at?: UnixTimestamp;
121
+ created_by?: string;
122
+ updated_at?: UnixTimestamp;
123
+ updated_by?: string;
124
+ })[], { options, unsafe }?: {
125
+ options?: InsertOneOptions;
126
+ unsafe?: boolean;
127
+ }): Promise<InsertManyResult<T>>;
112
128
  /**
113
129
  * Inserts a single document into the collection.
114
130
  * @param doc - The document to insert
@@ -169,6 +169,48 @@ export class MongoCollectionClass {
169
169
  getMongoConnector() {
170
170
  return this.mongoConnector;
171
171
  }
172
+ /**
173
+ * Inserts multiple documents into the collection.
174
+ * @param docs - The documents to insert
175
+ * @param options - The options for the insert operation
176
+ * @returns A promise that resolves to the result of the insert operation
177
+ */
178
+ async insertMany(docs, { options, unsafe = false } = {}) {
179
+ const newDocuments = docs.map((doc) => {
180
+ return {
181
+ ...doc,
182
+ _id: doc._id || generateRandomString({ length: 5 }),
183
+ created_at: doc.created_at || Dates.now('utc').unix_timestamp,
184
+ created_by: doc.created_by || 'system',
185
+ updated_at: doc.updated_at || Dates.now('utc').unix_timestamp,
186
+ updated_by: doc.updated_by || 'system',
187
+ };
188
+ });
189
+ // Ensure all documents have a unique ID
190
+ const foundIds = await this.mongoCollection.find({ _id: { $in: newDocuments.map(doc => doc._id) } }, { projection: { _id: 1 } }).toArray();
191
+ for (const newDocument of newDocuments) {
192
+ if (foundIds.find(id => id._id === newDocument._id)) {
193
+ newDocument._id = generateRandomString({ length: 5 });
194
+ }
195
+ }
196
+ const parsedDocuments = [];
197
+ for (const newDocument of newDocuments) {
198
+ let parsedDocument = newDocument;
199
+ if (!unsafe) {
200
+ try {
201
+ if (!this.createSchema) {
202
+ 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.');
203
+ }
204
+ parsedDocument = this.createSchema.parse(newDocument);
205
+ }
206
+ catch (error) {
207
+ throw new HttpException(HttpStatus.BAD_REQUEST, error.message, { cause: error });
208
+ }
209
+ }
210
+ parsedDocuments.push(parsedDocument);
211
+ }
212
+ return await this.mongoCollection.insertMany(parsedDocuments, options);
213
+ }
172
214
  /**
173
215
  * Inserts a single document into the collection.
174
216
  * @param doc - The document to insert
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/interfaces",
3
- "version": "20250924.1708.22",
3
+ "version": "20250925.1818.18",
4
4
  "author": "João de Vasconcelos & Jusi Monteiro",
5
5
  "license": "AGPL-3.0-or-later",
6
6
  "homepage": "https://github.com/tmlmobilidade/services#readme",