WMS_MAIN/database/migrations/20260813160000-create-return-waybill-permissions.js
liufumingren 2d1b9e98fa feat: 新增运单退回登记模块(returnWaybill)并同步接口文档
- 复用 wms_return_query_log 表,提供货主+运单号退回登记增删查改(create/update/list/detail/delete)
- create 幂等(同仓库+货主+运单号返回已有);货主合约校验;update 冲突 409
- 权限固定 ID 551-555 + 角色授权迁移
- 同步 methods/system.json 退回拆包/运单退回登记模块与 changes 变更记录
2026-08-13 21:40:57 +08:00

105 lines
3.7 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';
/**
* 运单退回登记模块权限记录 + 角色授权迁移。
*
* 权限明细(固定 ID 551-555,550 预留):
* 551 — returnWaybill.create API:运单退回登记
* 552 — returnWaybill.update API:运单退回登记修改
* 553 — returnWaybill.list API:运单退回登记列表
* 554 — returnWaybill.detail API:运单退回登记详情
* 555 — returnWaybill.delete API:运单退回登记删除
*
* 角色授权(系统预设角色,不含通配符角色):
* 作业类(551-552)→ 仓库组-员工(27) → 仓库组-主管(26)(仓库员工及以上)
* 查询/删除类(553-555)→ 客服组-员工(30) → 客服组-主管(28)(客服员工及以上)
* 经理(8)/仓库管理员(2)/超级管理员(7) 已持有 * 通配符,自动拥有所有权限,无需处理
*
* 幂等:INSERT IGNORE 创建权限记录;角色权限数组追加后去重排序,已存在跳过。
*/
const TABLE = 'wms_permission';
const ROLE_TABLE = 'wms_role';
const COLUMNS = 'id, path, component, name, redirect, meta_title, meta_icon, hidden, always_show, parent_id, type, sort, status, created_at, update_at';
/** 权限ID → 授权角色ID列表(不含通配符角色) */
const PERMISSION_ROLES = {
551: [ 27, 26 ],
552: [ 27, 26 ],
553: [ 30, 28 ],
554: [ 30, 28 ],
555: [ 30, 28 ],
};
module.exports = {
// eslint-disable-next-line no-unused-vars
async up(queryInterface, Sequelize) {
// ── 1. 创建 API 权限记录(固定 ID)──
const apis = [
[ 551, 'returnWaybill.create', 'API:运单退回登记' ],
[ 552, 'returnWaybill.update', 'API:运单退回登记修改' ],
[ 553, 'returnWaybill.list', 'API:运单退回登记列表' ],
[ 554, 'returnWaybill.detail', 'API:运单退回登记详情' ],
[ 555, 'returnWaybill.delete', 'API:运单退回登记删除' ],
];
for (const [ id, path, metaTitle ] of apis) {
await queryInterface.sequelize.query(
`INSERT IGNORE INTO \`${TABLE}\` (${COLUMNS}) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())`,
{ replacements: [ id, path, null, path, null, metaTitle, null, 0, 0, null, 'api', 0, 1 ] }
);
}
// ── 2. 角色授权注入 ──
for (const permId of Object.keys(PERMISSION_ROLES).map(Number)) {
for (const roleId of PERMISSION_ROLES[permId]) {
const rows = await queryInterface.sequelize.query(
`SELECT id, permissions FROM \`${ROLE_TABLE}\` WHERE id = ?`,
{ replacements: [ roleId ], type: queryInterface.sequelize.QueryTypes.SELECT }
);
const record = Array.isArray(rows) ? rows[0] : null;
if (!record) {
continue;
}
// 通配符角色跳过
if (record.permissions === '*') {
continue;
}
let perms;
try {
perms = JSON.parse(record.permissions || '[]');
} catch (err) {
continue;
}
if (!Array.isArray(perms)) {
continue;
}
if (perms.includes(permId)) {
continue;
}
perms.push(permId);
perms.sort((a, b) => a - b);
await queryInterface.sequelize.query(
`UPDATE \`${ROLE_TABLE}\` SET permissions = ? WHERE id = ?`,
{ replacements: [ JSON.stringify(perms), roleId ] }
);
}
}
},
// eslint-disable-next-line no-unused-vars
async down(queryInterface, Sequelize) {
const ids = [ 551, 552, 553, 554, 555 ];
const placeholders = ids.map(() => '?').join(', ');
await queryInterface.sequelize.query(
`DELETE FROM \`${TABLE}\` WHERE id IN (${placeholders})`,
{ replacements: ids }
);
},
};