Skip to content

Commit

Permalink
Merge pull request #151 from vtex-apps/feature/ignore-bindings
Browse files Browse the repository at this point in the history
Feature: Ignore bindings
  • Loading branch information
polishq authored Feb 27, 2024
2 parents 7a664fc + 4c29dc2 commit 6103930
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 158 deletions.
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

- Support for ignoring bindings, for the scenario where an account has configured bindings but has not "gone live" with them yet

## [2.15.3] - 2024-02-26

## [2.15.2] - 2024-01-25
Expand Down
6 changes: 6 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@
"description": "Routes that include the string below will be excluded from the sitemap. Useful for selecting which pages will be in that folder.",
"default": "",
"type": "string"
},
"ignoreBindings": {
"title": "Ignore bindings",
"description": "Generate a single sitemap even if store has multiple bindings - for example if bindings are in the process of being set up",
"type": "boolean",
"default": false
}
}
}
Expand Down
54 changes: 43 additions & 11 deletions node/middlewares/generateMiddlewares/generateSitemap.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Events, IOContext, Logger} from '@vtex/api'
import { Events, IOContext, Logger } from '@vtex/api'
import * as TypeMoq from 'typemoq'

import { Clients } from '../../clients'
import { generateSitemap } from './generateSitemap'
import {
GENERATE_APPS_ROUTES_EVENT,
GENERATE_PRODUCT_ROUTES_EVENT,
GENERATE_REWRITER_ROUTES_EVENT
GENERATE_REWRITER_ROUTES_EVENT,
} from './utils'

const eventsTypeMock = TypeMoq.Mock.ofInstance(Events)
Expand All @@ -28,7 +28,7 @@ const DEFAULT_REWRITER_ROUTES_PAYLOAD = {
report: {},
}

