Skip to content
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

Fix bug where transaction id is not recognised #2071

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('The agreed handler', () => {
cache: () => ({
helpers: {
transaction: {
get: async () => ({ id: 'id-111', cost: 0 }),
get: async () => ({ cost: 0 }),
set: async () => {}
},
status: {
Expand Down Expand Up @@ -90,6 +90,41 @@ describe('The agreed handler', () => {
expect(transactionPayload.id).toBe(v4guid)
})

it("doesn't overwrite transaction id if one is already set", async () => {
const setTransaction = jest.fn()
const transactionId = 'abc-123-def-456'
uuidv4.mockReturnValue('def-789-ghi-012')
salesApi.finaliseTransaction.mockReturnValueOnce({
permissions: []
})
const mockRequest = {
cache: () => ({
helpers: {
status: {
get: async () => ({
[COMPLETION_STATUS.agreed]: true,
[COMPLETION_STATUS.posted]: true,
[COMPLETION_STATUS.finalised]: false,
[RECURRING_PAYMENT]: false
}),
set: () => {}
},
transaction: {
get: async () => ({ cost: 0, id: transactionId }),
set: setTransaction
}
}
})
}

await agreedHandler(mockRequest, getRequestToolkit())

expect(salesApi.finaliseTransaction).toHaveBeenCalledWith(
transactionId,
undefined // prepareApiFinalisationPayload has no mocked return value
)
})

it('sends a recurring payment creation request to Gov.UK Pay', async () => {
const preparedPayment = Symbol('preparedPayment')
prepareRecurringPayment.mockResolvedValueOnce(preparedPayment)
Expand Down
5 changes: 4 additions & 1 deletion packages/gafl-webapp-service/src/handlers/agreed-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,9 @@ const finaliseTransaction = async (request, transaction, status) => {
export default async (request, h) => {
const status = await request.cache().helpers.status.get()
const transaction = await request.cache().helpers.transaction.get()
transaction.id = uuidv4()
if (!transaction.id) {
transaction.id = uuidv4()
}

// If the agreed flag is not set to true then throw an exception
if (!status[COMPLETION_STATUS.agreed]) {
Expand Down Expand Up @@ -279,6 +281,7 @@ export default async (request, h) => {

// If the transaction has already been finalised then redirect to the order completed page
if (!status[COMPLETION_STATUS.finalised]) {
console.log('finalising transaction', request, transaction, status)
await finaliseTransaction(request, transaction, status)
} else {
debug('Transaction %s already finalised, redirect to order complete: %s', transaction.id)
Expand Down
Loading