Skip to content

集合:SOS 事件(sos)

字数
1062 字
阅读时间
5 分钟

1. 目的与使用场景

  • 记录用户触发的 SOS 流程,包括触发来源、处置进度、守护者通知结果。支撑 SOS 云函数、风险升级、运维监控。由 /api/sos/trigger、人工值班后台使用。

2. Schema 定义

字段类型必填默认值约束/校验说明隐私分级
_idstring-TCB 自动主键P3
user_id指针id-指向 users._id发起人P3
trigger_sourcestringminiapp枚举(miniapp,floating_button,treehole_moderation,guardian)触发来源P3
entry_pointstringtoday枚举(today,global,treehole,report)入口P2
statusstringpending枚举(pending,contacting,connected,resolved,cancelled)流转状态P2
severity_levelstringmedium枚举(low,medium,high,critical)严重程度P3
guardian_contact_idsarray[]指针id通知的守护者P2
notify_resultsarray[]{contact_id,status,channel}通知结果P2
assigned_handlerstring--值班人员 IDP2
handler_rolestringcounselor枚举(counselor,ops,medical)处理角色P2
escalation_stepsarray[]{time,status,notes}升级日志P3
user_notesstring-长度 ≤ 512用户补充信息P3
location_hintobject-{geo: geo, description}位置线索P3
cancel_reasonstring-枚举(misclick,issue_resolved,other)取消原因P2
resolved_atdate-ISO 字符串关闭时间P2
createdAtdate-服务端时间创建时间P2
updatedAtdate-服务端时间更新时间P2

3. 索引与唯一约束

  • 单字段索引:user_idstatusseverity_level
  • 复合索引:[status, createdAt desc][user_id, createdAt desc]
  • 唯一性约束:无
  • 设计理由:值班台按状态与时间排序处理;用户历史查阅按时间倒序。

4. 访问控制(TCB 权限)

json
{
  "read": "user_id == auth.uid || auth.role in ['counselor','ops']",
  "write": "auth.role in ['sos_service','counselor','ops']"
}

用户可查看自身事件;辅导员/运营负责处置与更新。

  1. 关系与级联
  • user_idusers
  • guardian_contact_idsusers(守护者)
  • reports(risk_alert)、notifications(SOS 通知)、events(sos_fab_trigger)关联
  • 删除策略:原则上不删除;如应监管要求需脱敏用户信息,仅保留统计。
  • 反范式:notify_resultsescalation_steps 嵌入方便展示完整链路。
  1. 数据生命周期与合规
  • 留存:保留 3 年满足心理危机干预记录要求。
  • 匿名化:导出前去除个人标识,仅保留事件序列。
  • 审计:createdAtupdatedAtassigned_handlernotify_results
  1. API/云函数契约映射 | 接口/函数 | 读/写 | 使用字段 | 过滤条件 | 排序/分页 | 备注 | | /api/sos/trigger | 写 | trigger_source, entry_point, user_notes, location_hint | _openid = auth.openid | - | 用户触发 | | 值班后台 updateSosStatus | 写 | status, assigned_handler, escalation_steps, notify_results, resolved_at | _id | - | 人工处理 | | 护心周报生成 | 读 | severity_level, status, resolved_at | user_id, 时间范围 | - | 风险摘要 | | 通知服务 pushGuardianAlert | 读 | guardian_contact_ids, notify_results | status='pending' | - | 守护者通知 |

  2. 示例文档(≥3条)

json
{
  "_id": "sos_20240406_01",
  "user_id": "usr_002",
  "trigger_source": "miniapp",
  "entry_point": "today",
  "status": "contacting",
  "severity_level": "high",
  "guardian_contact_ids": ["usr_guard_01"],
  "notify_results": [{"contact_id": "usr_guard_01", "status": "sent", "channel": "phone"}],
  "assigned_handler": "counselor_zhang",
  "handler_role": "counselor",
  "escalation_steps": [{"time": "2024-04-06T14:05:00Z", "status": "contacting", "notes": "拨打守护者电话"}],
  "user_notes": "感到情绪失控,想找人聊聊",
  "createdAt": "2024-04-06T14:02:00Z",
  "updatedAt": "2024-04-06T14:06:00Z"
}
{
  "_id": "sos_20240403_02",
  "user_id": "usr_001",
  "trigger_source": "floating_button",
  "entry_point": "global",
  "status": "resolved",
  "severity_level": "medium",
  "assigned_handler": "ops_li",
  "escalation_steps": [{"time": "2024-04-03T21:10:00Z", "status": "connected", "notes": "IM 已接入"}],
  "resolved_at": "2024-04-03T21:30:00Z",
  "createdAt": "2024-04-03T21:05:00Z",
  "updatedAt": "2024-04-03T21:30:00Z"
}
{
  "_id": "sos_20240328_05",
  "user_id": "usr_003",
  "trigger_source": "treehole_moderation",
  "status": "cancelled",
  "severity_level": "medium",
  "cancel_reason": "misclick",
  "createdAt": "2024-03-28T12:00:00Z",
  "updatedAt": "2024-03-28T12:10:00Z"
}
  1. 常用查询样例(≥3条)
javascript
// 值班台拉取待处理事件
const pending = await db.collection('sos').where({ status: db.command.in(['pending','contacting']) })
  .orderBy('createdAt', 'asc').limit(50).get();

// 用户查看历史记录
const history = await db.collection('sos').where({ user_id: auth.uid })
  .orderBy('createdAt', 'desc').get();

// 统计当月高危事件
const highCount = await db.collection('sos').where({
  severity_level: 'critical',
  createdAt: db.command.gte(new Date(Date.now() - 30 * 86400000).toISOString())
}).count();
  1. 边界与错误码
  • 高频触发:同用户 10 分钟内重复触发返回 E_SOS_RATE_LIMIT
  • 未绑定守护者:写入时 guardian_contact_ids 为空则提醒,返回 E_SOS_NO_GUARDIAN
  • 越权更新:非值班角色修改返回 E_FORBIDDEN
  1. 变更影响评估
  • 字段调整需同步 /api/sos/trigger、守护者通知模板、埋点 sos_fab_trigger
  • 索引调整影响值班台响应速度与监控看板。
  1. 假设与待确认
  • 假设守护者均存在于 users 集合;待确认是否需要支持外部联系人号码。
  • 待确认是否需记录音频通话录音链接。

贡献者

The avatar of contributor named as Cai Hongyu Cai Hongyu

文件历史

撰写