// 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 offerCatalogRepo = new chevre.repository.OfferCatalog(mongoose.connection); const productRepo = new chevre.repository.Product(mongoose.connection); const cursor = eventRepo.getCursor( { // 'project.id': { $eq: project.id }, 'project.id': { $ne: '' }, 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 itemOfferedId = (event.offers)?.itemOffered?.id; const catalogId = (event).hasOfferCatalog?.id; if (typeof catalogId === 'string' && catalogId.length > 0) { if (typeof itemOfferedId === 'string' && itemOfferedId.length > 0) { console.log( 'already exist...', event.project.id, event.id, event.startDate, itemOfferedId, i); } else { const existingCatalogs = await offerCatalogRepo.search({ limit: 1, page: 1, project: { id: { $eq: event.project.id } }, id: { $in: [catalogId] } }); const existingCatalog = existingCatalogs.shift(); if (existingCatalog !== undefined) { const existingProducts = await productRepo.search({ limit: 1, page: 1, project: { id: { $eq: event.project.id } }, productID: { $eq: `${chevre.factory.product.ProductType.EventService}${catalogId}` } }); const existingProduct = existingProducts.shift(); if (existingProduct === undefined) { throw new Error(`product not found ${event.id}`); } console.log( 'updating event...', event.project.id, event.id, event.startDate, itemOfferedId, i); await eventRepo.updatePartiallyById({ id: event.id, attributes: { typeOf: event.typeOf, 'offers.itemOffered.id': String(existingProduct.id) } }); updateCount += 1; console.log( 'updated', event.project.id, event.id, event.startDate, itemOfferedId, i); } else { // カタログが存在しないのではもはや無効なのでスルー console.log( 'catalog not exist', event.project.id, event.id, event.startDate, itemOfferedId, i); } } } else { console.log( 'no catalog...', event.project.id, event.id, event.startDate, itemOfferedId, i); } }); console.log(i, 'events checked'); console.log(updateCount, 'events updated'); } // function offerCatalog2eventService(offerCatalog: chevre.factory.offerCatalog.IOfferCatalog): chevre.factory.product.IProduct { // tslint:disable-next-line:max-line-length // const serviceType: chevre.factory.product.IServiceType | undefined = (typeof offerCatalog.itemOffered.serviceType?.typeOf === 'string') // ? { // codeValue: offerCatalog.itemOffered.serviceType.codeValue, // inCodeSet: offerCatalog.itemOffered.serviceType.inCodeSet, // project: offerCatalog.itemOffered.serviceType.project, // typeOf: offerCatalog.itemOffered.serviceType.typeOf // } // : undefined; // if (typeof offerCatalog.id !== 'string' || offerCatalog.id.length === 0) { // throw new Error('offerCatalog.id undefined'); // } // return { // project: offerCatalog.project, // typeOf: chevre.factory.product.ProductType.EventService, // // productIDフォーマット確定(matches(/^[0-9a-zA-Z]+$/)に注意)(.isLength({ min: 3, max: 30 })に注意) // productID: `${chevre.factory.product.ProductType.EventService}${offerCatalog.id}`, // name: offerCatalog.name, // hasOfferCatalog: { id: offerCatalog.id, typeOf: offerCatalog.typeOf }, // ...(typeof serviceType?.typeOf === 'string') ? { serviceType } : undefined // }; // } main() .then() .catch(console.error);