WMS_MAIN/database/migrations/20260712-add-store-receiving-permission.js
liufumingren 3c53b3c8fa feat(storage): 入库/收货模块货主侧功能 (listStore, export)
- entry: 新增 listStore(货主维度列表)、export/exportTaskProgress(异步导出)

- entry: 新增 storeExport/storeExportTaskProgress(货主侧导出)

- receiving: 新增 getStoreReceivingList(货主维度收货单查询)

- receiving: 新增 export 异步导出 + fix 时间字段 created_at→updated_at

- 迁移: receiving_detail creator_id 去 zerofill + 货主收货权限
2026-07-27 03:47:49 +08:00

164 lines
5.5 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';
/**
* 货主侧收货单页面权限及 API 权限迁移。
*
* 变更内容:
* 1. 新增 web_pages 权限:收货单(货主) — 货主平台侧收货单列表页面
* 2. 新增 api 权限:account_platform.getStoreReceivingList — 货主侧收货单查询接口
* 3. 将上述权限授权给 货主管理员(store_admin_default, id=5) 角色
*
* 幂等设计:
* - 先查后插(按 path + type 唯一性判断)
* - 角色权限更新使用 JSON 数组合并去重
*/
const TABLE_PERMISSION = 'wms_permission';
const TABLE_ROLE = 'wms_role';
const STORE_ADMIN_ROLE_ID = 5;
/** @type {{path: string, name: string|null, meta_title: string, type: string, parent_id: number|null, sort: number}[]} */
const NEW_PERMISSIONS = [
{
path: 'receiving_owner',
name: 'OwnerReceiving',
meta_title: '收货单(货主)',
type: 'web_pages',
parent_id: 18, // 库存
sort: 0,
component: '@/views/storage/receiving_owner/index',
},
{
path: 'storage.receiving.getStoreReceivingList',
name: 'storage.receiving.getStoreReceivingList',
meta_title: '货主侧收货单列表查询',
type: 'api',
parent_id: null,
sort: 0,
component: null,
},
];
/**
* 获取角色当前权限 ID 数组。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口
* @param {number} roleId 角色ID
* @return {Promise<number[]>} 权限ID数组
*/
async function getRolePermissionIds(queryInterface, roleId) {
const [ rows ] = await queryInterface.sequelize.query(
`SELECT permissions FROM \`${TABLE_ROLE}\` WHERE id = :roleId`,
{ replacements: { roleId } }
);
if (!rows || rows.length === 0) return [];
const raw = rows[0].permissions;
if (!raw) return [];
try {
const parsed = JSON.parse(raw);
if (Array.isArray(parsed)) {
return parsed.map(Number).filter(n => Number.isFinite(n) && n > 0);
}
} catch (_) {
// ignore
}
return [];
}
/**
* 保存角色权限。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口
* @param {number} roleId 角色ID
* @param {number[]} permissionIds 权限ID数组
* @return {Promise<void>}
*/
async function saveRolePermissions(queryInterface, roleId, permissionIds) {
const unique = Array.from(new Set(permissionIds)).sort((a, b) => a - b);
const json = JSON.stringify(unique);
await queryInterface.sequelize.query(
`UPDATE \`${TABLE_ROLE}\` SET permissions = :permissions, updated_at = NOW() WHERE id = :roleId`,
{ replacements: { roleId, permissions: json } }
);
}
/**
* 正向迁移。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口
* @return {Promise<void>}
*/
module.exports.up = async queryInterface => {
const insertedIds = [];
for (const perm of NEW_PERMISSIONS) {
const [ existing ] = await queryInterface.sequelize.query(
`SELECT id FROM \`${TABLE_PERMISSION}\` WHERE path = :path AND type = :type LIMIT 1`,
{ replacements: { path: perm.path, type: perm.type } }
);
if (existing && existing.length > 0) {
console.log(`[Migration] 权限已存在,跳过: ${perm.path} (type=${perm.type}, id=${existing[0].id})`);
insertedIds.push(existing[0].id);
continue;
}
const [ result ] = await queryInterface.sequelize.query(
`INSERT INTO \`${TABLE_PERMISSION}\` (path, name, component, meta_title, type, parent_id, sort, status, created_at, update_at)
VALUES (:path, :name, :component, :meta_title, :type, :parent_id, :sort, 1, NOW(), NOW())`,
{ replacements: { ...perm } }
);
const newId = result;
console.log(`[Migration] 新增权限: ${perm.path} (type=${perm.type}, id=${newId})`);
insertedIds.push(newId);
}
// 更新 货主管理员 角色权限
const currentIds = await getRolePermissionIds(queryInterface, STORE_ADMIN_ROLE_ID);
const mergedIds = Array.from(new Set([ ...currentIds, ...insertedIds ])).sort((a, b) => a - b);
console.log(`[Migration] 货主管理员(id=${STORE_ADMIN_ROLE_ID}) 权限更新: [${currentIds.join(',')}] -> [${mergedIds.join(',')}]`);
await saveRolePermissions(queryInterface, STORE_ADMIN_ROLE_ID, mergedIds);
console.log(`[Migration] 完成:新增 ${insertedIds.length} 条权限,角色权限已更新`);
};
/**
* 回滚迁移。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口
* @return {Promise<void>}
*/
module.exports.down = async queryInterface => {
// 1. 从角色权限中移除新增的权限 ID
const pathsToRemove = NEW_PERMISSIONS.map(p => p.path);
const [ permRows ] = await queryInterface.sequelize.query(
`SELECT id FROM \`${TABLE_PERMISSION}\` WHERE path IN (:paths) AND type IN (:types)`,
{
replacements: {
paths: pathsToRemove,
types: NEW_PERMISSIONS.map(p => p.type),
},
}
);
const idsToRemove = permRows.map(r => r.id);
const currentIds = await getRolePermissionIds(queryInterface, STORE_ADMIN_ROLE_ID);
const filteredIds = currentIds.filter(id => !idsToRemove.includes(id));
await saveRolePermissions(queryInterface, STORE_ADMIN_ROLE_ID, filteredIds);
console.log(`[Migration Rollback] 角色权限已移除: ${idsToRemove.join(',')}`);
// 2. 删除新增的权限记录
for (const perm of NEW_PERMISSIONS) {
await queryInterface.sequelize.query(
`DELETE FROM \`${TABLE_PERMISSION}\` WHERE path = :path AND type = :type`,
{ replacements: { path: perm.path, type: perm.type } }
);
console.log(`[Migration Rollback] 已删除权限: ${perm.path}`);
}
};