diff --git a/CHANGELOG.md b/CHANGELOG.md index df83628..50d1293 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/graphql/schema.graphql b/graphql/schema.graphql index 9b35eb0..b1b5382 100644 --- a/graphql/schema.graphql +++ b/graphql/schema.graphql @@ -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 diff --git a/node/constants.ts b/node/constants.ts index c3a3844..4513036 100644 --- a/node/constants.ts +++ b/node/constants.ts @@ -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 = [ diff --git a/node/resolvers/queries/index.ts b/node/resolvers/queries/index.ts index c2764fa..9e24369 100644 --- a/node/resolvers/queries/index.ts +++ b/node/resolvers/queries/index.ts @@ -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, @@ -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 },