Skip to content

Commit

Permalink
Merge pull request #4 from vtex-apps/feature/PERMISSION-8_add-roles-c…
Browse files Browse the repository at this point in the history
…heck-query

PERMISSION-8 Adding new query hasUsers
  • Loading branch information
wender authored Aug 2, 2021
2 parents 24c1784 + 8b3f791 commit ce42a47
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added
- new Graphql query `hasUsers`
## [1.0.1] - 2021-08-02

### Fixed
Expand Down
24 changes: 24 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ Once your app is installed, this information will be automatically loaded on the

Now that your app is integrated, and you have associated your test user to a Role containing your app's permission, you can write a GraphQL query on your app to check the current user's permission within the context of your app, it's not necessary to declare your app name nor user credentials, the query will take care of these details


### Graphql queries

`checkUserPermission`

```graphql
query permissions {
checkUserPermission @context(provider: "vtex.storefront-permissions") {
Expand Down Expand Up @@ -110,3 +115,22 @@ The response will be a simple list with all the permissions the current user has
}
}
```


`hasUsers`

```graphql
query HasUsers {
hasUsers(slug: "sales-admin") @context(provider: "vtex.storefront-permissions")
}
```

The response will be a boolean

```JSON
{
"data": {
"hasUsers": true
}
}
```
1 change: 1 addition & 0 deletions graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ type Query {
getAppSettings: SettingsResponse @cacheControl(scope: PRIVATE)
# Roles
getRole(id: ID!): Role @cacheControl(scope: PRIVATE, maxAge: SHORT)
hasUsers(slug: String!): Boolean @cacheControl(scope: PRIVATE, maxAge: SHORT)
listRoles: [Role]
@cacheControl(scope: PRIVATE, maxAge: SHORT)
@settings(settingsType: "workspace")
Expand Down
13 changes: 13 additions & 0 deletions node/resolvers/Queries/Roles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { currentSchema } from '../../utils'
import { syncRoles } from '../Mutations/Roles'
import { getUserByRole } from './Users'

const config: any = currentSchema('b2b_roles')

Expand Down Expand Up @@ -50,6 +51,18 @@ export const searchRoles = async (_: any, params: any, ctx: Context) => {
return roles
}

export const hasUsers = async (_: any, params: any, ctx: Context) => {
const roles: any = await searchRoles(_, { query: `slug=${params.slug}` }, ctx)

if (roles.length) {
const usersByRole: any = await getUserByRole(_, { id: roles[0].id }, ctx)

return usersByRole.length > 0
}

return false
}

export const listRoles = async (_: any, __: any, ctx: Context) => {
try {
await syncRoles(ctx)
Expand Down
3 changes: 2 additions & 1 deletion node/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import schemas from '../mdSchema'
import { toHash } from '../utils'
import { deleteRole, saveRole } from './Mutations/Roles'
import { getRole, listRoles } from './Queries/Roles'
import { getRole, listRoles, hasUsers } from './Queries/Roles'
import { deleteUser, saveUser } from './Mutations/Users'
import { getFeaturesByModule, listFeatures } from './Queries/Features'
import {
Expand Down Expand Up @@ -45,6 +45,7 @@ export const resolvers = {
Query: {
getRole,
listRoles,
hasUsers,
getFeaturesByModule,
listFeatures,
getUser,
Expand Down

0 comments on commit ce42a47

Please sign in to comment.