@defra-fish/dynamics-lib 1.58.0 → 1.59.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra-fish/dynamics-lib",
3
- "version": "1.58.0",
3
+ "version": "1.59.0-rc.0",
4
4
  "description": "Framework to support integration with dynamics",
5
5
  "type": "module",
6
6
  "engines": {
@@ -43,5 +43,5 @@
43
43
  "simple-oauth2": "^4.3.0",
44
44
  "uuid": "^8.3.2"
45
45
  },
46
- "gitHead": "4d2c2ef1bf3016fa38140a1bd78bf26ba5adb68d"
46
+ "gitHead": "01bc1d91c4f25234b008dafe06b0525760f9c752"
47
47
  }
@@ -1,5 +1,7 @@
1
- import { contactForLicensee } from '../contact.queries.js'
1
+ import { contactForLicensee, contactForLicenseeNoReference } from '../contact.queries.js'
2
2
  import { dynamicsClient } from '../../client/dynamics-client.js'
3
+ import { Contact } from '../../entities/contact.entity.js'
4
+ import { PredefinedQuery } from '../predefined-query.js'
3
5
 
4
6
  jest.mock('dynamics-web-api', () => {
5
7
  return jest.fn().mockImplementation(() => {
@@ -97,4 +99,50 @@ describe('Contact Queries', () => {
97
99
  })
98
100
  })
99
101
  })
102
+
103
+ describe('contactForLicenseeNoReference', () => {
104
+ beforeEach(() => {
105
+ jest.resetAllMocks()
106
+
107
+ jest.spyOn(Contact.definition, 'mappings', 'get').mockReturnValue({
108
+ postcode: { field: 'mock_postcode' },
109
+ birthDate: { field: 'mock_birthdate' }
110
+ })
111
+
112
+ jest.spyOn(Contact.definition, 'defaultFilter', 'get').mockReturnValue('statecode eq 0')
113
+ })
114
+
115
+ it('should return a predefined query', () => {
116
+ const result = contactForLicenseeNoReference('03/12/1990', 'AB12 3CD')
117
+ expect(result).toBeInstanceOf(PredefinedQuery)
118
+ })
119
+
120
+ it('root should return Contact', () => {
121
+ const result = contactForLicenseeNoReference('03/12/1990', 'AB12 3CD')
122
+ expect(result._root).toEqual(Contact)
123
+ })
124
+
125
+ it('should use mocked values for mapping postcode and birth date', () => {
126
+ const result = contactForLicenseeNoReference('03/12/1990', 'AB12 3CD')
127
+ expect(result._retrieveRequest.filter).toEqual(
128
+ expect.stringContaining(Contact.definition.mappings.postcode.field),
129
+ expect.stringContaining(Contact.definition.mappings.birthDate.field)
130
+ )
131
+ })
132
+
133
+ it.each([
134
+ ['AB12 3CD', '03/12/1990'],
135
+ ['EF45 6GH', '05/06/1987'],
136
+ ['IJ78 9KL', '14/11/1975']
137
+ ])('should return correct retrieve request when postcode is %s and birth date is %s', (postcode, birthDate) => {
138
+ const result = contactForLicenseeNoReference(birthDate, postcode)
139
+
140
+ expect(result._retrieveRequest).toEqual({
141
+ collection: 'contacts',
142
+ expand: [],
143
+ filter: `mock_postcode eq '${postcode}' and mock_birthdate eq ${birthDate} and statecode eq 0`,
144
+ select: expect.any(Array)
145
+ })
146
+ })
147
+ })
100
148
  })
@@ -1,36 +1,41 @@
1
- import { permissionForLicensee, permissionForFullReferenceNumber } from '../permission.queries.js'
1
+ import { permissionForFullReferenceNumber, permissionForContacts } from '../permission.queries.js'
2
2
 
