支持订单归属修改与麦麦复核

This commit is contained in:
summer committed 2026-10-26 20:25:31 +08:00
1 parent ab9d3009ea
commit b3b67c8003
17 files changed
+829 -32

No files matched your search

+20 -5
View File
@@ -19,6 +19,7 @@ const OrderCancellations=require('./order-cancellations');
const OrderChanges=require('./order-changes');
const TeamMembers=require('./team-members');
const TeamFunds=require('./team-funds');
const OrderOwnership=require('./order-ownership');
const InventoryChanges=require('./inventory-changes');
const Recoveries=require('./recoveries');
// 仅服务器的新单生成流程能传入,客户端字段不能取得这个权限。
@@ -139,7 +140,7 @@ class Ledger {
OrderCancellations.initialize(this);
OrderChanges.initialize(this);
DailyFunds.initialize(this);
TeamMembers.initialize(this);TeamFunds.initialize(this);InventoryChanges.initialize(this);Recoveries.initialize(this);MaterialPurchaseChanges.initialize(this);
TeamMembers.initialize(this);TeamFunds.initialize(this);OrderOwnership.initialize(this);InventoryChanges.initialize(this);Recoveries.initialize(this);MaterialPurchaseChanges.initialize(this);
if(filename!==':memory:') fs.chmodSync(filename,0o600);
}
enableTeamMode(){
@@ -181,10 +182,16 @@ class Ledger {
this.db.exec('BEGIN IMMEDIATE');
try {
const prior=this.one('SELECT * FROM operations WHERE id=?',key);
if(prior){if(prior.hash!==hash)fail('这个操作号已用于其他内容,请重新操作',409);this.db.exec('COMMIT');return JSON.parse(prior.result);}
if(prior){if(prior.hash!==hash)fail('这个操作号已用于其他内容,请重新操作',409);const result=JSON.parse(prior.result);this.assertCachedOperationAccess(result,actor);this.db.exec('COMMIT');return result;}
const result=fn();this.syncTeamFunds(actor);this.run('INSERT INTO operations VALUES (?,?,?)',key,hash,JSON.stringify(result));this.db.exec('COMMIT');return result;
}catch(e){if(this.db.isTransaction)this.db.exec('ROLLBACK');throw e;}
}
assertCachedOperationAccess(result,actor){
if(!this.teamMode||actor?.role!=='assistant')return;
// 幂等结果可能含原订单、旧修改试算及其关联订单。失去归属后不可借重试读回旧快照。
const member=this.memberFor(actor),ids=new Set(JSON.stringify(result).match(/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/g)||[]);
for(const id of ids)if(this.one("SELECT entity_id FROM team_entities WHERE entity_id=? AND kind IN ('order','price_lock','movement') AND member<>? LIMIT 1",id,member))fail('记录不存在或无权访问',404);
}
touch(id){this.run('UPDATE orders SET revision=revision+1 WHERE id=?',id);this.enqueue('order:'+id);}
enqueue(entity){
const revision=entity.startsWith('order:')?this.one('SELECT revision FROM orders WHERE id=?',entity.slice(6)).revision:Math.max(Date.now(),(this.one('SELECT revision FROM sync WHERE entity=?',entity)?.revision||0)+1);
@@ -212,8 +219,16 @@ class Ledger {
requestOrderChange(id,input,actor){return OrderChanges.request(this,id,input,actor,priceLockEnv);}
approveOrderChange(id,input,actor){return OrderChanges.approve(this,id,input,actor,priceLockEnv);}
rejectOrderChange(id,input,actor){return OrderChanges.reject(this,id,input,actor,priceLockEnv);}
pendingOrderChange(id){return OrderChanges.pending(this,id);}
guardOrderChange(id,options){return OrderChanges.guard(this,id,options,priceLockEnv);}
pendingOrderChange(id){return OrderChanges.pending(this,id)||OrderOwnership.pending(this,id);}
guardOrderChange(id,options){OrderOwnership.guard(this,id,options,priceLockEnv);return OrderChanges.guard(this,id,options,priceLockEnv);}
orderOwnership(id,actor){return OrderOwnership.detail(this,id,actor,priceLockEnv);}
orderOwnershipChanges(query={},actor){return OrderOwnership.list(this,query,actor,priceLockEnv);}
orderOwnershipChange(id,actor){return OrderOwnership.read(this,id,actor,priceLockEnv);}
changeOrderOwnership(id,input,actor){return OrderOwnership.change(this,id,input,actor,priceLockEnv);}
approveOrderOwnership(id,input,actor){return OrderOwnership.approve(this,id,input,actor,priceLockEnv);}
rejectOrderOwnership(id,input,actor){return OrderOwnership.reject(this,id,input,actor,priceLockEnv);}
pendingOrderOwnership(id){return OrderOwnership.pending(this,id);}
orderOwnershipReportState(from,to){return OrderOwnership.reportState(this,from,to);}
orderCancellation(id,actor){return OrderCancellations.detail(this,id,actor,priceLockEnv);}
orderCancellations(query={},actor){return OrderCancellations.list(this,query,actor,priceLockEnv);}
orderCancellationRequest(id,actor){return OrderCancellations.read(this,id,actor,priceLockEnv);}
@@ -224,7 +239,7 @@ class Ledger {
guardOrderCancellation(id,options){return OrderCancellations.guard(this,id,options,priceLockEnv);}
detail(id){
const o=this.rawOrder(id);
Object.assign(o,OrderCancellations.projection(this,id),OrderChanges.projection(this,id));
Object.assign(o,OrderCancellations.projection(this,id),OrderChanges.projection(this,id),OrderOwnership.projection(this,id));
o.payments=this.rows('SELECT * FROM payments WHERE order_id=? ORDER BY date,created',id);
const priceLock=this.one('SELECT id,buy_payer,delivery FROM price_locks WHERE final_order_id=?',id);
if(priceLock){o.price_lock_id=priceLock.id;o.payments.push(...PriceLocks.inheritedPayments(this,id));o.payments.sort((a,b)=>a.date.localeCompare(b.date)||a.created.localeCompare(b.created));}