const DEFAULT_PRODUCT_ROUTES_PAYLOAD: ProductRoutesGenerationEvent= {
const DEFAULT_PRODUCT_ROUTES_PAYLOAD: ProductRoutesGenerationEvent = {
generationId: '1',
invalidProducts: 0,
page: 1,
Expand All @@ -42,7 +42,12 @@ describe('Test generate sitemap', () => {
super(ioContext.object)
}

public sendEvent = async (_: any,route: string, message?: any, __?: any)=> {
public sendEvent = async (
_: any,
route: string,
message?: any,
__?: any
) => {
eventSent(_, route, message)
}
}
Expand Down Expand Up @@ -70,6 +75,7 @@ describe('Test generate sitemap', () => {
enableAppsRoutes: true,
enableNavigationRoutes: true,
enableProductRoutes: true,
ignoreBindings: false,
},
},
vtex: {
Expand All @@ -81,9 +87,21 @@ describe('Test generate sitemap', () => {

it('Should send both events', async () => {
await generateSitemap(context)
expect(eventSent).toHaveBeenCalledWith('', GENERATE_REWRITER_ROUTES_EVENT, DEFAULT_REWRITER_ROUTES_PAYLOAD)
expect(eventSent).toHaveBeenCalledWith('', GENERATE_PRODUCT_ROUTES_EVENT, DEFAULT_PRODUCT_ROUTES_PAYLOAD)
expect(eventSent).toHaveBeenCalledWith('', GENERATE_APPS_ROUTES_EVENT, DEFAULT_APPS_ROUTES_PAYLOAD)
expect(eventSent).toHaveBeenCalledWith(
'',
GENERATE_REWRITER_ROUTES_EVENT,
DEFAULT_REWRITER_ROUTES_PAYLOAD
)
expect(eventSent).toHaveBeenCalledWith(
'',
GENERATE_PRODUCT_ROUTES_EVENT,
DEFAULT_PRODUCT_ROUTES_PAYLOAD
)
expect(eventSent).toHaveBeenCalledWith(
'',
GENERATE_APPS_ROUTES_EVENT,
DEFAULT_APPS_ROUTES_PAYLOAD
)
})

it('Should send only enabled events', async () => {
Expand All @@ -92,23 +110,37 @@ describe('Test generate sitemap', () => {
enableAppsRoutes: true,
enableNavigationRoutes: true,
enableProductRoutes: false,
ignoreBindings: false,
}

await generateSitemap(context)
expect(eventSent).toHaveBeenCalledWith('', GENERATE_REWRITER_ROUTES_EVENT, DEFAULT_REWRITER_ROUTES_PAYLOAD)
expect(eventSent).toHaveBeenCalledWith('', GENERATE_APPS_ROUTES_EVENT, DEFAULT_APPS_ROUTES_PAYLOAD)
expect(eventSent).toHaveBeenCalledWith(
'',
GENERATE_REWRITER_ROUTES_EVENT,
DEFAULT_REWRITER_ROUTES_PAYLOAD
)
expect(eventSent).toHaveBeenCalledWith(
'',
GENERATE_APPS_ROUTES_EVENT,
DEFAULT_APPS_ROUTES_PAYLOAD
)
expect(eventSent).toHaveBeenCalledTimes(2)

jest.clearAllMocks()
context.state.settings = {
disableRoutesTerm:'',
disableRoutesTerm: '',
enableAppsRoutes: false,
enableNavigationRoutes: false,
enableProductRoutes: true,
ignoreBindings: false,
}

await generateSitemap(context)
expect(eventSent).toHaveBeenCalledWith('', GENERATE_PRODUCT_ROUTES_EVENT, DEFAULT_PRODUCT_ROUTES_PAYLOAD)
expect(eventSent).toHaveBeenCalledWith(
'',
GENERATE_PRODUCT_ROUTES_EVENT,
DEFAULT_PRODUCT_ROUTES_PAYLOAD
)
expect(eventSent).toHaveBeenCalledTimes(1)
})
})
28 changes: 18 additions & 10 deletions node/middlewares/settings.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { appIdToAppAtMajor } from '@vtex/api'
import { APPS_ROUTES_INDEX, PRODUCT_ROUTES_INDEX, REWRITER_ROUTES_INDEX } from './generateMiddlewares/utils'
import {
APPS_ROUTES_INDEX,
PRODUCT_ROUTES_INDEX,
REWRITER_ROUTES_INDEX,
} from './generateMiddlewares/utils'

export interface Settings {
enableAppsRoutes: boolean
enableProductRoutes: boolean
enableNavigationRoutes: boolean
ignoreBindings: boolean
disableRoutesTerm: string
}

Expand All @@ -16,16 +21,21 @@ const DEFAULT_SETTINGS = {
enableAppsRoutes: true,
enableNavigationRoutes: true,
enableProductRoutes: true,
ignoreBindings: false,
}

const INDEX_MAP = {
disableRoutesTerm: '',
enableAppsRoutes: APPS_ROUTES_INDEX,
enableNavigationRoutes: REWRITER_ROUTES_INDEX,
enableProductRoutes: PRODUCT_ROUTES_INDEX,
ignoreBindings: '',
}

export async function settings(ctx: Context | EventContext, next: () => Promise<void>) {
export async function settings(
ctx: Context | EventContext,
next: () => Promise<void>
) {
const {
clients: { apps },
} = ctx
Expand All @@ -35,20 +45,18 @@ export async function settings(ctx: Context | EventContext, next: () => Promise<
...(await apps.getAppSettings(VTEX_APP_AT_MAJOR)),
}
const keys = Object.keys(appSettings) as Array<keyof Settings>
const enabledIndexFiles = keys.reduce(
(acc, key ) => {
if (appSettings[key] && INDEX_MAP[key]) {
acc.push(INDEX_MAP[key])
}
return acc
}, [] as string[])
const enabledIndexFiles = keys.reduce((acc, key) => {
if (appSettings[key] && INDEX_MAP[key]) {
acc.push(INDEX_MAP[key])
}
return acc
}, [] as string[])

ctx.state = {
...ctx.state,
enabledIndexFiles,
settings: appSettings,
}


await next()
}
Loading

0 comments on commit 6103930

Please sign in to comment.