GladysProject/Gladys

View on GitHub
server/lib/dashboard/dashboard.updateOrder.js

Summary

Maintainability
A
0 mins
Test Coverage
const { Op } = require('sequelize');

const Promise = require('bluebird');
const db = require('../../models');

/**
 * @description Update a dashboard.
 * @param {string} userId - The userId querying.
 * @param {Array<string>} dashboards - Dashboard selectors new order.
 * @example
 * gladys.dashboard.updateOrder('483b68cb-15ef-4ea3-80df-1e1bed5b402d', ['my-dashboard', 'other-dashboard']);
 */
async function updateOrder(userId, dashboards) {
  // Foreach dashboard, update its position
  await Promise.each(dashboards, async (dashboard, index) => {
    await db.Dashboard.update(
      { position: index },
      {
        where: {
          // I can edit dashboard I created or public dashboard
          [Op.or]: [
            {
              user_id: userId,
            },
            {
              visibility: 'public',
            },
          ],
          selector: dashboard,
        },
      },
    );
  });
}

module.exports = {
  updateOrder,
};