|
| 1 | +const prompt = require('../prompt'); |
| 2 | +const docClient = require('../docClient'); |
| 3 | + |
| 4 | +const dataSets = [ |
| 5 | + { |
| 6 | + targetUsername: 'john6', |
| 7 | + sourceUsernames: [ |
| 8 | + 'John_7fe98093-a62d-4dc7-89c1-9a2813acda5d', |
| 9 | + 'jhuang_7ef0dcac-d0a8-4803-846a-7f9456930cbb', |
| 10 | + ], |
| 11 | + }, |
| 12 | +]; |
| 13 | + |
| 14 | +const updateTableRecords = async (tableName, indexName, key = 'username', targetUsername, sourceUsername) => { |
| 15 | + const items = await docClient.queryAll({ |
| 16 | + TableName: tableName, |
| 17 | + IndexName: indexName, |
| 18 | + KeyConditionExpression: `${key} = :username`, |
| 19 | + ExpressionAttributeValues: { |
| 20 | + ':username': sourceUsername, |
| 21 | + }, |
| 22 | + ScanIndexForward: false, |
| 23 | + }); |
| 24 | + |
| 25 | + console.log(`Update ${items.length} items for ${tableName}`); |
| 26 | + |
| 27 | + if (items.length === 0) return; |
| 28 | + |
| 29 | + const updatedItems = items.map((item) => { |
| 30 | + item[key] = targetUsername; |
| 31 | + |
| 32 | + return item; |
| 33 | + }); |
| 34 | + |
| 35 | + return docClient.batchUpdate(tableName, updatedItems); |
| 36 | +}; |
| 37 | + |
| 38 | +(async () => { |
| 39 | + try { |
| 40 | + const { |
| 41 | + tableNames, |
| 42 | + } = await prompt(); |
| 43 | + |
| 44 | + const getTableName = (typeName) => { |
| 45 | + return tableNames.find((x) => x.startsWith(`${typeName}-`)); |
| 46 | + }; |
| 47 | + |
| 48 | + await Promise.all(dataSets.map(async ({ targetUsername, sourceUsernames }) => { |
| 49 | + await Promise.all(sourceUsernames.map(async (sourceUsername) => { |
| 50 | + // pull the userProjects first |
| 51 | + const userProjects = await docClient.queryAll({ |
| 52 | + TableName: getTableName('UserProject'), |
| 53 | + IndexName: 'byUser', |
| 54 | + KeyConditionExpression: `username = :username`, |
| 55 | + ExpressionAttributeValues: { |
| 56 | + ':username': sourceUsername, |
| 57 | + }, |
| 58 | + ScanIndexForward: false, |
| 59 | + }); |
| 60 | + |
| 61 | + const toUpdateProjects = []; |
| 62 | + const toUpdateUserProjects = []; |
| 63 | + // project managers |
| 64 | + if (userProjects.length > 0) { |
| 65 | + await Promise.all(userProjects.map(async (userProject) => { |
| 66 | + const { projectId } = userProject; |
| 67 | + |
| 68 | + const { Item: project } = await docClient.get({ |
| 69 | + TableName: getTableName('Project'), |
| 70 | + Key: { id: projectId }, |
| 71 | + }).promise(); |
| 72 | + |
| 73 | + if (project.managers.includes(sourceUsername)) { |
| 74 | + project.managers = JSON.parse(JSON.stringify(project.managers).replace(sourceUsername, targetUsername)); |
| 75 | + toUpdateProjects.push(project); |
| 76 | + } |
| 77 | + |
| 78 | + if (project.owner === sourceUsername) { |
| 79 | + project.owner = targetUsername; |
| 80 | + toUpdateProjects.push(project); |
| 81 | + } |
| 82 | + |
| 83 | + userProject.username = targetUsername; |
| 84 | + toUpdateUserProjects.push(userProject); |
| 85 | + })); |
| 86 | + } |
| 87 | + |
| 88 | + await Promise.all([ |
| 89 | + // project owner and manager |
| 90 | + toUpdateProjects.length > 0 && docClient.batchUpdate(getTableName('Project'), toUpdateProjects), |
| 91 | + // user project |
| 92 | + toUpdateUserProjects.length > 0 && docClient.batchUpdate(getTableName('UserProject'), toUpdateUserProjects), |
| 93 | + // contribution |
| 94 | + updateTableRecords( |
| 95 | + getTableName('Contribution'), |
| 96 | + 'byUsernameByCreatedAt', |
| 97 | + 'username', |
| 98 | + targetUsername, |
| 99 | + sourceUsername, |
| 100 | + ), |
| 101 | + // statement |
| 102 | + updateTableRecords( |
| 103 | + getTableName('Statement'), |
| 104 | + 'byUsernameByDate', |
| 105 | + 'username', |
| 106 | + targetUsername, |
| 107 | + sourceUsername, |
| 108 | + ), |
| 109 | + // user need |
| 110 | + updateTableRecords( |
| 111 | + getTableName('UserNeed'), |
| 112 | + 'byUser', |
| 113 | + 'username', |
| 114 | + targetUsername, |
| 115 | + sourceUsername, |
| 116 | + ), |
| 117 | + // user tag |
| 118 | + updateTableRecords( |
| 119 | + getTableName('UserTag'), |
| 120 | + 'byUser', |
| 121 | + 'username', |
| 122 | + targetUsername, |
| 123 | + sourceUsername, |
| 124 | + ), |
| 125 | + ]); |
| 126 | + |
| 127 | + // delete user |
| 128 | + await docClient.delete({ |
| 129 | + TableName: getTableName('User'), |
| 130 | + Key: { |
| 131 | + username: sourceUsername, |
| 132 | + }, |
| 133 | + }).promise(); |
| 134 | + })); |
| 135 | + })); |
| 136 | + } catch (e) { |
| 137 | + console.log(e); |
| 138 | + } |
| 139 | +})(); |
0 commit comments