Skip to content

Commit

Permalink
Merge pull request #66 from foyzulkarim/feature/server-refactor-user-…
Browse files Browse the repository at this point in the history
…module-cleanup

Cleanup user migration script
  • Loading branch information
foyzulkarim authored Mar 30, 2022
2 parents d4137d4 + 2104e66 commit e71403c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 24 deletions.
16 changes: 7 additions & 9 deletions server/setup/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(
Expand All @@ -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) {
Expand All @@ -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`
Expand Down
20 changes: 10 additions & 10 deletions server/setup/permissions.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@
"isAllowed": true,
"isDisabled": false
},
// /users-new permission for superadmin
// /users/new permission for superadmin
{
"resourceName": "/users/new",
"resourceAlias": "Users new",
Expand Down Expand Up @@ -420,7 +420,7 @@
"isAllowed": true,
"isDisabled": false
},
// /users-new permission for admin
// /users/new permission for admin
{
"resourceName": "/users/new",
"resourceAlias": "Users new",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions server/setup/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
35 changes: 32 additions & 3 deletions server/setup/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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;
};

Expand Down

0 comments on commit e71403c

Please sign in to comment.