22import yargs from 'yargs/yargs' ;
33import { hideBin } from 'yargs/helpers' ;
44import { AttributeValue } from '@aws-sdk/client-dynamodb' ;
5- import { retrieveTemplates , updateItem } from '@/src/utils/ddb-utils' ;
5+ import {
6+ deleteItem ,
7+ retrieveAllTemplates ,
8+ updateItem ,
9+ } from '@/src/utils/ddb-utils' ;
610import { backupData } from '@/src/utils/backup-utils' ;
711import { Parameters } from '@/src/utils/constants' ;
12+ import { findCognitoUser , getUserGroup } from './utils/cognito-utils' ;
13+ import {
14+ backupObject ,
15+ copyObjects ,
16+ deleteObjects ,
17+ getItemObjects ,
18+ } from './utils/s3-utils' ;
819
920function getParameters ( ) : Parameters {
1021 return yargs ( hideBin ( process . argv ) )
1122 . options ( {
12- sourceOwner : {
13- type : 'string' ,
14- demandOption : true ,
15- } ,
16- destinationOwner : {
17- type : 'string' ,
18- demandOption : true ,
19- } ,
2023 environment : {
2124 type : 'string' ,
2225 demandOption : true ,
@@ -29,18 +32,73 @@ async function updateItems(
2932 items : Record < string , AttributeValue > [ ] ,
3033 parameters : Parameters
3134) : Promise < void > {
32- for ( let i = 0 ; i < items . length ; i ++ ) {
33- const item = items [ i ] ;
34- await updateItem ( item , parameters ) ;
35- console . log (
36- `Updated ${ item . id . S } : ${ i + 1 } /${ items . length } (${ ( 100.0 * ( i + 1 ) ) / items . length } %)`
37- ) ;
35+ for ( const item of items ) {
36+ console . log ( item . owner . S ) ;
37+
38+ // Get owner id of this item
39+ const { owner, id, templateType, clientId } = item ;
40+
41+ //check if owner doesn't have CLIENT#
42+ if ( owner . S && ! owner . S ?. includes ( 'CLIENT#' ) ) {
43+ // check the user in cognito, if it exist then pull the client id
44+ const cognitoUser = await findCognitoUser ( owner . S ) ;
45+
46+ // check and get user groups - this is used when migrating for production
47+ const userClientIdFromGroup = await getUserGroup ( {
48+ Username : cognitoUser ?. username ,
49+ UserPoolId : cognitoUser ?. poolId ,
50+ } ) ;
51+
52+ // resolve client id
53+ const newClientId = ( userClientIdFromGroup ??
54+ cognitoUser ?. clientIdAttr ) as string ;
55+
56+ // check if this item has a client id, if yes check it matches the client id above. If it doesn't, throw error!
57+ if ( item . clientId && item . clientId . S !== newClientId ) {
58+ console . log ( {
59+ templateClientId : item . clientId ,
60+ cognitoClientId : newClientId ,
61+ } ) ;
62+
63+ throw new Error ( 'Invalid ids' ) ;
64+ }
65+ // if it matches make the required swaps (clientId, createdby and updatedby)
66+ // if item doesn't have a client id then create one and do the above
67+ // update the item and delete the previous one
68+ await Promise . all ( [
69+ updateItem ( item , parameters , newClientId ) ,
70+ deleteItem ( item , parameters ) ,
71+ ] ) ;
72+ }
73+
74+ //
75+ if ( templateType . S === 'LETTER' ) {
76+ //Retrieve item S3 object(s)
77+ const itemObjects = await getItemObjects ( id . S as string ) ;
78+
79+ //migrate to a new s3 location
80+ for ( const itemObject of itemObjects ) {
81+ const versionId = itemObject [ 'Key' ] . split ( '/' ) . reverse ( ) ;
82+ await Promise . all ( [
83+ copyObjects (
84+ owner . S as string ,
85+ itemObject [ 'Key' ] ,
86+ id . S as string ,
87+ versionId [ 0 ] ,
88+ clientId . S as string
89+ ) ,
90+ // delete previous objects
91+ deleteObjects ( itemObject [ 'Key' ] ) ,
92+ ] ) . then ( ( ) => console . log ( 'Object moved' ) ) ;
93+ }
94+ }
3895 }
3996}
4097
4198export async function performTransfer ( ) {
4299 const parameters = getParameters ( ) ;
43- const items = await retrieveTemplates ( parameters ) ;
100+ const items = await retrieveAllTemplates ( parameters ) ;
44101 await backupData ( items , parameters ) ;
102+ await backupObject ( parameters ) ;
45103 await updateItems ( items , parameters ) ;
46104}
0 commit comments