Skip to content

Commit

Permalink
Merge branch 'budsies' into 32518-magento2-cart-api
Browse files Browse the repository at this point in the history
  • Loading branch information
gorbunovav committed Jan 24, 2025
2 parents b5f1482 + b0d7902 commit 2d6a1c4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 19 deletions.
5 changes: 3 additions & 2 deletions config/local.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
"previewToken": "${STORYBLOK_TOKEN}",
"managementToken": "${STORYBLOK_MANAGEMENT_TOKEN}",
"hookSecret": "${STORYBLOK_HOOK_SECRET}",
"invalidate": "https://${VSF_DOMAIN}/invalidate?tag=storyblok&key=${CACHE_INVALIDATION_KEY}",
"invalidate": "https://${VSF_DOMAIN}/invalidate?tag={{tag}}&key=${CACHE_INVALIDATION_KEY}",
"spaceId": "${STORYBLOK_SPACE_ID}",
"settings": {
"appendStoreCodeFromHeader": true
Expand Down Expand Up @@ -285,7 +285,7 @@
"loadByAttributeMetadata": false
},
"product": {
"excludeFields": [ "updated_at", "created_at", "attribute_set_id", "status", "visibility", "tier_prices", "options_container", "msrp_display_actual_price_type", "has_options", "stock.manage_stock", "stock.use_config_min_qty", "stock.use_config_notify_stock_qty", "stock.stock_id", "stock.use_config_backorders", "stock.use_config_enable_qty_inc", "stock.enable_qty_increments", "stock.use_config_manage_stock", "stock.use_config_min_sale_qty", "stock.notify_stock_qty", "stock.use_config_max_sale_qty", "stock.use_config_max_sale_qty", "stock.qty_increments", "small_image", "sgn", "*.sgn"],
"excludeFields": [ "updated_at", "created_at", "attribute_set_id", "status", "tier_prices", "options_container", "msrp_display_actual_price_type", "has_options", "stock.manage_stock", "stock.use_config_min_qty", "stock.use_config_notify_stock_qty", "stock.stock_id", "stock.use_config_backorders", "stock.use_config_enable_qty_inc", "stock.enable_qty_increments", "stock.use_config_manage_stock", "stock.use_config_min_sale_qty", "stock.notify_stock_qty", "stock.use_config_max_sale_qty", "stock.use_config_max_sale_qty", "stock.qty_increments", "small_image", "sgn", "*.sgn"],
"includeFields": [
"category_ids",
"category",
Expand Down Expand Up @@ -334,6 +334,7 @@
"slug",
"disabled_upgrades",
"customizations",
"visibility",
"gift_card_images_data",
"am_gift_card_prices",
"am_open_amount_min",
Expand Down
5 changes: 4 additions & 1 deletion scripts/storyblok.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const config = require('config')

const { getClient } = require('../src/lib/elastic');
const { seedDatabase, seedStoryblokDatasources } = require('../src/api/extensions/vsf-storyblok-extension/sync');
const {initStoryblokClients} = require('../src/api/extensions/vsf-storyblok-extension/storyblok');
const { initStoryblokClients } = require('../src/api/extensions/vsf-storyblok-extension/storyblok');
const { cacheInvalidate } = require('../src/api/extensions/vsf-storyblok-extension/helpers');

const db = getClient(config);
initStoryblokClients(config);
Expand Down Expand Up @@ -35,6 +36,8 @@ program
await seedStoryblokDatasources(db, config);
}

await cacheInvalidate(config.storyblok, 'storyblok');

process.exit(0)
});

Expand Down
7 changes: 4 additions & 3 deletions src/api/extensions/vsf-storyblok-extension/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,12 @@ export const log = (string) => {
console.log('📖 : ' + string) // eslint-disable-line no-console
}

