From 5b85ed318ae20ac0d88273f8af363879f0c20778 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 31 May 2019 23:17:24 -0400 Subject: [PATCH] fix: #7625, on group rename update nav/widget items --- src/groups/update.js | 42 ++++++++++++++++++++++++++++++++++++++++++ src/widgets/index.js | 26 +++++++++++++++----------- 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/src/groups/update.js b/src/groups/update.js index 5075292a79..3dd68c97c6 100644 --- a/src/groups/update.js +++ b/src/groups/update.js @@ -216,6 +216,8 @@ module.exports = function (Groups) { } async.series([ async.apply(updateMemberGroupTitles, oldName, newName), + async.apply(updateNavigationItems, oldName, newName), + async.apply(updateWidgets, oldName, newName), async.apply(db.setObjectField, 'group:' + oldName, 'name', newName), async.apply(db.setObjectField, 'group:' + oldName, 'slug', utils.slugify(newName)), async.apply(db.deleteObjectField, 'groupslug:groupname', group.slug), @@ -293,4 +295,44 @@ module.exports = function (Groups) { }, ], callback); } + + function updateNavigationItems(oldName, newName, callback) { + const navigation = require('../navigation/admin'); + + async.waterfall([ + navigation.get, + function (navItems, next) { + navItems.forEach(function (navItem) { + if (navItem && Array.isArray(navItem.groups) && navItem.groups.includes(oldName)) { + navItem.groups.splice(navItem.groups.indexOf(oldName), 1, newName); + } + }); + + navigation.save(navItems, next); + }, + ], callback); + } + + function updateWidgets(oldName, newName, callback) { + const admin = require('../widgets/admin'); + const widgets = require('../widgets'); + async.waterfall([ + admin.get, + function (data, next) { + async.eachSeries(data.areas, function (area, next) { + if (!area.data.length) { + return setImmediate(next); + } + area.widgets = area.data; + area.widgets.forEach(function (widget) { + if (widget && widget.data && Array.isArray(widget.data.groups) && widget.data.groups.includes(oldName)) { + widget.data.groups.splice(widget.data.groups.indexOf(oldName), 1, newName); + } + }); + + widgets.setArea(area, next); + }, next); + }, + ], callback); + } }; diff --git a/src/widgets/index.js b/src/widgets/index.js index 216f4435e0..fcb9ada3f3 100644 --- a/src/widgets/index.js +++ b/src/widgets/index.js @@ -141,16 +141,7 @@ widgets.getWidgetDataForTemplates = function (templates, callback) { locations.forEach(function (location) { if (templateWidgetData && templateWidgetData[location]) { try { - returnData[template][location] = JSON.parse(templateWidgetData[location]); - returnData[template][location] = returnData[template][location].map(function (widget) { - if (widget) { - widget.data.groups = widget.data.groups || []; - if (widget.data.groups && !Array.isArray(widget.data.groups)) { - widget.data.groups = [widget.data.groups]; - } - } - return widget; - }); + returnData[template][location] = parseWidgetData(templateWidgetData[location]); } catch (err) { winston.error('can not parse widget data. template: ' + template + ' location: ' + location); returnData[template][location] = []; @@ -176,7 +167,7 @@ widgets.getArea = function (template, location, callback) { return callback(null, []); } try { - result = JSON.parse(result); + result = parseWidgetData(result); } catch (err) { return callback(err); } @@ -186,6 +177,19 @@ widgets.getArea = function (template, location, callback) { ], callback); }; +function parseWidgetData(data) { + const widgets = JSON.parse(data); + widgets.forEach(function (widget) { + if (widget) { + widget.data.groups = widget.data.groups || []; + if (widget.data.groups && !Array.isArray(widget.data.groups)) { + widget.data.groups = [widget.data.groups]; + } + } + }); + return widgets; +} + widgets.setArea = function (area, callback) { if (!area.location || !area.template) { return callback(new Error('Missing location and template data'));