Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: get quote enabled for user query #51

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- Add getQuoteEnabledForUser query to be used by the b2b-quotes app

## [2.5.4] - 2024-08-20

### Fixed
Expand Down
2 changes: 2 additions & 0 deletions graphql/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
type Query {
getAppSettings: AppSettings @cacheControl(scope: PRIVATE, maxAge: SHORT)
getQuoteEnabledForUser(email: String!): Boolean
@cacheControl(scope: PRIVATE, maxAge: SHORT)
getQuote(id: String): Quote
@withPermissions
@withSession
Expand Down
2 changes: 2 additions & 0 deletions node/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export const APP_NAME = 'b2b-quotes-graphql'
export const SCHEMA_VERSION = 'v1.3'
export const QUOTE_DATA_ENTITY = 'quotes'
export const B2B_USER_SCHEMA_VERSION = 'v0.1.2'
export const B2B_USER_DATA_ENTITY = 'b2b_users'
export const CRON_EXPRESSION = '0 */12 * * *'

export const QUOTE_FIELDS = [
Expand Down
51 changes: 51 additions & 0 deletions node/resolvers/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,42 @@ import { checkConfig } from '../utils/checkConfig'
import GraphQLError from '../../utils/GraphQLError'
import {
APP_NAME,
B2B_USER_DATA_ENTITY,
B2B_USER_SCHEMA_VERSION,
QUOTE_DATA_ENTITY,
QUOTE_FIELDS,
SCHEMA_VERSION,
} from '../../constants'

// This function checks if given email is an user part of a buyer org.
export const isUserPartOfBuyerOrg = async (email: string, ctx: Context) => {
const {
clients: { masterdata },
} = ctx

const where = `email=${email}`
const resp = await masterdata.searchDocumentsWithPaginationInfo({
dataEntity: B2B_USER_DATA_ENTITY,
fields: ['id'], // we don't need to fetch all fields, only if there is an entry or not
pagination: {
page: 1,
pageSize: 1, // we only need to know if there is at least one user entry
},
schema: B2B_USER_SCHEMA_VERSION,
...(where ? { where } : {}),
})

const { data } = (resp as unknown) as {
data: any
}

if (data.length > 0) {
return true
}

return false
}

const buildWhereStatement = async ({
permissions,
organization,
Expand Down Expand Up @@ -259,6 +290,26 @@ export const Query = {
throw new GraphQLError(error)
}
},
getQuoteEnabledForUser: async (
_: any,
{ email }: { email: string },
ctx: Context
) => {
const {
vtex: { logger },
} = ctx

try {
// if user is part of a buyer org, quote functionality is enabled
return await isUserPartOfBuyerOrg(email, ctx)
} catch (error) {
logger.error({
error,
message: 'getQuoteEnabledForUser-error',
})
throw new GraphQLError(error)
}
},
getAppSettings: async (_: void, __: void, ctx: Context) => {
const {
clients: { vbase },
Expand Down
Loading