From 7210b53d30c2681370ca500d8c415d514caa573a Mon Sep 17 00:00:00 2001 From: Tanakit Phetkumjad Date: Sun, 22 Nov 2026 02:37:49 +0700 Subject: [PATCH] verification: resolve report rounds for jobs never submitted for review Since the 2026-11-21 requirement change, jobs from unflagged companies publish immediately without JobSubmitted, so their report queue items have no submission_id. Dismiss/uphold on a suspended round returned 422 SUBMISSION_UNKNOWN and left the job stuck in SUSPENDED. Job decides REMOVE/RESTORE by queue_item_id only, so drop the guard and send JobVerified without submission_id when none exists. Update E02 and the admin queue contract accordingly (PM decision 2026-11-22). Co-Authored-By: Claude Opus 5.5 --- .../src/report/report-resolution.service.ts | 5 +-- .../test/report/report-resolution.spec.ts | 37 ++++++++++++++----- docs/api-contract.md | 2 +- docs/event-contract.md | 4 +- 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/apps/verification/src/report/report-resolution.service.ts b/apps/verification/src/report/report-resolution.service.ts index 0dc9226..74806cb 100644 --- a/apps/verification/src/report/report-resolution.service.ts +++ b/apps/verification/src/report/report-resolution.service.ts @@ -50,10 +50,9 @@ export class ReportResolutionService { await lockScope(tx, 'job', probe.targetId); const item = await tx.queueItem.findUniqueOrThrow({ where: { id: itemId } }); if (item.status === 'RESOLVED') throw new AppException('STATE_CONFLICT', 'รายการนี้ถูกตัดสินไปแล้ว', 409); + // ไม่บังคับ submission_id: ประกาศของบริษัทปกติเผยแพร่ทันทีโดยไม่เคยส่งตรวจ (ปรับ Requirement 2026-11-21) + // และ Job ตัดสิน REMOVE/RESTORE จาก queue_item_id เท่านั้น (event-contract E02) — มี submission ล่าสุดก็แนบไปตามเดิม const suspended = item.origin === 'REPORT_THRESHOLD'; - if (suspended && !item.submissionId) { - throw new AppException('SUBMISSION_UNKNOWN', 'รายการนี้ยังไม่ทราบ submission ที่ต้องตัดสิน', 422); - } const now = new Date(); const resolution = kind === 'dismiss' ? 'REPORT_DISMISSED' : 'REPORT_UPHELD'; diff --git a/apps/verification/test/report/report-resolution.spec.ts b/apps/verification/test/report/report-resolution.spec.ts index 794de7f..321893c 100644 --- a/apps/verification/test/report/report-resolution.spec.ts +++ b/apps/verification/test/report/report-resolution.spec.ts @@ -129,18 +129,37 @@ describe('คำตัดสินรายงานของ Admin: dismiss-rep expect((await t.prisma.adminAuditLog.findFirstOrThrow({ where: { targetId: item.id } })).action).to.equal('REPORT_UPHOLD'); }); - it('ไม่มี submission_id (ประกาศไม่เคยมีผลตรวจ) ⇒ 422 SUBMISSION_UNKNOWN และไม่เปลี่ยนอะไรเลย (rollback)', async () => { + // ปรับ Requirement 2026-11-21: บริษัทปกติเผยแพร่ทันทีโดยไม่ส่ง E01 ประกาศส่วนใหญ่จึงไม่มี submission + // Job ใช้แค่ queue_item_id ตัดสิน REMOVE/RESTORE (event-contract E02) ⇒ ต้องตัดสินได้ ไม่เช่นนั้นประกาศค้าง SUSPENDED + it('ประกาศที่ไม่เคยส่งตรวจ (ไม่มี submission_id) ⇒ dismiss ส่ง RESTORE ได้โดยไม่มี submission_id', async () => { const { jobId } = await job(false); await fileMany(jobId, 3); const item = await openItem(jobId); - const res = await act(item.id, 'dismiss-report'); - expect(res.status).to.equal(422); - expect(res.body.error.code).to.equal('SUBMISSION_UNKNOWN'); - expect((await t.prisma.queueItem.findUniqueOrThrow({ where: { id: item.id } })).status).to.equal('OPEN'); - expect(await verified()).to.have.length(0); - expect((await t.prisma.jobReport.findMany({ where: { jobId } })).every((r) => r.status === 'OPEN')).to.equal(true); - expect((await t.prisma.jobReportCounter.findUniqueOrThrow({ where: { jobId } })).suspendedAt).to.be.instanceOf(Date); - expect(await t.prisma.adminAuditLog.count()).to.equal(0); + expect(item).to.include({ origin: 'REPORT_THRESHOLD', submissionId: null }); + + await act(item.id, 'dismiss-report').expect(204); + + const out = await verified(); + expect(out).to.have.length(1); + expect(out[0]).to.include({ job_id: jobId, decision: 'RESTORE', decided_by: 'ADMIN', queue_item_id: item.id }); + expect(out[0]).to.not.have.property('submission_id'); + expect(await t.prisma.queueItem.findUniqueOrThrow({ where: { id: item.id } })).to.include({ status: 'RESOLVED', resolution: 'REPORT_DISMISSED' }); + expect((await t.prisma.jobReportCounter.findUniqueOrThrow({ where: { jobId } })).suspendedAt).to.equal(null); + }); + + it('ประกาศที่ไม่เคยส่งตรวจ (ไม่มี submission_id) ⇒ uphold ส่ง REMOVE ได้โดยไม่มี submission_id', async () => { + const { jobId } = await job(false); + await fileMany(jobId, 3); + const item = await openItem(jobId); + + await act(item.id, 'uphold-report').expect(204); + + const out = await verified(); + expect(out).to.have.length(1); + expect(out[0]).to.include({ job_id: jobId, decision: 'REMOVE', decided_by: 'ADMIN', queue_item_id: item.id }); + expect(out[0]).to.not.have.property('submission_id'); + expect((await t.prisma.jobReport.findMany({ where: { jobId } })).every((r) => r.status === 'UPHELD' && r.counted)).to.equal(true); + expect((await t.prisma.adminAuditLog.findFirstOrThrow({ where: { targetId: item.id } })).action).to.equal('REPORT_UPHOLD'); }); }); diff --git a/docs/api-contract.md b/docs/api-contract.md index 19fdd4f..bd60d26 100644 --- a/docs/api-contract.md +++ b/docs/api-contract.md @@ -197,7 +197,7 @@ Tag = ป้ายทักษะ/เทคโนโลยีของประ | GET | `/admin/queue` | AD | `?type=&priority=&overdue=true&status=` + Pagination มาตรฐาน (§1) — ไม่ส่ง `status` ⇒ เฉพาะรายการที่ยังไม่ปิด (`OPEN`/`IN_REVIEW`); ดูที่ปิดแล้วด้วย `status=RESOLVED` → รายการพร้อม `company_name`, `due_at`, `overdue`; เรียงค่าเริ่มต้น `FAST_TRACK` ก่อน แล้ว `due_at` น้อยไปมาก | | GET | `/admin/queue/{id}` | AD | (รายการ `REVIEW_REPORT` เพิ่ม `review:{review_id,rating,outcome_label,title,body,moderation_status}` และ `review_reports:[{id,reporter_user_id,reason,created_at}]` เฉพาะ `REVIEW_REPORT`; ฟิลด์ของประกาศเป็นค่าว่าง) รายละเอียด + `origin`, `submission_id`, `reports:[{id, reporter_user_id, category, detail, counted, status, created_at}]` (เฉพาะ `JOB_REPORT` — ตัวตนผู้รายงานเห็นได้เฉพาะ Admin ที่นี่ที่เดียว; ชนิดอื่น `[]`) | | POST | `/admin/queue/{id}/actions/{approve\|reject}` | AD | งาน `JOB_REVIEW` (ประกาศของบริษัทที่ติดธง `UNDER_REVIEW`) → ส่ง `JobVerified(ADMIN_APPROVE/ADMIN_REJECT)`; `reject` ต้องมี `reason`; `204`; `409 STATE_CONFLICT` (ตัดสินไปแล้ว/สองคนกดพร้อมกัน), `422 SUBMISSION_UNKNOWN` (รายการยังไม่ทราบ submission), `422 UNSUPPORTED_ITEM_TYPE` (ชนิดอื่นที่ยังไม่รองรับ) | -| POST | `/admin/queue/{id}/actions/{dismiss-report\|uphold-report}` | AD | เฉพาะ `JOB_REPORT` (ชนิดอื่น ⇒ `422 UNSUPPORTED_ITEM_TYPE`; ปิดแล้ว ⇒ `409 STATE_CONFLICT`; `204`; body `{reason?}`) **รายการที่ระงับแล้ว (`origin=REPORT_THRESHOLD`):** `dismiss` ⇒ `JobVerified(RESTORE)` + รายงานของรอบนี้ `DISMISSED` + รีเซ็ตตัวนับ + `consecutive_dismissed +1` ของผู้รายงานแต่ละคน; `uphold` ⇒ `JobVerified(REMOVE)` + รายงาน `UPHELD`/`counted=true` + `consecutive_dismissed=0` — ถ้ารายการไม่มี `submission_id` ⇒ `422 SUBMISSION_UNKNOWN` **รายการ "ไม่นับ" (`origin=REPORT_UNCOUNTED`, ประกาศยัง `PUBLISHED`):** `dismiss` ปิดรายงานและรีเซ็ตตัวนับ ไม่ส่ง Event; `uphold` ทำให้รายงานเป็น `counted=true` แล้วคำนวณตัวนับใหม่ ครบเกณฑ์ ⇒ ระงับตามปกติ (E04) ไม่ส่ง `REMOVE` และไม่มี action ระงับด้วยมือ (R5-G02) — เขียน audit `REPORT_DISMISS`/`REPORT_UPHOLD` | +| POST | `/admin/queue/{id}/actions/{dismiss-report\|uphold-report}` | AD | เฉพาะ `JOB_REPORT` (ชนิดอื่น ⇒ `422 UNSUPPORTED_ITEM_TYPE`; ปิดแล้ว ⇒ `409 STATE_CONFLICT`; `204`; body `{reason?}`) **รายการที่ระงับแล้ว (`origin=REPORT_THRESHOLD`):** `dismiss` ⇒ `JobVerified(RESTORE)` + รายงานของรอบนี้ `DISMISSED` + รีเซ็ตตัวนับ + `consecutive_dismissed +1` ของผู้รายงานแต่ละคน; `uphold` ⇒ `JobVerified(REMOVE)` + รายงาน `UPHELD`/`counted=true` + `consecutive_dismissed=0` — รายการที่ไม่มี `submission_id` (ประกาศไม่เคยส่งตรวจ) ตัดสินได้ตามปกติ Event ไม่มี `submission_id` (event-contract E02; ปรับ 2026-11-22) **รายการ "ไม่นับ" (`origin=REPORT_UNCOUNTED`, ประกาศยัง `PUBLISHED`):** `dismiss` ปิดรายงานและรีเซ็ตตัวนับ ไม่ส่ง Event; `uphold` ทำให้รายงานเป็น `counted=true` แล้วคำนวณตัวนับใหม่ ครบเกณฑ์ ⇒ ระงับตามปกติ (E04) ไม่ส่ง `REMOVE` และไม่มี action ระงับด้วยมือ (R5-G02) — เขียน audit `REPORT_DISMISS`/`REPORT_UPHOLD` | | POST | `/admin/queue/{id}/actions/{publish-review\|remove-review}` | AD | เฉพาะ `REVIEW_REPORT` (ชนิดอื่น ⇒ `422 UNSUPPORTED_ITEM_TYPE`; ตัดสินแล้ว/สถานะรีวิวเปลี่ยนไปแล้ว ⇒ `409 STATE_CONFLICT`); `204`; body `{reason?}` (`remove-review` **ต้องมี `reason`** 1–500 ตัวอักษร ไม่เช่นนั้น `400`) → ส่ง `ReviewModerationResult` (`ADMIN_PUBLISH`/`ADMIN_REMOVE`, `reasons` มี `ADMIN` และ `REPORTED` เมื่อเป็นรายการ `REVIEW_REPORT`); `publish-review` ของ `REVIEW_REPORT` = ยืนยันให้รีวิวคงอยู่ | | POST | `/admin/queue/{id}/actions/company-clear-flag` | AD | `COMPANY_ACCOUNT`: เคลียร์ธง `UNDER_REVIEW` + ปิดรายการ + `CompanyFlagChanged(active=false)`; `204`; body `{reason?}`; ชนิดอื่น ⇒ `422 UNSUPPORTED_ITEM_TYPE`; ปิดแล้ว ⇒ `409 STATE_CONFLICT`; audit `FLAG_CLEAR` | diff --git a/docs/event-contract.md b/docs/event-contract.md index f792da0..2ebc74f 100644 --- a/docs/event-contract.md +++ b/docs/event-contract.md @@ -157,7 +157,7 @@ outbox_events(id=event_id, type, version, payload jsonb, correlation_id, ```ts { job_id: string - submission_id: string + submission_id?: string // บังคับทุก decision ยกเว้น REMOVE | RESTORE (ดูด้านล่าง) decision: 'APPROVE' | 'PENDING_REVIEW' // ผลคัดกรอง: บริษัทติดธง ⇒ PENDING_REVIEW เสมอ (APPROVE สงวนไว้) | 'ADMIN_APPROVE' | 'ADMIN_REJECT' // คำตัดสิน Admin | 'REMOVE' | 'RESTORE' // ผลตัดสินคำร้อง Report (RESTORE = ยกคำร้อง) @@ -167,7 +167,7 @@ outbox_events(id=event_id, type, version, payload jsonb, correlation_id, queue_item_id?: string // บังคับเมื่อ decision = REMOVE | RESTORE: รายการคิวรายงานที่ตัดสิน (= queue_item_id ของ E04 ที่ระงับประกาศ) } ``` -**`REMOVE`/`RESTORE` ไม่ตรวจ `submission_id` (PM ตัดสิน 2026-11-18):** คำตัดสินเรื่องรายงานไม่ใช่ผลตรวจเนื้อหา — Job ใช้เฉพาะ `queue_item_id` (รอบรายงาน) และ **ไม่เทียบ `submission_id` กับ `current_submission_id`** (ถ้าเทียบ รายการรายงานที่เปิดไว้บน submission A แล้วประกาศถูกส่งตรวจใหม่เป็น B จะทำให้คำตัดสินถูกข้ามและประกาศค้าง `SUSPENDED`) ส่วนคำตัดสินที่เป็นผลตรวจเนื้อหา (`APPROVE`, `PENDING_REVIEW`, `AUTO_REJECT`, `APPEAL_ACCEPTED`, `ADMIN_APPROVE`, `ADMIN_REJECT`) ยังต้องผ่าน Current-Submission Guard ตาม data-schema §8.2 • `submission_id` ใน payload ของ `REMOVE`/`RESTORE` ยังเป็นฟิลด์บังคับ (Verification ส่ง submission ล่าสุด ณ ตอนยกระดับรายงาน) แต่ Job ไม่ใช้ตัดสิน +**`REMOVE`/`RESTORE` ไม่ตรวจ `submission_id` (PM ตัดสิน 2026-11-18):** คำตัดสินเรื่องรายงานไม่ใช่ผลตรวจเนื้อหา — Job ใช้เฉพาะ `queue_item_id` (รอบรายงาน) และ **ไม่เทียบ `submission_id` กับ `current_submission_id`** (ถ้าเทียบ รายการรายงานที่เปิดไว้บน submission A แล้วประกาศถูกส่งตรวจใหม่เป็น B จะทำให้คำตัดสินถูกข้ามและประกาศค้าง `SUSPENDED`) ส่วนคำตัดสินที่เป็นผลตรวจเนื้อหา (`APPROVE`, `PENDING_REVIEW`, `AUTO_REJECT`, `APPEAL_ACCEPTED`, `ADMIN_APPROVE`, `ADMIN_REJECT`) ยังต้องผ่าน Current-Submission Guard ตาม data-schema §8.2 • `submission_id` ใน payload ของ `REMOVE`/`RESTORE` **ไม่บังคับ (PM ตัดสิน 2026-11-22)** — ถ้าประกาศเคยส่งตรวจ Verification แนบ submission ล่าสุด ณ ตอนยกระดับรายงาน; ถ้าไม่เคย (บริษัทปกติเผยแพร่ทันทีตาม Requirement 2026-11-21) ไม่มีฟิลด์นี้ และ Verification ต้องไม่ปฏิเสธคำตัดสินด้วย `SUBMISSION_UNKNOWN` (เดิมทำให้ประกาศค้าง `SUSPENDED`) Job ไม่ใช้ค่านี้ตัดสินอยู่แล้ว **กันเหตุการณ์ข้ามรอบ (REMOVE/RESTORE):** Job เก็บ `queue_item_id` ของ E04 ที่ระงับประกาศอยู่ แล้วรับ `REMOVE`/`RESTORE` เฉพาะที่ `queue_item_id` ตรงกัน — ไม่ตรงหรือประกาศไม่ได้ถูกระงับอยู่ให้ข้ามพร้อม log ไม่ throw/retry (ผลตัดสินของรอบเก่าที่มาช้าจึงไม่กระทบการระงับรอบใหม่); ค่านี้ส่งเฉพาะ `REMOVE`/`RESTORE` การระงับชั่วคราว (`SUSPENDED`) เกิดจาก E04 `ReportThresholdReached` ไม่ใช่ `JobVerified`