// tslint:disable:no-console // import * as moment from 'moment'; import * as mongoose from 'mongoose'; import { chevre } from '../../../lib/index'; import { ISS_PREFIX, USERPOOL_ID_NEW } from './checkOrderMembershipTasks'; import { migrateUser } from './migrateCognitoUser'; const project = { id: String(process.env.PROJECT_ID) }; const PRODUCT_TYPE = chevre.factory.product.ProductType.EventService; // tslint:disable-next-line:max-func-body-length async function main() { await mongoose.connect(process.env.MONGOLAB_URI); const ownershipInfoRepo = new chevre.repository.OwnershipInfo(mongoose.connection); const now = new Date(); const cursor = ownershipInfoRepo.getCursor( { // 'ownedBy.id': { // $exists: true, // $eq: 'bec066cd-2e82-4857-8ac2-b8b25a6947e8' // }, ownedFrom: { // $gte: moment('2022-01-01T00:00:00+09:00') // .toDate(), $lte: now // $lte: moment('2021-12-25T22:53:09.579Z') // .toDate() }, // ownedThrough: { // $gte: now // }, 'ownedBy.typeOf': { $exists: true, $eq: chevre.factory.personType.Person }, 'typeOfGood.issuedThrough.typeOf': { $exists: true, $eq: PRODUCT_TYPE }, 'project.id': { $eq: project.id } }, { // _id: 1, // ownedFrom: 1, // typeOfGood: 1 } ); console.log('ownershipInfos found'); let i = 0; let notFoundCount = 0; let updateCount = 0; await cursor.eachAsync(async (doc) => { i += 1; const ownershipInfo = >doc.toObject(); const owner = ownershipInfo.ownedBy; let iss = ([] | undefined>owner.identifier)?.find((p) => p.name === 'iss')?.value; if (typeof iss === 'string') { iss = iss.replace(ISS_PREFIX, ''); } console.log('iss:', iss); // 新会員ならok if (iss === USERPOOL_ID_NEW) { console.log('already new userPool', ownershipInfo.typeOfGood.identifier, ownershipInfo.ownedFrom, i, notFoundCount); } else { // 旧会員の場合、新会員の存在確認 console.log( 'checking...', ownershipInfo.typeOfGood.identifier, ownershipInfo.ownedFrom, owner.id, owner.memberOf?.membershipNumber, i, notFoundCount); const personRepo = new chevre.repository.Person({ userPoolId: USERPOOL_ID_NEW }); try { const newUserAttributes = await personRepo.getUserAttributes({ username: `SSKTS_${owner.id}` }); const newUserId = newUserAttributes.additionalProperty?.find((p) => p.name === 'sub')?.value; const person = await personRepo.findById({ userId: String(newUserId) }); console.log( 'found', ownershipInfo.typeOfGood.identifier, ownershipInfo.ownedFrom, owner.id, owner.memberOf?.membershipNumber, person.id, i, notFoundCount); // 所有権の所有者を新会員に置換 const newOwner: chevre.factory.person.IPerson = { ...owner, id: person.id, identifier: [ ...(Array.isArray(person.identifier)) ? person.identifier : [], // 手動移行した証拠を残す { name: 'ownedByMigratedManually', value: '1' } ], memberOf: person.memberOf }; updateCount += 1; console.log( 'updating ownedBy...', ownershipInfo.typeOfGood.identifier, ownershipInfo.ownedFrom, newOwner.id, newOwner.memberOf?.membershipNumber, i, notFoundCount, updateCount); // const result = await ownershipInfoRepo.ownershipInfoModel.updateOne( // { _id: ownershipInfo.id }, // { ownedBy: newOwner } // ) // .exec(); // console.log( // 'ownedBy updated', // ownershipInfo.typeOfGood.identifier, // ownershipInfo.ownedFrom, // newOwner.id, // newOwner.memberOf?.membershipNumber, // i, notFoundCount, updateCount, // result); } catch (error) { // NotFoundエラーは、新ユーザープールでのユーザーがまだ存在しないだけ if (error.code === 'UserNotFoundException') { notFoundCount += 1; console.log( 'not found', ownershipInfo.typeOfGood.identifier, ownershipInfo.ownedFrom, owner.id, owner.memberOf?.membershipNumber, i, notFoundCount); // migrateUser await migrateUser({ id: owner.id }); } else { throw error; } } } }); console.log(i, 'infos checked', PRODUCT_TYPE); console.log(notFoundCount, 'people not found', PRODUCT_TYPE); console.log(updateCount, 'ownershipInfos updated', PRODUCT_TYPE); } main() .then() .catch(console.error);