@defra-fish/dynamics-lib 1.76.0 → 1.77.0-rc.10

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.76.0",
3
+ "version": "1.77.0-rc.10",
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": "2287ca0defd4b61d66cbea6f0df84568265b036f"
46
+ "gitHead": "b41c703037390290c9403cafc2e0252ef63e149e"
47
47
  }
@@ -1,7 +1,8 @@
1
- import { contactAndPermissionForLicensee, contactForLicenseeNoReference } from '../contact.queries.js'
1
+ import { contactAndPermissionForLicensee, contactForLicenseeNoReference, contactForLicenseeByPersonalDetails } from '../contact.queries.js'
2
2
  import { Contact } from '../../entities/contact.entity.js'
3
3
  import { Permission } from '../../entities/permission.entity.js'
4
4
  import { PredefinedQuery } from '../predefined-query.js'
5
+ import { escapeODataStringValue } from '../../client/util.js'
5
6
 
6
7
  jest.mock('dynamics-web-api', () => {
7
8
  return {
@@ -13,17 +14,21 @@ jest.mock('dynamics-web-api', () => {
13
14
  }
14
15
  })
15
16
 
17
+ jest.mock('../../client/util.js', () => ({
18
+ escapeODataStringValue: jest.fn(value => value)
19
+ }))
20
+
16
21
  describe('Contact Queries', () => {
17
22
  describe('contactForLicenseeNoReference', () => {
18
23
  beforeEach(() => {
19
- jest.resetAllMocks()
24
+ const defaultFilter = 'statecode eq 77'
20
25
 
21
26
  jest.spyOn(Contact.definition, 'mappings', 'get').mockReturnValue({
22
27
  postcode: { field: 'mock_postcode' },
23
28
  birthDate: { field: 'mock_birthdate' }
24
29
  })
25
30
 
26
- jest.spyOn(Contact.definition, 'defaultFilter', 'get').mockReturnValue('statecode eq 0')
31
+ jest.spyOn(Contact.definition, 'defaultFilter', 'get').mockReturnValue(defaultFilter)
27
32
  })
28
33
 
29
34
  it('should return a predefined query', () => {
@@ -36,14 +41,6 @@ describe('Contact Queries', () => {
36
41
  expect(result._root).toEqual(Contact)
37
42
  })
38
43
 
39
- it('should use mocked values for mapping postcode and birth date', () => {
40
- const result = contactForLicenseeNoReference('03/12/1990', 'AB12 3CD')
41
- expect(result._retrieveRequest.filter).toEqual(
42
- expect.stringContaining(Contact.definition.mappings.postcode.field),
43
- expect.stringContaining(Contact.definition.mappings.birthDate.field)
44
- )
45
- })
46
-
47
44
  it.each([
48
45
  ['AB12 3CD', '03/12/1990'],
49
46
  ['EF45 6GH', '05/06/1987'],
@@ -54,7 +51,7 @@ describe('Contact Queries', () => {
54
51
  expect(result._retrieveRequest).toEqual({
55
52
  collection: 'contacts',
56
53
  expand: [],
57
- filter: `mock_postcode eq '${postcode}' and mock_birthdate eq ${birthDate} and statecode eq 0`,
54
+ filter: `mock_postcode eq '${postcode}' and mock_birthdate eq ${birthDate} and ${Contact.definition.defaultFilter}`,
58
55
  select: expect.any(Array)
59
56
  })
60
57
  })
@@ -62,12 +59,13 @@ describe('Contact Queries', () => {
62
59
 
63
60
  describe('contactAndPermissionForLicensee', () => {
64
61
  beforeEach(() => {
65
- jest.resetAllMocks()
62
+ const defaultFilter = 'statecode eq 77'
66
63
 
67
64
  jest.spyOn(Contact.definition, 'mappings', 'get').mockReturnValue({
68
65
  id: { field: 'contactid' },
69
66
  postcode: { field: 'defra_postcode' }
70
67
  })
68
+ jest.spyOn(Permission.definition, 'defaultFilter', 'get').mockReturnValue(defaultFilter)
71
69
  })
72
70
 
73
71
  it('should return a predefined query', () => {
@@ -84,7 +82,7 @@ describe('Contact Queries', () => {
84
82
  const result = contactAndPermissionForLicensee('ABC123', 'AB12 3CD')
85
83
 
86
84
  expect(result._retrieveRequest.filter).toEqual(
87
- "endswith(defra_name, 'ABC123') and statecode eq 0 and defra_ContactId/defra_postcode eq 'AB12 3CD'"
85
+ `endswith(defra_name, 'ABC123') and ${Permission.definition.defaultFilter} and defra_ContactId/defra_postcode eq 'AB12 3CD'`
88
86
  )
89
87
  })
90
88
 
@@ -116,7 +114,7 @@ describe('Contact Queries', () => {
116
114
 
117
115
  expect(result._retrieveRequest).toEqual({
118
116
  collection: 'defra_permissions',
119
- filter: `endswith(defra_name, '${permissionLast6}') and statecode eq 0 and defra_ContactId/defra_postcode eq '${postcode}'`,
117
+ filter: `endswith(defra_name, '${permissionLast6}') and ${Permission.definition.defaultFilter} and defra_ContactId/defra_postcode eq '${postcode}'`,
120
118
  orderBy: ['defra_issuedate desc', 'defra_ContactId/contactid asc'],
121
119
  expand: [
122
120
  {
@@ -141,3 +139,84 @@ describe('Contact Queries', () => {
141
139
  )
142
140
  })
143
141
  })
142
+
143
+ describe('contactForLicenseeByPersonalDetails', () => {
144
+ beforeEach(() => {
145
+ const defaultFilter = 'statecode eq 77'
146
+
147
+ jest.spyOn(Contact.definition, 'mappings', 'get').mockReturnValue({
148
+ firstName: { field: 'mock_firstname' },
149
+ lastName: { field: 'mock_lastname' },
150
+ premises: { field: 'mock_premises' },
151
+ postcode: { field: 'mock_postcode' },
152
+ birthDate: { field: 'mock_birthdate' }
153
+ })
154
+
155
+ jest.spyOn(Contact.definition, 'defaultFilter', 'get').mockReturnValue(defaultFilter)
156
+ })
157
+
158
+ it.each([
159
+ { details: { licenseeFirstName: 'Brenin' }, testfield: 'first name', fieldName: 'licenseeFirstName' },
160
+ { details: { licenseeLastName: 'Pysgotwr' }, testfield: 'last name', fieldName: 'licenseeLastName' },
161
+ { details: { licenseePremises: 'Dunmanifestin' }, testfield: 'premises', fieldName: 'licenseePremises' },
162
+ { details: { licenseePostcode: 'GZ1 1UH' }, testfield: 'post code', fieldName: 'licenseePostcode' }
163
+ ])('escapes value of $testfield', ({ testfield: _t, details, fieldName }) => {
164
+ for (let x = 0; x < 4; x++) {
165
+ escapeODataStringValue.mockImplementationOnce(value => `!@£${value}$%%`)
166
+ }
167
+ const query = contactForLicenseeByPersonalDetails({
168
+ licenseeFirstName: 'firstName',
169
+ licenseeLastName: 'lastName',
170
+ licenseePremises: '15',
171
+ licenseePostcode: 'AB1 1AB',
172
+ licenseeBirthDate: '2000-01-01',
173
+ ...details
174
+ })
175
+
176
+ expect(query._retrieveRequest.filter).toEqual(expect.stringContaining(`!@£${details[fieldName]}$%%`))
177
+ })
178
+
179
+ it.each([
180
+ ['Smeagol', 'Ring', '2000-10-03', '12', 'AB12 3CD'],
181
+ ['Frodo', 'Baggins', '1993-09-22', 'Dunmanifestin', 'EF45 6GH'],
182
+ ['Samwise', 'Gamgee', '1998-03-06', '1507', 'IJ78 9KL']
183
+ ])(
184
+ 'should return correct retrieve request when first name is %s, last name is %s, birth date is %s, premises is %s and postcode is %s',
185
+ (licenseeFirstName, licenseeLastName, licenseeBirthDate, licenseePremises, licenseePostcode) => {
186
+ const result = contactForLicenseeByPersonalDetails({
187
+ licenseeFirstName,
188
+ licenseeLastName,
189
+ licenseeBirthDate,
190
+ licenseePremises,
191
+ licenseePostcode
192
+ })
193
+
194
+ expect(result._retrieveRequest).toEqual({
195
+ collection: 'contacts',
196
+ expand: [],
197
+ filter: `mock_firstname eq '${licenseeFirstName}' and mock_lastname eq '${licenseeLastName}' and mock_premises eq '${licenseePremises}' and mock_postcode eq '${licenseePostcode}' and mock_birthdate eq ${licenseeBirthDate} and ${Contact.definition.defaultFilter}`,
198
+ select: expect.any(Array)
199
+ })
200
+ }
201
+ )
202
+
203
+ it('should return a predefined query', () => {
204
+ const result = contactForLicenseeByPersonalDetails({
205
+ licenseeFirstName: 'Gandalf',
206
+ licenseeLastName: 'Grey',
207
+ licenseeBirthDate: '1960-10-03',
208
+ licenseePostcode: 'AB12 3CD'
209
+ })
210
+ expect(result).toBeInstanceOf(PredefinedQuery)
211
+ })
212
+
213
+ it('root should return Contact', () => {
214
+ const result = contactForLicenseeByPersonalDetails({
215
+ licenseeFirstName: 'Aragorn',
216
+ licenseeLastName: 'Elessar',
217
+ licenseeBirthDate: '1989-04-20',
218
+ licenseePostcode: 'AB12 3CD'
219
+ })
220
+ expect(result._root).toEqual(Contact)
221
+ })
222
+ })
@@ -48,3 +48,33 @@ export const contactAndPermissionForLicensee = (permissionLast6Characters, licen
48
48
 
49
49
  return query
50
50
  }
51
+
52
+ /**
53
+ * Gets the query to find a contact using their name, postcode and date of birth
54
+ *
55
+ * @param {string} licenseeFirstName
56
+ * @param {string} licenseeLastName
57
+ * @param {string} licenseeBirthDate
58
+ * @param {string} licenseePremises
59
+ * @param {string} licenseePostcode
60
+ * @returns {PredefinedQuery<Contact>}
61
+ */
62
+ export const contactForLicenseeByPersonalDetails = ({
63
+ licenseeFirstName,
64
+ licenseeLastName,
65
+ licenseeBirthDate,
66
+ licenseePremises,
67
+ licenseePostcode
68
+ }) => {
69
+ const { firstName, lastName, premises, postcode, birthDate } = Contact.definition.mappings
70
+ const filter = `${firstName.field} eq '${escapeODataStringValue(licenseeFirstName)}' and ${lastName.field} eq '${escapeODataStringValue(
71
+ licenseeLastName
72
+ )}' and ${premises.field} eq '${escapeODataStringValue(licenseePremises)}' and ${postcode.field} eq '${escapeODataStringValue(
73
+ licenseePostcode
74
+ )}' and ${birthDate.field} eq ${licenseeBirthDate} and ${Contact.definition.defaultFilter}`
75
+ return new PredefinedQuery({
76
+ root: Contact,
77
+ filter,
78
+ expand: []
79
+ })
80
+ }