Skip to content

Added archive, restore and delete to member custom fields API - #29393

Merged
rob-ghost merged 1 commit into
mainfrom
feat/ber-3803-archive-restore-fields-api
Jul 17, 2026
Merged

rob-ghost merged 1 commit into
mainfrom
feat/ber-3803-archive-restore-fields-api

Conversation

@rob-ghost

Copy link
Copy Markdown
Contributor

Problem

Custom field definitions could be archived, but the API had no way to see archived fields or bring them back. Archiving was a one-way soft delete with no path to permanently remove a field, and since field names stay globally unique, an archived field could silently block reuse of its name with no way to reach it. Settings needs to show archived fields alongside active ones, and admins need a safe, deliberate way to permanently delete a field and its data.

Solution

Archived fields are now hidden by default but reachable: the definitions list takes a status filter so Settings can pull active and archived together in one request, and a field carries its status so the UI can group them. Archiving and restoring are a single status change over the existing update route. Deleting a field now permanently removes it and its stored member values, and is only allowed once the field has been archived, so permanent data loss is always a two-step.

@coderabbitai

coderabbitai Bot commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 2cd1db1b-3411-4a93-ab12-35ccc8f24177

📥 Commits

Reviewing files that changed from the base of the PR and between 83bea3f and 9ad1758.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (12)
  • apps/admin-x-framework/src/api/actions.ts
  • apps/admin-x-framework/test/unit/api/actions.test.ts
  • ghost/core/core/server/api/endpoints/member-custom-fields.ts
  • ghost/core/core/server/services/members-custom-fields/actions.ts
  • ghost/core/core/server/services/members-custom-fields/definitions-service.ts
  • ghost/core/core/server/services/members-custom-fields/index.ts
  • ghost/core/core/server/services/members-custom-fields/models.ts
  • ghost/core/core/server/services/members-custom-fields/schema.ts
  • ghost/core/core/server/services/members-custom-fields/serializers.ts
  • ghost/core/package.json
  • ghost/core/test/e2e-api/admin/member-custom-fields.test.ts
  • pnpm-workspace.yaml
🚧 Files skipped from review as they are similar to previous changes (8)
  • ghost/core/core/server/api/endpoints/member-custom-fields.ts
  • ghost/core/package.json
  • pnpm-workspace.yaml
  • ghost/core/core/server/services/members-custom-fields/serializers.ts
  • ghost/core/core/server/services/members-custom-fields/actions.ts
  • ghost/core/core/server/services/members-custom-fields/models.ts
  • ghost/core/core/server/services/members-custom-fields/definitions-service.ts
  • ghost/core/test/e2e-api/admin/member-custom-fields.test.ts

Walkthrough

Custom fields now expose status in database, domain, and API schemas. Browsing accepts NQL filters while defaulting to active fields. Status updates archive or restore fields and record lifecycle actions, while permanent deletion is permitted only for archived fields. Tests cover filtering, lifecycle transitions, value persistence, cascade deletion, and action history.

Possibly related PRs

  • TryGhost/Ghost#29321: Introduces the custom-fields foundation extended here with filtering and lifecycle action support.
  • TryGhost/Ghost#29373: Builds on the status lifecycle model for member custom-field value behavior.

Suggested labels: preview

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title matches the main change: member custom fields now support archive, restore, and permanent delete actions.
Description check ✅ Passed The description accurately summarizes the new status filter, status transitions, and permanent deletion behavior.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/ber-3803-archive-restore-fields-api

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@nx-cloud

nx-cloud Bot commented Jul 16, 2026 •

Copy link
Copy Markdown

🤖 Nx Cloud AI Fix

Ensure the fix-ci command is configured to always run in your CI pipeline to get automatic fixes in future runs. For more information, please see https://nx.dev/ci/features/self-healing-ci


View your CI Pipeline Execution ↗ for commit 83bea3f

Command Status Duration Result
nx run ghost:test:integration ✅ Succeeded 3m 6s View ↗
nx run ghost:test:ci:integration ✅ Succeeded 5s View ↗
nx run @tryghost/admin:test:acceptance ✅ Succeeded 5m 23s View ↗
nx run-many -t test:unit -p @tryghost/admin-x-f... ✅ Succeeded 4m 19s View ↗
nx run @tryghost/koenig-lexical:test:acceptance ✅ Succeeded 2m 33s View ↗
nx run ghost:test:legacy ✅ Succeeded 3m View ↗
nx run ghost:test:e2e ✅ Succeeded 2m 45s View ↗
nx run ghost-admin:test ✅ Succeeded 3m 4s View ↗
Additional runs (8) ✅ Succeeded ... View ↗

💡 Verify your cache is correct by running tasks in a sandbox. Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-07-17 10:58:44 UTC

@codecov

codecov Bot commented Jul 16, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.56098% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.27%. Comparing base (d8cc811) to head (9ad1758).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...vices/members-custom-fields/definitions-service.ts 96.05% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #29393      +/-   ##
==========================================
- Coverage   74.30%   74.27%   -0.03%     
==========================================
  Files        1595     1595              
  Lines      139505   139583      +78     
  Branches    16946    16949       +3     
