|
@@ -22,12 +22,21 @@ async function call(params,role='super-admin',company='co'){
|
|
|
before(async()=>{
|
|
before(async()=>{
|
|
|
await db.exec(readFileSync(new URL('../sql/legacy-direct-sync.sql',import.meta.url),'utf8'));
|
|
await db.exec(readFileSync(new URL('../sql/legacy-direct-sync.sql',import.meta.url),'utf8'));
|
|
|
await db.exec(readFileSync(new URL('../sql/legacy-credit-balances.sql',import.meta.url),'utf8'));
|
|
await db.exec(readFileSync(new URL('../sql/legacy-credit-balances.sql',import.meta.url),'utf8'));
|
|
|
- await db.exec('CREATE TABLE "CommonModel" ("objectId" text PRIMARY KEY,company text,"sourceKey" text,"generalId" numeric,"itemId" numeric,"modelId" numeric,"createdAt" timestamptz,"updatedAt" timestamptz,status numeric,"teachingManaged" boolean,title text)');
|
|
|
|
|
|
|
+ await db.exec('CREATE TABLE "CommonModel" ("objectId" text PRIMARY KEY,company text,"sourceKey" text,"generalId" numeric,"itemId" numeric,"modelId" numeric,"createdAt" timestamptz,"updatedAt" timestamptz,status numeric,"teachingManaged" boolean,title text,subtitle text)');
|
|
|
for(const [m,s] of Object.entries(specs)){
|
|
for(const [m,s] of Object.entries(specs)){
|
|
|
const fields=legacyAddonFields[m]||(m==='52'?['sy','yb','lj','yp']:['content','synopsis','mp4','ico','author','source']);
|
|
const fields=legacyAddonFields[m]||(m==='52'?['sy','yb','lj','yp']:['content','synopsis','mp4','ico','author','source']);
|
|
|
await db.exec('CREATE TABLE "'+s.className+'" ("objectId" text PRIMARY KEY,company text,id numeric,"sourceKey" text,"createdAt" timestamptz,"updatedAt" timestamptz,"teachingManaged" boolean,'+fields.map(f=>'"'+f+'" text').join(',')+')');
|
|
await db.exec('CREATE TABLE "'+s.className+'" ("objectId" text PRIMARY KEY,company text,id numeric,"sourceKey" text,"createdAt" timestamptz,"updatedAt" timestamptz,"teachingManaged" boolean,'+fields.map(f=>'"'+f+'" text').join(',')+')');
|
|
|
}
|
|
}
|
|
|
await db.exec('ALTER TABLE "MemoryPracticeRecord" ADD "rewardTaskKey" text, ADD "rewardDuplicate" boolean, ADD "assignmentVersion" numeric DEFAULT 0');
|
|
await db.exec('ALTER TABLE "MemoryPracticeRecord" ADD "rewardTaskKey" text, ADD "rewardDuplicate" boolean, ADD "assignmentVersion" numeric DEFAULT 0');
|
|
|
|
|
+ await db.exec(`ALTER TABLE "CourseAppointment" ADD "categoryEvidence" text, ADD "courseCategoryName" text, ADD "teachingRuleId" text,
|
|
|
|
|
+ ADD "teacherPaySnapshot" numeric, ADD "creditCostsSnapshot" jsonb, ADD "creditHoldStatus" text, ADD "bindingId" text, ADD "courseCategoryKey" text,
|
|
|
|
|
+ ADD "durationMinutes" numeric, ADD "deliveryMode" text;
|
|
|
|
|
+ CREATE TABLE "TeachingCourseCategory"(company text,key text,active boolean);
|
|
|
|
|
+ CREATE TABLE "TeachingCompensationRule"(company text,"objectId" text,"categoryKey" text,"durationMinutes" numeric,"deliveryMode" text,"effectiveFrom" text,active boolean,"teacherPay" numeric,"creditCosts" jsonb,revision integer,"createdAt" timestamptz DEFAULT now());
|
|
|
|
|
+ CREATE TABLE "LessonRecordFact"(company text,yyds text);
|
|
|
|
|
+ CREATE TABLE "LessonCompletionReport"(company text,"appointmentId" text,"appointmentGeneralId" numeric);
|
|
|
|
|
+ CREATE TABLE "WritingAudit"(target text,"before" jsonb,"after" jsonb);
|
|
|
|
|
+ CREATE FUNCTION xs_payroll_audit(text,text,text,text,text,jsonb,jsonb) RETURNS void LANGUAGE sql AS $$ INSERT INTO "WritingAudit" VALUES($4,$6,$7) $$`);
|
|
|
const rewardSource=readFileSync(new URL('../sql/review-rewards.sql',import.meta.url),'utf8');await db.exec(rewardSource.match(/CREATE OR REPLACE FUNCTION xiaoshu_review_key[\s\S]*?\$\$;/)[0]);await db.exec(rewardSource.match(/CREATE OR REPLACE FUNCTION xiaoshu_safe_shanghai_timestamp[\s\S]*?\$\$;/)[0]);
|
|
const rewardSource=readFileSync(new URL('../sql/review-rewards.sql',import.meta.url),'utf8');await db.exec(rewardSource.match(/CREATE OR REPLACE FUNCTION xiaoshu_review_key[\s\S]*?\$\$;/)[0]);await db.exec(rewardSource.match(/CREATE OR REPLACE FUNCTION xiaoshu_safe_shanghai_timestamp[\s\S]*?\$\$;/)[0]);
|
|
|
await db.exec(`CREATE TABLE "ReviewReward" ("objectId" text,company text,"taskId" text,amount numeric,status text,
|
|
await db.exec(`CREATE TABLE "ReviewReward" ("objectId" text,company text,"taskId" text,amount numeric,status text,
|
|
|
"coachId" numeric,"studentId" numeric,"plannedDate" text,"completedAt" timestamptz,"completionRole" text,"completedBy" text);
|
|
"coachId" numeric,"studentId" numeric,"plannedDate" text,"completedAt" timestamptz,"completionRole" text,"completedBy" text);
|
|
@@ -321,3 +330,107 @@ test('旧预约换编号或改到新系统时段,原子保留新课与时长
|
|
|
assert.equal((await run(append(8741))).postimages[0].common.status,99);
|
|
assert.equal((await run(append(8741))).postimages[0].common.status,99);
|
|
|
assert.deepEqual(await native(),before);
|
|
assert.deepEqual(await native(),before);
|
|
|
});
|
|
});
|
|
|
|
|
+
|
|
|
|
|
+test('new 398 appointments confirm and price on sync only with consistent confirmed same-year evidence',async()=>{
|
|
|
|
|
+ await sql("UPDATE \"LegacyDirectRun\" SET status='running',\"leaseUntil\"=now()+interval '5 minutes' WHERE \"runId\"=$1",[runId]);
|
|
|
|
|
+ const now=Date.now(), day=offset=>new Date(now+offset*86400000).toLocaleDateString('sv-SE',{timeZone:'Asia/Shanghai'});
|
|
|
|
|
+ const yearOf=date=>{const [year,month]=date.split('-').map(Number);return month>=9?year:year-1;};
|
|
|
|
|
+ const schoolYear=yearOf(day(0)), incomingDay=yearOf(day(2))===schoolYear?day(2):day(0);
|
|
|
|
|
+ await db.exec(`CREATE TABLE IF NOT EXISTS "_User" ("objectId" text PRIMARY KEY,company text,username text,"legacyUserId" numeric,"legacyGroupId" numeric,"legacyUserData" jsonb);
|
|
|
|
|
+ CREATE TABLE IF NOT EXISTS "CourseCreditReservation"(company text,"appointmentId" text);
|
|
|
|
|
+ ALTER TABLE "_User" ADD COLUMN IF NOT EXISTS nickname text,
|
|
|
|
|
+ ADD COLUMN IF NOT EXISTS "isDeleted" boolean, ADD COLUMN IF NOT EXISTS "isDisabled" boolean;
|
|
|
|
|
+ INSERT INTO "_User"("objectId",company,username,"legacyUserId","legacyGroupId",nickname) VALUES
|
|
|
|
|
+ ('stage-early','co','stage-early',8801,1,'小明初二'),
|
|
|
|
|
+ ('stage-conflict','co','stage-conflict',8802,1,'小明高一'),
|
|
|
|
|
+ ('stage-profile-only','co','stage-profile-only',8803,1,'小明初三'),
|
|
|
|
|
+ ('stage-expired','co','stage-expired',8804,1,'小明初三');
|
|
|
|
|
+ `);
|
|
|
|
|
+ async function prior(id, student, date, evidence='人工核实初中'){
|
|
|
|
|
+ await sql('INSERT INTO "CourseAppointment"("objectId",company,id,kcid,szyh,yysj,"courseCategoryKey","categoryEvidence") VALUES($1,$2,$3,\'398\',$4,$5,\'middle_writing\',$6)',['history-'+id,'co',id,student,date,evidence]);
|
|
|
|
|
+ await sql('INSERT INTO "CommonModel"("objectId",company,"modelId","itemId","generalId",status) VALUES($1,$2,54,$3,$3,99)',['history-common-'+id,'co',id]);
|
|
|
|
|
+ }
|
|
|
|
|
+ const oldMonth='09-03 18:00';
|
|
|
|
|
+ await prior(8801,'8801',schoolYear+'-'+oldMonth);
|
|
|
|
|
+ await prior(8802,'8802',schoolYear+'-'+oldMonth);
|
|
|
|
|
+ await prior(8803,'8803',schoolYear+'-'+oldMonth,'同步自动核对:初中');
|
|
|
|
|
+ await prior(8804,'8804',(schoolYear-1)+'-'+oldMonth);
|
|
|
|
|
+ const append=(id,student,values={})=>({generalId:id,common:{objectId:'write-c'+id,status:99,subtitle:'阅读写作'},addon:{
|
|
|
|
|
+ objectId:'write-a'+id,id,szyh:String(student),pl:'76',kcid:'398',yykcid:'398',dslx:'2',dszt:'0',jffs:'线上',
|
|
|
|
|
+ bxrq:incomingDay,yysj:incomingDay+' 21:00',...values}});
|
|
|
|
|
+ const run=async request=>{const r=await call(request);assert.equal(r.success,true,r.message);return r.data;};
|
|
|
|
|
+ const request={action:'append',modelId:54,batchKey:'sync-writing-stage-append',rows:[
|
|
|
|
|
+ append(8901,8801),append(8902,8802),append(8903,8803),append(8904,8804),append(8905,8801,{dszt:'30'}),append(8906,8801,{kcid:'399',yykcid:'399'})
|
|
|
|
|
+ ]};
|
|
|
|
|
+ const first=await run(request);
|
|
|
|
|
+ const [ready,...waiting]=first.postimages.map(x=>x.addon);
|
|
|
|
|
+ assert.equal(ready.courseCategoryKey,'middle_writing');
|
|
|
|
|
+ assert.match(ready.categoryEvidence,/^同步自动核对:/);
|
|
|
|
|
+ assert.match(ready.teachingRuleId,/^seed:middle_writing:60:online:/);
|
|
|
|
|
+ assert.equal(ready.durationMinutes,60);
|
|
|
|
|
+ assert.equal(ready.deliveryMode,'online');
|
|
|
|
|
+ assert.equal(ready.teacherPaySnapshot,null);
|
|
|
|
|
+ assert.equal(ready.creditCostsSnapshot,null);
|
|
|
|
|
+ assert.equal(ready.creditHoldStatus,null);
|
|
|
|
|
+ assert.ok(waiting.every(x=>!x.courseCategoryKey&&!x.teachingRuleId));
|
|
|
|
|
+ assert.deepEqual(JSON.parse(JSON.stringify(await run(request))),JSON.parse(JSON.stringify(first)));
|
|
|
|
|
+ assert.equal((await sql('SELECT count(*)::int n FROM "WritingAudit"'))[0].n,1);
|
|
|
|
|
+ assert.equal((await sql('SELECT count(*)::int n FROM "CourseCreditReservation" WHERE "appointmentId"=$1',['write-c8901']))[0].n,0);
|
|
|
|
|
+
|
|
|
|
|
+ const patch=(row,changes,batchKey)=>({action:'patch',modelId:54,batchKey,rows:[{
|
|
|
|
|
+ generalId:row.generalId,commonId:row.common.objectId,addonId:row.addon.objectId,commonVersion:row.common.updatedAt,
|
|
|
|
|
+ addonVersion:row.addon.updatedAt,values:changes}]});
|
|
|
|
|
+ const insertRule=`INSERT INTO "TeachingCompensationRule"
|
|
|
|
|
+ (company,"objectId","categoryKey","durationMinutes","deliveryMode","effectiveFrom",active,"teacherPay","creditCosts",revision) VALUES`;
|
|
|
|
|
+ await sql(insertRule+`
|
|
|
|
|
+ ('co','middle-30-original','middle_writing',30,'offline',$1,true,31,'[{"account":"Purse","amount":1}]',1),
|
|
|
|
|
+ ('co','middle-30-revised','middle_writing',30,'offline',$1,true,32,'[{"account":"Purse","amount":1}]',2)`,[incomingDay]);
|
|
|
|
|
+ const moved=(await run(patch(first.postimages[0],{jffs:'线下',dslx:'1'},'sync-writing-stage-move'))).postimages[0];
|
|
|
|
|
+ assert.equal(moved.addon.teachingRuleId,'middle-30-revised');
|
|
|
|
|
+ assert.equal(moved.addon.courseCategoryKey,'middle_writing');
|
|
|
|
|
+ assert.equal(moved.addon.durationMinutes,30);
|
|
|
|
|
+ assert.equal(moved.addon.deliveryMode,'offline');
|
|
|
|
|
+ assert.equal(moved.addon.teacherPaySnapshot,null);
|
|
|
|
|
+ const reassigned=(await run(patch(moved,{szyh:'8802'},'sync-writing-stage-reassign'))).postimages[0];
|
|
|
|
|
+ assert.equal(reassigned.addon.courseCategoryKey,null);
|
|
|
|
|
+ assert.equal(reassigned.addon.teachingRuleId,null);
|
|
|
|
|
+ assert.equal(reassigned.addon.teacherPaySnapshot,null);
|
|
|
|
|
+ assert.equal(reassigned.addon.creditCostsSnapshot,null);
|
|
|
|
|
+ assert.equal(reassigned.addon.durationMinutes,null);
|
|
|
|
|
+ assert.equal(reassigned.addon.deliveryMode,null);
|
|
|
|
|
+
|
|
|
|
|
+ await sql(insertRule+` ('co','middle-30-disabled','middle_writing',30,'offline',$1,false,32,'[{"account":"Purse","amount":1}]',3)`,[incomingDay]);
|
|
|
|
|
+ const noObsoleteRule=(await run({action:'append',modelId:54,batchKey:'sync-writing-retired-rule',rows:[
|
|
|
|
|
+ append(8911,8801,{dslx:'1',jffs:'线下'})
|
|
|
|
|
+ ]})).postimages[0];
|
|
|
|
|
+ assert.ok(!noObsoleteRule.addon.teachingRuleId||noObsoleteRule.addon.teachingRuleId.startsWith('seed:'));
|
|
|
|
|
+
|
|
|
|
|
+ await db.exec(`INSERT INTO "LessonRecordFact" VALUES('co','8912')`);
|
|
|
|
|
+ const fact=(await run({action:'append',modelId:54,batchKey:'sync-writing-fact',rows:[append(8912,8801)]})).postimages[0];
|
|
|
|
|
+ assert.equal(fact.addon.courseCategoryKey,null);
|
|
|
|
|
+ assert.equal(fact.addon.teachingRuleId,null);
|
|
|
|
|
+ const beforeReport=(await run({action:'append',modelId:54,batchKey:'sync-writing-before-report',rows:[append(8913,8801)]})).postimages[0];
|
|
|
|
|
+ await db.exec(`INSERT INTO "LessonCompletionReport" VALUES('co','write-c8913',8913)`);
|
|
|
|
|
+ assert.equal((await run(patch(beforeReport,{dslx:'1'},'sync-writing-protect-report'))).updated,0);
|
|
|
|
|
+
|
|
|
|
|
+ const duplicateCandidate=(await run({action:'append',modelId:54,batchKey:'sync-writing-coach-slot',rows:[append(8914,8801)]})).postimages[0];
|
|
|
|
|
+ await sql(`INSERT INTO "CourseAppointment"("objectId",company,id,"sourceKey",szyh,pl,kcid,yykcid,yysj,bxrq,dszt,dslx)
|
|
|
|
|
+ VALUES('native-writing-a','co',8915,'schedule:native-writing','8801','77','398','398',$1,$2,'0','2')`,[incomingDay+' 21:00',incomingDay]);
|
|
|
|
|
+ await db.exec(`INSERT INTO "CommonModel"("objectId",company,"modelId","itemId","generalId",status,"sourceKey")
|
|
|
|
|
+ VALUES('native-writing-c','co',54,8915,8915,99,'schedule:native-writing')`);
|
|
|
|
|
+ const duplicate=await run(patch(duplicateCandidate,{pl:'77'},'sync-writing-duplicate-coach'));
|
|
|
|
|
+ assert.equal(duplicate.duplicates,1);
|
|
|
|
|
+ assert.equal(duplicate.postimages[0].common.status,-2);
|
|
|
|
|
+
|
|
|
|
|
+ const manual=(await run({action:'append',modelId:54,batchKey:'sync-writing-manual',rows:[
|
|
|
|
|
+ append(8907,8801,{courseCategoryKey:'high_writing',categoryEvidence:'人工核对高中',teacherPaySnapshot:65})
|
|
|
|
|
+ ]})).postimages[0];
|
|
|
|
|
+ const protectedResult=await run(patch(manual,{dslx:'1'},'sync-writing-protect-manual'));
|
|
|
|
|
+ assert.equal(protectedResult.updated,0);
|
|
|
|
|
+ assert.equal(protectedResult.conflicts.length,1);
|
|
|
|
|
+
|
|
|
|
|
+ await sql('INSERT INTO "TeachingCourseCategory" VALUES ($1,$2,false)',['co','middle_writing']);
|
|
|
|
|
+ const disabled=(await run({action:'append',modelId:54,batchKey:'sync-writing-disabled',rows:[append(8908,8801)]})).postimages[0];
|
|
|
|
|
+ assert.equal(disabled.addon.courseCategoryKey,null);
|
|
|
|
|
+ assert.equal(disabled.addon.teachingRuleId,null);
|
|
|
|
|
+});
|