Skip to content

Commit

Permalink
fix: compare object using stringfy, because of arrays
Browse files Browse the repository at this point in the history
jira: B2BTEAM-1260
  • Loading branch information
Rudge committed Sep 19, 2023
1 parent 447154a commit cc4bb22
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
52 changes: 52 additions & 0 deletions node/utils/metrics/organization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,58 @@ describe('given an organization to update data', () => {
kind: 'update-organization-graphql-event',
}

expect(sendMetric).toHaveBeenCalledWith(metricParam)
})
})
describe('when just the name, status and tradeName', () => {
const logger = jest.fn() as unknown as Logger

const currentOrganization: Organization = {
collections: [{ id: '149', name: 'Teste 2 Jay' }],
costCenters: [],
created: '2023-05-26T17:59:51.665Z',
customFields: [],
id: '166d3921-fbef-11ed-83ab-16759f4a0add',
name: 'Antes',
paymentTerms: [],
priceTables: [],
salesChannel: '1',
sellers: [],
status: 'inactive',
tradeName: 'Antes',
}

const fieldsUpdated = {
collections: [{ id: '149', name: 'Teste 2 Jay' }],
customFields: [],
name: 'Depois',
paymentTerms: [],
priceTables: [],
salesChannel: '1',
sellers: [],
status: 'active',
tradeName: 'Depois',
}

beforeEach(async () => {
await sendUpdateOrganizationMetric(
logger,
fieldsUpdated,
currentOrganization
)
})

it('should metric just the properties changed', () => {
const metricParam = {
description: 'Update Organization Action - Graphql',
fields: {
update_details: {
properties: ['name', 'status', 'tradeName'],
},
},
kind: 'update-organization-graphql-event',
}

expect(sendMetric).toHaveBeenCalledWith(metricParam)
})
})
Expand Down
7 changes: 6 additions & 1 deletion node/utils/metrics/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ const getFieldsNamesByFieldsUpdated = (
if (Object.prototype.hasOwnProperty.call(updatedProperties, key)) {
const value = updatedProperties[key as keyof typeof updatedProperties]

if (value !== currentOrganizationData[key as keyof Organization]) {
// I tried to compare the objects value !== currentOrganizationData[key as keyof Organization,
// but it was not working, so I use JSON.stringify
if (
JSON.stringify(value) !==
JSON.stringify(currentOrganizationData[key as keyof Organization])
) {
updatedPropName.push(key)
}
}
Expand Down

0 comments on commit cc4bb22

Please sign in to comment.