// tslint:disable:no-console // import * as moment from 'moment'; import * as mongoose from 'mongoose'; import { chevre } from '../../../lib/index'; import { migrateUser } from './migrateCognitoUser'; const project = { id: String(process.env.PROJECT_ID) }; export const USERPOOL_ID_OLD = String(process.env.USERPOOL_ID_OLD); export const USERPOOL_ID_NEW = String(process.env.USERPOOL_ID_NEW); export const ISS_PREFIX = 'https://cognito-idp.ap-northeast-1.amazonaws.com/'; // tslint:disable-next-line:max-func-body-length export async function main() { await mongoose.connect(process.env.MONGOLAB_URI); const taskRepo = new chevre.repository.Task(mongoose.connection); // const now = new Date(); const cursor = await taskRepo.taskModel.find( { // _id: { $eq: '60398ca3bdadde000a308709' }, status: { $eq: chevre.factory.taskStatus.Ready }, name: { $eq: chevre.factory.taskName.OrderProgramMembership }, 'project.id': { $eq: project.id } }, { // _id: 1, } ) .sort({ runsAt: chevre.factory.sortType.Ascending }) .cursor(); console.log('tasks found'); let i = 0; let notFoundCount = 0; let updateCount = 0; await cursor.eachAsync(async (doc) => { i += 1; const task = >doc.toObject(); const owner = task.data.agent; let iss = ([] | undefined>owner.identifier)?.find((p) => p.name === 'iss')?.value; if (typeof iss === 'string') { iss = iss.replace(ISS_PREFIX, ''); } // 新会員ならok if (iss === USERPOOL_ID_NEW) { console.log('already new userPool', task.runsAt, i, notFoundCount); } else { // 旧会員の場合、新会員の存在確認 console.log( 'checking...', task.runsAt, 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', task.runsAt, 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: 'dataAgentMigratedManually', value: '1' } ] }; updateCount += 1; console.log( 'updating data.agent...', task.runsAt, owner.id, owner.memberOf?.membershipNumber, person.id, i, notFoundCount, updateCount); const result = await taskRepo.taskModel.updateOne( { _id: task.id }, { 'data.agent': newOwner } ) .exec(); console.log( 'data.agent updated', task.runsAt, owner.id, owner.memberOf?.membershipNumber, person.id, i, notFoundCount, updateCount, result); } catch (error) { // NotFoundエラーは、新ユーザープールでのユーザーがまだ存在しないだけ if (error.code === 'UserNotFoundException') { notFoundCount += 1; console.log( 'not found', task.runsAt, owner.id, owner.memberOf?.membershipNumber, i, notFoundCount); // migrateUser await migrateUser({ id: owner.id }); } else { throw error; } } } }); console.log(i, 'infos checked'); console.log(notFoundCount, 'people not found'); console.log(updateCount, 'tasks updated'); } main() .then() .catch(console.error);