// tslint:disable:no-console import * as moment from 'moment'; import * as mongoose from 'mongoose'; import { chevre } from '../../../lib/index'; // const project = { id: String(process.env.PROJECT_ID) }; async function main() { await mongoose.connect(process.env.MONGOLAB_URI); const eventRepo = new chevre.repository.Event(mongoose.connection); const cursor = eventRepo.getCursor( { 'project.id': { $eq: '' }, typeOf: { $eq: chevre.factory.eventType.ScreeningEvent }, startDate: { $gte: moment() .add(-1, 'month') .toDate() } // _id: { $eq: 'al6afd7np' } }, { // _id: 1, } ); console.log('events found'); let i = 0; let updateCount = 0; await cursor.eachAsync(async (doc) => { i += 1; const event: chevre.factory.event.screeningEvent.IEvent = doc.toObject(); const oldEventId = event.additionalProperty?.find((p) => p.name === 'oldEventId')?.value; if (typeof oldEventId === 'string' && event.id === oldEventId) { console.log( 'already exist...', event.project.id, event.id, event.startDate, oldEventId, i); } else { const newAdditionalProperty: chevre.factory.propertyValue.IPropertyValue[] = [ ...(Array.isArray(event.additionalProperty)) ? event.additionalProperty : [], { name: 'oldEventId', value: String(event.id) } ]; console.log( 'updating event...', event.project.id, event.id, event.startDate, oldEventId, newAdditionalProperty, i); await eventRepo.updatePartiallyById({ id: event.id, attributes: { typeOf: event.typeOf, additionalProperty: newAdditionalProperty } }); updateCount += 1; console.log( 'updated', event.project.id, event.id, event.startDate, oldEventId, i); } }); console.log(i, 'events checked'); console.log(updateCount, 'events updated'); } main() .then() .catch(console.error);