|
| 1 | +import fs from 'node:fs'; |
| 2 | +import process from 'node:process'; |
| 3 | +import yargs from 'yargs/yargs'; |
| 4 | +import { hideBin } from 'yargs/helpers'; |
| 5 | +import { |
| 6 | + DynamoDBDocumentClient, |
| 7 | + UpdateCommand, |
| 8 | + UpdateCommandInput, |
| 9 | +} from '@aws-sdk/lib-dynamodb'; |
| 10 | +import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; |
| 11 | +import { SendMessageCommand, SQSClient } from '@aws-sdk/client-sqs'; |
| 12 | +import { $TemplateDtoSchema } from 'nhs-notify-backend-client'; |
| 13 | +import { z } from 'zod/v4'; |
| 14 | +import { GetCallerIdentityCommand, STSClient } from '@aws-sdk/client-sts'; |
| 15 | + |
| 16 | +const ddb = DynamoDBDocumentClient.from( |
| 17 | + new DynamoDBClient({ region: 'eu-west-2' }), |
| 18 | + { |
| 19 | + marshallOptions: { removeUndefinedValues: true }, |
| 20 | + } |
| 21 | +); |
| 22 | + |
| 23 | +const sqs = new SQSClient({ region: 'eu-west-2' }); |
| 24 | + |
| 25 | +const sts = new STSClient({ region: 'eu-west-2' }); |
| 26 | + |
| 27 | +export async function getAccountId(): Promise<string> { |
| 28 | + const callerIdentity = await sts.send(new GetCallerIdentityCommand()); |
| 29 | + const accountId = callerIdentity.Account; |
| 30 | + if (!accountId) { |
| 31 | + throw new Error('Unable to get account ID from caller'); |
| 32 | + } |
| 33 | + return accountId; |
| 34 | +} |
| 35 | + |
| 36 | +const { environment, file, newCampaignId, clientId, supplier } = yargs( |
| 37 | + hideBin(process.argv) |
| 38 | +) |
| 39 | + .options({ |
| 40 | + environment: { |
| 41 | + type: 'string', |
| 42 | + demandOption: true, |
| 43 | + }, |
| 44 | + clientId: { |
| 45 | + type: 'string', |
| 46 | + demandOption: true, |
| 47 | + }, |
| 48 | + file: { |
| 49 | + type: 'string', |
| 50 | + demandOption: true, |
| 51 | + }, |
| 52 | + newCampaignId: { |
| 53 | + type: 'string', |
| 54 | + demandOption: true, |
| 55 | + }, |
| 56 | + supplier: { |
| 57 | + type: 'string', |
| 58 | + default: 'MBA', |
| 59 | + }, |
| 60 | + }) |
| 61 | + .parseSync(); |
| 62 | + |
| 63 | +async function main() { |
| 64 | + const acct = await getAccountId(); |
| 65 | + |
| 66 | + // eslint-disable-next-line security/detect-non-literal-fs-filename |
| 67 | + const templateIds = fs |
| 68 | + .readFileSync(file, 'utf8') |
| 69 | + .split('\n') |
| 70 | + .map((s) => s.trim()) |
| 71 | + .filter(Boolean); |
| 72 | + |
| 73 | + for (const id of templateIds) { |
| 74 | + const updateInput: UpdateCommandInput = { |
| 75 | + TableName: `nhs-notify-${environment}-app-api-templates`, |
| 76 | + Key: { |
| 77 | + id, |
| 78 | + owner: `CLIENT#${clientId}`, |
| 79 | + }, |
| 80 | + UpdateExpression: |
| 81 | + 'SET templateStatus = :waitingForProofProofStatus, files.proofs = :emptyObj, campaignId = :newId REMOVE sftpSendLockTime', |
| 82 | + ExpressionAttributeValues: { |
| 83 | + ':newId': newCampaignId, |
| 84 | + ':pendingProofStatus': 'PENDING_PROOF_REQUEST', |
| 85 | + ':waitingForProofProofStatus': 'WAITING_FOR_PROOF', |
| 86 | + ':proofAvailableStatus': 'PROOF_AVAILABLE', |
| 87 | + ':submittedStatus': 'SUBMITTED', |
| 88 | + ':emptyObj': {}, |
| 89 | + }, |
| 90 | + ConditionExpression: |
| 91 | + 'attribute_exists(id) AND templateStatus in (:pendingProofStatus, :waitingForProofProofStatus, :submittedStatus, :proofAvailableStatus)', |
| 92 | + ReturnValues: 'ALL_NEW', |
| 93 | + ReturnValuesOnConditionCheckFailure: 'ALL_OLD', |
| 94 | + }; |
| 95 | + |
| 96 | + const res = await ddb.send(new UpdateCommand(updateInput)); |
| 97 | + |
| 98 | + console.log(`updated ${id}`); |
| 99 | + |
| 100 | + const template = z |
| 101 | + .intersection( |
| 102 | + $TemplateDtoSchema, |
| 103 | + z.object({ |
| 104 | + templateType: z.literal('LETTER'), |
| 105 | + clientId: z.string(), |
| 106 | + personalisationParameters: z.array(z.string()), |
| 107 | + createdBy: z.string(), |
| 108 | + }) |
| 109 | + ) |
| 110 | + .parse(res.Attributes); |
| 111 | + |
| 112 | + const proofRequest = { |
| 113 | + campaignId: newCampaignId, |
| 114 | + language: template.language, |
| 115 | + letterType: template.letterType, |
| 116 | + pdfVersionId: template.files.pdfTemplate.currentVersion, |
| 117 | + personalisationParameters: template.personalisationParameters, |
| 118 | + supplier, |
| 119 | + templateId: id, |
| 120 | + templateName: template.name, |
| 121 | + testDataVersionId: template.files.testDataCsv?.currentVersion, |
| 122 | + user: { userId: template.createdBy, clientId: template.clientId }, |
| 123 | + }; |
| 124 | + |
| 125 | + await sqs.send( |
| 126 | + new SendMessageCommand({ |
| 127 | + QueueUrl: `https://sqs.eu-west-2.amazonaws.com/${acct}/nhs-notify-${environment}-app-sftp-upload-queue`, |
| 128 | + MessageBody: JSON.stringify(proofRequest), |
| 129 | + }) |
| 130 | + ); |
| 131 | + |
| 132 | + console.log(`requested proof for ${id}`); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// eslint-disable-next-line unicorn/prefer-top-level-await |
| 137 | +main().catch((error) => { |
| 138 | + console.error(error); |
| 139 | + // eslint-disable-next-line unicorn/no-process-exit |
| 140 | + process.exit(1); |
| 141 | +}); |
0 commit comments