Skip to content

Commit

Permalink
Merge branch 'master' into B2BTEAM-1866-fix-quote-expired-error
Browse files Browse the repository at this point in the history
  • Loading branch information
enzomerca authored Sep 4, 2024
2 parents c1cab65 + 58d4f06 commit f1808cd
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Set viewedByCustomer value corectly on quote creation

## [2.6.0] - 2024-09-04

### 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
6 changes: 2 additions & 4 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"vendor": "vtex",
"name": "b2b-quotes-graphql",
"version": "2.5.4",
"version": "2.6.0",
"title": "B2B Quotes GraphQL",
"description": "Backend for the B2B Quotes & Carts app",
"builders": {
Expand All @@ -14,9 +14,7 @@
"vtex.b2b-organizations-graphql": "0.x",
"vtex.orders-broadcast": "0.x"
},
"registries": [
"smartcheckout"
],
"registries": ["smartcheckout"],
"policies": [
{
"name": "vbase-read-write"
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
2 changes: 1 addition & 1 deletion node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vtex.b2b-quotes-graphql",
"version": "2.5.4",
"version": "2.6.0",
"dependencies": {
"co-body": "^6.0.0",
"graphql": "^14.0.0",
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

0 comments on commit f1808cd

Please sign in to comment.