Repository navigation
feat(intake): add session answer endpoint (#5) - #112
Conversation
📝 WalkthroughWalkthrough新增问询会话单条答案提交接口,支持创建者校验、答案规范化、会话状态更新、候选字段持久化、审计记录及下一题返回,并覆盖多数据库迁移和接口测试。 Changes问询会话答案提交
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant 成员
participant HTTP路由
participant 答案提交服务
participant 数据库
participant 审计事件
成员->>HTTP路由: POST session_id/answers
HTTP路由->>答案提交服务: 传递认证信息和答案请求
答案提交服务->>数据库: 校验会话并保存答案
答案提交服务->>数据库: 更新会话状态和缺失字段
答案提交服务->>审计事件: 写入字段及候选状态元数据
答案提交服务-->>HTTP路由: 返回提交响应
HTTP路由-->>成员: 201 Created
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
Preview readyThe preview for this build is available at https://pr-112-638fb7ca7235.angui.cg8.site. Temporary demo accounts (all use the password
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
tests/api/intake_session_answers_post.rs (1)
38-90: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win建议补充
ready_for_confirmation状态流转的覆盖。现有用例断言了
status == "collecting",但未覆盖“提交后必填字段全部满足 → 会话状态转为ready_for_confirmation”这一被改动的正向路径。这是submit_intake_answer的关键分支,建议补一个用例把该会话的所有必填字段答完并断言最终status。需要我生成该测试吗?🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/api/intake_session_answers_post.rs` around lines 38 - 90, 补充一个覆盖 submit_intake_answer 完整正向流程的测试:创建会话后依次提交所有必填字段的答案,确保每次请求成功,并断言最后一次响应及会话记录的 status 为 ready_for_confirmation,而不是 collecting;复用现有的 answer_request、create_family_session 和 TestContext 测试工具。Source: Path instructions
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/services/intake_session_service.rs`:
- Around line 121-151: Update the insert error handling in the answer-submission
flow around set_answer and intake_session_answers::ActiveModel::insert so a
unique-constraint conflict caused by concurrent duplicate submissions is
converted to ApiError::Conflict with the existing duplicate-answer message.
Preserve propagation of all other database errors as ApiError::Database.
---
Nitpick comments:
In `@tests/api/intake_session_answers_post.rs`:
- Around line 38-90: 补充一个覆盖 submit_intake_answer
完整正向流程的测试:创建会话后依次提交所有必填字段的答案,确保每次请求成功,并断言最后一次响应及会话记录的 status 为
ready_for_confirmation,而不是 collecting;复用现有的 answer_request、create_family_session
和 TestContext 测试工具。
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 2d9a2ae6-1409-4dba-9b53-112ebd3f33b3
📒 Files selected for processing (16)
docs/openapi.yamlmigration/sql/mysql/down/0013_drop_intake_session_answers.sqlmigration/sql/mysql/up/0013_create_intake_session_answers.sqlmigration/sql/postgres/down/0013_drop_intake_session_answers.sqlmigration/sql/postgres/up/0013_create_intake_session_answers.sqlmigration/sql/sqlite/down/0013_drop_intake_session_answers.sqlmigration/sql/sqlite/up/0013_create_intake_session_answers.sqlmigration/src/lib.rsmigration/src/m0013_create_intake_session_answers.rssrc/entities/intake_session_answers.rssrc/entities/mod.rssrc/models.rssrc/routes/intake_sessions.rssrc/services/intake_session_service.rstests/api/intake_session_answers_post.rstests/api/mod.rs
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/services/intake_session_service.rs (1)
165-170: 🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift避免不同字段并发提交时丢失答案。
两个请求提交不同字段时,都会从同一份旧
answers_json构造更新;两次答案行插入都能成功,但后一次updated_session.update会覆盖前一次写入的 JSON,导致已存在的intake_session_answers记录与会话答案、缺失字段及状态不一致。请用会话行锁,或基于版本/原updated_at的 CAS 加重试,串行化“读取答案→更新会话”的流程。As per path instructions, "Review Rust/Actix code for authentication, authorization, privacy, error handling, concurrency, API compatibility, and data-loss risks."
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/services/intake_session_service.rs` around lines 165 - 170, 在处理会话提交的函数中,将读取答案到更新会话的整个流程串行化:使用会话行锁,或基于原始 updated_at/版本字段执行 CAS 并在冲突时重试,确保每次更新都基于最新 answers_json。重点修改构造 updated_session 并调用 update 的逻辑,同时保持答案行插入、缺失字段处理和状态更新的一致性,避免后一次提交覆盖并发请求已写入的答案。Source: Path instructions
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@src/services/intake_session_service.rs`:
- Around line 165-170: 在处理会话提交的函数中,将读取答案到更新会话的整个流程串行化:使用会话行锁,或基于原始
updated_at/版本字段执行 CAS 并在冲突时重试,确保每次更新都基于最新 answers_json。重点修改构造 updated_session
并调用 update 的逻辑,同时保持答案行插入、缺失字段处理和状态更新的一致性,避免后一次提交覆盖并发请求已写入的答案。
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 187ed78f-737b-4ff1-a41c-56be7848e29c
📒 Files selected for processing (2)
src/services/intake_session_service.rstests/api/intake_session_answers_post.rs
🚧 Files skipped from review as they are similar to previous changes (1)
- tests/api/intake_session_answers_post.rs
Preview readyThe preview for this build is available at https://pr-112-2f04874a30d8.angui.cg8.site. Temporary demo accounts (all use the password
|
Implements #5. Adds the family-owned intake answer endpoint, draft candidate persistence, SQLite/PostgreSQL/MySQL migration 0013, OpenAPI contract, and API coverage for authorization, closed or unknown sessions, prompt-like text, length limits, duplicate submissions, and audit redaction. Verification: cargo fmt --check; cargo clippy --workspace --all-targets --all-features --locked -- -D warnings; cargo test --workspace --all-features --locked.
Summary by CodeRabbit
401、会话不存在(避免信息泄露)404、会话关闭或重复提交409、超长答案400。