Skip to content

Commit 304e8f4

Browse files
committed
code cleanup
1 parent 1c6a39d commit 304e8f4

File tree

2 files changed

+49
-78
lines changed

2 files changed

+49
-78
lines changed

src/upgradeDB.js

+46-75
Original file line numberDiff line numberDiff line change
@@ -299,83 +299,54 @@ upgradeFuncs.push({
299299

300300
upgradeFuncs.push({
301301
description: 'Create default roles with permissions and update user groups',
302-
func() {
303-
return new Promise(async (resolve, reject) => {
304-
try {
305-
// Fetch channels and get the role names with their associated channels
306-
const channels = await ChannelModel.find()
307-
const existingRoles = JSON.parse(JSON.stringify(roles)) // Deep clone the roles object
308-
309-
channels.forEach(channel => {
310-
if (Array.isArray(channel.allow)) {
311-
if (channel.txViewAcl && channel.txViewAcl.length > 0) {
312-
channel.txViewAcl.forEach(role => {
313-
if (!existingRoles[role]) {
314-
existingRoles[role] = { permissions: {} }
315-
}
316-
if (!existingRoles[role].permissions['transaction-view-specified']) {
317-
existingRoles[role].permissions['transaction-view-specified'] = []
318-
}
319-
existingRoles[role].permissions['transaction-view-specified'].push(channel.name)
320-
if (!existingRoles[role].permissions['channel-view-specified']) {
321-
existingRoles[role].permissions['channel-view-specified'] = []
322-
}
323-
existingRoles[role].permissions['channel-view-specified'].push(channel.name)
324-
existingRoles[role].permissions['client-view-all'] = true
325-
})
326-
}
327-
if (channel.txRerunAcl && channel.txRerunAcl.length > 0) {
328-
channel.txRerunAcl.forEach(role => {
329-
if (!existingRoles[role]) {
330-
existingRoles[role] = { permissions: {} }
331-
}
332-
if (!existingRoles[role].permissions['transaction-rerun-specified']) {
333-
existingRoles[role].permissions['transaction-rerun-specified'] = []
334-
}
335-
existingRoles[role].permissions['transaction-rerun-specified'].push(channel.name)
336-
if (!existingRoles[role].permissions['channel-view-specified']) {
337-
existingRoles[role].permissions['channel-view-specified'] = []
338-
}
339-
existingRoles[role].permissions['channel-view-specified'].push(channel.name)
340-
existingRoles[role].permissions['client-view-all'] = true
341-
})
342-
}
343-
if (channel.txViewFullAcl && channel.txViewFullAcl.length > 0) {
344-
channel.txViewFullAcl.forEach(role => {
345-
if (!existingRoles[role]) {
346-
existingRoles[role] = { permissions: {} }
347-
}
348-
if (!existingRoles[role].permissions['transaction-view-body-specified']) {
349-
existingRoles[role].permissions['transaction-view-body-specified'] = []
350-
}
351-
existingRoles[role].permissions['transaction-view-body-specified'].push(channel.name)
352-
if (!existingRoles[role].permissions['channel-view-specified']) {
353-
existingRoles[role].permissions['channel-view-specified'] = []
354-
}
355-
existingRoles[role].permissions['channel-view-specified'].push(channel.name)
356-
existingRoles[role].permissions['client-view-all'] = true
357-
})
358-
}
359-
}
360-
})
302+
func: async () => {
303+
try {
304+
const channels = await ChannelModel.find()
305+
const existingRoles = JSON.parse(JSON.stringify(roles)) // Deep clone the roles object
306+
307+
const updateRolePermissions = (role, permissionType, channelName) => {
308+
if (!existingRoles[role]) {
309+
existingRoles[role] = { permissions: {} }
310+
}
311+
if (!existingRoles[role].permissions[permissionType]) {
312+
existingRoles[role].permissions[permissionType] = []
313+
}
314+
existingRoles[role].permissions[permissionType].push(channelName)
315+
existingRoles[role].permissions['channel-view-specified'] = existingRoles[role].permissions['channel-view-specified'] || []
316+
existingRoles[role].permissions['channel-view-specified'].push(channelName)
317+
existingRoles[role].permissions['client-view-all'] = true
318+
}
361319

362-
// Create or update roles
363-
for (const [roleName, roleData] of Object.entries(existingRoles)) {
364-
await RoleModel.findOneAndUpdate(
365-
{ name: roleName },
366-
{ name: roleName, permissions: roleData.permissions },
367-
{ upsert: true, new: true }
368-
)
369-
logger.info(`Role ${roleName} created or updated with permissions`)
320+
channels.forEach(channel => {
321+
if (Array.isArray(channel.allow)) {
322+
const aclTypes = [
323+
{ acl: channel.txViewAcl, permissionType: 'transaction-view-specified' },
324+
{ acl: channel.txRerunAcl, permissionType: 'transaction-rerun-specified' },
325+
{ acl: channel.txViewFullAcl, permissionType: 'transaction-view-body-specified' }
326+
]
327+
328+
aclTypes.forEach(({ acl, permissionType }) => {
329+
if (acl && acl.length > 0) {
330+
acl.forEach(role => updateRolePermissions(role, permissionType, channel.name))
331+
}
332+
})
370333
}
334+
})
371335

372-
logger.info('Successfully updated roles')
373-
resolve()
374-
} catch (err) {
375-
logger.error(`Error updating roles: ${err}`)
376-
reject(err)
377-
}
378-
})
336+
// Create or update roles
337+
await Promise.all(Object.entries(existingRoles).map(([roleName, roleData]) =>
338+
RoleModel.findOneAndUpdate(
339+
{ name: roleName },
340+
{ name: roleName, permissions: roleData.permissions },
341+
{ upsert: true, new: true }
342+
)
343+
))
344+
345+
logger.info('Successfully updated roles')
346+
} catch (err) {
347+
logger.error(`Error updating roles: ${err}`)
348+
throw err
349+
}
379350
}
380351
})
381352

@@ -389,7 +360,7 @@ async function upgradeDbInternal() {
389360
const dbVer =
390361
(await DbVersionModel.findOne()) ||
391362
new DbVersionModel({version: 0, lastUpdated: new Date()})
392-
const upgradeFuncsToRun = upgradeFuncs.slice(dbVer.version)
363+
const upgradeFuncsToRun = upgradeFuncs.slice(dbVer.version)
393364
for (const upgradeFunc of upgradeFuncsToRun) {
394365
await upgradeFunc.func()
395366
dbVer.version++

test/unit/upgradeDBTest.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -579,10 +579,10 @@ describe('Upgrade DB Tests', () => {
579579

580580
await upgradeFunc()
581581

582-
const roles = await RoleModel.find()
583-
roles.length.should.be.exactly(Object.keys(roles).length)
582+
const existingRoles = await RoleModel.find()
583+
existingRoles.length.should.be.exactly(Object.keys(roles).length)
584584

585-
const adminRoles = roles.filter(r => r.name === 'admin')
585+
const adminRoles = existingRoles.filter(r => r.name === 'admin')
586586
adminRoles.length.should.be.exactly(1)
587587
})
588588

0 commit comments

Comments
 (0)