Skip to content

Commit 21be0e0

Browse files
m-salaudeenchris-elliott-nhsd
authored andcommitted
CCM-10429: Templates and S3 migration
1 parent 0f49884 commit 21be0e0

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

data-migration/user-transfer/src/user-transfer.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
listCognitoUsers,
2020
UserData,
2121
} from './utils/cognito-utils';
22-
import { backupObject, deleteObjects, handleS3Copy } from './utils/s3-utils';
22+
import { backupObject, handleS3Copy, handleS3Delete } from './utils/s3-utils';
2323

2424
const DRY_RUN = true;
2525

@@ -83,10 +83,12 @@ async function migrateTemplatesAndS3Data(
8383

8484
for (const user of users) {
8585
for (const item of items) {
86-
const { id } = item;
86+
const { id, templateType } = item;
8787
if (id.S === user.userId) {
8888
// copy s3 data
89-
const sourceKey = await handleS3Copy(user, id.S as string, DRY_RUN);
89+
if (templateType.S === 'LETTER') {
90+
await handleS3Copy(user, id.S as string, DRY_RUN);
91+
}
9092

9193
// migrate templates
9294
await updateItem(item, parameters, user, DRY_RUN);
@@ -95,7 +97,9 @@ async function migrateTemplatesAndS3Data(
9597
await deleteItem(item, parameters);
9698

9799
// delete migrated s3 data
98-
deleteObjects(sourceKey);
100+
if (templateType.S === 'LETTER') {
101+
await handleS3Delete(user, id.S as string, DRY_RUN);
102+
}
99103
}
100104
}
101105
}

data-migration/user-transfer/src/utils/s3-utils.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,28 @@ export async function handleS3Copy(
162162
const destinationKey = sourceKey.replace(user.userId, user.clientId);
163163
if (DRY_RUN) {
164164
console.log(
165-
`[DRY_RUN] Would migrate S3 object: ${sourceBucket}/${sourceKey}} -> ${sourceBucket}/${destinationKey}`
165+
`[DRY_RUN] Would migrate S3 object: ${sourceBucket}/${sourceKey} -> ${sourceBucket}/${destinationKey}`
166166
);
167167
} else {
168168
await copyObjects(user.userId, sourceKey, user.clientId);
169-
return sourceKey;
169+
}
170+
}
171+
}
172+
173+
export async function handleS3Delete(
174+
user: UserData,
175+
templateId: string,
176+
DRY_RUN: boolean = false
177+
) {
178+
const itemObjects = await getItemObjects(templateId);
179+
for (const itemObject of itemObjects) {
180+
const sourceKey = itemObject['Key'];
181+
if (DRY_RUN) {
182+
console.log(
183+
`[DRY_RUN] Would delete S3 object: ${sourceBucket}/${sourceKey}`
184+
);
185+
} else {
186+
await deleteObjects(sourceKey);
170187
}
171188
}
172189
}

0 commit comments

Comments
 (0)