Skip to content

Commit

Permalink
Merge pull request #127 from vtex-apps/feature/B2BTEAM-1260-metrics
Browse files Browse the repository at this point in the history
feat: add metric on updating organization data
  • Loading branch information
Rudge authored Sep 25, 2023
2 parents 8de6ac2 + c46cb02 commit 2476f58
Show file tree
Hide file tree
Showing 8 changed files with 368 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- Add metrics on updating organization data

## [0.37.0] - 2023-09-19


Expand Down
4 changes: 2 additions & 2 deletions node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "vtex.b2b-organizations",
"version": "0.37.0",
"dependencies": {
"@types/lodash": "4.14.74",
"@vtex/api": "6.45.20",
"atob": "^2.1.2",
"co-body": "^6.0.0",
Expand Down Expand Up @@ -29,8 +30,7 @@
"tslint-config-prettier": "^1.18.0",
"tslint-config-vtex": "^2.1.0",
"typescript": "3.9.7",
"vtex.b2b-organizations-graphql": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.b2b-organizations-graphql",
"vtex.storefront-permissions": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.storefront-permissions"
"vtex.storefront-permissions": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.storefront-permissions"
},
"scripts": {
"lint": "tsc --noEmit && tslint -c tslint.json './**/*.ts'",
Expand Down
37 changes: 24 additions & 13 deletions node/resolvers/Mutations/Organizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import B2BSettings from '../Queries/Settings'
import CostCenters from './CostCenters'
import MarketingTags from './MarketingTags'
import checkConfig from '../config'
import { sendUpdateOrganizationMetric } from '../../utils/metrics/organization'

const createUserAndAttachToOrganization = async ({
storefrontPermissions,
Expand Down Expand Up @@ -372,15 +373,17 @@ const Organizations = {
ctx
)) as B2BSettingsInput

let currentOrganizationData: Organization | undefined

try {
const currentData: Organization = await masterdata.getDocument({
currentOrganizationData = await masterdata.getDocument({
dataEntity: ORGANIZATION_DATA_ENTITY,
fields: ORGANIZATION_FIELDS,
id,
})

if (
currentData.status !== status &&
currentOrganizationData?.status !== status &&
notifyUsers &&
settings?.transactionEmailSettings?.organizationStatusChanged
) {
Expand All @@ -398,22 +401,30 @@ const Organizations = {
}

try {
const fields = {
collections,
...((tradeName || tradeName === '') && { tradeName }),
customFields,
name,
paymentTerms,
priceTables,
...(salesChannel && { salesChannel }),
...(sellers && { sellers }),
status,
}

await masterdata.updatePartialDocument({
dataEntity: ORGANIZATION_DATA_ENTITY,
fields: {
collections,
...((tradeName || tradeName === '') && { tradeName }),
customFields,
name,
paymentTerms,
priceTables,
...(salesChannel && { salesChannel }),
...(sellers && { sellers }),
status,
},
fields,
id,
})

sendUpdateOrganizationMetric(logger, {
account: ctx.vtex.account,
currentOrganizationData,
updatedProperties: fields,
})

return { status: 'success', message: '' }
} catch (error) {
logger.error({
Expand Down
9 changes: 9 additions & 0 deletions node/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,20 @@ interface Organization {
id: string
name: string
tradeName?: string
collections: Collection[]
costCenters: string[]
paymentTerms: PaymentTerm[]
priceTables?: string[]
status: string
created: string
customFields?: CustomField[]
salesChannel?: string
sellers?: Seller[]
}

interface Collection {
id: string
name: string
}

interface CostCenter {
Expand Down
11 changes: 10 additions & 1 deletion node/utils/metrics/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,19 @@ type ImpersonateB2BUserMetric = {
description: 'Impersonate B2B User Action - Graphql'
}

interface UpdateOrganizationMetric {
kind: 'update-organization-graphql-event'
description: 'Update Organization Action - Graphql'
}

export type Metric = {
name: 'b2b-suite-buyerorg-data'
account: string
} & (ImpersonateUserMetric | ImpersonateB2BUserMetric)
} & (
| ImpersonateUserMetric
| ImpersonateB2BUserMetric
| UpdateOrganizationMetric
)

export const sendMetric = async (metric: Metric) => {
try {
Expand Down
223 changes: 223 additions & 0 deletions node/utils/metrics/organization.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
import {
randAirportName,
randAlpha,
randAlphaNumeric,
randCompanyName,
randFirstName,
randFullName,
randLastName,
randPastDate,
randStatus,
randSuperheroName,
randWord,
} from '@ngneat/falso'
import type { Logger } from '@vtex/api/lib/service/logger/logger'

import type { Seller } from '../../clients/sellers'
import { sendMetric } from './metrics'
import type { UpdateOrganizationParams } from './organization'
import { sendUpdateOrganizationMetric } from './organization'

jest.mock('./metrics')
afterEach(() => {
jest.resetAllMocks()
})

describe('given an organization to update data', () => {
describe('when change all properties', () => {
const logger = jest.fn() as unknown as Logger

const account = randWord()

const currentOrganization: Organization = {
collections: [{ name: randWord() } as Collection],
costCenters: [],
created: randPastDate().toISOString(),
customFields: [{ name: randWord() } as CustomField],
id: randAlphaNumeric().toString(),
name: randCompanyName(),
paymentTerms: [{ name: randAlphaNumeric() } as PaymentTerm],
priceTables: [randAlpha()],
salesChannel: randAlpha(),
sellers: [{ name: randFullName() } as Seller],
status: randAlpha(),
tradeName: randCompanyName(),
}

const fieldsUpdated: Partial<Organization> = {
collections: [{ name: randSuperheroName() } as Collection],
costCenters: [],
customFields: [{ name: randAirportName() } as CustomField],
name: randCompanyName(),
paymentTerms: [{ name: randLastName() } as PaymentTerm],
priceTables: [randFirstName()],
salesChannel: randAirportName(),
sellers: [{ name: randFullName() } as Seller],
status: randStatus(),
tradeName: randCompanyName(),
}

const updateOrganizationParams: UpdateOrganizationParams = {
account,
currentOrganizationData: currentOrganization,
updatedProperties: fieldsUpdated,
}

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

it('should metrify that all properties changed', () => {
const metricParam = {
account,
description: 'Update Organization Action - Graphql',
fields: {
update_details: {
properties: [
'collections',
'customFields',
'name',
'paymentTerms',
'priceTables',
'salesChannel',
'sellers',
'status',
'tradeName',
],
},
},
kind: 'update-organization-graphql-event',
name: 'b2b-suite-buyerorg-data',
}

expect(sendMetric).toHaveBeenCalledWith(metricParam)
})
})

describe('when no change properties data', () => {
const logger = jest.fn() as unknown as Logger

const account = randWord()

const collections = [{ name: randWord() } as Collection]
const customFields = [{ name: randWord() } as CustomField]
const name = randWord()
const paymentTerms = [{ name: randWord() } as PaymentTerm]
const priceTables = [randWord()]
const salesChannel = randWord()
const sellers = [{ name: randFullName() } as Seller]
const status = randWord()
const tradeName = randWord()

const currentOrganization: Organization = {
collections,
costCenters: [],
created: randWord(),
customFields,
id: randWord(),
name,
paymentTerms,
priceTables,
salesChannel,
sellers,
status,
tradeName,
}

const fieldsUpdated = {
collections,
customFields,
name,
paymentTerms,
priceTables,
salesChannel,
sellers,
status,
tradeName,
}

const updateOrganizationParams: UpdateOrganizationParams = {
account,
currentOrganizationData: currentOrganization,
updatedProperties: fieldsUpdated,
}

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

it('should metric no properties changed', () => {
const metricParam = {
account,
description: 'Update Organization Action - Graphql',
fields: {
update_details: {
properties: [],
},
},
kind: 'update-organization-graphql-event',
name: 'b2b-suite-buyerorg-data',
}

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

const account = randWord()

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',
}

const updateOrganizationParams: UpdateOrganizationParams = {
account,
currentOrganizationData: currentOrganization,
updatedProperties: fieldsUpdated,
}

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

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

expect(sendMetric).toHaveBeenCalledWith(metricParam)
})
})
})
Loading

0 comments on commit 2476f58

Please sign in to comment.