Admin API for data cleanup, so corrections no longer need direct database access

未关闭
#1,888 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
15/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
活跃
技术栈
mongodb, typescript

调研方向

This is a multi-part design proposal, not one change. Start with the existing PATCH /instrument-records/:id and subject DELETE routes, and with the audit logging in #1889, which the issue calls a prerequisite. Item 7 (soft delete) is marked as needing a decision, so it should be settled before work starts. Done means the maintainers have split the proposal into smaller issues, each with unit and e2e tests.

由索引模型根据 Issue 内容生成。

描述

Area: API Area: Shared Area: Web Difficulty: High Enhancement Needs Decision Priority: Medium

Context

An admin cleaned up the data on a production instance (v2.5.2): about 28,000 duplicate bulk-uploaded records, test subjects, empty sessions, and a year typo in record dates. Some of the work was possible through the API. The rest needed direct mongosh access to the database, which is risky, needs shell access to the host, and leaves no trace in the application. This issue lists what the API should offer so the next cleanup needs no direct database access.

Related: #1883 (no session delete), #1884 (upload duplicates), #1887 (future dates), #1889 (writes are not audited).

What had to be done outside the API, or was hard through it

Task What we had to do Why
Delete sessions left empty after record deletes (86 sessions) deleteMany in mongosh No session delete endpoint (#1883)
Correct the date of 17 records and their sessions updateMany in mongosh PATCH /instrument-records/:id accepts only data; there is no session update
Move records that were saved on the wrong subject (same mouse, with and without a group) Not possible; the records were deleted instead PATCH cannot change subjectId
Delete 847 subjects and 37 records 884 single DELETE calls No bulk delete. The run hit HTTP 429 after about 250 calls and a 60 s network timeout later, so it needed retry and resume logic in a client script
Find duplicates, empty sessions, future dates, data outside a group Download every subject, session and record (about 36 MB of JSON) and analyse it offline No integrity report
Back up before each change, restore if needed mongodump over ssh; a custom mongosh script for partial restore No admin export or restore by ID

Audit gap: after all these API deletes, GET /audit/logs?action=DELETE returns 0 entries, because subject, session and instrument-record writes are not audited. This is tracked in #1889. Direct database edits are not audited either, which is one more reason to replace them with the endpoints below.

Proposal

  1. Audit logging (#1889). Every new endpoint below must write audit entries. #1889 adds them for the existing subject, session, instrument-record and group writes, so it is a prerequisite for this issue.

  2. Session endpoints: DELETE /sessions/:id, refused while records or assignments use the session (see #1883), and PATCH /sessions/:id for date.

  3. Record correction: allow PATCH /instrument-records/:id to change date, and subjectId to another subject in the same group, with access checks and an audit entry. The record's session follows: either update it if no other record uses it, or create a new one.

  4. Bulk delete with dry run: POST /subjects/bulk/delete and POST /instrument-records/bulk/delete that take a list of IDs and dryRun: true|false. A dry run returns exactly what would be removed (subjects, records, sessions, files). A real run executes in one transaction (or in documented chunks), removes sessions left empty, and writes one audit entry per entity. This replaces hundreds of rate-limited calls.

  5. Admin integrity report: GET /admin/integrity (ADMIN only) returning counts and IDs for:

    • exact duplicate records (same subject, instrument, date and data)
    • sessions with no record or assignment
    • record or session dates in the future
    • records or subjects with no group
    • subjects with no records
    • subject IDs that differ only by case or punctuation

    A web page under Admin could show it.

  6. Admin export and restore by ID: an ADMIN-only download of subjects, sessions and records with their original IDs, and an import that inserts only documents whose ID is missing (it never overwrites). Then an admin can restore deleted data without database access.

  7. Needs decision: soft delete. As an alternative to items 4 and 6, mark subjects and records as deleted with a retention period, list them in a trash view, and purge them later.

Each change needs a unit test and an e2e test.

主要语言
TypeScript
星标
119
派生
19
平均合并
1 天 18 小时
30 天内合并 PR
61

环境准备

  • 提供 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

DouglasNeuroInformatics/OpenDataCapture 的其他 Issue

查看 DouglasNeuroInformatics/OpenDataCapture 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。