WMS_MAIN/database/migrations/20260807130000-add-stockout-api-permissions.js

85 lines
3.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 为 storage.stockout.* 添加 API 权限并授权。
*
* 权限 ID(固定):
* 504 — storage.stockout.list 出库单列表
* 505 — storage.stockout.listItems 出库单明细
* 506 — storage.stockout.confirm 出库单确认
* 507 — storage.stockout.updateExpress 更新运单号
*
* 授权角色:经理(8)、仓库组-主管(26)、客服组-主管(28)、生产组-主管(29)、客服组-员工(30)
*/
module.exports = {
up: async queryInterface => {
// ===== 第一步:写入权限行 =====
const perms = [
[ 504, 'storage.stockout.list', '出库单列表' ],
[ 505, 'storage.stockout.listItems', '出库单明细' ],
[ 506, 'storage.stockout.confirm', '出库单确认' ],
[ 507, 'storage.stockout.updateExpress', '更新运单号' ],
];
for (const [ id, method, desc ] of perms) {
await queryInterface.sequelize.query(`
INSERT INTO wms_permission (id, path, component, name, redirect, meta_title, meta_icon, hidden, always_show, parent_id, type, sort, status, created_at, update_at)
VALUES (${id}, '${method}', NULL, '${method}', NULL, 'API:${desc}', NULL, 0, 0, NULL, 'api', 0, 1, NOW(), NOW())
ON DUPLICATE KEY UPDATE path='${method}', meta_title='API:${desc}', type='api'
`);
}
// ===== 第二步:授权给客服组员工及以上角色 =====
const roleIds = [ 8, 26, 28, 29, 30 ]; // 经理 + 各主管 + 客服组-员工
const newPermIds = [ 504, 505, 506, 507 ];
for (const roleId of roleIds) {
for (const permId of newPermIds) {
await queryInterface.sequelize.query(`
UPDATE wms_role
SET permissions = JSON_ARRAY_APPEND(permissions, '$', CAST(${permId} AS UNSIGNED)),
updated_at = NOW()
WHERE id = ${roleId}
AND permissions IS NOT NULL
AND permissions != '*'
AND permissions != '[]'
AND JSON_VALID(permissions) = 1
AND JSON_CONTAINS(permissions, '${permId}', '$') = 0
`);
}
}
},
down: async queryInterface => {
// 移除授权
const roleIds = [ 8, 26, 28, 29, 30 ];
const permIdStr = '504, 505, 506, 507';
for (const roleId of roleIds) {
await queryInterface.sequelize.query(`
UPDATE wms_role
SET permissions = JSON_REMOVE(
JSON_REMOVE(
JSON_REMOVE(
JSON_REMOVE(permissions, JSON_UNQUOTE(JSON_SEARCH(permissions, 'one', '504'))),
JSON_UNQUOTE(JSON_SEARCH(permissions, 'one', '505'))
),
JSON_UNQUOTE(JSON_SEARCH(permissions, 'one', '506'))
),
JSON_UNQUOTE(JSON_SEARCH(permissions, 'one', '507'))
),
updated_at = NOW()
WHERE id = ${roleId}
AND permissions IS NOT NULL
AND permissions != '*'
AND permissions != '[]'
`);
}
// 删除权限行
await queryInterface.sequelize.query(`
DELETE FROM wms_permission WHERE id IN (504, 505, 506, 507)
`);
},
};