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

feat: add withdraw link #59

Merged
merged 7 commits into from
Feb 1, 2023
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
22 changes: 21 additions & 1 deletion app/api/applications.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,27 @@ async function getApplications (searchType, searchText, limit, offset, filterSta
return { applications: [], total: 0, applicationStatus: [] }
}
}

async function withdrawApplication (reference, user, status) {
const url = `${applicationApiUri}/application/${reference}`
const options = {
payload: {
user,
status
},
json: true
}
try {
await Wreck.put(url, options)
return true
} catch (err) {
console.log(err)
return false
}
}

module.exports = {
getApplications,
getApplication
getApplication,
withdrawApplication
}
13 changes: 13 additions & 0 deletions app/frontend/src/css/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,16 @@ a.pagination__link:focus {
.govuk-summary-list {
margin-bottom: 60px !important;
}

.govuk-link {
color: #d4351c;
}

.govuk-panel--confirmation {
background-color: #fafafa;
border-color: #000000;
border-width: medium;
h1 {
color: #000000;
}
}
1 change: 1 addition & 0 deletions app/plugins/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const routes = [].concat(
require('../routes/login'),
require('../routes/logout'),
require('../routes/privacy-policy'),
require('../routes/withdraw-application'),
require('../routes/view-application')
)

Expand Down
8 changes: 7 additions & 1 deletion app/routes/view-application.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ module.exports = {
reference: Joi.string().valid()
}),
query: Joi.object({
page: Joi.number().greater(0).default(1)
page: Joi.number().greater(0).default(1),
withdraw: Joi.bool().default(false)
})
},
handler: async (request, h) => {
Expand All @@ -27,13 +28,18 @@ module.exports = {

const status = upperFirstLetter(application.status.status.toLowerCase())
const statusClass = getStyleClassByStatus(application.status.status)
const withdrawLinkStatus = ['IN CHECK', 'AGREED']
const withdrawConfirmationForm = application.status.status !== 'WITHDRAWN' && withdrawLinkStatus.includes(application.status.status) && request.query.withdraw

return h.view('view-application', {
applicationId: application.reference,
status,
statusClass,
organisationName: application?.data?.organisation?.name,
vetVisit: application?.vetVisit,
claimed: application?.claimed,
withdrawLink: withdrawLinkStatus.includes(application.status.status),
withdrawConfirmationForm,
payment: application?.payment,
...new ViewModel(application),
page: request.query.page
Expand Down
22 changes: 22 additions & 0 deletions app/routes/withdraw-application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const Joi = require('joi')
const { withdrawApplication } = require('../api/applications')

module.exports = {
method: 'POST',
path: '/withdraw-application',
options: {
validate: {
payload: Joi.object({
withdrawConfirmation: Joi.string().valid('yes', 'no'),
reference: Joi.string().valid(),
page: Joi.number().greater(0).default(1)
})
},
handler: async (request, h) => {
if (request.payload.withdrawConfirmation === 'yes') {
await withdrawApplication(request.payload.reference, 'admin', 2)
}
return h.redirect(`/view-application/${request.payload.reference}?page=${request?.payload?.page || 1}`)
}
}
}
12 changes: 12 additions & 0 deletions app/views/macros/withdraw-confirmation-form.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% macro viewWithdrawConfirmationForm(applicationId, page, crumb) %}
<div class="govuk-panel govuk-panel--confirmation govuk-!-text-align-left">
<h1 class="govuk-panel__title-s govuk-!-font-size-36 govuk-!-margin-top-1">Are you sure you want to withdraw?</h1>
<form method="POST" autocomplete="off" novalidate="novalidate" action="/withdraw-application">
<button class="govuk-button govuk-button govuk-!-margin-bottom-3" name="withdrawConfirmation" value="yes">Yes</button>
<button class="govuk-button govuk-button--secondary govuk-!-margin-bottom-3" name="withdrawConfirmation" value="no">No</button>
<input type="hidden" name="reference" value="{{ applicationId }}" />
<input type="hidden" name="page" value="{{ page }}" />
<input type="hidden" name="crumb" value="{{crumb}}"/>
</form>
</div>
{% endmacro %}
7 changes: 7 additions & 0 deletions app/views/view-application.njk
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% extends './layouts/layout.njk' %}
{% from "./macros/view-application-tabs.njk" import viewApplicationTabs %}
{% from "./macros/withdraw-confirmation-form.njk" import viewWithdrawConfirmationForm %}

{% block pageTitle %}
{{ siteTitle }}: User Application
Expand All @@ -23,7 +24,13 @@
{{ govukSummaryList({
rows: model.listData.rows
}) }}
{% if withdrawConfirmationForm %}
{{ viewWithdrawConfirmationForm(applicationId, page, crumb) }}
{% endif %}
{{ viewApplicationTabs(model, vetVisit, claimed, payment, status) }}
{% if withdrawLink %}
<a class="govuk-link govuk-body" href="/view-application/{{ applicationId }}?page={{page}}&withdraw=true">Withdraw</a>
{% endif %}
</div>
</div>
{% endblock %}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ffc-ahwr-backoffice",
"version": "1.14.10",
"version": "1.15.0",
"description": "Back office of the health and welfare of your livestock",
"homepage": "https://github.com/DEFRA/ffc-ahwr-backoffice",
"main": "app/index.js",
Expand Down
88 changes: 88 additions & 0 deletions test/integration/narrow/routes/withdraw-application.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const cheerio = require('cheerio')
const expectPhaseBanner = require('../../../utils/phase-banner-expect')
const { administrator } = require('../../../../app/auth/permissions')
const getCrumbs = require('../../../utils/get-crumbs')

const applications = require('../../../../app/api/applications')
jest.mock('../../../../app/api/applications')

const reference = 'AHWR-555A-FD4C'

applications.withdrawApplication = jest.fn().mockResolvedValue(true)

describe('View Application test', () => {
let crumb
const url = '/withdraw-application/'
jest.mock('../../../../app/auth')
const auth = { strategy: 'session-auth', credentials: { scope: [administrator] } }

beforeEach(async () => {
crumb = await getCrumbs(global.__SERVER__)
jest.clearAllMocks()
})

describe(`POST ${url} route`, () => {
test('returns 302 no auth', async () => {
const options = {
method: 'POST',
url
}
const res = await global.__SERVER__.inject(options)
expect(res.statusCode).toBe(302)
})

test('returns 403', async () => {
const options = {
method: 'POST',
url,
auth,
payload: {
reference
}
}
const res = await global.__SERVER__.inject(options)
expect(res.statusCode).toBe(403)
const $ = cheerio.load(res.payload)
expect($('h1.govuk-heading-l').text()).toEqual('403 - Forbidden')
expectPhaseBanner.ok($)
})

test('Approve withdraw application', async () => {
const options = {
method: 'POST',
url,
auth,
headers: { cookie: `crumb=${crumb}` },
payload: {
reference,
withdrawConfirmation: 'yes',
page: 1,
crumb
}
}
const res = await global.__SERVER__.inject(options)
expect(applications.withdrawApplication).toHaveBeenCalledTimes(1)
expect(res.statusCode).toBe(302)
expect(res.headers.location).toEqual(`/view-application/${reference}?page=1`)
})

test('Cancel withdraw application', async () => {
const options = {
method: 'POST',
url,
auth,
headers: { cookie: `crumb=${crumb}` },
payload: {
reference,
withdrawConfirmation: 'no',
page: 1,
crumb
}
}
const res = await global.__SERVER__.inject(options)
expect(applications.withdrawApplication).toHaveBeenCalledTimes(0)
expect(res.statusCode).toBe(302)
expect(res.headers.location).toEqual(`/view-application/${reference}?page=1`)
})
})
})
44 changes: 43 additions & 1 deletion test/unit/api/applications.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const limit = 20
const offset = 0
let searchText = ''
let searchType = ''
const { getApplications, getApplication } = require('../../../app/api/applications')
const { getApplications, getApplication, withdrawApplication } = require('../../../app/api/applications')
describe('Application API', () => {
it('GetApplications should return empty applications array', async () => {
jest.mock('@hapi/wreck')
Expand Down Expand Up @@ -144,4 +144,46 @@ describe('Application API', () => {
expect(Wreck.get).toHaveBeenCalledTimes(1)
expect(Wreck.get).toHaveBeenCalledWith(`${applicationApiUri}/application/get/${appRef}`, options)
})

it('WithdrawApplication should return false if api not available', async () => {
jest.mock('@hapi/wreck')
const options = {
payload: {
user: 'test',
status: 2
},
json: true
}
Wreck.put = jest.fn(async function (_url, _options) {
throw (new Error('fakeError'))
})
const response = await withdrawApplication(appRef, 'test', 2)
expect(response).toBe(false)
expect(Wreck.put).toHaveBeenCalledTimes(1)
expect(Wreck.put).toHaveBeenCalledWith(`${applicationApiUri}/application/${appRef}`, options)
})

it('WithdrawApplication should return true after successful API request', async () => {
jest.mock('@hapi/wreck')
const options = {
payload: {
user: 'test',
status: 2
},
json: true
}
const wreckResponse = {
res: {
statusCode: 200
}
}

Wreck.put = jest.fn(async function (_url, _options) {
return wreckResponse
})
const response = await withdrawApplication(appRef, 'test', 2)
expect(response).toBe(true)
expect(Wreck.put).toHaveBeenCalledTimes(1)
expect(Wreck.put).toHaveBeenCalledWith(`${applicationApiUri}/application/${appRef}`, options)
})
})