Skip to content

Commit

Permalink
add end date parameter, wait for date validation changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Dormand authored and Scott Dormand committed Nov 19, 2024
1 parent c264b25 commit 88cdc0c
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -583,9 +583,9 @@ describe('sales-api-connector', () => {
it('retrieves all items using .getAll()', async () => {
const expectedResponse = { foo: 'bar' }
fetch.mockReturnValue({ ok: true, status: 200, statusText: 'OK', text: async () => JSON.stringify(expectedResponse) })
await expect(salesApi.authenticate('AAAAAA', '1980-03-02', 'BS9 4PT')).resolves.toEqual(expectedResponse)
await expect(salesApi.authenticate('AAAAAA', '1980-03-02', 'BS9 4PT', '2024-11-18')).resolves.toEqual(expectedResponse)
expect(fetch).toHaveBeenCalledWith(
'http://0.0.0.0:4000/authenticate/renewal/AAAAAA?licenseeBirthDate=1980-03-02&licenseePostcode=BS9%204PT',
'http://0.0.0.0:4000/authenticate/renewal/AAAAAA?licenseeBirthDate=1980-03-02&licenseePostcode=BS9%204PT&licenceEndDate=2024-11-18',
{
method: 'get',
headers: expect.any(Object),
Expand Down
5 changes: 3 additions & 2 deletions packages/connectors-lib/src/sales-api-connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,14 @@ export const countries = new QueryBuilder(new URL('/option-sets/defra_country',
* @param postcode
* @returns {Promise<*>}
*/
export const authenticate = async (referenceNumber, birthDate, postcode) =>
export const authenticate = async (referenceNumber, birthDate, postcode, endDate) =>
exec2xxOrNull(
call(
new URL(
`/authenticate/renewal/${referenceNumber}?${querystring.stringify({
licenseeBirthDate: birthDate,
licenseePostcode: postcode
licenseePostcode: postcode,
licenceEndDate: endDate
})}`,
urlBase
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { permissionForLicensee, permissionForFullReferenceNumber } from '../perm

describe('Permission Queries', () => {
describe('permissionForLicensee', () => {
it('builds a filter to run a query for a permission with a given licensee date of birth and postcode', async () => {
const query = permissionForLicensee('ABC123', '2000-01-01', 'AB12 3CD')
it('builds a filter to run a query for a permission with a given licensee date of birth, postcode and licence end date', async () => {
const query = permissionForLicensee('ABC123', '2000-01-01', 'AB12 3CD', '2024-11-18')
expect(query.toRetrieveRequest()).toEqual({
collection: 'defra_permissions',
expand: expect.arrayContaining([
Expand All @@ -12,7 +12,7 @@ describe('Permission Queries', () => {
expect.objectContaining({ property: 'defra_defra_permission_defra_concessionproof_PermissionId' })
]),
filter:
"endswith(defra_name, 'ABC123') and defra_ContactId/defra_postcode eq 'AB12 3CD' and defra_ContactId/birthdate eq 2000-01-01 and statecode eq 0",
"endswith(defra_name, 'ABC123') and defra_ContactId/defra_postcode eq 'AB12 3CD' and defra_ContactId/birthdate eq 2000-01-01 and defra_enddate eq 2024-11-18 and statecode eq 0",
select: expect.any(Array)
})
})
Expand Down
4 changes: 3 additions & 1 deletion packages/dynamics-lib/src/queries/permission.queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import { ConcessionProof } from '../entities/concession-proof.entity.js'
* @param permissionReferenceNumber the reference number of the permission used to perform the lookup
* @param licenseeBirthDate the birth date of the contact associated with the permission
* @param licenseePostcode the postcode of the contact associated with the permission
* @param licenceEndDate the end date of the permission
* @returns {PredefinedQuery}
*/
export const permissionForLicensee = (permissionReferenceNumber, licenseeBirthDate, licenseePostcode) => {
export const permissionForLicensee = (permissionReferenceNumber, licenseeBirthDate, licenseePostcode, licenceEndDate) => {
const { licensee, permit, concessionProofs } = Permission.definition.relationships
let filter = `endswith(${Permission.definition.mappings.referenceNumber.field}, '${escapeODataStringValue(permissionReferenceNumber)}')`
filter += ` and ${licensee.property}/${licensee.entity.definition.mappings.postcode.field} eq '${escapeODataStringValue(
licenseePostcode
)}'`
filter += ` and ${licensee.property}/${licensee.entity.definition.mappings.birthDate.field} eq ${licenseeBirthDate}`
filter += ` and ${Permission.definition.mappings.endDate.field} eq ${licenceEndDate}`
filter += ` and ${Permission.definition.defaultFilter}`
return new PredefinedQuery({
root: Permission,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ describe.each([
'date-of-birth-month': 1,
'date-of-birth-day': 1,
postcode: 'AB1 1AB',
referenceNumber: 'ABC123'
referenceNumber: 'ABC123',
endDate: '2024-11-18'
}
}),
setCurrentPermission: async () => {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ export default async (request, h) => {

const permission = await request.cache().helpers.status.getCurrentPermission()
const referenceNumber = payload.referenceNumber || permission.referenceNumber
const endDate = payload.endDate || permission.endDate

// Authenticate
const authenticationResult = await salesApi.authenticate(referenceNumber, dateOfBirth, postcode)
const authenticationResult = await salesApi.authenticate(referenceNumber, dateOfBirth, postcode, endDate)

const linkInactive = async reason => {
await request.cache().helpers.status.setCurrentPermission({
Expand Down
10 changes: 8 additions & 2 deletions packages/sales-api-service/src/schema/authenticate.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ export const authenticateRenewalRequestParamsSchema = Joi.object({
}).label('authenticate-renewal-request-params')

export const authenticateRenewalRequestQuerySchema = Joi.object({
licenseeBirthDate: validation.contact.createBirthDateValidator(Joi).description('The date of birth of the licensee'),
licenseeBirthDate: validation.contact
.createBirthDateValidator(Joi)
.description('The date of birth of the licensee'),
licenseePostcode: Joi.alternatives().try(
validation.contact.createUKPostcodeValidator(Joi).description('The postcode of the licensee'),
validation.contact.createOverseasPostcodeValidator(Joi)
)
),
// AWAIT DATE VALIDATOR
licenceEndDate: validation.date
.createRealDateValidator(Joi)
.description('The end date of the licence')
}).label('authenticate-renewal-request-query')

export const authenticateRenewalResponseSchema = Joi.object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ describe('authenticate handler', () => {
executeQuery.mockResolvedValueOnce([{ entity: MOCK_CONCESSION_PROOF_ENTITY, expanded: { concession: { entity: MOCK_CONCESSION } } }])
const result = await server.inject({
method: 'GET',
url: '/authenticate/renewal/CD379B?licenseeBirthDate=2000-01-01&licenseePostcode=AB12 3CD'
url: '/authenticate/renewal/CD379B?licenseeBirthDate=2000-01-01&licenseePostcode=AB12 3CD&licenceEndDate=2020-12-13'
})
expect(permissionForLicensee).toHaveBeenCalledWith('CD379B', '2000-01-01', 'AB12 3CD')
expect(permissionForLicensee).toHaveBeenCalledWith('CD379B', '2000-01-01', 'AB12 3CD', '2020-12-13')
expect(result.statusCode).toBe(200)
expect(JSON.parse(result.payload)).toMatchObject({
permission: expect.objectContaining({
Expand Down Expand Up @@ -73,26 +73,26 @@ describe('authenticate handler', () => {
])
})

it('should call permissionForLicensee with the licence number, dob and postcode for a renewal request, ', async () => {
it('should call permissionForLicensee with the licence number, dob, postcode and end date for a renewal request, ', async () => {
await server.inject({
method: 'GET',
url: '/authenticate/renewal/CD379B?licenseeBirthDate=2000-01-01&licenseePostcode=AB12 3CD'
url: '/authenticate/renewal/CD379B?licenseeBirthDate=2000-01-01&licenseePostcode=AB12 3CD&licenceEndDate=2020-12-13'
})
expect(permissionForLicensee).toHaveBeenCalledWith('CD379B', '2000-01-01', 'AB12 3CD')
})

it('returns 200 from a renewal request', async () => {
const result = await server.inject({
method: 'GET',
url: '/authenticate/renewal/CD379B?licenseeBirthDate=2000-01-01&licenseePostcode=AB12 3CD'
url: '/authenticate/renewal/CD379B?licenseeBirthDate=2000-01-01&licenseePostcode=AB12 3CD&licenceEndDate=2020-12-13'
})
expect(result.statusCode).toBe(200)
})

it('returns permission from a renewal request', async () => {
const result = await server.inject({
method: 'GET',
url: '/authenticate/renewal/CD379B?licenseeBirthDate=2000-01-01&licenseePostcode=AB12 3CD'
url: '/authenticate/renewal/CD379B?licenseeBirthDate=2000-01-01&licenseePostcode=AB12 3CD&licenceEndDate=2020-12-13'
})
expect(JSON.parse(result.payload)).toMatchObject({
permission: expect.objectContaining({
Expand All @@ -109,7 +109,7 @@ describe('authenticate handler', () => {
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(jest.fn())
const result = await server.inject({
method: 'GET',
url: '/authenticate/renewal/CD379B?licenseeBirthDate=2000-01-01&licenseePostcode=AB12 3CD'
url: '/authenticate/renewal/CD379B?licenseeBirthDate=2000-01-01&licenseePostcode=AB12 3CD&licenceEndDate=2020-12-13'
})
expect(result.statusCode).toBe(500)
expect(JSON.parse(result.payload)).toMatchObject({
Expand All @@ -124,7 +124,7 @@ describe('authenticate handler', () => {
executeQuery.mockResolvedValueOnce([])
const result = await server.inject({
method: 'GET',
url: '/authenticate/renewal/CD379B?licenseeBirthDate=2000-01-01&licenseePostcode=AB12 3CD'
url: '/authenticate/renewal/CD379B?licenseeBirthDate=2000-01-01&licenseePostcode=AB12 3CD&licenceEndDate=2020-12-13'
})
expect(result.statusCode).toBe(401)
expect(JSON.parse(result.payload)).toMatchObject({
Expand Down
8 changes: 6 additions & 2 deletions packages/sales-api-service/src/server/routes/authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ export default [
path: '/authenticate/renewal/{referenceNumber}',
options: {
handler: async (request, h) => {
const { licenseeBirthDate, licenseePostcode } = request.query
console.log('hit')
const { licenseeBirthDate, licenseePostcode, licenceEndDate } = request.query
console.log('request.query', request.query)
console.log('licenceEndDate', licenceEndDate)
console.log('licenseeBirthDate', licenseeBirthDate)
const results = await executeWithErrorLog(
permissionForLicensee(request.params.referenceNumber, licenseeBirthDate, licenseePostcode)
permissionForLicensee(request.params.referenceNumber, licenseeBirthDate, licenseePostcode, licenceEndDate)
)

if (results.length === 1) {
Expand Down

0 comments on commit 88cdc0c

Please sign in to comment.