Skip to content

Commit

Permalink
update entity tests, add comments where changes are needed
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottDormand96 committed Nov 15, 2023
1 parent 9ee6de1 commit 5e63bd1
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,153 +6,149 @@ describe('recurring payment entity', () => {
optionSetData = await retrieveGlobalOptionSets().cached()
})

it('maps from dynamics', async () => {
const recurringPayment = RecurringPayment.fromResponse(
{
'@odata.etag': 'W/"11528905"',
defra_recurringpaymentid: 'b5b24adf-2e83-ea11-a811-000d3a649213',
defra_name: '18569ba8-094e-4e8c-9911-bfedd5ccc17a',
defra_nextduedate: '2019-12-14T00:00:00Z',
defra_cancelleddate: null,
defra_cancelledreason: null,
defra_enddate: '2019-12-15T00:00:00Z',
defra_agreementid: 'c9267c6e-573d-488b-99ab-ea18431fc472',
defra_publicid: '649-213',
statecode: 1,
_defra_contact_value: 'b3d33cln-2e83-ea11-a811-000d3a649213',
_defra_activepermission_value: 'a5b24adf-2e83-ea11-a811-000d3a649213'
},
optionSetData
)

const expectedFields = {
id: 'b5b24adf-2e83-ea11-a811-000d3a649213',
name: '18569ba8-094e-4e8c-9911-bfedd5ccc17a',
nextDueDate: '2019-12-14T00:00:00Z',
cancelledDate: null,
cancelledReason: null,
endDate: '2019-12-15T00:00:00Z',
agreementId: 'c9267c6e-573d-488b-99ab-ea18431fc472',
publicId: '649-213',
status: 1,
activePermission: 'a5b24adf-2e83-ea11-a811-000d3a649213',
contactId: 'b3d33cln-2e83-ea11-a811-000d3a649213'
}

expect(recurringPayment).toBeInstanceOf(RecurringPayment)
expect(recurringPayment).toMatchObject(expect.objectContaining({ etag: 'W/"11528905"', ...expectedFields }))
expect(recurringPayment.toJSON()).toMatchObject(expect.objectContaining(expectedFields))
expect(JSON.parse(recurringPayment.toString())).toMatchObject(expect.objectContaining(expectedFields))
})

