|
@@ -1,3 +1,26 @@
|
|
|
|
|
+// Called under the same tenant credit lock as scheduling and cancellation.
|
|
|
|
|
+async function directMatchingNativeBooking(db,co,common,old){
|
|
|
|
|
+ if(Number(common.status)!==99||old.dszt!=='0'||old.teachingManaged||common.teachingManaged||
|
|
|
|
|
+ old.bindingId||old.courseCategoryKey||old.creditHoldStatus||old.creditCostsSnapshot?.length||old.teacherPaySnapshot!=null||
|
|
|
|
|
+ old.kssj||old.jssj||![old.szyh,old.pl,old.kcid].every(id=>/^[1-9]\d*$/.test(id))||
|
|
|
|
|
+ (old.yykcid&&old.yykcid!==old.kcid))return null;
|
|
|
|
|
+ // The operator identified member 252 in this tenant as a test account.
|
|
|
|
|
+ if(co==='7pIbDBJmKx'&&old.szyh==='252')return null;
|
|
|
|
|
+ const slot=/^(\d{4}-\d\d-\d\d) (?:[01]\d|2[0-3]):[0-5]\d(?::00)?$/.exec(old.yysj||'');
|
|
|
|
|
+ if(!slot||(old.bxrq&&old.bxrq!==slot[1]))return null;
|
|
|
|
|
+ return db.oneOrNone(`SELECT min(c."generalId") AS id
|
|
|
|
|
+ FROM "CourseAppointment" n JOIN "CommonModel" c ON c.company=n.company AND c."modelId"=54 AND c."itemId"=n.id
|
|
|
|
|
+ WHERE n.company=$1 AND n.szyh=$2 AND n.pl=$3 AND n.kcid=$4 AND n.dszt IN ('0','10','11','20','30','40') AND c.status=99
|
|
|
|
|
+ AND n."sourceKey" LIKE 'schedule:%' AND c."sourceKey"=n."sourceKey"
|
|
|
|
|
+ AND n.yysj IN ($5,$5||':00') AND COALESCE(NULLIF(n.bxrq,''),left(n.yysj,10))=left($5,10)
|
|
|
|
|
+ AND COALESCE(NULLIF(n.yykcid,''),n.kcid)=n.kcid
|
|
|
|
|
+ AND left($5,10)>=to_char(now() AT TIME ZONE 'Asia/Shanghai','YYYY-MM-DD')
|
|
|
|
|
+ AND EXISTS(SELECT 1 FROM "_User" u WHERE u.company=$1 AND u."legacyUserId"=($2::text)::numeric
|
|
|
|
|
+ AND u."legacyGroupId"=1 AND NOT COALESCE(u."isDeleted",false) AND NOT COALESCE(u."isDisabled",false))
|
|
|
|
|
+ AND NOT EXISTS(SELECT 1 FROM "CourseCreditReservation" r WHERE r.company=$1 AND r."appointmentId"=$6)
|
|
|
|
|
+ AND NOT EXISTS(SELECT 1 FROM "LessonRecord" l WHERE l.company=$1 AND l.yyds IN ($7,$8))
|
|
|
|
|
+ HAVING count(*)=1`,[co,old.szyh,old.pl,old.kcid,old.yysj.slice(0,16),common.objectId,String(common.generalId),String(old.id)]);
|
|
|
|
|
+}
|
|
|
async function handler(request,response){
|
|
async function handler(request,response){
|
|
|
try{
|
|
try{
|
|
|
const u=request.user;if(!u)return response.status(401).json({success:false});await u.fetch({useMasterKey:true});if(u.get('isDisabled')||u.get('isDeleted')||u.get('adminRoleKey')!=='super-admin'||u.get('company')?.id!==__COMPANY_ID__)return response.status(403).json({success:false});
|
|
const u=request.user;if(!u)return response.status(401).json({success:false});await u.fetch({useMasterKey:true});if(u.get('isDisabled')||u.get('isDeleted')||u.get('adminRoleKey')!=='super-admin'||u.get('company')?.id!==__COMPANY_ID__)return response.status(403).json({success:false});
|
|
@@ -58,7 +81,7 @@ try{
|
|
|
if(!aa||cc.objectId!==incoming.commonId||aa.objectId!==incoming.addonId||!sameVersion(cc.updatedAt,incoming.commonVersion)||!sameVersion(aa.updatedAt,incoming.addonVersion)||!(await directUnchanged(t,co,m,gid,cc,aa,unchanged))){
|
|
if(!aa||cc.objectId!==incoming.commonId||aa.objectId!==incoming.addonId||!sameVersion(cc.updatedAt,incoming.commonVersion)||!sameVersion(aa.updatedAt,incoming.addonVersion)||!(await directUnchanged(t,co,m,gid,cc,aa,unchanged))){
|
|
|
out.conflicts.push({generalId:gid,reason:'新系统已有修改、版本变化或来源不可确认'});continue;
|
|
out.conflicts.push({generalId:gid,reason:'新系统已有修改、版本变化或来源不可确认'});continue;
|
|
|
}
|
|
}
|
|
|
- if(m===2||m===52||cc.teachingManaged||aa.teachingManaged||m===54&&(aa.bindingId||aa.creditHoldStatus||aa.courseCategoryKey)||m===58&&aa.enrollmentStatus==='active'){out.conflicts.push({generalId:gid,reason:'保留新系统课程内容或教学凭据'});continue;}
|
|
|
|
|
|
|
+ if(m===2||m===52||cc.teachingManaged||aa.teachingManaged||m===54&&(Number(cc.status)===-2||aa.bindingId||aa.creditHoldStatus||aa.courseCategoryKey)||m===58&&aa.enrollmentStatus==='active'){out.conflicts.push({generalId:gid,reason:'保留新系统课程内容或教学凭据'});continue;}
|
|
|
const fields=Object.keys(incoming.values||{});if(!fields.length||fields.some(f=>!s.fields.includes(f)))throw Error('Invalid patch fields');
|
|
const fields=Object.keys(incoming.values||{});if(!fields.length||fields.some(f=>!s.fields.includes(f)))throw Error('Invalid patch fields');
|
|
|
if(m===60){
|
|
if(m===60){
|
|
|
const applied=(await t.one('SELECT xs_legacy_review_patch($1,$2,$3::jsonb,$4::jsonb) result',[co,aa.objectId,JSON.stringify(incoming.values),JSON.stringify(aa)])).result;
|
|
const applied=(await t.one('SELECT xs_legacy_review_patch($1,$2,$3::jsonb,$4::jsonb) result',[co,aa.objectId,JSON.stringify(incoming.values),JSON.stringify(aa)])).result;
|
|
@@ -67,6 +90,15 @@ try{
|
|
|
out.updated++;
|
|
out.updated++;
|
|
|
}
|
|
}
|
|
|
const after=await t.oneOrNone(readSql,[co,m,[gid]]);if(!after?.common||!after.addon)throw Error('Atomic projection verification failed');
|
|
const after=await t.oneOrNone(readSql,[co,m,[gid]]);if(!after?.common||!after.addon)throw Error('Atomic projection verification failed');
|
|
|
|
|
+ if(m===54){
|
|
|
|
|
+ const kept=await directMatchingNativeBooking(t,co,after.common,after.addon);
|
|
|
|
|
+ if(kept){
|
|
|
|
|
+ // Keep both source IDs for reconciliation; only the native slot is bookable.
|
|
|
|
|
+ after.common=(await t.one('UPDATE "CommonModel" c SET status=-2,"updatedAt"=now() WHERE company=$1 AND "objectId"=$2 RETURNING to_jsonb(c) AS data',[co,after.common.objectId])).data;
|
|
|
|
|
+ out.duplicates++;
|
|
|
|
|
+ out.conflicts.push({generalId:gid,reason:'与新系统预约 '+kept.id+' 重复,保留新系统时长'});
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
if(m===60&&p.action==='append')await t.one('SELECT xs_legacy_review_track($1,$2)',[co,after.addon.objectId]);
|
|
if(m===60&&p.action==='append')await t.one('SELECT xs_legacy_review_track($1,$2)',[co,after.addon.objectId]);
|
|
|
if(p.action==='patch'){const expected=(await t.one('SELECT to_jsonb(v) expected FROM jsonb_populate_record(NULL::'+q(s.className)+',$1::jsonb) v',[JSON.stringify(incoming.values)])).expected;if(Object.keys(incoming.values).some(f=>JSON.stringify(expected[f])!==JSON.stringify(after.addon[f])))throw Error('Post-write field verification failed');}
|
|
if(p.action==='patch'){const expected=(await t.one('SELECT to_jsonb(v) expected FROM jsonb_populate_record(NULL::'+q(s.className)+',$1::jsonb) v',[JSON.stringify(incoming.values)])).expected;if(Object.keys(incoming.values).some(f=>JSON.stringify(expected[f])!==JSON.stringify(after.addon[f])))throw Error('Post-write field verification failed');}
|
|
|
if(m===60&&after.reward&&!(Number(after.reward.amount)===0&&['historical','exception'].includes(after.reward.status)))throw Error('Historical reward verification failed');
|
|
if(m===60&&after.reward&&!(Number(after.reward.amount)===0&&['historical','exception'].includes(after.reward.status)))throw Error('Historical reward verification failed');
|