export const cacheInvalidate = async (config) => {
export const cacheInvalidate = async (config, tag) => {
if (config.invalidate) {
log(`Invalidating cache... (${config.invalidate})`)
const uri = config.invalidate.replace('{{tag}}', tag)
log(`Invalidating cache... (${uri})`)
await rp({
uri: config.invalidate
uri
})
log('Invalidated cache ✅')
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/extensions/vsf-storyblok-extension/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module.exports = ({ config }) => {

api.get('/full', protectRoute(config), async (req, res) => {
await fullSync(db, config)
await cacheInvalidate(config.storyblok)
await cacheInvalidate(config.storyblok, 'storyblok')
log('Stories synced!')
apiStatus(res)
})
Expand Down
47 changes: 35 additions & 12 deletions src/api/extensions/vsf-storyblok-extension/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ function indexStories ({ db, config, stories = [] }) {
})
}

function getCacheTagsByStoriesSlugs (slugs) {
return slugs.map((slug) => `storyblok_${slug}`)
}

async function indexStory ({ db, config, story }) {
const transformedStory = transformStory(config.storyblok.storiesIndex, story)

Expand Down Expand Up @@ -87,7 +91,7 @@ const handleHook = async (db, config, params) => {

if (action === 'branch_deployed') {
await fullSync(db, config)
await cacheInvalidate(config.storyblok)
await cacheInvalidate(config.storyblok, 'storyblok')
return
}

Expand All @@ -98,19 +102,28 @@ const handleHook = async (db, config, params) => {

if (response.data.story && response.data.story.is_folder) {
await fullSync(db, config)
await cacheInvalidate(config.storyblok)
await cacheInvalidate(config.storyblok, 'storyblok')
return
}
} catch (e) {}

await handleActionForStory(db, config, id, action, cv)
await handleActionForRelatedStories(db, config, id, action, cv)

await cacheInvalidate(config.storyblok)
} catch (e) { }

const updatedStoriesSlugs = await handleActionForStory(db, config, id, action, cv)

Check warning on line 110 in src/api/extensions/vsf-storyblok-extension/sync.js

View workflow job for this annotation

GitHub Actions / build (10.x)

'handleActionForStory' was used before it was defined

Check warning on line 110 in src/api/extensions/vsf-storyblok-extension/sync.js

View workflow job for this annotation

GitHub Actions / build (12.x)

'handleActionForStory' was used before it was defined
const updatedRelatedStoriesSlugs = await handleActionForRelatedStories(db, config, id, action, cv)

Check warning on line 111 in src/api/extensions/vsf-storyblok-extension/sync.js

View workflow job for this annotation

GitHub Actions / build (10.x)

'handleActionForRelatedStories' was used before it was defined

Check warning on line 111 in src/api/extensions/vsf-storyblok-extension/sync.js

View workflow job for this annotation

GitHub Actions / build (12.x)

'handleActionForRelatedStories' was used before it was defined

await cacheInvalidate(
config.storyblok,
getCacheTagsByStoriesSlugs(
[
...updatedStoriesSlugs,
...updatedRelatedStoriesSlugs
]
)
)
}

const handleActionForRelatedStories = async (db, config, id, action, cv) => {
const size = 10
const updatedStoriesSlugs = []

switch (action) {
case 'deleted':
Expand All @@ -133,6 +146,7 @@ const handleActionForRelatedStories = async (db, config, id, action, cv) => {

for (const storyToReindex of response.data.stories) {
log(`Try to reindex story ${storyToReindex.full_slug} with ${id} ID reference`)
updatedStoriesSlugs.push(storyToReindex.full_slug)

await indexStory({ db, config, story: storyToReindex })
}
Expand All @@ -146,9 +160,13 @@ const handleActionForRelatedStories = async (db, config, id, action, cv) => {
break
}
}

return updatedStoriesSlugs
}

const handleActionForStory = async (db, config, id, action, cv) => {
const updatedStoriesSlugs = []

switch (action) {
case 'published': {
let storiesToPublish = []
Expand Down Expand Up @@ -178,12 +196,14 @@ const handleActionForStory = async (db, config, id, action, cv) => {

for (const storyToDelete of storiesToDelete) {
log(`Try to delete old copy of story ${storyToDelete.full_slug}`)
updatedStoriesSlugs.push(storyToDelete.full_slug)

await deleteStory({ db, config, story: storyToDelete })
}

for (const storyToPublish of storiesToPublish) {
log(`Try to reindex story ${storyToPublish.full_slug}`)
updatedStoriesSlugs.push(storyToPublish.full_slug)

await indexStory({ db, config, story: storyToPublish })
}
Expand All @@ -196,6 +216,7 @@ const handleActionForStory = async (db, config, id, action, cv) => {

for (const storyToDelete of stories) {
log(`Try to delete story ${storyToDelete.full_slug}`)
updatedStoriesSlugs.push(storyToDelete.full_slug)

await deleteStory({ db, config, story: storyToDelete });
}
Expand All @@ -208,6 +229,8 @@ const handleActionForStory = async (db, config, id, action, cv) => {
break
}
}

return updatedStoriesSlugs
}

const seedDatabase = async (db, config) => {
Expand Down Expand Up @@ -245,7 +268,7 @@ const seedStoryblokDatasources = async (db, config) => {
body: {
'size': 1000,
'sort': [
{'name.keyword': 'asc'}
{ 'name.keyword': 'asc' }
],
'query': {
'constant_score': {
Expand Down Expand Up @@ -306,14 +329,14 @@ const seedStoryblokDatasources = async (db, config) => {
body: {
'size': 1000,
'sort': [
{'name.keyword': 'asc'}
{ 'name.keyword': 'asc' }
],
'query': {
'constant_score': {
'filter': {
'bool': {
'must': [
{'term': {'is_active': true}}
{ 'term': { 'is_active': true } }
]
}
}
Expand All @@ -328,7 +351,7 @@ const seedStoryblokDatasources = async (db, config) => {
body: {
'size': 1000,
'sort': [
{'name.keyword': 'asc'}
{ 'name.keyword': 'asc' }
],
'query': {}
}
Expand Down

0 comments on commit 2d6a1c4

Please sign in to comment.