==========================================
+ Hits       103657   103680      +23     
- Misses      34831    34852      +21     
- Partials     1017     1051      +34     
Flag Coverage Δ
admin-tests 55.45% <ø> (-0.02%) ⬇️
e2e-tests 76.35% <97.56%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@rob-ghost
rob-ghost force-pushed the feat/ber-3790-member-custom-field-values branch from 48e2b5b to 8b59c74 Compare July 16, 2026 20:07
@rob-ghost
rob-ghost force-pushed the feat/ber-3803-archive-restore-fields-api branch 2 times, most recently from 85019c4 to 004c289 Compare July 16, 2026 20:28
@rob-ghost
rob-ghost force-pushed the feat/ber-3790-member-custom-field-values branch from 8b59c74 to f1b7e4b Compare July 16, 2026 21:08
@rob-ghost
rob-ghost force-pushed the feat/ber-3803-archive-restore-fields-api branch from 004c289 to 48832df Compare July 16, 2026 21:29
@rob-ghost
rob-ghost force-pushed the feat/ber-3790-member-custom-field-values branch from f1b7e4b to 6905787 Compare July 16, 2026 21:40
@rob-ghost
rob-ghost force-pushed the feat/ber-3803-archive-restore-fields-api branch from 48832df to ab25788 Compare July 16, 2026 21:56

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 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
`@ghost/core/core/server/services/members-custom-fields/definitions-service.ts`:
- Around line 218-227: The lifecycle mutations in definitions-service.ts must be
made atomic: in destroy, delete using both key and archived status, inspect the
affected-row count, and only record the delete action when one row was removed;
in the update path around lines 198-205, constrain the update by key and
expected prior status, then record the action only when exactly one row
transitions.
🪄 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

Run ID: 9989b67f-f5b2-4e1b-a8d8-5306acc6dbed

📥 Commits

Reviewing files that changed from the base of the PR and between 6905787 and ab25788.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (9)
  • ghost/core/core/server/api/endpoints/member-custom-fields.ts
  • ghost/core/core/server/services/members-custom-fields/actions.ts
  • ghost/core/core/server/services/members-custom-fields/definitions-service.ts
  • ghost/core/core/server/services/members-custom-fields/models.ts
  • ghost/core/core/server/services/members-custom-fields/schema.ts
  • ghost/core/core/server/services/members-custom-fields/serializers.ts
  • ghost/core/package.json
  • ghost/core/test/e2e-api/admin/member-custom-fields.test.ts
  • pnpm-workspace.yaml

Comment on lines +218 to +227
async destroy(context: RequestContext, key: string): Promise<void> {
const field = await this.knex(TABLE).where('key', key).first();
if (!field) {
throw new errors.NotFoundError({message: 'Custom field not found.'});
}
if (field.status !== FIELD_STATUS.archived) {
await this.knex(TABLE).where('key', key).update({status: FIELD_STATUS.archived, updated_at: new Date()});
await this.recordAction({context, verb: 'archive', subject: key});
throw new errors.ValidationError({message: 'Only archived custom fields can be deleted. Archive the field first.'});
}
await this.knex(TABLE).where('key', key).del();
await this.recordAction({context, verb: 'delete', subject: key});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Make lifecycle state checks and mutations atomic.

Both paths make decisions from a stale pre-read and then mutate by key alone, allowing concurrent transitions to violate lifecycle guarantees.

  • ghost/core/core/server/services/members-custom-fields/definitions-service.ts#L218-L227: delete only where both the key and archived status still match; inspect the affected-row count.
  • ghost/core/core/server/services/members-custom-fields/definitions-service.ts#L198-L205: update only from the expected prior status and record an action only when one row actually transitions.
📍 Affects 1 file
  • ghost/core/core/server/services/members-custom-fields/definitions-service.ts#L218-L227 (this comment)
  • ghost/core/core/server/services/members-custom-fields/definitions-service.ts#L198-L205
🤖 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 `@ghost/core/core/server/services/members-custom-fields/definitions-service.ts`
around lines 218 - 227, The lifecycle mutations in definitions-service.ts must
be made atomic: in destroy, delete using both key and archived status, inspect
the affected-row count, and only record the delete action when one row was
removed; in the update path around lines 198-205, constrain the update by key
and expected prior status, then record the action only when exactly one row
transitions.

@rob-ghost
rob-ghost force-pushed the feat/ber-3790-member-custom-field-values branch from 6905787 to db23b9d Compare July 16, 2026 23:02
Base automatically changed from feat/ber-3790-member-custom-field-values to main July 17, 2026 08:30
@rob-ghost
rob-ghost force-pushed the feat/ber-3803-archive-restore-fields-api branch from ab25788 to 0438e0a Compare July 17, 2026 08:34
@rob-ghost
rob-ghost marked this pull request as ready for review July 17, 2026 09:35
@rob-ghost
rob-ghost force-pushed the feat/ber-3803-archive-restore-fields-api branch from 0438e0a to 83bea3f Compare July 17, 2026 10:30
ref BER-3803

Status now travels on the field definition so the list can group active vs
archived. Archive and restore are a single PUT status flip over the edit route,
matching how newsletters move between states, and the change is only written and
logged when the status actually flips. Browse hides archived fields by default
and takes a filter option (filter=status:[active,archived]) run through
nql -> mongo-knex; a filter not referencing status keeps the active-only default
and a malformed filter is a 400. DELETE now permanently removes the row
(FK-cascading member values) and is gated on the field already being archived.

Each lifecycle change (create, rename, archive, restore, delete) is recorded in
the action log with the field's name in the action context, so the timeline
stays legible after a hard delete when the row itself is gone; a rename also
records the previous name.

The Settings history log renders the definition lifecycle events
(member_custom_field) with a "Custom field" label instead of the raw resource
type, and the field name from the action context shows as the entry description.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant