Skip to content

Commit

Permalink
Merge pull request #151 from vtex-apps/feat/B2BTEAM-1576-RemoveUser
Browse files Browse the repository at this point in the history
feat: Add new removeUserWithEmail graphql API for bulk import use case
  • Loading branch information
pabloppupulin authored Mar 20, 2024
2 parents 181127c + 642d12e commit 8b52446
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 1 deletion.
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 new removeUserWithEmail graphql API for bulk import use case

## [0.48.5] - 2024-02-29

### Fixed
Expand Down
5 changes: 5 additions & 0 deletions graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ type Mutation {
email: String!
canImpersonate: Boolean = false
): MutationResponse @checkAdminAccess @cacheControl(scope: PRIVATE)
removeUserWithEmail(
orgId: ID!
costId: ID!
email: String!
): MutationResponse @checkAdminAccess @cacheControl(scope: PRIVATE)
updateUser(
id: ID
roleId: ID!
Expand Down
13 changes: 13 additions & 0 deletions node/clients/storefrontPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import getOrganizationsByEmail from '../queries/getOrganizationsByEmail'
import getPermission from '../queries/getPermission'
import getRole from '../queries/getRole'
import getUser from '../queries/getUser'
import getUsersByEmail from '../queries/getUsersByEmail'
import listAllUsers from '../queries/listAllUsers'
import listRoles from '../queries/listRoles'
import listUsers from '../queries/listUsers'
Expand Down Expand Up @@ -126,6 +127,18 @@ export default class StorefrontPermissions extends AppGraphQLClient {
})
}

public getUsersByEmail = async (
email: string,
orgId: string,
costId: string
): Promise<any> => {
return this.query({
extensions: this.getPersistedQuery(),
query: getUsersByEmail,
variables: { email, orgId, costId },
})
}

public getB2BUser = async (id: string): Promise<any> => {
return this.query({
extensions: this.getPersistedQuery(),
Expand Down
16 changes: 16 additions & 0 deletions node/queries/getUsersByEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { print } from 'graphql'
import gql from 'graphql-tag'

export default print(gql`
query getUsersByEmail($orgId: ID, $costId: ID, $email: String!) {
getUsersByEmail(orgId: $orgId, costId: $costId, email: $email) {
id
roleId
userId
orgId
costId
name
email
}
}
`)
55 changes: 54 additions & 1 deletion node/resolvers/Mutations/Users.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MessageSFPUserAddError, StatusAddUserError } from '../../constants'
import GraphQLError from '../../utils/GraphQLError'
import GraphQLError, { getErrorMessage } from '../../utils/GraphQLError'
import type { ImpersonateMetricParams } from '../../utils/metrics/impersonate'
import {
sendImpersonateB2BUserMetric,
Expand Down Expand Up @@ -368,6 +368,59 @@ const Users = {
)
},

removeUserWithEmail: async (
_: void,
{ orgId, costId, email }: UserArgs,
ctx: Context
) => {
const {
clients: { events, storefrontPermissions: storefrontPermissionsClient },
vtex: { logger },
} = ctx as any

storefrontPermissionsClient
.getUsersByEmail(email, orgId, costId)
.then((result: any) => {
const user = result.data.getUsersByEmail[0]

if (!user) return

const id = user.id
const userId = user.userId

const fields = {
email,
id,
userId,
}

return storefrontPermissionsClient
.deleteUser(fields)
.then((response: any) => {
events.sendEvent('', 'b2b-organizations-graphql.removeUser', {
id,
email,
})
sendRemoveUserMetric(ctx, logger, ctx.vtex.account, fields)
return response.data.deleteUser
})
.catch((error: any) => {
logger.error({
error,
message: 'removeUser-deleteUserError',
})
return { status: 'error', message: error }
})
})
.catch((error: any) => {
logger.error({
error,
message: 'getUsers-error',
})
throw new GraphQLError(getErrorMessage(error))
})
},

/**
*
* Mutation to remove a user
Expand Down

0 comments on commit 8b52446

Please sign in to comment.