UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

70 lines (62 loc) 2.34 kB
// 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) }; // const EXCLUDED_PROJECT_ID = process.env.EXCLUDED_PROJECT_ID; // tslint:disable-next-line:max-func-body-length async function main() { await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false }); const reservationsRepo = await chevre.repository.Reservation.createInstance(mongoose.connection); const cursor = reservationsRepo.getCursor( { bookingTime: { $lte: moment() // tslint:disable-next-line:no-magic-numbers .add(-365, 'days') .toDate() }, reservationStatus: { $in: [ chevre.factory.reservationStatusType.ReservationCancelled // chevre.factory.reservationStatusType.ReservationConfirmed ] } // 'project.id': { $eq: project.id }, // 'project.id': { $ne: EXCLUDED_PROJECT_ID }, }, { // paymentMethods: 1, // project: 1, // orderDate: 1 } ); console.log('docs found'); let i = 0; let updatedCount = 0; await cursor.eachAsync(async (doc) => { i += 1; const reservation: chevre.factory.reservation.eventReservation.IReservation & { previousReservationStatus: string; } = doc.toObject(); console.log( 'checking...', reservation.project.id, reservation.id, reservation.previousReservationStatus, reservation.reservationStatus, reservation.bookingTime, i ); if (reservation.previousReservationStatus !== chevre.factory.reservationStatusType.ReservationPending) { // throw new Error('invalid previousReservationStatus'); } else { await reservationsRepo.deleteByIds({ project: { id: reservation.project.id }, ids: [reservation.id] }); updatedCount += 1; } }); console.log(i, 'docs checked'); console.log(updatedCount, 'docs updated'); } main() .then() .catch(console.error);