diff --git a/server/setup/permissions.js b/server/setup/permissions.js index 32d8747..3e5f930 100644 --- a/server/setup/permissions.js +++ b/server/setup/permissions.js @@ -3,11 +3,9 @@ const parser = require("jsonc-parser"); const dataStr = fs.readFileSync("./setup/permissions.jsonc", "utf8"); -const { - save, - searchOne, - update, -} = require("../src/modules/permission/service"); +const { save, searchOne, update } = require("../src/core/repository"); +const { name: permissionModel } = require("../src/modules/permission/model"); +const { name: resourceModel } = require("../src/modules/resource/model"); const seed = async (logger) => { const data = parser.parse(dataStr); @@ -18,13 +16,13 @@ const seed = async (logger) => { ); const itemExists = await searchOne( { resourceName: item.resourceName, roleName: item.roleName }, - "Permission" + permissionModel ); if (!itemExists) { const role = await searchOne({ name: item.roleName }, "Role"); const resource = await searchOne( { name: item.resourceName }, - "Resource" + resourceModel ); try { const savedItem = await save( @@ -33,7 +31,7 @@ const seed = async (logger) => { roleId: role._id, resourceId: resource._id, }, - "Permission" + permissionModel ); logger.info(`Saved permission id: ${savedItem._id}`); } catch (error) { @@ -42,7 +40,7 @@ const seed = async (logger) => { } else { const updatedItem = await update( { _id: itemExists._id, ...item }, - "Permission" + permissionModel ); logger.info( `Permission ${item.resourceName} for ${item.roleName} of id ${updatedItem._id} is updated` diff --git a/server/setup/permissions.jsonc b/server/setup/permissions.jsonc index c446a77..dfa0fa3 100644 --- a/server/setup/permissions.jsonc +++ b/server/setup/permissions.jsonc @@ -390,7 +390,7 @@ "isAllowed": true, "isDisabled": false }, - // /users-new permission for superadmin + // /users/new permission for superadmin { "resourceName": "/users/new", "resourceAlias": "Users new", @@ -420,7 +420,7 @@ "isAllowed": true, "isDisabled": false }, - // /users-new permission for admin + // /users/new permission for admin { "resourceName": "/users/new", "resourceAlias": "Users new", @@ -450,9 +450,9 @@ "isAllowed": true, "isDisabled": false }, - // /roles-new permission for superadmin + // /roles/new permission for superadmin { - "resourceName": "/roles-new", + "resourceName": "/roles/new", "resourceAlias": "Roles new", "roleName": "superadmin", "roleAlias": "Superadmin", @@ -480,9 +480,9 @@ "isAllowed": true, "isDisabled": false }, - // /resources-new permission for superadmin + // /resources/new permission for superadmin { - "resourceName": "/resources-new", + "resourceName": "/resources/new", "resourceAlias": "Resources new", "roleName": "superadmin", "roleAlias": "Superadmin", @@ -510,9 +510,9 @@ "isAllowed": true, "isDisabled": false }, - // /permissions-new permission for superadmin + // /permissions/new permission for superadmin { - "resourceName": "/permissions-new", + "resourceName": "/permissions/new", "resourceAlias": "Permissions new", "roleName": "superadmin", "roleAlias": "Superadmin", @@ -540,9 +540,9 @@ "isAllowed": true, "isDisabled": false }, - // /products-new permission for user + // /products/new permission for user { - "resourceName": "/products-new", + "resourceName": "/products/new", "resourceAlias": "Products new", "roleName": "user", "roleAlias": "User", diff --git a/server/setup/resources.js b/server/setup/resources.js index d8fc374..960ebc2 100644 --- a/server/setup/resources.js +++ b/server/setup/resources.js @@ -2,9 +2,10 @@ const fs = require("fs"); const parser = require("jsonc-parser"); const dataStr = fs.readFileSync("./setup/resources.jsonc", "utf8"); -const { save, searchOne } = require("../src/modules/resource/service"); +const { save, searchOne } = require("../src/core/repository"); +const { name: model } = require("../src/modules/resource/model"); -const model = "Resource"; +// const model = "Resource"; const seed = async (logger) => { const data = parser.parse(dataStr); await Promise.all( diff --git a/server/setup/users.js b/server/setup/users.js index 56d1340..78e2ca1 100644 --- a/server/setup/users.js +++ b/server/setup/users.js @@ -4,8 +4,12 @@ const { createUser, searchOne, updateAll, + update, } = require("../src/modules/auth/service"); +const User = require("../src/modules/auth/model"); +const { name: roleModel } = require("../src/modules/role/model"); + const seed = async (logger) => { await Promise.all( data.map(async (user) => { @@ -28,16 +32,41 @@ const seed = async (logger) => { const migrate = async (logger) => { logger.info("User starting"); - const superadmin = await searchOne({ username: "superadmin" }, "User"); - if (!superadmin) { + const superadminUser = await searchOne({ username: "superadmin" }, "User"); + if (!superadminUser) { throw new Error("Superadmin user not found"); } + const adminRole = await searchOne({ name: "admin" }, "Role"); + if (!adminRole) { + throw new Error("Admin role not found"); + } + + const superadminRole = await searchOne({ name: "superadmin" }, "Role"); + if (!adminRole) { + throw new Error("Superadmin role not found"); + } + const response = await updateAll( {}, - { createdBy: superadmin._id, updatedBy: superadmin._id }, + { + createdBy: superadminUser._id, + updatedBy: superadminUser._id, + roleId: adminRole._id, + roleAlias: adminRole.alias, + }, + "User" + ); + logger.info(`Migrated ${response.nModified} users`); + const saUpdateResponse = await update( + { + ...superadminUser, + roleId: superadminRole._id, + roleAlias: superadminRole.alias, + }, "User" ); + logger.info(`Migrated superadmin user ${saUpdateResponse.nModified}`); return response; };