@@ -7,19 +7,21 @@ import {
77 retrieveAllTemplates ,
88 updateItem ,
99} from '@/src/utils/ddb-utils' ;
10- import { backupData } from '@/src/utils/backup-utils' ;
10+ import {
11+ backupData ,
12+ backupToJSON ,
13+ deleteJSONFile ,
14+ readJSONFile ,
15+ } from '@/src/utils/backup-utils' ;
1116import { Parameters } from '@/src/utils/constants' ;
1217import {
13- findCognitoUser ,
14- getUserGroup ,
18+ getUserGroupAndClientId ,
1519 listCognitoUsers ,
20+ UserData ,
1621} from './utils/cognito-utils' ;
17- import {
18- backupObject ,
19- copyObjects ,
20- deleteObjects ,
21- getItemObjects ,
22- } from './utils/s3-utils' ;
22+ import { backupObject , deleteObjects , handleS3Copy } from './utils/s3-utils' ;
23+
24+ const DRY_RUN = true ;
2325
2426function getParameters ( ) : Parameters {
2527 return yargs ( hideBin ( process . argv ) )
@@ -32,85 +34,91 @@ function getParameters(): Parameters {
3234 type : 'string' ,
3335 demandOption : true ,
3436 } ,
37+ accessKeyId : {
38+ type : 'string' ,
39+ demandOption : true ,
40+ } ,
41+ secretAccessKey : {
42+ type : 'string' ,
43+ demandOption : true ,
44+ } ,
45+ userPoolId : {
46+ type : 'string' ,
47+ demandOption : true ,
48+ } ,
49+ sessionToken : {
50+ type : 'string' ,
51+ demandOption : true ,
52+ } ,
53+ region : {
54+ type : 'string' ,
55+ demandOption : true ,
56+ } ,
57+ flag : {
58+ type : 'string' ,
59+ demandOption : false ,
60+ } ,
3561 } )
3662 . parseSync ( ) ;
3763}
3864
39- async function updateItems (
65+ async function getUserData ( parameters : Parameters ) {
66+ const usernames = await listCognitoUsers ( parameters ) ;
67+ if ( ! usernames || usernames . length === 0 ) {
68+ throw new Error ( 'No users found' ) ;
69+ }
70+ const userGroupAndClientId = await getUserGroupAndClientId (
71+ usernames as string [ ] ,
72+ parameters
73+ ) ;
74+ // download users to a json file
75+ await backupToJSON ( userGroupAndClientId ) ;
76+ }
77+
78+ async function migrateTemplatesAndS3Data (
4079 items : Record < string , AttributeValue > [ ] ,
4180 parameters : Parameters
4281) : Promise < void > {
43- for ( const item of items ) {
44- console . log ( item . id . S , item . owner . S ) ;
45-
46- // Get owner id of this item
47- const { owner, id, templateType, clientId } = item ;
82+ const users : UserData [ ] = await readJSONFile ( 'users.json' ) ;
4883
49- //check if owner doesn't have CLIENT#
50- if ( owner . S && ! owner . S ?. startsWith ( 'CLIENT#' ) ) {
51- // check the user in cognito, if it exist then pull the client id
52- const cognitoUser = await findCognitoUser ( owner . S ) ;
84+ for ( const user of users ) {
85+ for ( const item of items ) {
86+ const { id } = item ;
87+ if ( id . S === user . userId ) {
88+ // copy s3 data
89+ const sourceKey = await handleS3Copy ( user , id . S as string , DRY_RUN ) ;
5390
54- // check and get user groups - this is used when migrating for production
55- const userClientIdFromGroup = await getUserGroup ( {
56- Username : cognitoUser ?. username ,
57- UserPoolId : cognitoUser ?. poolId ,
58- } ) ;
91+ // migrate templates
92+ await updateItem ( item , parameters , user , DRY_RUN ) ;
5993
60- // resolve client id
61- const newClientId = ( userClientIdFromGroup ??
62- cognitoUser ?. clientIdAttr ) as string ;
94+ // delete previous template
95+ await deleteItem ( item , parameters ) ;
6396
64- // check if this item has a client id, if yes check it matches the client id above. If it doesn't, throw error!
65- if ( item . clientId && item . clientId . S !== newClientId ) {
66- console . log ( {
67- templateClientId : item . clientId ,
68- cognitoClientId : newClientId ,
69- } ) ;
70-
71- throw new Error ( 'Invalid ids' ) ;
72- }
73- // if it matches make the required swaps (clientId, createdby and updatedby)
74- // if item doesn't have a client id then create one and do the above
75- // update the item and delete the previous one
76-
77- // migrate and update item
78- await updateItem ( item , parameters , newClientId ) ;
79-
80- // check if migration was successful
81-
82- // delete migrated item
83- await deleteItem ( item , parameters ) ;
84- }
85-
86- //
87- if ( templateType . S === 'LETTER' ) {
88- //Retrieve item S3 object(s)
89- const itemObjects = await getItemObjects ( id . S as string ) ;
90-
91- //migrate to a new s3 location
92- for ( const itemObject of itemObjects ) {
93- await Promise . all ( [
94- copyObjects (
95- owner . S as string ,
96- itemObject [ 'Key' ] ,
97- clientId . S as string
98- ) ,
99- // delete previous objects
100- deleteObjects ( itemObject [ 'Key' ] ) ,
101- ] ) . then ( ( ) => console . log ( 'Object moved' ) ) ;
97+ // delete migrated s3 data
98+ deleteObjects ( sourceKey ) ;
10299 }
103100 }
104101 }
102+
103+ await deleteJSONFile ( 'users.json' ) ;
104+ console . log ( 'Migration completed successfully' ) ;
105105}
106106
107107export async function performTransfer ( ) {
108108 const parameters = getParameters ( ) ;
109+
110+ // if flag = user, then, get cognito users with their clientId and save to a json file
111+ if ( parameters . flag && parameters . flag === 'user' ) {
112+ await getUserData ( parameters ) ;
113+ }
114+
115+ // retrieve and backup all templates
109116 const items = await retrieveAllTemplates ( parameters ) ;
110- console . log ( { items } ) ;
111- console . log ( await listCognitoUsers ( ) ) ;
112- // retrieve all user (id and clientId) in cognito and save to a JSON file
113- // await backupData(items, parameters);
114- // await backupObject(parameters);
115- // await updateItems(items, parameters);
117+ await backupData ( items , parameters ) ;
118+
119+ // retrieve and backup all S3 data
120+ await backupObject ( parameters ) ;
121+
122+ // Migrate
123+ await migrateTemplatesAndS3Data ( items , parameters ) ;
116124}
0 commit comments