-
Notifications
You must be signed in to change notification settings - Fork 5
CCM-12345: Letter Fail Script #590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
masl2
wants to merge
8
commits into
main
Choose a base branch
from
fix/CCM-_fail-long-group-ids
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d56c1ae
script outline
masl2 03fd48e
run npm install
Vlasis-Perdikidis f0a4eeb
remove attempt 1
masl2 0e0a52f
script updates
masl2 d5f83b5
lint, units, types
masl2 5ba1d6a
package updates
masl2 ea347f8
identify bad filter expression
masl2 141df91
fix unit tests and lint errors
Vlasis-Perdikidis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
|
masl2 marked this conversation as resolved.
Outdated
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #!/usr/bin/env tsx | ||
|
|
||
| // Script: update-failed-letters.ts | ||
| // Usage: tsx scripts/update-failed-letters.ts <supplierId> <status> | ||
| // Description: Looks up letters for a given supplierId and status, and updates letter statuses to FAILED where groupId is longer than 100 characters. | ||
|
|
||
| import { LetterRepository } from "@internal/datastore/src/letter-repository"; | ||
| import { getDbContext } from "@internal/datastore/src/db-context"; | ||
| import pino from "pino"; | ||
|
|
||
| const logger = pino({ level: "info" }); | ||
|
|
||
| async function main() { | ||
| const [supplierId, status] = process.argv.slice(2); | ||
| if (!supplierId || !status) { | ||
| logger.error("Usage: tsx scripts/update-failed-letters.ts <supplierId> <status>"); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const db = await getDbContext(); | ||
| const letterRepo = new LetterRepository(db, logger); | ||
|
|
||
| logger.info(`Looking up letters for supplierId=${supplierId}, status=${status}`); | ||
| const letters = await letterRepo.getLettersBySupplierAndStatus(supplierId, status); | ||
|
masl2 marked this conversation as resolved.
Outdated
|
||
|
|
||
| let updatedCount = 0; | ||
| for (const letter of letters) { | ||
| if (letter.groupId && letter.groupId.length > 100) { | ||
| logger.info(`Updating letter ${letter.id} (groupId length: ${letter.groupId.length}) to FAILED`); | ||
| await letterRepo.updateLetterStatus(letter.id, "FAILED"); | ||
|
masl2 marked this conversation as resolved.
Outdated
|
||
| updatedCount++; | ||
| } | ||
| } | ||
|
|
||
| logger.info(`Updated ${updatedCount} letters to FAILED.`); | ||
|
masl2 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| main().catch((err) => { | ||
| logger.error({ err }, "Script failed"); | ||
| process.exit(1); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "dependencies": { | ||
| "@aws-sdk/client-dynamodb": "^3.984.0", | ||
| "@aws-sdk/lib-dynamodb": "^3.1008.0", | ||
| "pino": "^10.3.0", | ||
| "yargs": "^17.7.2" | ||
| }, | ||
| "main": "src/cli/index.ts", | ||
| "name": "letter-status-fixer", | ||
| "private": true, | ||
| "scripts": { | ||
| "fix-status": "tsx src/cli/index.ts fix-status" | ||
| }, | ||
| "type": "module", | ||
| "version": "0.1.0" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { hideBin } from "yargs/helpers"; | ||
| import yargs from "yargs"; | ||
| import { QueryCommand, QueryCommandOutput, UpdateCommand } from "@aws-sdk/lib-dynamodb"; | ||
| import { createLetterDocClient } from "../infrastructure/letters-repo-factory"; | ||
|
|
||
| async function updateFailedLetters(environment: string, supplierId: string, status: string) { | ||
| const { docClient, log, config } = createLetterDocClient(environment); | ||
| const compoundKey = `${supplierId}#${status}`; | ||
|
|
||
| let lastKey: Record<string, any> | undefined = undefined; | ||
| let updatedCount = 0; | ||
|
|
||
| do { | ||
|
masl2 marked this conversation as resolved.
|
||
| const queryCmd = new QueryCommand({ | ||
| TableName: config.lettersTableName, | ||
| IndexName: config.supplierStatusIndex, | ||
| KeyConditionExpression: "supplierStatus = :ss", | ||
| ExpressionAttributeValues: { ":ss": compoundKey }, | ||
| ExclusiveStartKey: lastKey, | ||
| }); | ||
| const result = await docClient.send(queryCmd) as QueryCommandOutput; | ||
|
masl2 marked this conversation as resolved.
Outdated
|
||
| for (const item of result.Items || []) { | ||
| if (item.groupId && item.groupId.length > 100) { | ||
|
masl2 marked this conversation as resolved.
Outdated
|
||
| log.info(`Updating letter ${item.letterId} (groupId length: ${item.groupId.length}) to FAILED`); | ||
| const updateCmd = new UpdateCommand({ | ||
| TableName: config.lettersTableName, | ||
| Key: { letterId: item.letterId }, | ||
| UpdateExpression: "SET #status = :failed", | ||
| ExpressionAttributeNames: { "#status": "status" }, | ||
| ExpressionAttributeValues: { ":failed": "FAILED" }, | ||
| }); | ||
| await docClient.send(updateCmd); | ||
|
masl2 marked this conversation as resolved.
Outdated
|
||
| updatedCount++; | ||
| } | ||
| } | ||
| lastKey = result.LastEvaluatedKey; | ||
| } while (lastKey); | ||
|
|
||
| log.info(`Updated ${updatedCount} letters to FAILED.`); | ||
|
masl2 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| async function main() { | ||
| await yargs(hideBin(process.argv)) | ||
| .command( | ||
| "fix-status", | ||
| "Update letters with long groupId to FAILED", | ||
| { | ||
| environment: { type: "string", demandOption: true }, | ||
| supplierId: { type: "string", demandOption: true }, | ||
| status: { type: "string", demandOption: true }, | ||
| }, | ||
| async (argv) => { | ||
| await updateFailedLetters(argv.environment, argv.supplierId, argv.status); | ||
| } | ||
| ) | ||
| .demandCommand(1) | ||
| .parse(); | ||
| } | ||
|
|
||
| if (require.main === module) { | ||
| main().catch((error) => { | ||
| console.error(error); | ||
| process.exitCode = 1; | ||
| }); | ||
| } | ||
14 changes: 14 additions & 0 deletions
14
scripts/utilities/letter-status-fixer/src/infrastructure/letters-repo-factory.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; | ||
| import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; | ||
| import pino from "pino"; | ||
|
|
||
| export function createLetterDocClient(environment: string) { | ||
| const ddbClient = new DynamoDBClient({}); | ||
| const docClient = DynamoDBDocumentClient.from(ddbClient); | ||
| const log = pino(); | ||
| const config = { | ||
| lettersTableName: process.env.LETTERS_TABLE || `nhs-${environment}-supapi-letters`, | ||
| supplierStatusIndex: process.env.SUPPLIER_STATUS_INDEX || "supplierStatus", | ||
| }; | ||
| return { docClient, log, config }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "esModuleInterop": true, | ||
| "module": "ES2020", | ||
| "moduleResolution": "node", | ||
| "outDir": "dist", | ||
| "resolveJsonModule": true, | ||
| "rootDir": "src", | ||
| "skipLibCheck": true, | ||
| "strict": true, | ||
| "target": "ES2022" | ||
| }, | ||
| "extends": "../../../tsconfig.base.json", | ||
| "include": [ | ||
| "src/**/*" | ||
| ] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.