-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from vtex-apps/feature/B2BTEAM-1255-buyer-org…
…-users-metrics feat: buyer org users metrics
- Loading branch information
Showing
7 changed files
with
253 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { randEmail, randWord } from '@ngneat/falso' | ||
import type { Logger } from '@vtex/api/lib/service/logger/logger' | ||
|
||
import { sendMetric } from './metrics' | ||
import { | ||
sendAddUserMetric, | ||
sendRemoveUserMetric, | ||
sendUpdateUserMetric, | ||
} from './user' | ||
|
||
jest.mock('./metrics') | ||
afterEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
describe('given an action for a user', () => { | ||
describe('when add user', () => { | ||
const logger = jest.fn() as unknown as Logger | ||
|
||
const account = randWord() | ||
|
||
const userArgs: Partial<UserArgs> = { | ||
email: randEmail(), | ||
id: randWord(), | ||
userId: randWord(), | ||
} | ||
|
||
beforeEach(async () => { | ||
await sendAddUserMetric(logger, account, userArgs) | ||
}) | ||
|
||
it('should metrify the action', () => { | ||
const metricParam = { | ||
account, | ||
description: 'Add User Action - Graphql', | ||
fields: userArgs, | ||
kind: 'add-user-graphql-event', | ||
name: 'b2b-suite-buyerorg-data', | ||
} | ||
|
||
expect(sendMetric).toHaveBeenCalledWith(metricParam) | ||
}) | ||
}) | ||
|
||
describe('when remove user', () => { | ||
const logger = jest.fn() as unknown as Logger | ||
|
||
const account = randWord() | ||
|
||
const userArgs: Partial<UserArgs> = { | ||
email: randEmail(), | ||
id: randWord(), | ||
userId: randWord(), | ||
} | ||
|
||
beforeEach(async () => { | ||
await sendRemoveUserMetric(logger, account, userArgs) | ||
}) | ||
|
||
it('should metrify the action', () => { | ||
const metricParam = { | ||
account, | ||
description: 'Remove User Action - Graphql', | ||
fields: userArgs, | ||
kind: 'remove-user-graphql-event', | ||
name: 'b2b-suite-buyerorg-data', | ||
} | ||
|
||
expect(sendMetric).toHaveBeenCalledWith(metricParam) | ||
}) | ||
}) | ||
|
||
describe('when update user', () => { | ||
const logger = jest.fn() as unknown as Logger | ||
|
||
const account = randWord() | ||
|
||
const userArgs: Partial<UserArgs> = { | ||
email: randEmail(), | ||
id: randWord(), | ||
userId: randWord(), | ||
} | ||
|
||
beforeEach(async () => { | ||
await sendUpdateUserMetric(logger, account, userArgs) | ||
}) | ||
|
||
it('should metrify the action', () => { | ||
const metricParam = { | ||
account, | ||
description: 'Update User Action - Graphql', | ||
fields: userArgs, | ||
kind: 'update-user-graphql-event', | ||
name: 'b2b-suite-buyerorg-data', | ||
} | ||
|
||
expect(sendMetric).toHaveBeenCalledWith(metricParam) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import type { Logger } from '@vtex/api/lib/service/logger/logger' | ||
|
||
import type { Metric } from './metrics' | ||
import { sendMetric } from './metrics' | ||
|
||
interface UserMetricType { | ||
description: string | ||
kind: string | ||
} | ||
|
||
const userMetricType = { | ||
add: { | ||
description: 'Add User Action - Graphql', | ||
kind: 'add-user-graphql-event', | ||
} as UserMetricType, | ||
remove: { | ||
description: 'Remove User Action - Graphql', | ||
kind: 'remove-user-graphql-event', | ||
} as UserMetricType, | ||
update: { | ||
description: 'Update User Action - Graphql', | ||
kind: 'update-user-graphql-event', | ||
} as UserMetricType, | ||
} | ||
|
||
class UserMetric implements Metric { | ||
public readonly description: string | ||
public readonly kind: string | ||
public readonly account: string | ||
public readonly fields: Partial<UserArgs> | ||
public readonly name = 'b2b-suite-buyerorg-data' | ||
|
||
constructor( | ||
account: string, | ||
{ kind, description }: UserMetricType, | ||
fields: Partial<UserArgs> | ||
) { | ||
this.account = account | ||
this.fields = fields | ||
this.kind = kind | ||
this.description = description | ||
} | ||
} | ||
|
||
const sendUserMetric = async (logger: Logger, userMetric: UserMetric) => { | ||
try { | ||
await sendMetric(userMetric) | ||
} catch (error) { | ||
logger.error({ | ||
error, | ||
message: `Error to send metrics from user action ${userMetric.kind}`, | ||
}) | ||
} | ||
} | ||
|
||
export const sendRemoveUserMetric = async ( | ||
logger: Logger, | ||
account: string, | ||
userArgs: Partial<UserArgs> | ||
) => { | ||
await sendUserMetric( | ||
logger, | ||
new UserMetric(account, userMetricType.remove, userArgs) | ||
) | ||
} | ||
|
||
export const sendAddUserMetric = async ( | ||
logger: Logger, | ||
account: string, | ||
userArgs: Partial<UserArgs> | ||
) => { | ||
await sendUserMetric( | ||
logger, | ||
new UserMetric(account, userMetricType.add, userArgs) | ||
) | ||
} | ||
|
||
export const sendUpdateUserMetric = async ( | ||
logger: Logger, | ||
account: string, | ||
userArgs: Partial<UserArgs> | ||
) => { | ||
await sendUserMetric( | ||
logger, | ||
new UserMetric(account, userMetricType.update, userArgs) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
sonar.projectKey=vtex-apps_b2b-organizations-graphql | ||
sonar.organization=vtex-apps | ||
sonar.coverage.exclusions=**/*.test.*, */__mocks__/**.*, */node_modules/**.*, node/index.ts | ||
sonar.exclusions=**/*.test.*, */__mocks__/**.*, */node_modules/**.*, node/index.ts | ||
sonar.language=ts | ||
sonar.javascript.lcov.reportPaths=./node/coverage/lcov.info |