Skip to content

Commit

Permalink
Merge pull request #67 from foyzulkarim/feature/refactor-data-seeding
Browse files Browse the repository at this point in the history
Feature/refactor data seeding
  • Loading branch information
foyzulkarim authored Apr 1, 2022
2 parents e71403c + 01ce22e commit d43163a
Show file tree
Hide file tree
Showing 27 changed files with 545 additions and 210 deletions.
4 changes: 3 additions & 1 deletion client-pro/src/pages/permission/entry/index.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { Form, Card, message } from 'antd';
import { Form, Card, message, AutoComplete } from 'antd';
import ProForm, {
ProFormCheckbox,
ProFormSelect,
Expand All @@ -12,6 +12,7 @@ const EntryForm = (props) => {
const [form] = Form.useForm();
const [role, setRole] = React.useState(null);
const [resource, setResource] = React.useState(null);
const [resources, setResources] = React.useState([]);

// get roles
const fetchRoles = async () => {
Expand All @@ -24,6 +25,7 @@ const EntryForm = (props) => {
const fetchResources = async () => {
const result = await getResources();
const options = result.data.map(r => ({ label: r.alias, value: r._id, resource: r }));
setResources(options);
return options;
};

Expand Down
12 changes: 11 additions & 1 deletion client-pro/src/pages/role/role-entry/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@ const EntryForm = (props) => {

const onFinish = async (values) => {
console.log(values, form);
run(values);
// run(values);
const result = await save(values);
console.log(result);

if (result instanceof Error) {
message.error(result.message);
}
else {
message.success(result.message);
form.resetFields();
}
};

return (
Expand Down
76 changes: 45 additions & 31 deletions client-pro/src/pages/role/role-list/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,52 @@ import { Button, message, Pagination, Form, Row, Col, Input, DatePicker, Modal }
import React, { useState, useRef, useEffect } from 'react';
import { PageContainer, } from '@ant-design/pro-layout';
import ProTable from '@ant-design/pro-table';
import { history } from 'umi';
import { history, useAccess } from 'umi';
import { count, search, remove } from '../service';

const DeleteButton = (props) => {

const { confirm } = Modal;
const { elementId } = props;

const showDeleteConfirm = (item) => {
confirm({
title: `Do you Want to delete ${item.name}?`,
icon: <ExclamationCircleOutlined />,
content: `${item.name} will be deleted permanently.`,
okText: 'Yes',
okType: 'danger',
cancelText: 'No',
onOk: async () => {
console.log('OK');
const r = await remove(item._id);
if (r.success) {
message.success(r.message);
setFetchRoles(true);
}
},
onCancel() {
console.log('Cancel');
},
});
};

const access = useAccess();
const isVisible = access.canShow(elementId);
if (isVisible) {
const isDisabled = access.isDisabled(elementId);
return isDisabled ? <span>Delete</span> : <a
key="config"
onClick={() => {
showDeleteConfirm(props.record);
}}
>
Delete
</a>;
}
return null;
}


const TableList = () => {
const actionRef = useRef();
Expand Down Expand Up @@ -36,28 +79,6 @@ const TableList = () => {
}
};

const showDeleteConfirm = (product) => {
confirm({
title: `Do you Want to delete ${product.name}?`,
icon: <ExclamationCircleOutlined />,
content: `${product.name} will be deleted permanently.`,
okText: 'Yes',
okType: 'danger',
cancelText: 'No',
onOk: async () => {
console.log('OK');
const r = await remove(product._id);
if (r.success) {
message.success(r.message);
setFetchRoles(true);
}
},
onCancel() {
console.log('Cancel');
},
});
};

const fetchRoleCount = async () => {
const result = await count({ ...searchObject });
setTotal(result.total);
Expand Down Expand Up @@ -124,14 +145,7 @@ const TableList = () => {
dataIndex: 'option',
valueType: 'option',
render: (_, record) => [
<a
key="config"
onClick={() => {
showDeleteConfirm(record);
}}
>
Delete
</a>,
<DeleteButton key="delete" record={record} elementId='user-list-delete-btn' />,
],
},
];
Expand Down
7 changes: 4 additions & 3 deletions client-pro/src/pages/user/user-list/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { count, search, remove } from '../service';
const DeleteButton = (props) => {

const { confirm } = Modal;
const { elementId } = props;

const showDeleteConfirm = (product) => {
confirm({
Expand All @@ -33,9 +34,9 @@ const DeleteButton = (props) => {
};

const access = useAccess();
const isVisible = access.canShow('user-list-delete-btn');
const isVisible = access.canShow(elementId);
if (isVisible) {
const isDisabled = access.isDisabled('user-list-delete-btn');
const isDisabled = access.isDisabled(elementId);
return isDisabled ? <span>Delete</span> : <a
key="config"
onClick={() => {
Expand Down Expand Up @@ -147,7 +148,7 @@ const TableList = () => {
dataIndex: 'option',
valueType: 'option',
render: (_, record) => [
<DeleteButton key="delete" record={record} />,
<DeleteButton key="delete" record={record} elementId='user-list-delete-btn' />,
],
},
];
Expand Down
6 changes: 6 additions & 0 deletions server/setup/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ require("dotenv").config();
const logger = require("../src/core/logger");

const { migrate: userMigrate } = require("./users");
const { migrate: resourceMigrate } = require("./resources");
const { migrate: roleMigrate } = require("./roles");
const { migrate: permissionMigrate } = require("./permissions");

logger.info("Migration starting");
const isMongoDbUrl = JSON.parse(
Expand All @@ -18,6 +21,9 @@ const migrate = async () => {
await mongoose.connect(uri, options);
logger.info("Connected to MongoDB");
await userMigrate(logger);
await resourceMigrate(logger);
await roleMigrate(logger);
await permissionMigrate(logger);
logger.info(`Migration finished`);
process.exit(0);
};
Expand Down
27 changes: 25 additions & 2 deletions server/setup/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ const parser = require("jsonc-parser");

const dataStr = fs.readFileSync("./setup/permissions.jsonc", "utf8");

const { save, searchOne, update } = require("../src/core/repository");
const {
save,
searchOne,
update,
updateAll,
} = require("../src/core/repository");
const { name: permissionModel } = require("../src/modules/permission/model");
const { name: resourceModel } = require("../src/modules/resource/model");

Expand Down Expand Up @@ -51,4 +56,22 @@ const seed = async (logger) => {
logger.info(`Seeding users finished`);
};

module.exports = { seed };
const migrate = async (logger) => {
logger.info(`Starting migration of permissions`);
const superadminUser = await searchOne({ username: "superadmin" }, "User");
if (!superadminUser) {
throw new Error("Superadmin user not found");
}

await updateAll(
{ createdBy: { $exists: false } },
{
createdBy: superadminUser._id,
updatedBy: superadminUser._id,
},
permissionModel
);
logger.info(`Migration of permissions finished`);
};

module.exports = { seed, migrate };
Loading

0 comments on commit d43163a

Please sign in to comment.