WMS_MAIN/database/migrations/20260608033847-set-role-preset-permissions.js

170 lines
12 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';
/**
* 系统角色(is_system=1)预设权限迁移
*
* 背景:
* wms_permission 表已扩容到 349 条(6 种类型),当前系统角色的 permissions
* 字段(JSON 数组)覆盖范围不足,新增的 API/pda_pages/workerstation_pages 等
* 权限未被纳入旧的角色预设中。
*
* 范围:
* - 仅更新 is_system=1 的角色(12 个,排除 id=2 仓库管理员和 id=7 超级管理员,
* 二者保持 "*" 不变)
* - 仅使用 status=1 的活跃权限
* - 不修改 wms_permission 表
*
* 幂等设计:
* UPDATE wms_role SET permissions = ? WHERE id = ? AND is_system = 1
*/
const TABLE = 'wms_role';
/** @type {{id: number, role_code: string, permissions: string, desc: string}[]} */
const ROLE_UPDATES = [
{
id: 5,
role_code: 'store_admin_default',
name: '货主管理员',
// 货主管理员:web_pages(设置4,货主17,成员审核50,货主列表244,店铺模块366), block(商家基础数据48), api(货主/店铺相关 52,53,308,363,364,365,367-372,376,379,380)
permissions: '[4,17,48,50,52,53,244,308,363,364,365,366,367,368,369,370,371,372,376,379,380]',
old: '[4,48,50,52,53]',
},
{
id: 8,
role_code: '2',
name: '经理',
// 经理:全部 status=1 权限(206条),排除权限管理(5,7,119,120,121)、测试(187,245,378)
permissions: '[1,4,6,8,9,11,12,13,14,15,16,17,18,19,20,21,22,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,43,44,45,46,47,48,49,50,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,244,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,281,283,290,291,292,293,294,295,296,297,298,299,300,303,304,305,306,307,308,309,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,360,361,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,379,380,381,382,383,384]',
old: '[4-121,244]',
},
{
id: 26,
role_code: 'warehouse',
name: '仓库组-主管',
// 仓库组-主管:全部活跃权限排除权限管理(5,6,7,119,120,121)、财务(28,31-37)、数据模块(16)、测试(187,245,378)
permissions: '[1,4,8,9,11,12,13,14,15,17,18,19,20,21,22,24,25,26,27,29,30,38,39,40,41,43,44,45,46,47,48,49,50,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,244,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,281,283,290,291,292,293,294,295,296,297,298,299,300,303,304,305,306,307,308,309,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,360,361,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,379,380,381,382,383,384]',
old: '[4-121]',
},
{
id: 27,
role_code: 'warehouse_role',
name: '仓库组-员工',
// 仓库组-员工:同仓库组-主管,排除权限管理、财务、数据模块、测试
permissions: '[1,4,8,9,11,12,13,14,15,17,18,19,20,21,22,24,25,26,27,29,30,38,39,40,41,43,44,45,46,47,48,49,50,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,244,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,281,283,290,291,292,293,294,295,296,297,298,299,300,303,304,305,306,307,308,309,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,360,361,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,379,380,381,382,383,384]',
old: '[4-121]',
},
{
id: 28,
role_code: '05',
name: '客服组-主管',
// 客服组-主管:排除权限管理(5,6,7,119,120,121)、财务(28,31-37)、数据(16)、测试(187,245,378)、PDA拣货/复核/打包/补货、工位称重/退件
permissions: '[1,4,8,9,11,12,13,14,15,17,18,19,20,21,22,24,25,26,27,29,30,38,39,40,41,43,44,45,46,47,48,49,50,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,244,254,255,256,278,281,283,290,291,292,293,294,295,296,297,298,299,300,303,304,305,306,307,308,309,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,361,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,379,380,381,382,383,384]',
old: '[1,4-121,244-336]',
},
{
id: 30,
role_code: '04',
name: '客服组-员工',
// 客服组-员工:同客服组-主管
permissions: '[1,4,8,9,11,12,13,14,15,17,18,19,20,21,22,24,25,26,27,29,30,38,39,40,41,43,44,45,46,47,48,49,50,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,244,254,255,256,278,281,283,290,291,292,293,294,295,296,297,298,299,300,303,304,305,306,307,308,309,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,361,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,379,380,381,382,383,384]',
old: '[1,4-121,244-336]',
},
{
id: 29,
role_code: '03',
name: '生产组-主管',
// 生产组-主管:全部活跃权限排除权限管理(5,6,7,119,120,121)、财务(28,31-37)、数据(16)、测试(187,245,378)
permissions: '[1,4,8,9,11,12,13,14,15,17,18,19,20,21,22,24,25,26,27,29,30,38,39,40,41,43,44,45,46,47,48,49,50,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,244,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,281,283,290,291,292,293,294,295,296,297,298,299,300,303,304,305,306,307,308,309,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,360,361,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,379,380,381,382,383,384]',
old: '[4-121]',
},
{
id: 31,
role_code: '002',
name: '生产组-员工',
// 生产组-员工:同生产组-主管
permissions: '[1,4,8,9,11,12,13,14,15,17,18,19,20,21,22,24,25,26,27,29,30,38,39,40,41,43,44,45,46,47,48,49,50,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,244,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,281,283,290,291,292,293,294,295,296,297,298,299,300,303,304,305,306,307,308,309,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,360,361,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,379,380,381,382,383,384]',
old: '[4-121]',
},
{
id: 32,
role_code: '021',
name: '生产组-拣货',
// 生产组-拣货:排除复核发货(266-269)、打包(270-276)、权限管理(5,6,7,119,120,121)、财务(28,31-37)、数据(16)、测试(187,245,378)、工位站(356,359,360)
permissions: '[1,4,8,9,11,12,13,14,15,17,18,19,20,21,22,24,25,26,27,29,30,38,39,40,41,43,44,45,46,47,48,49,50,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,244,254,255,256,257,258,259,260,261,262,263,264,265,277,278,279,281,283,290,291,292,293,294,295,296,297,298,299,300,303,304,305,306,307,308,309,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,357,358,361,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,379,380,381,382,383,384]',
old: '[4-121]',
},
{
id: 33,
role_code: '0113',
name: '拣货组组员',
// 拣货组组员:同生产组-拣货
permissions: '[1,4,8,9,11,12,13,14,15,17,18,19,20,21,22,24,25,26,27,29,30,38,39,40,41,43,44,45,46,47,48,49,50,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,244,254,255,256,257,258,259,260,261,262,263,264,265,277,278,279,281,283,290,291,292,293,294,295,296,297,298,299,300,303,304,305,306,307,308,309,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,357,358,361,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,379,380,381,382,383,384]',
old: '[4-121]',
},
{
id: 34,
role_code: '024',
name: '生产组-复核',
// 生产组-复核:排除拣货(257-265)、打包(270-276)、权限管理(5,6,7,119,120,121)、财务(28,31-37)、数据(16)、测试(187,245,378)
permissions: '[1,4,8,9,11,12,13,14,15,17,18,19,20,21,22,24,25,26,27,29,30,38,39,40,41,43,44,45,46,47,48,49,50,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,244,254,255,256,266,267,268,269,277,278,279,281,283,290,291,292,293,294,295,296,297,298,299,300,303,304,305,306,307,308,309,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,360,361,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,379,380,381,382,383,384]',
old: '[4-121]',
},
{
id: 36,
role_code: '0211',
name: '其他普工',
// 其他普工:排除设置模块(4-9,40,41,80,81,336,366,375)、财务、数据、测试、系统、敏感API、仓库管理API、视频
permissions: '[1,11,12,13,14,15,17,18,19,20,21,22,24,25,26,27,29,30,38,39,43,44,45,46,48,49,50,54,55,57,58,61,62,63,64,65,66,67,68,69,70,71,72,74,75,76,77,78,79,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,108,244,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,281,283,290,291,292,293,294,295,296,297,298,299,300,303,304,305,306,307,309,333,334,335,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,357,358,361,364,365,370,373,374,376,377,379,380,381,382,383,384]',
old: '[4-121]',
},
];
/**
* 正向迁移:更新 12 个系统角色的 permissions 字段。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口
* @return {Promise<void>} 无返回值
*/
module.exports.up = async queryInterface => {
let updatedCount = 0;
for (const role of ROLE_UPDATES) {
const [ result ] = await queryInterface.sequelize.query(
`UPDATE \`${TABLE}\` SET permissions = :permissions, updated_at = NOW()
WHERE id = :id AND is_system = 1`,
{ replacements: { id: role.id, permissions: role.permissions } }
);
if (result && (result.affectedRows > 0 || result.rowCount > 0 || result.changedRows > 0)) {
updatedCount++;
}
}
console.log(
`[Migration] wms_role 预设权限更新:共 ${ROLE_UPDATES.length} 个角色,实际更新 ${updatedCount} 个`
);
};
/**
* 回滚迁移:恢复到旧的 permissions 值。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口
* @return {Promise<void>} 无返回值
*/
module.exports.down = async queryInterface => {
let revertedCount = 0;
for (const role of ROLE_UPDATES) {
const [ result ] = await queryInterface.sequelize.query(
`UPDATE \`${TABLE}\` SET permissions = :permissions, updated_at = NOW()
WHERE id = :id AND is_system = 1`,
{ replacements: { id: role.id, permissions: role.old } }
);
if (result && (result.affectedRows > 0 || result.rowCount > 0 || result.changedRows > 0)) {
revertedCount++;
}
}
console.log(
`[Migration] wms_role 预设权限回滚:共 ${ROLE_UPDATES.length} 个角色,实际回滚 ${revertedCount} 个`
);
};