3
3
  describe('Permission Queries', () => {
4
- describe('permissionForLicensee', () => {
5
- it('builds a filter to run a query for a permission with a given licensee date of birth and postcode', async () => {
6
- const query = permissionForLicensee('ABC123', '2000-01-01', 'AB12 3CD')
7
- expect(query.toRetrieveRequest()).toEqual({
8
- collection: 'defra_permissions',
9
- expand: expect.arrayContaining([
10
- expect.objectContaining({ property: 'defra_ContactId' }),
11
- expect.objectContaining({ property: 'defra_PermitId' }),
12
- expect.objectContaining({ property: 'defra_defra_permission_defra_concessionproof_PermissionId' })
13
- ]),
14
- filter:
15
- "endswith(defra_name, 'ABC123') and defra_ContactId/defra_postcode eq 'AB12 3CD' and defra_ContactId/birthdate eq 2000-01-01 and statecode eq 0",
16
- select: expect.any(Array)
17
- })
18
- })
4
+ describe('permissionForFullReferenceNumber', () => {
5
+ it.each([['ABC123'], ['DEF456'], ['GHI789']])(
6
+ 'builds a filter to run a query for a permission with a full reference number whch is %s',
7
+ async referenceNumber => {
8
+ const query = permissionForFullReferenceNumber(referenceNumber)
9
+ expect(query.toRetrieveRequest()).toEqual({
10
+ collection: 'defra_permissions',
11
+ expand: expect.arrayContaining([
12
+ expect.objectContaining({ property: 'defra_ContactId' }),
13
+ expect.objectContaining({ property: 'defra_PermitId' }),
14
+ expect.objectContaining({ property: 'defra_defra_permission_defra_concessionproof_PermissionId' })
15
+ ]),
16
+ filter: `defra_name eq '${referenceNumber}' and statecode eq 0`,
17
+ select: expect.any(Array)
18
+ })
19
+ }
20
+ )
19
21
  })
20
22
 
21
- describe('permissionForFullReferenceNumber', () => {
22
- it('builds a filter to run a query for a permission with a full reference number', async () => {
23
- const query = permissionForFullReferenceNumber('ABC123')
24
- expect(query.toRetrieveRequest()).toEqual({
25
- collection: 'defra_permissions',
26
- expand: expect.arrayContaining([
27
- expect.objectContaining({ property: 'defra_ContactId' }),
28
- expect.objectContaining({ property: 'defra_PermitId' }),
29
- expect.objectContaining({ property: 'defra_defra_permission_defra_concessionproof_PermissionId' })
30
- ]),
31
- filter: "defra_name eq 'ABC123' and statecode eq 0",
32
- select: expect.any(Array)
33
- })
34
- })
23
+ describe('permissionForContacts', () => {
24
+ it.each([['AB12 3CD'], ['EF45 6GH'], ['IJ78 9KL']])(
25
+ 'builds a filter to run a query for a permission with contact id which is %s',
26
+ async contactId => {
27
+ const query = permissionForContacts([contactId])
28
+ expect(query.toRetrieveRequest()).toEqual({
29
+ collection: 'defra_permissions',
30
+ expand: expect.arrayContaining([
31
+ expect.objectContaining({ property: 'defra_ContactId' }),
32
+ expect.objectContaining({ property: 'defra_PermitId' }),
33
+ expect.objectContaining({ property: 'defra_defra_permission_defra_concessionproof_PermissionId' })
34
+ ]),
35
+ filter: `(defra_ContactId/contactid eq '${contactId}') and statecode eq 0`,
36
+ select: expect.any(Array)
37
+ })
38
+ }
39
+ )
35
40
  })
36
41
  })
@@ -1,4 +1,7 @@
1
1
  import { dynamicsClient } from '../client/dynamics-client.js'
2
+ import { Contact } from '../entities/contact.entity.js'
3
+ import { escapeODataStringValue } from '../client/util.js'
4
+ import { PredefinedQuery } from './predefined-query.js'
2
5
 
3
6
  /**
4
7
  * @typedef {Object} ContactByLicenceAndPostcode
@@ -33,3 +36,15 @@ export const contactForLicensee = (permissionReferenceNumberLast6Characters, lic
33
36
 
34
37
  return dynamicsClient.executeUnboundAction('defra_GetContactByLicenceAndPostcode', request)
35
38
  }
39
+
40
+ export const contactForLicenseeNoReference = (licenseeBirthDate, licenseePostcode) => {
41
+ const { postcode, birthDate } = Contact.definition.mappings
42
+ const filter = `${postcode.field} eq '${escapeODataStringValue(licenseePostcode)}' and ${birthDate.field} eq ${licenseeBirthDate} and ${
43
+ Contact.definition.defaultFilter
44
+ }`
45
+ return new PredefinedQuery({
46
+ root: Contact,
47
+ filter,
48
+ expand: []
49
+ })
50
+ }
@@ -3,36 +3,26 @@ import { Permission } from '../entities/permission.entity.js'
3
3
  import { escapeODataStringValue } from '../client/util.js'
4
4
  import { ConcessionProof } from '../entities/concession-proof.entity.js'
5
5
 
6
- /**
7
- * Builds a query to retrieve a permission and related entities for a given reference number and related contact information
8
- *
9
- * @param permissionReferenceNumber the reference number of the permission used to perform the lookup
10
- * @param licenseeBirthDate the birth date of the contact associated with the permission
11
- * @param licenseePostcode the postcode of the contact associated with the permission
12
- * @returns {PredefinedQuery}
13
- */
14
- export const permissionForLicensee = (permissionReferenceNumber, licenseeBirthDate, licenseePostcode) => {
6
+ export const permissionForFullReferenceNumber = permissionReferenceNumber => {
15
7
  const { licensee, permit, concessionProofs } = Permission.definition.relationships
16
- let filter = `endswith(${Permission.definition.mappings.referenceNumber.field}, '${escapeODataStringValue(permissionReferenceNumber)}')`
17
- filter += ` and ${licensee.property}/${licensee.entity.definition.mappings.postcode.field} eq '${escapeODataStringValue(
18
- licenseePostcode
19
- )}'`
20
- filter += ` and ${licensee.property}/${licensee.entity.definition.mappings.birthDate.field} eq ${licenseeBirthDate}`
21
- filter += ` and ${Permission.definition.defaultFilter}`
8
+ const filter = `${Permission.definition.mappings.referenceNumber.field} eq '${escapeODataStringValue(permissionReferenceNumber)}' and ${
9
+ Permission.definition.defaultFilter
10
+ }`
22
11
  return new PredefinedQuery({
23
12
  root: Permission,
24
13
  filter: filter,
25
- expand: [licensee, permit, concessionProofs]
14
+ expand: [licensee, permit, { ...concessionProofs, expand: [ConcessionProof.definition.relationships.concession] }]
26
15
  })
27
16
  }
28
17
 
29
- export const permissionForFullReferenceNumber = permissionReferenceNumber => {
18
+ export const permissionForContacts = contactIds => {
30
19
  const { licensee, permit, concessionProofs } = Permission.definition.relationships
31
- let filter = `${Permission.definition.mappings.referenceNumber.field} eq '${escapeODataStringValue(permissionReferenceNumber)}'`
32
- filter += ` and ${Permission.definition.defaultFilter}`
20
+ const formattedContactIds = contactIds.map(id => `defra_ContactId/contactid eq '${id}'`).join(' or ')
21
+ const filter = `(${formattedContactIds}) and ${Permission.definition.defaultFilter}`
22
+
33
23
  return new PredefinedQuery({
34
24
  root: Permission,
35
25
  filter: filter,
36
- expand: [licensee, permit, { ...concessionProofs, expand: [ConcessionProof.definition.relationships.concession] }]
26
+ expand: [licensee, permit, concessionProofs]
37
27
  })
38
28
  }