-
Notifications
You must be signed in to change notification settings - Fork 0
[HOTE-983] feat: Generate Notification: Order Dispatched #316
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
cptiv2020
wants to merge
12
commits into
main
Choose a base branch
from
feature/hote-983/generate-notify-for-order-dispatched
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.
+821
−14
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d7d3c92
add initial code with comments to fullfill
cptiv2020 46c6832
introduced logic to send message, extracted new services
cptiv2020 e622c7b
refactoring code, spliting to new classes
cptiv2020 6856bd6
replaced strings w enums
cptiv2020 a1b72e6
refactoring
cptiv2020 3eef93a
added try catch for error
cptiv2020 985312f
fix tests
cptiv2020 f9eda87
Merge branch 'main' into feature/hote-983/generate-notify-for-order-d…
cptiv2020 c6320aa
Merge branch 'feature/hote-983/generate-notify-for-order-dispatched' …
cptiv2020 7474c85
introduced notify service to handle updated status
cptiv2020 33b318d
fix after review
cptiv2020 bc9caf2
wrap status check with try
cptiv2020 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
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,66 @@ | ||
| import { NotifyEventCode } from "../types/notify-message"; | ||
| import { type DBClient } from "./db-client"; | ||
| import { | ||
| NotificationAuditDbClient, | ||
| type NotificationAuditEntryParams, | ||
| NotificationAuditStatus, | ||
| } from "./notification-audit-db-client"; | ||
|
|
||
| const mockQuery = jest.fn(); | ||
|
|
||
| describe("NotificationAuditDbClient", () => { | ||
| let client: NotificationAuditDbClient; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
|
|
||
| const dbClient: DBClient = { | ||
| query: mockQuery, | ||
| withTransaction: jest.fn(), | ||
| close: jest.fn().mockResolvedValue(undefined), | ||
| }; | ||
|
|
||
| client = new NotificationAuditDbClient(dbClient); | ||
| }); | ||
|
|
||
| it("should insert notification audit entry", async () => { | ||
| const params: NotificationAuditEntryParams = { | ||
| messageReference: "123e4567-e89b-12d3-a456-426614174000", | ||
| eventCode: NotifyEventCode.OrderDispatched, | ||
| correlationId: "123e4567-e89b-12d3-a456-426614174001", | ||
| status: NotificationAuditStatus.SENT, | ||
| }; | ||
|
|
||
| mockQuery.mockResolvedValue({ | ||
| rows: [], | ||
| rowCount: 1, | ||
| }); | ||
|
|
||
| await expect(client.insertNotificationAuditEntry(params)).resolves.toBeUndefined(); | ||
|
|
||
| expect(mockQuery).toHaveBeenCalledWith(expect.stringContaining("notification_audit"), [ | ||
| params.messageReference, | ||
| null, | ||
| params.eventCode, | ||
| null, | ||
| params.correlationId, | ||
| params.status, | ||
| ]); | ||
| }); | ||
|
|
||
| it("should throw when notification audit insert affects no rows", async () => { | ||
| mockQuery.mockResolvedValue({ | ||
| rows: [], | ||
| rowCount: 0, | ||
| }); | ||
|
|
||
| await expect( | ||
| client.insertNotificationAuditEntry({ | ||
| messageReference: "123e4567-e89b-12d3-a456-426614174000", | ||
| eventCode: NotifyEventCode.OrderDispatched, | ||
| correlationId: "123e4567-e89b-12d3-a456-426614174001", | ||
| status: NotificationAuditStatus.SENT, | ||
| }), | ||
| ).rejects.toThrow("Failed to insert notification audit entry"); | ||
| }); | ||
| }); |
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,66 @@ | ||
| import { type NotifyEventCode } from "../types/notify-message"; | ||
| import { type DBClient } from "./db-client"; | ||
|
|
||
| export enum NotificationAuditStatus { | ||
| QUEUED = "QUEUED", | ||
| SENT = "SENT", | ||
| FAILED = "FAILED", | ||
| } | ||
|
|
||
| export interface NotificationAuditEntryParams { | ||
| messageReference: string; | ||
| eventCode: NotifyEventCode; | ||
| correlationId: string; | ||
| status: NotificationAuditStatus; | ||
| notifyMessageId?: string | null; | ||
| routingPlanId?: string | null; | ||
| } | ||
|
|
||
| export class NotificationAuditDbClient { | ||
| constructor(private readonly dbClient: DBClient) {} | ||
|
|
||
| async insertNotificationAuditEntry(params: NotificationAuditEntryParams): Promise<void> { | ||
| const { | ||
| messageReference, | ||
| notifyMessageId = null, | ||
| eventCode, | ||
| routingPlanId = null, | ||
| correlationId, | ||
| status, | ||
| } = params; | ||
|
|
||
| const query = ` | ||
| INSERT INTO notification_audit ( | ||
| message_reference, | ||
| notify_message_id, | ||
| event_code, | ||
| routing_plan_id, | ||
| correlation_id, | ||
| status | ||
| ) | ||
| VALUES ($1::uuid, $2, $3, $4::uuid, $5::uuid, $6) | ||
| `; | ||
|
|
||
| try { | ||
| const result = await this.dbClient.query(query, [ | ||
| messageReference, | ||
| notifyMessageId, | ||
| eventCode, | ||
| routingPlanId, | ||
| correlationId, | ||
| status, | ||
| ]); | ||
|
|
||
| if (result.rowCount === 0) { | ||
| throw new Error("Failed to insert notification audit entry"); | ||
| } | ||
| } catch (error) { | ||
| throw new Error( | ||
| `Failed to insert notification audit entry for messageReference ${messageReference}`, | ||
| { | ||
| cause: error, | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
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
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
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,55 @@ | ||
| import { type DBClient } from "./db-client"; | ||
| import { PatientDbClient } from "./patient-db-client"; | ||
|
|
||
| const mockQuery = jest.fn(); | ||
|
|
||
| describe("PatientDbClient", () => { | ||
| let patientDbClient: PatientDbClient; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
|
|
||
| const dbClient: DBClient = { | ||
| query: mockQuery, | ||
| withTransaction: jest.fn(), | ||
| close: jest.fn().mockResolvedValue(undefined), | ||
| }; | ||
|
|
||
| patientDbClient = new PatientDbClient(dbClient); | ||
| }); | ||
|
|
||
| describe("get", () => { | ||
| it("should return notify recipient data", async () => { | ||
| mockQuery.mockResolvedValue({ | ||
| rows: [ | ||
| { | ||
| nhs_number: "1234567890", | ||
| birth_date: "1990-04-20", | ||
| }, | ||
| ], | ||
| rowCount: 1, | ||
| }); | ||
|
|
||
| const result = await patientDbClient.get("some-mocked-patient-id"); | ||
|
|
||
| expect(result).toEqual({ | ||
| nhsNumber: "1234567890", | ||
| birthDate: "1990-04-20", | ||
| }); | ||
| expect(mockQuery).toHaveBeenCalledWith(expect.stringContaining("patient_mapping"), [ | ||
| "some-mocked-patient-id", | ||
| ]); | ||
| }); | ||
|
|
||
| it("should throw when patient record does not exist", async () => { | ||
| mockQuery.mockResolvedValue({ | ||
| rows: [], | ||
| rowCount: 0, | ||
| }); | ||
|
|
||
| await expect(patientDbClient.get("missing-patient-id")).rejects.toThrow( | ||
| "Failed to fetch notify recipient data", | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
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 @@ | ||
| import { type DBClient } from "./db-client"; | ||
|
|
||
| export interface Patient { | ||
| nhsNumber: string; | ||
| birthDate: string; | ||
| } | ||
|
|
||
| export class PatientDbClient { | ||
| constructor(private readonly dbClient: DBClient) {} | ||
|
|
||
| async get(patientId: string): Promise<Patient> { | ||
| const query = ` | ||
| SELECT nhs_number, birth_date | ||
| FROM patient_mapping | ||
| WHERE patient_uid = $1::uuid | ||
| LIMIT 1; | ||
| `; | ||
|
|
||
| try { | ||
| const result = await this.dbClient.query< | ||
| { nhs_number: string; birth_date: string }, | ||
| [string] | ||
| >(query, [patientId]); | ||
|
|
||
| if (result.rowCount === 0 || !result.rows[0]) { | ||
| throw new Error(`Notify recipient not found for patientId ${patientId}`); | ||
| } | ||
|
|
||
| const row = result.rows[0]; | ||
|
|
||
| return { | ||
| nhsNumber: row.nhs_number, | ||
| birthDate: row.birth_date, | ||
| }; | ||
cptiv2020 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (error) { | ||
| throw new Error(`Failed to fetch notify recipient data for patientId ${patientId}`, { | ||
| cause: error, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
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
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.