增加每日助理人数查询并调整登录及对账展示

This commit is contained in:
summer committed 2026-10-24 16:09:45 +08:00
1 parent 8a4d1df248
commit 8d9039aacf
9 files changed
+207 -43

No files matched your search

+38 -1
View File
@@ -60,6 +60,36 @@ test('助理两项独立于原人数,空白未记录与0区分,旧请求缺
assert.equal(f.ledger.rows('SELECT * FROM daily_people_counts').length,1);
});
test('新请求省略原人数只更新两位助理,新日兼容0且旧日原值保留,不由分项求和',t=>{
const f=fixture(t),input={date:D,assistant1Count:8,assistant2Count:0,revision:0},key=randomUUID();
assert.deepEqual(f.write(input,assistant,key),entry(D,0,1,8,0));assert.equal(f.ledger.one('SELECT count FROM daily_people_counts WHERE date=?',D).count,0);
f.save(D2,47,0);assert.deepEqual(f.write({date:D2,assistant1Count:10,assistant2Count:12,revision:1}),entry(D2,47,2,10,12));
assert.deepEqual(f.write({date:D2,assistant1Count:null,assistant2Count:' ',revision:2}),entry(D2,47,3));
assert.deepEqual(f.write({date:D2,assistant1Count:0,revision:3}),entry(D2,47,4,0,null));
const before=allContents(f.ledger);assert.deepEqual(f.write(input,assistant,key),entry(D,0,1,8,0));assert.equal(allContents(f.ledger),before);
assert.throws(()=>f.write({date:D2,assistant2Count:3,revision:3}),{status:409});assert.throws(()=>f.write({date:D2,count:null,assistant2Count:3,revision:4}),/人数/);assert.equal(allContents(f.ledger),before);
const audit=JSON.parse(f.ledger.one("SELECT detail FROM audit WHERE entity=? ORDER BY id DESC LIMIT 1",'people-count:'+D2).detail);assert.equal(audit.count,47);assert.equal(audit.previousCount,47);
assert.deepEqual(f.ledger.peopleCountSummary(D2,D),aggregate(D,D2,47,2,{total:8,recordedDays:2},{total:0,recordedDays:1}));
});
test('每日人数历史只查一次并逐日补齐闰日与缺录,零计记录天数,两位合计独立且不返回旧人数',t=>{
const f=fixture(t),from='2024-02-28',date='2024-03-02';
f.write({date:from,count:99,revision:0});f.write({date:'2024-02-29',assistant1Count:0,assistant2Count:3,revision:0});f.write({date,assistant1Count:5,assistant2Count:0,revision:0});
const before=allContents(f.ledger),changes=f.ledger.one('SELECT total_changes() AS count').count,originalRows=f.ledger.rows;let selects=0,result;
f.ledger.rows=function(sql,...args){if(sql.includes('FROM daily_people_counts'))selects++;return originalRows.call(this,sql,...args);};
try{result=f.ledger.peopleCountHistory({from,date});}finally{f.ledger.rows=originalRows;}
assert.equal(selects,1);assert.deepEqual(result.rows,[{date:from,assistant1Count:null,assistant2Count:null},{date:'2024-02-29',assistant1Count:0,assistant2Count:3},{date:'2024-03-01',assistant1Count:null,assistant2Count:null},{date,assistant1Count:5,assistant2Count:0}]);
assert.deepEqual(result.summary,{assistant1:{total:5,recordedDays:2},assistant2:{total:3,recordedDays:2}});assert.equal(result.today,today());assert.ok(result.rows.every(row=>!Object.hasOwn(row,'count')));assert.ok(!Object.hasOwn(result,'count'));
assert.equal(allContents(f.ledger),before);assert.equal(f.ledger.one('SELECT total_changes() AS count').count,changes);
});
test('每日人数默认本月至今天,含366天边界、同一天和空日,非法或过大日期范围不读写',t=>{
const f=fixture(t),current=today(),result=f.ledger.peopleCountHistory();assert.equal(result.from,current.slice(0,7)+'-01');assert.equal(result.date,current);assert.equal(result.rows.at(-1).date,current);assert.ok(result.rows.every(row=>row.date<=current&&row.assistant1Count===null&&row.assistant2Count===null));
assert.deepEqual(result.summary,{assistant1:{total:0,recordedDays:0},assistant2:{total:0,recordedDays:0}});assert.equal(f.ledger.peopleCountHistory({from:'2024-01-01',date:'2024-12-31'}).rows.length,366);
assert.deepEqual(f.ledger.peopleCountHistory({from:D,date:D}).rows,[{date:D,assistant1Count:null,assistant2Count:null}]);
const before=allContents(f.ledger);for(const query of [{from:'2024-01-01',date:'2025-01-01'},{from:D2,date:D},{from:'2023-02-29',date:'2023-03-01'},{from:'',date:D},{from:D,date:'2099-01-01'},{from:'1999-12-31',date:D}])assert.throws(()=>f.ledger.peopleCountHistory(query),{status:400});assert.equal(allContents(f.ledger),before);
});
test('助理人数只接受空白null或范围内整数,非法任一项和并发旧版本全部不写入',t=>{
const f=fixture(t);f.write({date:D,count:7,assistant1Count:1,assistant2Count:2,revision:0});const before=allContents(f.ledger);
for(const name of ['assistant1Count','assistant2Count'])for(const value of [undefined,true,false,[],{},'-1','0.1','1.0','1e3',Infinity,NaN,1000001,Number.MAX_SAFE_INTEGER+1])
@@ -96,7 +126,7 @@ test('人数登记修改不改变销售成本利润、收款、库存、财务
const f=fixture(t),input={date:D,customer:'合成客户',salesperson:'合成开单人',lines:[{name:'合成商品',unit:'g',quantity:'1',pocket:'',payer:'owner',cost:'80',sale:'100'}],fee_items:[]};
f.ledger.transact(randomUUID(),owner,input,()=>f.ledger.issueOrder(input,owner));f.ledger.transact(randomUUID(),owner,{date:D},()=>f.ledger.closeDay(D,owner,'orderDate'));
const tables=['orders','payments','expenses','after_sales','settlements','movements','journal','meta','sync','closes','inventory_snapshots'],before=contents(f.ledger,tables),r=f.ledger.reportByOrderDate(D),book=f.ledger.orderBook({date:D});
f.save(D,0,0);f.write({date:D,count:20,assistant1Count:0,assistant2Count:7,revision:1},assistant);f.write({date:D2,count:9,assistant1Count:100,assistant2Count:null,revision:0});
f.save(D,20,0);f.write({date:D,assistant1Count:0,assistant2Count:7,revision:1},assistant);f.write({date:D2,assistant1Count:100,assistant2Count:null,revision:0});
assert.equal(contents(f.ledger,tables),before);const after=f.ledger.reportByOrderDate(D);
assert.equal(after.reportRevision,r.reportRevision);assert.equal(after.close.stale,false);assert.equal(f.ledger.report(D).close.stale,false);assert.equal(f.ledger.inventorySnapshot(D).stale,false);
assert.deepEqual(after.summary,r.summary);assert.deepEqual(after.orderRows,r.orderRows);assert.deepEqual(after.accounts,r.accounts);assert.deepEqual(after.payerCosts,r.payerCosts);
@@ -145,6 +175,7 @@ test('HTTP人数须登录与CSRF,双方可改,相同版本并发只成功一
const base='http://127.0.0.1:'+app.server.address().port;let cookie='';
const call=(route,data,key=randomUUID(),origin=base)=>fetch(base+route,{method:data?'POST':'GET',headers:{...(cookie?{Cookie:cookie}:{}),...(data?{'Content-Type':'application/json',Origin:origin,'X-Requested-With':'maimai-ledger','Idempotency-Key':key}:{} )},...(data?{body:JSON.stringify(data)}:{})});
assert.equal((await call('/api/people-count?date='+D)).status,401);assert.equal((await call('/api/people-count',{date:D,count:0,revision:0})).status,401);
assert.equal((await call('/api/people-count-history?from='+D+'&date='+D3)).status,401);
let login=await call('/api/demo-login',{role:'assistant'});cookie=login.headers.get('set-cookie').split(';')[0];
assert.deepEqual(await (await call('/api/people-count')).json(),entry(today(),null,0));assert.equal(app.ledger.rows('SELECT * FROM daily_people_counts').length,0);
for(const date of ['2099-01-01','2026-02-30',''])assert.equal((await call('/api/people-count?date='+date)).status,400);
@@ -161,4 +192,10 @@ test('HTTP人数须登录与CSRF,双方可改,相同版本并发只成功一
const independent=await call('/api/people-count',{date:D,count:0,assistant1Count:8,assistant2Count:0,revision:3});assert.deepEqual(await independent.json(),entry(D,0,4,8,0));
const legacyWrite=await call('/api/people-count',{date:D,count:12,revision:4});assert.deepEqual(await legacyWrite.json(),entry(D,12,5,8,0));
const cleared=await call('/api/people-count',{date:D,count:12,assistant1Count:null,assistant2Count:'',revision:5});assert.deepEqual(await cleared.json(),entry(D,12,6));
const splitKey=randomUUID(),split={date:D,assistant1Count:0,assistant2Count:9,revision:6};const savedSplit=await call('/api/people-count',split,splitKey);assert.equal(savedSplit.status,200);assert.deepEqual(await savedSplit.json(),entry(D,12,7,0,9));
assert.deepEqual(await (await call('/api/people-count',split,splitKey)).json(),entry(D,12,7,0,9));
const newDay=await call('/api/people-count',{date:D2,assistant1Count:null,assistant2Count:0,revision:0});assert.equal(newDay.status,200);assert.deepEqual(await newDay.json(),entry(D2,0,1,null,0));
for(const role of ['owner','assistant']){
login=await call('/api/demo-login',{role});cookie=login.headers.get('set-cookie').split(';')[0];const before=allContents(app.ledger),response=await call('/api/people-count-history?from='+D+'&date='+D3);assert.equal(response.status,200);const history=await response.json();assert.equal(history.rows.length,3);assert.deepEqual(history.summary,{assistant1:{total:0,recordedDays:1},assistant2:{total:9,recordedDays:2}});assert.ok(history.rows.every(row=>!Object.hasOwn(row,'count')));assert.equal((await call('/api/people-count-history?from=2024-01-01&date=2025-01-01')).status,400);assert.equal(allContents(app.ledger),before);
}
});