From 8750ee04a6e3afc06fc17e887039404dbc7597f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Wed, 10 Nov 2021 20:40:34 -0500 Subject: [PATCH] refactor: make a single call to set widgets per template --- src/socket.io/admin/widgets.js | 8 +++----- src/widgets/index.js | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/socket.io/admin/widgets.js b/src/socket.io/admin/widgets.js index 7dfc8e7ab7..b2a4032f49 100644 --- a/src/socket.io/admin/widgets.js +++ b/src/socket.io/admin/widgets.js @@ -1,14 +1,12 @@ 'use strict'; -const async = require('async'); const widgets = require('../../widgets'); const Widgets = module.exports; -Widgets.set = function (socket, data, callback) { +Widgets.set = async function (socket, data) { if (!Array.isArray(data)) { - return callback(new Error('[[error:invalid-data]]')); + throw new Error('[[error:invalid-data]]'); } - - async.eachSeries(data, widgets.setArea, callback); + await widgets.setAreas(data); }; diff --git a/src/widgets/index.js b/src/widgets/index.js index 572121b021..bda9682736 100644 --- a/src/widgets/index.js +++ b/src/widgets/index.js @@ -167,6 +167,25 @@ widgets.setArea = async function (area) { await db.setObjectField(`widgets:${area.template}`, area.location, JSON.stringify(area.widgets)); }; +widgets.setAreas = async function (areas) { + const templates = {}; + areas.forEach((area) => { + if (!area.location || !area.template) { + throw new Error('Missing location and template data'); + } + templates[area.template] = templates[area.template] || {}; + templates[area.template][area.location] = JSON.stringify(area.widgets); + }); + + const keys = []; + const data = []; + Object.keys(templates).forEach((tpl) => { + keys.push(`widgets:${tpl}`); + data.push(templates[tpl]); + }); + await db.setObjectBulk(keys, data); +}; + widgets.reset = async function () { const defaultAreas = [ { name: 'Draft Zone', template: 'global', location: 'header' },