reconcile-legacy-lesson-pay.test.mjs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import assert from 'node:assert/strict';
  2. import { test } from 'node:test';
  3. import { PGlite } from '@electric-sql/pglite';
  4. import { legacyLessonPayEvidence, legacyLessonPayPreviewSql, legacyLessonPayApplySql } from '../reconcile-legacy-lesson-pay.mjs';
  5. const date = '2026-11-04 21:40';
  6. const lesson = { GeneralID: 300, yyds: '200', xymz: '20', jsmz: '30', kclx: '2', plpjsj: date };
  7. const order = { GeneralID: 200, szyh: '20', pl: '30', dslx: '2', dszt: '30', yongjin: '45' };
  8. test('旧课酬取销课原金额,并拒绝重复、取消和身份不一致的来源', () => {
  9. const evidence = legacyLessonPayEvidence([lesson], [order], 30);
  10. assert.equal(evidence.length, 1);
  11. assert.equal(evidence[0].amount, 45);
  12. assert.equal(legacyLessonPayEvidence([lesson], [{ ...order, yongjin: '0' }], 30)[0].amount, 0);
  13. for (const changes of [{ szyh: '21' }, { pl: '31' }, { dslx: '1' }, { dszt: '0' }, { yongjin: '' }]) {
  14. assert.deepEqual(legacyLessonPayEvidence([lesson], [{ ...order, ...changes }], 30), []);
  15. }
  16. assert.deepEqual(legacyLessonPayEvidence([lesson, { ...lesson, GeneralID: 301 }], [order], 30), []);
  17. assert.deepEqual(legacyLessonPayEvidence([lesson], [order, order], 30), []);
  18. });
  19. test('仅补记原始课次缺失的快照,按版本更新并保护现有快照和工资单', async () => {
  20. const db = new PGlite();
  21. try {
  22. await db.exec(`
  23. CREATE TABLE "CommonModel" (company text, "modelId" numeric, "generalId" numeric, "itemId" numeric, status numeric, "sourceKey" text);
  24. CREATE TABLE "LessonRecord" (company text, id numeric, "objectId" text, "sourceKey" text,
  25. "updatedAt" timestamptz, xymz text, jsmz text, yyds text, kclx text, plpjsj text, "teacherPaySnapshot" numeric);
  26. CREATE TABLE "CourseAppointment" (company text, id numeric, "sourceKey" text, szyh text, pl text, "teacherPaySnapshot" numeric);
  27. CREATE TABLE "PayrollLine" (company text, kind text, "standardLessonKey" text, "lessonId" text);
  28. INSERT INTO "CommonModel" VALUES ('co',59,300,301,99,'legacy:model:59:general:300'),('co',54,200,201,99,'legacy:model:54:general:200');
  29. INSERT INTO "LessonRecord" VALUES ('co',301,'lesson','legacy:model:59:general:300','2026-11-07T00:00:00Z','20','30','200','2','${date}',NULL);
  30. INSERT INTO "CourseAppointment" VALUES ('co',201,'legacy:model:54:general:200','20','30',NULL);
  31. `);
  32. const evidence = legacyLessonPayEvidence([lesson], [order], 30);
  33. const preview = () => db.query(legacyLessonPayPreviewSql, ['co', JSON.stringify(evidence)]).then(r => r.rows);
  34. const apply = version => db.query(legacyLessonPayApplySql, ['co', JSON.stringify(evidence.map(r => ({ ...r, version })))]).then(r => r.rows);
  35. assert.equal((await preview()).length, 1);
  36. await db.exec(`INSERT INTO "PayrollLine" VALUES ('co','lesson','legacy:300','lesson')`);
  37. assert.deepEqual(await preview(), []);
  38. await db.exec(`DELETE FROM "PayrollLine"; UPDATE "CourseAppointment" SET "teacherPaySnapshot"=0`);
  39. assert.deepEqual(await preview(), []);
  40. await db.exec(`UPDATE "CourseAppointment" SET "teacherPaySnapshot"=NULL; UPDATE "LessonRecord" SET "sourceKey"='cloud:completed'`);
  41. assert.deepEqual(await preview(), []);
  42. await db.exec(`UPDATE "LessonRecord" SET "sourceKey"='legacy:model:59:general:300'; UPDATE "CommonModel" SET status=-2 WHERE "modelId"=54`);
  43. assert.deepEqual(await preview(), []);
  44. await db.exec(`UPDATE "CommonModel" SET status=99 WHERE "modelId"=54`);
  45. assert.deepEqual(await apply('old version'), []);
  46. const [ready] = await preview();
  47. const [written] = await apply(ready.version);
  48. assert.equal(Number(written.amount), 45);
  49. assert.deepEqual(await preview(), []);
  50. assert.deepEqual(await apply(ready.version), []);
  51. assert.equal((await db.query('SELECT "teacherPaySnapshot" FROM "CourseAppointment"')).rows[0].teacherPaySnapshot, null);
  52. } finally {
  53. await db.close();
  54. }
  55. });