路由表化:迁移订单及订单修改/归属/作废/售后审批路由
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
900fc2f527
commit
babb914c59
5 files changed
+219
-116
No files matched your search
-115
@@ -277,20 +277,6 @@ function createApp(options = {}) {
|
||||
return send(res, 200, actorLedger.materialPurchaseChange(materialChangeMatch[1], actor));
|
||||
}
|
||||
const materialChangeRequest = route.match(/^\/api\/material-purchases\/([a-f0-9-]{36})\/change$/);
|
||||
if (route === '/api/after-sales/changes' && !isPost)
|
||||
return send(res, 200, actorLedger.afterSaleChanges(Object.fromEntries(url.searchParams), actor));
|
||||
const afterSaleChangeMatch = route.match(
|
||||
/^\/api\/after-sales\/changes\/([a-f0-9-]{36})(?:\/(approve|reject))?$/,
|
||||
);
|
||||
if (afterSaleChangeMatch && !isPost) {
|
||||
if (afterSaleChangeMatch[2]) fail('地址不存在', 404);
|
||||
return send(res, 200, actorLedger.afterSaleChange(afterSaleChangeMatch[1], actor));
|
||||
}
|
||||
const afterSaleMatch = route.match(/^\/api\/after-sales\/([a-f0-9-]{36})(?:\/(update))?$/);
|
||||
if (afterSaleMatch && !isPost) {
|
||||
if (afterSaleMatch[2]) fail('地址不存在', 404);
|
||||
return send(res, 200, actorLedger.afterSale(afterSaleMatch[1], actor));
|
||||
}
|
||||
if (route === '/api/customer-materials' && !isPost)
|
||||
return send(res, 200, actorLedger.customerMaterials(Object.fromEntries(url.searchParams), actor));
|
||||
if (route === '/api/recoveries' && !isPost) {
|
||||
@@ -351,28 +337,6 @@ function createApp(options = {}) {
|
||||
if (lockChangeMatch[2]) fail('地址不存在', 404);
|
||||
return send(res, 200, actorLedger.priceLockChange(lockChangeMatch[1], actor));
|
||||
}
|
||||
if (route === '/api/order-note-tags' && !isPost) return send(res, 200, OrderNoteTags.list(actorLedger, actor));
|
||||
if (route === '/api/order-changes' && !isPost)
|
||||
return send(res, 200, actorLedger.orderChanges(Object.fromEntries(url.searchParams), actor));
|
||||
if (route === '/api/order-ownership-changes' && !isPost)
|
||||
return send(res, 200, actorLedger.orderOwnershipChanges(Object.fromEntries(url.searchParams), actor));
|
||||
const ownershipMatch = route.match(/^\/api\/order-ownership-changes\/([a-f0-9-]{36})(?:\/(approve|reject))?$/);
|
||||
if (ownershipMatch && !isPost) {
|
||||
if (ownershipMatch[2]) fail('地址不存在', 404);
|
||||
return send(res, 200, actorLedger.orderOwnershipChange(ownershipMatch[1], actor));
|
||||
}
|
||||
const changeMatch = route.match(/^\/api\/order-changes\/([a-f0-9-]{36})(?:\/(approve|reject))?$/);
|
||||
if (changeMatch && !isPost) {
|
||||
if (changeMatch[2]) fail('地址不存在', 404);
|
||||
return send(res, 200, actorLedger.orderChangeRequest(changeMatch[1], actor));
|
||||
}
|
||||
if (route === '/api/order-cancellations' && !isPost)
|
||||
return send(res, 200, actorLedger.orderCancellations(Object.fromEntries(url.searchParams), actor));
|
||||
const cancellationMatch = route.match(/^\/api\/order-cancellations\/([a-f0-9-]{36})(?:\/(approve|reject))?$/);
|
||||
if (cancellationMatch && !isPost) {
|
||||
if (cancellationMatch[2]) fail('地址不存在', 404);
|
||||
return send(res, 200, actorLedger.orderCancellationRequest(cancellationMatch[1], actor));
|
||||
}
|
||||
const lockMatch = route.match(
|
||||
/^\/api\/price-locks\/([a-f0-9-]+)(?:\/(deliver|edit-context|edit|cancel|change-requests))?$/,
|
||||
);
|
||||
@@ -418,41 +382,8 @@ function createApp(options = {}) {
|
||||
}
|
||||
if (route === '/api/inventory/snapshots' && !isPost)
|
||||
return send(res, 200, actorLedger.inventorySnapshots(Object.fromEntries(url.searchParams)));
|
||||
if (route === '/api/order-book' && !isPost) {
|
||||
const book = actorLedger.orderBook(Object.fromEntries(url.searchParams));
|
||||
if (actor.role !== 'owner') book.peopleCount = null;
|
||||
return send(res, 200, book);
|
||||
}
|
||||
if (route === '/api/order-templates' && !isPost)
|
||||
return send(res, 200, actorLedger.orderTemplates(Object.fromEntries(url.searchParams)));
|
||||
const templateMatch = route.match(/^\/api\/order-templates\/([a-f0-9-]{36})(?:\/(update|delete))?$/);
|
||||
if (templateMatch && !isPost) {
|
||||
if (templateMatch[2]) fail('地址不存在', 404);
|
||||
return send(res, 200, actorLedger.orderTemplate(templateMatch[1]));
|
||||
}
|
||||
if (route === '/api/returned-stock-lots' && !isPost)
|
||||
return send(res, 200, actorLedger.returnedStockLots(Object.fromEntries(url.searchParams), actor));
|
||||
if (route === '/api/paper-number' && !isPost)
|
||||
return send(res, 200, { number: actorLedger.paperNumber(url.searchParams.get('date') || today()) });
|
||||
const orderAfterSaleMatch = route.match(/^\/api\/orders\/([a-f0-9-]+)\/(after-sale|after-sale-preview)$/);
|
||||
if (orderAfterSaleMatch && !isPost) {
|
||||
if (orderAfterSaleMatch[2] !== 'after-sale') fail('地址不存在', 404);
|
||||
return send(res, 200, actorLedger.orderAfterSale(orderAfterSaleMatch[1], actor));
|
||||
}
|
||||
const match = route.match(/^\/api\/orders\/([a-f0-9-]+)(?:\/([a-z]+))?$/);
|
||||
if (match && !isPost) {
|
||||
if (match[2] === 'public') return send(res, 200, actorLedger.publicOrder(match[1]));
|
||||
if (match[2] === 'edit') return send(res, 200, actorLedger.orderEdit(match[1], actor));
|
||||
if (match[2] === 'change') return send(res, 200, actorLedger.orderChange(match[1], actor));
|
||||
if (match[2] === 'ownership') return send(res, 200, actorLedger.orderOwnership(match[1], actor));
|
||||
if (match[2] === 'cancellation') return send(res, 200, actorLedger.orderCancellation(match[1], actor));
|
||||
if (!match[2])
|
||||
return send(res, 200, {
|
||||
...actorLedger.detail(match[1]),
|
||||
printStatus: PrintStatus.projection(actorLedger, 'order', match[1]),
|
||||
});
|
||||
fail('地址不存在', 404);
|
||||
}
|
||||
if (route === '/api/report' && !isPost) {
|
||||
const report = actorLedger.reportFor(Object.fromEntries(url.searchParams), actor);
|
||||
if (actor.role !== 'owner') report.peopleCount = null;
|
||||
@@ -511,12 +442,6 @@ function createApp(options = {}) {
|
||||
// 读取请求体期间账号可能已被停用或重置密码,写入前再次核验会话。
|
||||
const checkedActor = user(req);
|
||||
if (!checkedActor || checkedActor.id !== actor.id) fail('登录已失效,请重新登录', 401);
|
||||
if (orderAfterSaleMatch?.[2] === 'after-sale-preview')
|
||||
return send(
|
||||
res,
|
||||
200,
|
||||
OrderChanges.previewAfterSale(actorLedger, orderAfterSaleMatch[1], b, actor, actorLedger.businessEnv()),
|
||||
);
|
||||
if (route === '/api/material-pools/quote') return send(res, 200, actorLedger.materialPoolQuote(b, actor));
|
||||
if (route === '/api/recoveries/quote') return send(res, 200, actorLedger.recoveryQuote(b, actor));
|
||||
if (recoveryMatch?.[2] === 'quote')
|
||||
@@ -534,15 +459,6 @@ function createApp(options = {}) {
|
||||
return actorLedger.approveRecoveryChange(recoveryChangeMatch[1], b, actor);
|
||||
if (recoveryChangeMatch?.[2] === 'reject')
|
||||
return actorLedger.rejectRecoveryChange(recoveryChangeMatch[1], b, actor);
|
||||
if (route === '/api/order-templates') return actorLedger.saveOrderTemplate(null, b, actor);
|
||||
if (templateMatch && templateMatch[2] === 'update')
|
||||
return actorLedger.saveOrderTemplate(templateMatch[1], b, actor);
|
||||
if (templateMatch && templateMatch[2] === 'delete')
|
||||
return actorLedger.deleteOrderTemplate(templateMatch[1], b, actor);
|
||||
if (route === '/api/orders/issue')
|
||||
return actorLedger.issueOrder(OrderNoteTags.submission(actorLedger, b, actor, b.id), actor);
|
||||
if (route === '/api/orders')
|
||||
return actorLedger.saveOrder(OrderNoteTags.submission(actorLedger, b, actor, b.id), actor);
|
||||
if (route === '/api/material-purchases') return actorLedger.createMaterialPurchase(b, actor);
|
||||
if (route === '/api/material-pools/rolling-enable') return actorLedger.enableRollingMaterial(b, actor);
|
||||
if (materialChangeRequest)
|
||||
@@ -567,37 +483,6 @@ function createApp(options = {}) {
|
||||
return actorLedger.approvePriceLockChange(lockChangeMatch[1], b, actor);
|
||||
if (lockChangeMatch && lockChangeMatch[2] === 'reject')
|
||||
return actorLedger.rejectPriceLockChange(lockChangeMatch[1], b, actor);
|
||||
if (route === '/api/after-sales') return actorLedger.saveAfterSale(b, actor);
|
||||
if (afterSaleMatch && afterSaleMatch[2] === 'update')
|
||||
return actorLedger.updateAfterSale(afterSaleMatch[1], b, actor);
|
||||
if (afterSaleChangeMatch && afterSaleChangeMatch[2] === 'approve')
|
||||
return actorLedger.approveAfterSaleChange(afterSaleChangeMatch[1], b, actor);
|
||||
if (afterSaleChangeMatch && afterSaleChangeMatch[2] === 'reject')
|
||||
return actorLedger.rejectAfterSaleChange(afterSaleChangeMatch[1], b, actor);
|
||||
if (changeMatch && changeMatch[2] === 'approve')
|
||||
return actorLedger.approveOrderChange(changeMatch[1], b, actor);
|
||||
if (changeMatch && changeMatch[2] === 'reject')
|
||||
return actorLedger.rejectOrderChange(changeMatch[1], b, actor);
|
||||
if (ownershipMatch && ownershipMatch[2] === 'approve')
|
||||
return actorLedger.approveOrderOwnership(ownershipMatch[1], b, actor);
|
||||
if (ownershipMatch && ownershipMatch[2] === 'reject')
|
||||
return actorLedger.rejectOrderOwnership(ownershipMatch[1], b, actor);
|
||||
if (match && match[2] === 'ownership') return actorLedger.changeOrderOwnership(match[1], b, actor);
|
||||
if (match && match[2] === 'change')
|
||||
return actorLedger.requestOrderChange(
|
||||
match[1],
|
||||
(b.action ?? 'edit') === 'edit' ? OrderNoteTags.submission(actorLedger, b, actor, match[1]) : b,
|
||||
actor,
|
||||
);
|
||||
if (cancellationMatch && cancellationMatch[2] === 'approve')
|
||||
return actorLedger.approveOrderCancellation(cancellationMatch[1], b, actor);
|
||||
if (cancellationMatch && cancellationMatch[2] === 'reject')
|
||||
return actorLedger.rejectOrderCancellation(cancellationMatch[1], b, actor);
|
||||
if (match && match[2] === 'cancellation') return actorLedger.requestOrderCancellation(match[1], b, actor);
|
||||
if (match && match[2] === 'canceltoday') return actorLedger.cancelOrderToday(match[1], b, actor);
|
||||
if (match && match[2] === 'edit')
|
||||
return actorLedger.editOrder(match[1], OrderNoteTags.submission(actorLedger, b, actor, match[1]), actor);
|
||||
if (match && match[2]) return actorLedger.action(match[1], match[2], b, actor);
|
||||
if (route === '/api/stock') return actorLedger.stock(b, actor);
|
||||
if (inventoryMovementMatch && inventoryMovementMatch[2] === 'update')
|
||||
return actorLedger.updateInventoryMovement(inventoryMovementMatch[1], b, actor);
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
'use strict';
|
||||
// 全部路由按声明顺序匹配;同一请求方式下路径互有重叠的路由都在同一文件内按先具体后通配排列。
|
||||
module.exports = [...require('./shop.routes'), ...require('./funds.routes'), ...require('./print.routes')];
|
||||
module.exports = [
|
||||
...require('./shop.routes'),
|
||||
...require('./orders.routes'),
|
||||
...require('./order-changes.routes'),
|
||||
...require('./funds.routes'),
|
||||
...require('./print.routes'),
|
||||
];
|
||||
@@ -0,0 +1,47 @@
|
||||
'use strict';
|
||||
// 订单修改/归属/作废申请与审批,售后单及售后修改审批。
|
||||
const { reviewRoutes } = require('./review');
|
||||
|
||||
module.exports = [
|
||||
...reviewRoutes('/api/order-changes', {
|
||||
list: 'orderChanges',
|
||||
one: 'orderChangeRequest',
|
||||
approve: 'approveOrderChange',
|
||||
reject: 'rejectOrderChange',
|
||||
}),
|
||||
...reviewRoutes('/api/order-ownership-changes', {
|
||||
list: 'orderOwnershipChanges',
|
||||
one: 'orderOwnershipChange',
|
||||
approve: 'approveOrderOwnership',
|
||||
reject: 'rejectOrderOwnership',
|
||||
}),
|
||||
...reviewRoutes('/api/order-cancellations', {
|
||||
list: 'orderCancellations',
|
||||
one: 'orderCancellationRequest',
|
||||
approve: 'approveOrderCancellation',
|
||||
reject: 'rejectOrderCancellation',
|
||||
}),
|
||||
...reviewRoutes('/api/after-sales/changes', {
|
||||
list: 'afterSaleChanges',
|
||||
one: 'afterSaleChange',
|
||||
approve: 'approveAfterSaleChange',
|
||||
reject: 'rejectAfterSaleChange',
|
||||
}),
|
||||
{
|
||||
method: 'POST',
|
||||
path: '/api/after-sales',
|
||||
write: true,
|
||||
handler: ({ ledger, body, actor }) => ledger.saveAfterSale(body, actor),
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/api/after-sales/:id(uuid)',
|
||||
handler: ({ ledger, params: { id }, actor }) => ledger.afterSale(id, actor),
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
path: '/api/after-sales/:id(uuid)/update',
|
||||
write: true,
|
||||
handler: ({ ledger, params: { id }, body, actor }) => ledger.updateAfterSale(id, body, actor),
|
||||
},
|
||||
];
|
||||
@@ -0,0 +1,145 @@
|
||||
'use strict';
|
||||
// 订单、订单簿、模板、备注标签、纸质单号。
|
||||
const OrderChanges = require('../../order-changes');
|
||||
const OrderNoteTags = require('../../order-note-tags');
|
||||
const PrintStatus = require('../../print-status');
|
||||
const { today } = require('../../ledger');
|
||||
|
||||
const ORDER = '/api/orders/:id(hexid)';
|
||||
// 编辑类提交先经备注标签整理;id 为订单号(新建时取请求体里的 id)。
|
||||
const submission = ({ ledger, body, actor }, id) => OrderNoteTags.submission(ledger, body, actor, id);
|
||||
|
||||
module.exports = [
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/api/order-book',
|
||||
handler: ({ ledger, query, actor }) => {
|
||||
const book = ledger.orderBook(query);
|
||||
if (actor.role !== 'owner') book.peopleCount = null;
|
||||
return book;
|
||||
},
|
||||
},
|
||||
{ method: 'GET', path: '/api/order-templates', handler: ({ ledger, query }) => ledger.orderTemplates(query) },
|
||||
{
|
||||
method: 'POST',
|
||||
path: '/api/order-templates',
|
||||
write: true,
|
||||
handler: ({ ledger, body, actor }) => ledger.saveOrderTemplate(null, body, actor),
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/api/order-templates/:id(uuid)',
|
||||
handler: ({ ledger, params: { id } }) => ledger.orderTemplate(id),
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
path: '/api/order-templates/:id(uuid)/update',
|
||||
write: true,
|
||||
handler: ({ ledger, params: { id }, body, actor }) => ledger.saveOrderTemplate(id, body, actor),
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
path: '/api/order-templates/:id(uuid)/delete',
|
||||
write: true,
|
||||
handler: ({ ledger, params: { id }, body, actor }) => ledger.deleteOrderTemplate(id, body, actor),
|
||||
},
|
||||
{ method: 'GET', path: '/api/order-note-tags', handler: ({ ledger, actor }) => OrderNoteTags.list(ledger, actor) },
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/api/paper-number',
|
||||
handler: ({ ledger, url }) => ({ number: ledger.paperNumber(url.searchParams.get('date') || today()) }),
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: ORDER + '/after-sale',
|
||||
handler: ({ ledger, params: { id }, actor }) => ledger.orderAfterSale(id, actor),
|
||||
},
|
||||
{
|
||||
// 试算:读请求体、复核会话后直接返回,不进入事务。
|
||||
method: 'POST',
|
||||
path: ORDER + '/after-sale-preview',
|
||||
handler: ({ ledger, params: { id }, body, actor }) =>
|
||||
OrderChanges.previewAfterSale(ledger, id, body, actor, ledger.businessEnv()),
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: ORDER,
|
||||
handler: ({ ledger, params: { id } }) => ({
|
||||
...ledger.detail(id),
|
||||
printStatus: PrintStatus.projection(ledger, 'order', id),
|
||||
}),
|
||||
},
|
||||
{ method: 'GET', path: ORDER + '/public', handler: ({ ledger, params: { id } }) => ledger.publicOrder(id) },
|
||||
{ method: 'GET', path: ORDER + '/edit', handler: ({ ledger, params: { id }, actor }) => ledger.orderEdit(id, actor) },
|
||||
{
|
||||
method: 'GET',
|
||||
path: ORDER + '/change',
|
||||
handler: ({ ledger, params: { id }, actor }) => ledger.orderChange(id, actor),
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: ORDER + '/ownership',
|
||||
handler: ({ ledger, params: { id }, actor }) => ledger.orderOwnership(id, actor),
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: ORDER + '/cancellation',
|
||||
handler: ({ ledger, params: { id }, actor }) => ledger.orderCancellation(id, actor),
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
path: '/api/orders/issue',
|
||||
write: true,
|
||||
handler: ctx => ctx.ledger.issueOrder(submission(ctx, ctx.body.id), ctx.actor),
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
path: '/api/orders',
|
||||
write: true,
|
||||
handler: ctx => ctx.ledger.saveOrder(submission(ctx, ctx.body.id), ctx.actor),
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
path: ORDER + '/ownership',
|
||||
write: true,
|
||||
handler: ({ ledger, params: { id }, body, actor }) => ledger.changeOrderOwnership(id, body, actor),
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
path: ORDER + '/change',
|
||||
write: true,
|
||||
handler: ctx => {
|
||||
const id = ctx.params.id;
|
||||
return ctx.ledger.requestOrderChange(
|
||||
id,
|
||||
(ctx.body.action ?? 'edit') === 'edit' ? submission(ctx, id) : ctx.body,
|
||||
ctx.actor,
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
path: ORDER + '/cancellation',
|
||||
write: true,
|
||||
handler: ({ ledger, params: { id }, body, actor }) => ledger.requestOrderCancellation(id, body, actor),
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
path: ORDER + '/canceltoday',
|
||||
write: true,
|
||||
handler: ({ ledger, params: { id }, body, actor }) => ledger.cancelOrderToday(id, body, actor),
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
path: ORDER + '/edit',
|
||||
write: true,
|
||||
handler: ctx => ctx.ledger.editOrder(ctx.params.id, submission(ctx, ctx.params.id), ctx.actor),
|
||||
},
|
||||
{
|
||||
// 其余订单动作(confirm、receipt、refund 等);须排在上面各具体动作之后。
|
||||
method: 'POST',
|
||||
path: ORDER + '/:action(word)',
|
||||
write: true,
|
||||
handler: ({ ledger, params: { id, action }, body, actor }) => ledger.action(id, action, body, actor),
|
||||
},
|
||||
];
|
||||
@@ -0,0 +1,20 @@
|
||||
'use strict';
|
||||
// 审批类资源的三条标准路由:申请列表、单条详情、approve/reject 审批(写入事务内)。
|
||||
function reviewRoutes(base, { list, one, approve, reject }) {
|
||||
return [
|
||||
{ method: 'GET', path: base, handler: ({ ledger, query, actor }) => ledger[list](query, actor) },
|
||||
{
|
||||
method: 'GET',
|
||||
path: base + '/:id(uuid)',
|
||||
handler: ({ ledger, params: { id }, actor }) => ledger[one](id, actor),
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
path: base + '/:id(uuid)/:decision(approve|reject)',
|
||||
write: true,
|
||||
handler: ({ ledger, params: { id, decision }, body, actor }) =>
|
||||
ledger[decision === 'approve' ? approve : reject](id, body, actor),
|
||||
},
|
||||
];
|
||||
}
|
||||
module.exports = { reviewRoutes };
|
||||
Reference in new issue
Block a user