Преглед на файлове

fix(lessons): resolve confirmed word bookings against the active enrollment

彭峰 преди 1 час
родител
ревизия
60eea1751a
променени са 2 файла, в които са добавени 45 реда и са изтрити 1 реда
  1. 19 1
      scripts/sql/legacy-appointment-preparation.sql
  2. 26 0
      scripts/tests/legacy-appointment-preparation.test.mjs

+ 19 - 1
scripts/sql/legacy-appointment-preparation.sql

@@ -3,6 +3,7 @@
 CREATE OR REPLACE FUNCTION xs_prepare_legacy_appointment(co text,aid text,actor text,seed_rules jsonb) RETURNS jsonb LANGUAGE plpgsql AS $$
 DECLARE c "CommonModel";a "CourseAppointment";b "CourseBinding";u "_User";op "_User";
  category text;category_name text;course text;day date;minutes numeric;mode text;rule jsonb;costs jsonb;cost jsonb;
+ selected_binding text;
  notes_legacy boolean=false;legacy_trial boolean=false;existing "CourseCreditReservation";before_data jsonb;changed boolean=false;
 BEGIN
  PERFORM pg_advisory_xact_lock(hashtext('xs-credit:'||co));
@@ -33,6 +34,23 @@ BEGIN
   category_name=COALESCE(NULLIF(a."courseCategoryName",''),CASE category WHEN 'primary_writing' THEN '小学语法写作课' WHEN 'middle_writing' THEN '中学语法写作课' WHEN 'high_writing' THEN '高中语法写作课' END);
  ELSE
  PERFORM xs_course_size(co,course);
+ selected_binding=NULLIF(a."bindingId",'');
+ -- Old bookings have no new-system binding ID. Only a confirmed ordinary word
+ -- lesson can prefer one active word enrollment over its imported pending copies.
+ -- Resolve it here so an untouched source booking can still be moved or cancelled by sync.
+ IF selected_binding IS NULL AND a.dslx IN ('1','2') AND (category='word' OR (category IS NULL AND EXISTS(
+   SELECT 1 FROM "CourseTeachingProfile" p WHERE p.company=co AND p."courseId"::text=course AND p.confirmed
+   GROUP BY p."courseId" HAVING bool_and(p."categoryKey" IS NOT DISTINCT FROM 'word')))) THEN
+  SELECT min(cb."objectId") FILTER (WHERE cb."enrollmentStatus"='active') INTO selected_binding
+  FROM "CourseBinding" cb JOIN "CommonModel" bc ON bc.company=cb.company AND bc."modelId"=58 AND bc."itemId"=cb.id
+  WHERE cb.company=co AND cb.yhid::text=a.szyh::text AND cb.kcid::text=course AND bc.status=99
+    AND COALESCE(cb."enrollmentStatus",'pending') IN ('active','pending')
+  HAVING count(*) FILTER (WHERE cb."enrollmentStatus"='active')=1
+    AND bool_and(CASE WHEN cb."enrollmentStatus"='active' THEN cb."categoryKey" IS NOT DISTINCT FROM 'word'
+                      ELSE COALESCE(cb."categoryKey",'word')='word' AND
+                           (COALESCE(cb."sourceKey",'') ~ '^legacy[:\-]' OR
+                            COALESCE(cb."sourceKey",'') ~ '^\[\["(ID|GeneralID)",[0-9]+\]\]$') END);
+ END IF;
  -- A textbook may have separate trial and word enrollments for the same student.
  -- Respect an explicit binding and resolve an unbound trial from one locked query.
  BEGIN
@@ -41,7 +59,7 @@ BEGIN
   WHERE cb.company=co AND cb.yhid::text=a.szyh::text AND cb.kcid::text=course AND bc.status=99
   AND COALESCE(cb."enrollmentStatus",'pending') IN ('active','pending')
   AND (category IS NULL OR cb."categoryKey"=category OR cb."categoryKey" IS NULL OR (legacy_trial AND cb."categoryKey"='word' AND cb."enrollmentStatus"='active'))
-  AND (NULLIF(a."bindingId",'') IS NULL OR cb."objectId"=a."bindingId")
+  AND (selected_binding IS NULL OR cb."objectId"=selected_binding)
   AND (NOT legacy_trial OR NULLIF(a."bindingId",'') IS NOT NULL OR (cb."categoryKey"='trial' AND cb."enrollmentStatus"='active')
     OR NOT EXISTS(SELECT 1 FROM "CourseBinding" trial
       JOIN "CommonModel" trial_common ON trial_common.company=trial.company AND trial_common."modelId"=58 AND trial_common."itemId"=trial.id

+ 26 - 0
scripts/tests/legacy-appointment-preparation.test.mjs

@@ -223,3 +223,29 @@ test('旧阅读写作开课以一致的初三档案和本学年记录补齐;
   assert.ok(reads>=5);
  }finally{await db.exec('ROLLBACK');}
 });
+
+test('同步的普通单词预约选中唯一有效授权,保留旧开课,并拒绝不明确的分类',async()=>{
+ await db.exec('BEGIN');
+ try{
+  await db.exec(`UPDATE "CourseBinding" SET "enrollmentStatus"='pending',"categoryKey"=NULL;
+   INSERT INTO "CourseBinding" SELECT 'current-word',company,80,yhid,kcid,'word','单词课','active','enrollment:current',now() FROM "CourseBinding" WHERE "objectId"='binding';
+   INSERT INTO "CommonModel"("objectId",company,"modelId","itemId",status) VALUES('current-word-common','co',58,80,99)`);
+  for(const [duration,sourceKey] of [['1','legacy:model:58:general:10'],['2','[["GeneralID",10]]']]){
+   await q('UPDATE "CourseBinding" SET "sourceKey"=$1 WHERE "objectId"=$2',[sourceKey,'binding']);
+   const id=await fixture({dslx:duration});
+   assert.equal((await prepare(id)).bindingId,'current-word');
+   assert.equal((await prepare(id)).changed,false);
+  }
+  assert.equal((await one('SELECT "enrollmentStatus" FROM "CourseBinding" WHERE "objectId"=\'binding\'')).enrollmentStatus,'pending');
+  await db.exec(`UPDATE "CourseBinding" SET "enrollmentStatus"='active',"categoryKey"='word' WHERE "objectId"='binding'`);
+  await db.exec('SAVEPOINT ambiguous');
+  await assert.rejects(prepare(await fixture()),/唯一/);
+  await db.exec('ROLLBACK TO SAVEPOINT ambiguous');
+  assert.equal((await prepare(await fixture({bindingId:'binding'}))).bindingId,'binding');
+  await db.exec(`UPDATE "CourseBinding" SET "enrollmentStatus"='pending' WHERE "objectId"='binding'; UPDATE "CourseTeachingProfile" SET confirmed=false`);
+  await db.exec('SAVEPOINT ambiguous');
+  await assert.rejects(prepare(await fixture()),/唯一/);
+  await db.exec('ROLLBACK TO SAVEPOINT ambiguous');
+  assert.equal((await prepare(await fixture({courseCategoryKey:'word'}))).bindingId,'current-word');
+ }finally{await db.exec('ROLLBACK');}
+});