Skip to content

Commit

Permalink
Amend so that we expand contact and active permission in a single fetc
Browse files Browse the repository at this point in the history
h, plus filter just on date when retrieving recurring payments
  • Loading branch information
jaucourt committed Nov 17, 2023
1 parent 5e63bd1 commit 1f9533d
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ describe('recurring payment entity', () => {
statecode: 1,
_defra_activepermission_value: 'a5b24adf-2e83-ea11-a811-000d3a649213',
_defra_contact_value: 'b3d33cln-2e83-ea11-a811-000d3a649213',
'defra_contact@odata.bind': `$${contact.uniqueContentId}`,
'defra_permission@odata.bind': `$${permission.uniqueContentId}`
'defra_Contact@odata.bind': `$${contact.uniqueContentId}`,
'defra_ActivePermission@odata.bind': `$${permission.uniqueContentId}`
})
)
})
Expand Down Expand Up @@ -145,8 +145,8 @@ describe('recurring payment entity', () => {
statecode: 1,
_defra_activepermission_value: 'a5b24adf-2e83-ea11-a811-000d3a649213',
_defra_contact_value: 'b3d33cln-2e83-ea11-a811-000d3a649213',
'defra_contact@odata.bind': `$${contact.uniqueContentId}`,
'defra_permission@odata.bind': `$${permission.uniqueContentId}`
'defra_Contact@odata.bind': `$${contact.uniqueContentId}`,
'defra_ActivePermission@odata.bind': `$${permission.uniqueContentId}`
})
)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export class RecurringPayment extends BaseEntity {
publicId: { field: 'defra_publicid', type: 'string' }
},
relationships: {
contact: { property: 'defra_contact', entity: Contact, parent: true },
activePermission: { property: 'defra_permission', entity: Permission, parent: true }
contact: { property: 'defra_Contact', entity: Contact, parent: true },
activePermission: { property: 'defra_ActivePermission', entity: Permission, parent: true }
}
}))

Expand Down
27 changes: 4 additions & 23 deletions packages/dynamics-lib/src/queries/recurring-payment.queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,13 @@ import { PredefinedQuery } from './predefined-query.js'
* @returns {PredefinedQuery}
*/
export const findDueRecurringPayments = date => {
// const { contact, permission } = RecurringPayment.definition.relationships
let filter = ''
const dueDates = []
for (let i = 0; i <= 10; i += 2) {
const dateToCheck = new Date(date)
dateToCheck.setDate(dateToCheck.getDate() - i)
dueDates.push(dateToCheck)
}
const { contact, activePermission } = RecurringPayment.definition.relationships

for (const dueDate of dueDates) {
const startOfDay = new Date(dueDate)
startOfDay.setHours(0, 0, 0)

const endOfDay = new Date(dueDate)
endOfDay.setHours(23, 59, 59)

filter += `${RecurringPayment.definition.mappings.cancelledDate.field} eq null and ${RecurringPayment.definition.defaultFilter} and ${
RecurringPayment.definition.mappings.nextDueDate.field
} ge ${startOfDay.toISOString()} and ${RecurringPayment.definition.mappings.nextDueDate.field} le ${endOfDay.toISOString()} or `
}

filter = filter.replace(/\sor\s$/, '')
const filter = `Microsoft.Dynamics.CRM.On(PropertyName='defra_nextduedate', PropertyValue='${date}')`

return new PredefinedQuery({
root: RecurringPayment,
filter: filter
// expand: [contact, permission]
filter: filter,
expand: [contact, activePermission]
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,6 @@ describe('recurring payments service', () => {
expect(result).toEqual([expected])
})

it('findById is called with Contact and contactId', async () => {
const mockRecurringPayments = [getMockRecurringPayment()]
dynamicsLib.executeQuery.mockResolvedValueOnce(mockRecurringPayments)
const contactId = mockRecurringPayments[0].entity.contactId

await getRecurringPayments(new Date())

expect(dynamicsLib.findById).toHaveBeenNthCalledWith(1, Contact, contactId)
})

it('findById is called with Permission and activePermission', async () => {
const mockRecurringPayments = [getMockRecurringPayment()]
dynamicsLib.executeQuery.mockResolvedValueOnce(mockRecurringPayments)
const activePermission = mockRecurringPayments[0].entity.activePermission

await getRecurringPayments(new Date())

expect(dynamicsLib.findById).toHaveBeenNthCalledWith(2, Permission, activePermission)
})

it('executeQuery is called with findDueRecurringPayments with a date', async () => {
const mockRecurringPayments = [getMockRecurringPayment()]
const mockDate = new Date()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import { Contact, executeQuery, findById, findDueRecurringPayments, Permission, RecurringPayment } from '@defra-fish/dynamics-lib'

export const getRecurringPayments = async date => {
const recurringPayments = []
const dueRecurringPayments = await executeQuery(findDueRecurringPayments(date))
// REMOVE
for (const rp of dueRecurringPayments) {
rp.entity.contactId = await findById(Contact, rp.entity.contactId)
rp.entity.activePermission = await findById(Permission, rp.entity.activePermission)
recurringPayments.push(rp.entity)
}
return recurringPayments
return executeQuery(findDueRecurringPayments(date))
}

/**
Expand All @@ -18,10 +10,8 @@ export const getRecurringPayments = async date => {
* @returns {Promise<{recurringPayment: null, contact: null}>}
*/
export const processRecurringPayment = async (transactionRecord, contact) => {
let recurringPayment = null
let permission = null
if (transactionRecord.payment?.recurring) {
recurringPayment = new RecurringPayment()
const recurringPayment = new RecurringPayment()
recurringPayment.name = transactionRecord.payment.recurring.name
recurringPayment.nextDueDate = transactionRecord.payment.recurring.nextDueDate
recurringPayment.cancelledDate = transactionRecord.payment.recurring.cancelledDate
Expand All @@ -30,9 +20,10 @@ export const processRecurringPayment = async (transactionRecord, contact) => {
recurringPayment.agreementId = transactionRecord.payment.recurring.agreementId
recurringPayment.publicId = transactionRecord.payment.recurring.publicId
recurringPayment.status = transactionRecord.payment.recurring.status
permission = transactionRecord.permissions[0]
const [permission] = transactionRecord.permissions
recurringPayment.bindToEntity(RecurringPayment.definition.relationships.activePermission, permission)
recurringPayment.bindToEntity(RecurringPayment.definition.relationships.contact, contact)
return { recurringPayment }
}
return { recurringPayment }
return { recurringPayment: null }
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jest.mock('../../contacts.service.js', () => ({
}))

jest.mock('../../recurring-payments.service.js', () => ({
...jest.requireActual('../../recurring-payments.service.js')
processRecurringPayment: () => ({})
}))

jest.mock('@defra-fish/business-rules-lib', () => ({
Expand Down

0 comments on commit 1f9533d

Please sign in to comment.