WMS_MAIN/database/migrations/20260915100000-add-return-unpack-transfer-waybills-permission.js
liufumingren 8757dc6c2e feat(returnUnpack): 新增运单批量转入退件登记接口
- 新增 returnUnpack.transferByWaybills(批量入参 waybill_codes,trim+去重+过滤空值,上限 200,仓库隔离由 ctx.user.store_id 注入)
- 规则:命中运单统一置 wms_order_express.is_back='Y',不做其他前置条件限制;business_key>1 子单跳过
- 明细:按订单原本明细 1:1 复制(订单 items JSON 优先,为空回退 wms_order_items 分表;经订单索引表 wms_order_id_location_{warehouseId} 定位车道,复用 afterSale._resolveOrderDetail)
- 幂等:同仓库+同运单号 status != 'cancelled' 的拆包单直接复用(reused=true),已有明细不重复写入,复用单明细为空时按订单明细补写
- 重构:create 的取号+插主表抽为公共私有方法 _insertMainWithRetry(uk_unpack_no 冲突退避重试),create 与 transferByWaybills 共用
- 权限:新增迁移 20260915100000-add-return-unpack-transfer-waybills-permission(权限 ID 568,授权仓库组-员工 27 → 主管 26)
2026-09-15 15:38:50 +08:00

89 lines
2.9 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 568,当前迁移内最大已用 ID=567):
* 568 — returnUnpack.transferByWaybills API:运单批量转入退件登记
*
* 角色授权(系统预设角色,不含通配符角色):
* 作业类(568)→ 仓库组-员工(27) → 仓库组-主管(26)(仓库员工及以上)
* 经理(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 = {
568: [ 27, 26 ],
};
module.exports = {
// eslint-disable-next-line no-unused-vars
async up(queryInterface, Sequelize) {
// ── 1. 创建 API 权限记录(固定 ID)──
const apis = [
[ 568, 'returnUnpack.transferByWaybills', '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) {
await queryInterface.sequelize.query(
`DELETE FROM \`${TABLE}\` WHERE id = 568`
);
},
};