it('maps to dynamics', async () => {
const contact = new Contact()
const permission = new Permission()
const createRecurringPayment = (contact, permission, cancelledDate, cancelledReason) => {
const recurringPayment = new RecurringPayment()

recurringPayment.name = 'Test Name'
recurringPayment.nextDueDate = '2019-12-14T00:00:00Z'
recurringPayment.cancelledDate = null
recurringPayment.cancelledReason = null
recurringPayment.cancelledDate = cancelledDate
recurringPayment.cancelledReason = cancelledReason
recurringPayment.endDate = '2019-12-15T00:00:00Z'
recurringPayment.agreementId = 'c9267c6e-573d-488b-99ab-ea18431fc472'
recurringPayment.publicId = '649-213'
recurringPayment.status = 1
recurringPayment.contactId = 'b3d33cln-2e83-ea11-a811-000d3a649213'
recurringPayment.activePermission = 'a5b24adf-2e83-ea11-a811-000d3a649213'

recurringPayment.bindToEntity(RecurringPayment.definition.relationships.contact, contact)
recurringPayment.bindToEntity(RecurringPayment.definition.relationships.activePermission, permission)

const dynamicsEntity = recurringPayment.toRequestBody()
expect(dynamicsEntity).toMatchObject(
expect.objectContaining({
defra_name: 'Test Name',
defra_nextduedate: '2019-12-14T00:00:00Z',
defra_cancelleddate: null,
defra_cancelledreason: null,
defra_enddate: '2019-12-15T00:00:00Z',
defra_agreementid: 'c9267c6e-573d-488b-99ab-ea18431fc472',
defra_publicid: '649-213',
statecode: 1,
_defra_activepermission_value: 'a5b24adf-2e83-ea11-a811-000d3a649213',
_defra_contact_value: 'b3d33cln-2e83-ea11-a811-000d3a649213',
'[email protected]': `$${contact.uniqueContentId}`,
'[email protected]': `$${permission.uniqueContentId}`
})
)
})

it('maps from dynamics with cancelled', async () => {
const recurringPayment = RecurringPayment.fromResponse(
{
'@odata.etag': 'W/"11528905"',
defra_recurringpaymentid: 'b5b24adf-2e83-ea11-a811-000d3a649213',
defra_name: '18569ba8-094e-4e8c-9911-bfedd5ccc17a',
defra_nextduedate: '2019-12-14T00:00:00Z',
defra_cancelleddate: '2019-12-14T00:00:00Z',
defra_cancelledreason: 910400195,
defra_enddate: '2019-12-15T00:00:00Z',
defra_agreementid: 'c9267c6e-573d-488b-99ab-ea18431fc472',
defra_publicid: '649-213',
statecode: 1,
_defra_contact_value: 'b3d33cln-2e83-ea11-a811-000d3a649213',
_defra_activepermission_value: 'a5b24adf-2e83-ea11-a811-000d3a649213'
},
optionSetData
)
return recurringPayment
}

const expectedFields = {
id: 'b5b24adf-2e83-ea11-a811-000d3a649213',
name: '18569ba8-094e-4e8c-9911-bfedd5ccc17a',
nextDueDate: '2019-12-14T00:00:00Z',
cancelledDate: '2019-12-14T00:00:00Z',
cancelledReason: expect.objectContaining({ id: 910400195, label: 'User cancelled', description: 'User cancelled' }),
endDate: '2019-12-15T00:00:00Z',
agreementId: 'c9267c6e-573d-488b-99ab-ea18431fc472',
publicId: '649-213',
status: 1,
activePermission: 'a5b24adf-2e83-ea11-a811-000d3a649213',
contactId: 'b3d33cln-2e83-ea11-a811-000d3a649213'
const getRecurringPayment = (overrides = {}) => {
const defaultResponse = {
'@odata.etag': 'W/"11528905"',
defra_recurringpaymentid: 'b5b24adf-2e83-ea11-a811-000d3a649213',
defra_name: '18569ba8-094e-4e8c-9911-bfedd5ccc17a',
defra_nextduedate: '2019-12-14T00:00:00Z',
defra_cancelleddate: '2019-12-14T00:00:00Z',
defra_cancelledreason: 910400195,
defra_enddate: '2019-12-15T00:00:00Z',
defra_agreementid: 'c9267c6e-573d-488b-99ab-ea18431fc472',
defra_publicid: '649-213',
statecode: 1,
_defra_contact_value: 'b3d33cln-2e83-ea11-a811-000d3a649213',
_defra_activepermission_value: 'a5b24adf-2e83-ea11-a811-000d3a649213'
}

expect(recurringPayment).toBeInstanceOf(RecurringPayment)
expect(recurringPayment).toMatchObject(expect.objectContaining({ etag: 'W/"11528905"', ...expectedFields }))
expect(recurringPayment.toJSON()).toMatchObject(expect.objectContaining(expectedFields))
expect(JSON.parse(recurringPayment.toString())).toMatchObject(expect.objectContaining(expectedFields))
const response = { ...defaultResponse, ...overrides }

return RecurringPayment.fromResponse(response, optionSetData)
}

describe('cancelled data', () => {
it('is a recurring payment', () => {
const recurringPayment = getRecurringPayment()
expect(recurringPayment).toBeInstanceOf(RecurringPayment)
})

it('matches expectedFields', () => {
const recurringPayment = getRecurringPayment()
expect(recurringPayment).toMatchObject(
expect.objectContaining({
etag: 'W/"11528905"',
id: 'b5b24adf-2e83-ea11-a811-000d3a649213',
name: '18569ba8-094e-4e8c-9911-bfedd5ccc17a',
nextDueDate: '2019-12-14T00:00:00Z',
cancelledDate: '2019-12-14T00:00:00Z',
cancelledReason: expect.objectContaining({ id: 910400195, label: 'User cancelled', description: 'User cancelled' }),
endDate: '2019-12-15T00:00:00Z',
agreementId: 'c9267c6e-573d-488b-99ab-ea18431fc472',
publicId: '649-213',
status: 1,
activePermission: 'a5b24adf-2e83-ea11-a811-000d3a649213',
contactId: 'b3d33cln-2e83-ea11-a811-000d3a649213'
})
)
})

it('maps to dynamics', async () => {
const contact = new Contact()
const permission = new Permission()
const recurringPayment = createRecurringPayment(
contact,
permission,
'2019-10-14T00:00:00Z',
optionSetData.defra_cancelledreason.options['910400195']
)
const dynamicsEntity = recurringPayment.toRequestBody()
expect(dynamicsEntity).toMatchObject(
expect.objectContaining({
defra_name: 'Test Name',
defra_nextduedate: '2019-12-14T00:00:00Z',
defra_cancelleddate: '2019-10-14T00:00:00Z',
defra_cancelledreason: 910400195,
defra_enddate: '2019-12-15T00:00:00Z',
defra_agreementid: 'c9267c6e-573d-488b-99ab-ea18431fc472',
defra_publicid: '649-213',
statecode: 1,
_defra_activepermission_value: 'a5b24adf-2e83-ea11-a811-000d3a649213',
_defra_contact_value: 'b3d33cln-2e83-ea11-a811-000d3a649213',
'[email protected]': `$${contact.uniqueContentId}`,
'[email protected]': `$${permission.uniqueContentId}`
})
)
})
})

it('maps to dynamics with cancelled', async () => {
const contact = new Contact()
const permission = new Permission()
const recurringPayment = new RecurringPayment()
recurringPayment.name = 'Test Name'
recurringPayment.nextDueDate = '2019-12-14T00:00:00Z'
recurringPayment.cancelledDate = '2019-12-14T00:00:00Z'
recurringPayment.cancelledReason = optionSetData.defra_cancelledreason.options['910400195']
recurringPayment.endDate = '2019-12-15T00:00:00Z'
recurringPayment.agreementId = 'c9267c6e-573d-488b-99ab-ea18431fc472'
recurringPayment.publicId = '649-213'
recurringPayment.status = 1
recurringPayment.contactId = 'b3d33cln-2e83-ea11-a811-000d3a649213'
recurringPayment.activePermission = 'a5b24adf-2e83-ea11-a811-000d3a649213'
recurringPayment.bindToEntity(RecurringPayment.definition.relationships.contact, contact)
recurringPayment.bindToEntity(RecurringPayment.definition.relationships.activePermission, permission)
describe('no cancelled data', () => {
it('is a recurring payment', () => {
const recurringPayment = getRecurringPayment({ defra_cancelleddate: null, defra_cancelledreason: null })
expect(recurringPayment).toBeInstanceOf(RecurringPayment)
})

it('matches expectedFields', () => {
const recurringPayment = getRecurringPayment({ defra_cancelleddate: null, defra_cancelledreason: null })
expect(recurringPayment).toMatchObject(
expect.objectContaining({
etag: 'W/"11528905"',
id: 'b5b24adf-2e83-ea11-a811-000d3a649213',
name: '18569ba8-094e-4e8c-9911-bfedd5ccc17a',
nextDueDate: '2019-12-14T00:00:00Z',
cancelledDate: null,
cancelledReason: null,
endDate: '2019-12-15T00:00:00Z',
agreementId: 'c9267c6e-573d-488b-99ab-ea18431fc472',
publicId: '649-213',
status: 1,
activePermission: 'a5b24adf-2e83-ea11-a811-000d3a649213',
contactId: 'b3d33cln-2e83-ea11-a811-000d3a649213'
})
)
})

const dynamicsEntity = recurringPayment.toRequestBody()
expect(dynamicsEntity).toMatchObject(
expect.objectContaining({
defra_name: 'Test Name',
defra_nextduedate: '2019-12-14T00:00:00Z',
defra_cancelleddate: '2019-12-14T00:00:00Z',
defra_cancelledreason: 910400195,
defra_enddate: '2019-12-15T00:00:00Z',
defra_agreementid: 'c9267c6e-573d-488b-99ab-ea18431fc472',
defra_publicid: '649-213',
statecode: 1,
_defra_activepermission_value: 'a5b24adf-2e83-ea11-a811-000d3a649213',
_defra_contact_value: 'b3d33cln-2e83-ea11-a811-000d3a649213',
'[email protected]': `$${contact.uniqueContentId}`,
'[email protected]': `$${permission.uniqueContentId}`
})
)
it('maps to dynamics', async () => {
const contact = new Contact()
const permission = new Permission()
const recurringPayment = createRecurringPayment(contact, permission, null, null)
const dynamicsEntity = recurringPayment.toRequestBody()
expect(dynamicsEntity).toMatchObject(
expect.objectContaining({
defra_name: 'Test Name',
defra_nextduedate: '2019-12-14T00:00:00Z',
defra_cancelleddate: null,
defra_cancelledreason: null,
defra_enddate: '2019-12-15T00:00:00Z',
defra_agreementid: 'c9267c6e-573d-488b-99ab-ea18431fc472',
defra_publicid: '649-213',
statecode: 1,
_defra_activepermission_value: 'a5b24adf-2e83-ea11-a811-000d3a649213',
_defra_contact_value: 'b3d33cln-2e83-ea11-a811-000d3a649213',
'[email protected]': `$${contact.uniqueContentId}`,
'[email protected]': `$${permission.uniqueContentId}`
})
)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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) {
Expand All @@ -33,5 +34,6 @@ export const findDueRecurringPayments = date => {
return new PredefinedQuery({
root: RecurringPayment,
filter: filter
// expand: [contact, permission]
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Contact, executeQuery, findById, findDueRecurringPayments, Permission,
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)
Expand Down

0 comments on commit 5e63bd1

Please sign in to comment.