diff --git a/server/src/updaters/role-updaters.js b/server/src/updaters/role-updaters.js index 797b089ed..608704991 100644 --- a/server/src/updaters/role-updaters.js +++ b/server/src/updaters/role-updaters.js @@ -1,103 +1,103 @@ // @flow import type { Viewer } from '../session/viewer'; import type { ThreadType } from 'lib/types/thread-types'; import _isEqual from 'lodash/fp/isEqual'; import invariant from 'invariant'; import { dbQuery, SQL } from '../database'; import { fetchRoles } from '../fetchers/role-fetchers'; import { getRolePermissionBlobsForChat } from '../creators/role-creator'; import createIDs from '../creators/id-creator'; async function updateRoles( viewer: Viewer, threadID: string, threadType: ThreadType, ): Promise { const currentRoles = await fetchRoles(threadID); const currentRolePermissions = {}; const currentRoleIDs = {}; for (const roleInfo of currentRoles) { currentRolePermissions[roleInfo.name] = roleInfo.permissions; currentRoleIDs[roleInfo.name] = roleInfo.id; } const rolePermissions = getRolePermissionBlobsForChat(threadType); if (_isEqual(rolePermissions)(currentRolePermissions)) { return; } const promises = []; if (rolePermissions.Admins && !currentRolePermissions.Admins) { const [id] = await createIDs('roles', 1); const newRow = [id, threadID, 'Admins', rolePermissions.Admins, Date.now()]; const insertQuery = SQL` INSERT INTO roles (id, thread, name, permissions, creation_time) VALUES ${[newRow]} `; promises.push(dbQuery(insertQuery)); const setAdminQuery = SQL` UPDATE memberships SET role = ${id} - WHERE thread = ${threadID} AND user = ${viewer.userID} + WHERE thread = ${threadID} AND user = ${viewer.userID} AND role > 0 `; promises.push(dbQuery(setAdminQuery)); } else if (!rolePermissions.Admins && currentRolePermissions.Admins) { invariant( currentRoleIDs.Admins && currentRoleIDs.Members, 'ids should exist for both Admins and Members roles', ); const id = currentRoleIDs.Admins; const deleteQuery = SQL` DELETE r, i FROM roles r LEFT JOIN ids i ON i.id = r.id WHERE r.id = ${id} `; promises.push(dbQuery(deleteQuery)); const updateMembershipsQuery = SQL` UPDATE memberships SET role = ${currentRoleIDs.Members} - WHERE thread = ${threadID} + WHERE thread = ${threadID} AND role > 0 `; promises.push(dbQuery(updateMembershipsQuery)); } const updatePermissions = {}; for (const name in currentRoleIDs) { const currentPermissions = currentRolePermissions[name]; const permissions = rolePermissions[name]; if ( !permissions || !currentPermissions || _isEqual(permissions)(currentPermissions) ) { continue; } const id = currentRoleIDs[name]; updatePermissions[id] = permissions; } if (Object.values(updatePermissions).length > 0) { const updateQuery = SQL` UPDATE roles SET permissions = CASE id `; for (const id in updatePermissions) { const permissionsBlob = JSON.stringify(updatePermissions[id]); updateQuery.append(SQL` WHEN ${id} THEN ${permissionsBlob} `); } updateQuery.append(SQL` ELSE permissions END WHERE thread = ${threadID} `); promises.push(dbQuery(updateQuery)); } await Promise.all(promises); } export { updateRoles };