@loomcore/api 0.0.46 → 0.0.48

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.
@@ -195,16 +195,6 @@ export class GenericApiService {
195
195
  const entities = await this.onBeforeUpdate(userContext, preparedEntities);
196
196
  const operations = [];
197
197
  const entityIds = [];
198
- console.log('--- DIAGNOSTICS: Entities received in batchUpdate ---');
199
- entities.forEach((entity, index) => {
200
- const entityWithId = entity;
201
- if (entityWithId && entityWithId._id) {
202
- console.log(` [${index}]: ID is ${entityWithId._id.toString()}, Type is ${entityWithId._id.constructor.name}`);
203
- }
204
- else {
205
- console.log(` [${index}]: Entity has no _id or is null.`);
206
- }
207
- });
208
198
  for (const entity of entities) {
209
199
  const { _id, ...updateData } = entity;
210
200
  if (!_id || !(_id instanceof ObjectId)) {
@@ -380,7 +370,6 @@ export class GenericApiService {
380
370
  return transformedEntity;
381
371
  }
382
372
  stripSenderProvidedSystemProperties(userContext, doc, allowId = false) {
383
- console.log(`in stripSenderProvidedSystemProperties, allowId is ${allowId}`);
384
373
  const isSystemUser = userContext.user?._id === 'system';
385
374
  if (isSystemUser) {
386
375
  return;
@@ -404,21 +393,11 @@ export class GenericApiService {
404
393
  }
405
394
  }
406
395
  async prepareDataForBatchUpdate(userContext, entities) {
407
- console.log('--- DIAGNOSTICS: Executing prepareDataForBatchUpdate ---');
408
- return Promise.all(entities.map(item => {
409
- const allowId = true;
410
- console.log(`in prepareDataForBatchUpdate, item is ${JSON.stringify(item)}, and allowId is ${allowId}`);
411
- return this.prepareEntity(userContext, item, false, allowId);
412
- }));
396
+ return Promise.all(entities.map(item => this.prepareEntity(userContext, item, false, true)));
413
397
  }
414
398
  async prepareEntity(userContext, entity, isCreate, allowId = false) {
415
- console.log(`ENTRY: prepareEntity called with allowId = ${allowId}`);
416
399
  const preparedEntity = _.clone(entity);
417
- console.log(`in prepareEntity, pluralResourceName is ${this.pluralResourceName}`);
418
- console.log(`before stripping system properties, preparedEntity is ${JSON.stringify(preparedEntity)}`);
419
- console.log(`allowId is ${allowId}`);
420
400
  this.stripSenderProvidedSystemProperties(userContext, preparedEntity, allowId);
421
- console.log(`after stripping system properties, preparedEntity is ${JSON.stringify(preparedEntity)}`);
422
401
  if (this.modelSpec?.isAuditable) {
423
402
  if (isCreate) {
424
403
  this.auditForCreate(userContext, preparedEntity);
@@ -431,27 +410,19 @@ export class GenericApiService {
431
410
  if (this.modelSpec) {
432
411
  let entityId = null;
433
412
  if (allowId) {
434
- console.log(`_id is present and equals ${preparedEntity._id}`);
435
413
  entityId = preparedEntity._id;
436
414
  }
415
+ console.log(`preparedEntity is ${JSON.stringify(preparedEntity)}`);
437
416
  cleanedEntity = this.modelSpec.decode(preparedEntity);
417
+ console.log(`cleanedEntity is ${JSON.stringify(cleanedEntity)}`);
438
418
  if (allowId && entityId) {
439
- console.log(`after decode, restoring _id to ${entityId}`);
440
419
  cleanedEntity._id = entityId;
441
420
  }
442
421
  }
443
422
  if (!this.modelSpec?.fullSchema) {
444
423
  throw new ServerError(`Cannot prepare entity: No model specification with schema provided for ${this.pluralResourceName}`);
445
424
  }
446
- const finalEntity = dbUtils.convertStringsToObjectIds(cleanedEntity, this.modelSpec.fullSchema);
447
- const finalEntityWithId = finalEntity;
448
- if (finalEntityWithId && finalEntityWithId._id) {
449
- console.log(`--- DIAGNOSTICS: In prepareEntity, after conversion, ID is ${finalEntityWithId._id.toString()}, Type is ${finalEntityWithId._id.constructor.name} ---`);
450
- }
451
- else {
452
- console.log('--- DIAGNOSTICS: In prepareEntity, after conversion, entity has no _id or is null. ---');
453
- }
454
- return finalEntity;
425
+ return dbUtils.convertStringsToObjectIds(cleanedEntity, this.modelSpec.fullSchema);
455
426
  }
456
427
  prepareQuery(userContext, query) {
457
428
  return query;
@@ -6,5 +6,5 @@ export declare class MultiTenantApiService<T extends IEntity> extends GenericApi
6
6
  constructor(db: Db, pluralResourceName: string, singularResourceName: string, modelSpec?: IModelSpec);
7
7
  protected prepareQuery(userContext: IUserContext, query: any): any;
8
8
  protected prepareQueryOptions(userContext: IUserContext, queryOptions: IQueryOptions): IQueryOptions;
9
- protected prepareEntity(userContext: IUserContext, entity: T, isCreate: boolean): Promise<T | Partial<T>>;
9
+ protected prepareEntity(userContext: IUserContext, entity: T, isCreate: boolean, allowId?: boolean): Promise<T | Partial<T>>;
10
10
  }
@@ -28,14 +28,14 @@ export class MultiTenantApiService extends GenericApiService {
28
28
  }
29
29
  return this.tenantDecorator.applyTenantToQueryOptions(userContext, queryOptions, this.pluralResourceName);
30
30
  }
31
- async prepareEntity(userContext, entity, isCreate) {
31
+ async prepareEntity(userContext, entity, isCreate, allowId = false) {
32
32
  if (!config?.app?.isMultiTenant) {
33
- return super.prepareEntity(userContext, entity, isCreate);
33
+ return super.prepareEntity(userContext, entity, isCreate, allowId);
34
34
  }
35
35
  if (!userContext || !userContext._orgId) {
36
36
  throw new BadRequestError('A valid userContext was not provided to MultiTenantApiService.prepareEntity');
37
37
  }
38
- const preparedEntity = await super.prepareEntity(userContext, entity, isCreate);
38
+ const preparedEntity = await super.prepareEntity(userContext, entity, isCreate, allowId);
39
39
  const orgIdField = this.tenantDecorator.getOrgIdField();
40
40
  preparedEntity[orgIdField] = userContext._orgId;
41
41
  return preparedEntity;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loomcore/api",
3
- "version": "0.0.46",
3
+ "version": "0.0.48",
4
4
  "private": false,
5
5
  "description": "Loom Core Api - An opinionated Node.js api using Typescript, Express, and MongoDb",
6
6
  "scripts": {