diff --git a/packages/apollo-collaboration-server/src/changes/changes.service.ts b/packages/apollo-collaboration-server/src/changes/changes.service.ts index a2391d0fd..71b9704e9 100644 --- a/packages/apollo-collaboration-server/src/changes/changes.service.ts +++ b/packages/apollo-collaboration-server/src/changes/changes.service.ts @@ -42,6 +42,14 @@ import { MessagesGateway } from '../messages/messages.gateway' import { PluginsService } from '../plugins/plugins.service' import { FindChangeDto } from './dto/find-change.dto' +const STATUS_ZERO_CHANGE_TYPES = new Set([ + 'AddAssemblyAndFeaturesFromFileChange', + 'AddAssemblyFromExternalChange', + 'AddAssemblyFromFileChange', + 'AddFeaturesFromFileChange', + 'AddFeatureChange', +]) + export class ChangesService { constructor( @InjectModel(Feature.name) @@ -158,51 +166,54 @@ export class ChangesService { ) } }) - this.logger.debug?.('*** TEMPORARY DATA INSERTTED ***') - // Set "temporary document" -status --> "valid" -status i.e. (-1 --> 0) - await this.featureModel.db.transaction(async () => { - this.logger.debug( - 'Updates "temporary document" -status --> "valid" -status', - ) - try { - // We cannot use Mongo 'session' / transaction here because Mongo has 16 MB limit for transaction - await this.refSeqChunkModel.updateMany( - { $and: [{ status: -1, user: uniqUserId }] }, - { $set: { status: 0 } }, - ) - await this.featureModel.updateMany( - { $and: [{ status: -1, user: uniqUserId }] }, - { $set: { status: 0 } }, - ) - await this.assemblyModel.updateMany( - { $and: [{ status: -1, user: uniqUserId }] }, - { $set: { status: 0 } }, - ) - await this.refSeqModel.updateMany( - { $and: [{ status: -1, user: uniqUserId }] }, - { $set: { status: 0 } }, - ) - } catch (error) { - // Clean up old "temporary document" -documents + + if (STATUS_ZERO_CHANGE_TYPES.has(change.typeName)) { + this.logger.debug?.('*** TEMPORARY DATA INSERTTED ***') + // Set "temporary document" -status --> "valid" -status i.e. (-1 --> 0) + await this.featureModel.db.transaction(async () => { this.logger.debug( - '*** UPDATE STATUS EXCEPTION - Start to clean up old temporary documents...', + 'Updates "temporary document" -status --> "valid" -status', ) - // We cannot use Mongo 'session' / transaction here because Mongo has 16 MB limit for transaction - await this.assemblyModel.deleteMany({ - $and: [{ status: -1, user: uniqUserId }], - }) - await this.featureModel.deleteMany({ - $and: [{ status: -1, user: uniqUserId }], - }) - await this.refSeqModel.deleteMany({ - $and: [{ status: -1, user: uniqUserId }], - }) - await this.refSeqChunkModel.deleteMany({ - $and: [{ status: -1, user: uniqUserId }], - }) - throw new UnprocessableEntityException(String(error)) - } - }) + try { + // We cannot use Mongo 'session' / transaction here because Mongo has 16 MB limit for transaction + await this.refSeqChunkModel.updateMany( + { $and: [{ status: -1, user: uniqUserId }] }, + { $set: { status: 0 } }, + ) + await this.featureModel.updateMany( + { $and: [{ status: -1, user: uniqUserId }] }, + { $set: { status: 0 } }, + ) + await this.assemblyModel.updateMany( + { $and: [{ status: -1, user: uniqUserId }] }, + { $set: { status: 0 } }, + ) + await this.refSeqModel.updateMany( + { $and: [{ status: -1, user: uniqUserId }] }, + { $set: { status: 0 } }, + ) + } catch (error) { + // Clean up old "temporary document" -documents + this.logger.debug( + '*** UPDATE STATUS EXCEPTION - Start to clean up old temporary documents...', + ) + // We cannot use Mongo 'session' / transaction here because Mongo has 16 MB limit for transaction + await this.assemblyModel.deleteMany({ + $and: [{ status: -1, user: uniqUserId }], + }) + await this.featureModel.deleteMany({ + $and: [{ status: -1, user: uniqUserId }], + }) + await this.refSeqModel.deleteMany({ + $and: [{ status: -1, user: uniqUserId }], + }) + await this.refSeqChunkModel.deleteMany({ + $and: [{ status: -1, user: uniqUserId }], + }) + throw new UnprocessableEntityException(String(error)) + } + }) + } this.logger.debug?.(`CHANGE DOC: ${JSON.stringify(changeDoc)}`) if (!changeDoc) {