Skip to content

Extracted gift-links side effects into ports#28977

Merged
jonatansberg merged 2 commits into
mainfrom
refactor/gift-links-cqrs
Jul 7, 2026
Merged

Extracted gift-links side effects into ports#28977
jonatansberg merged 2 commits into
mainfrom
refactor/gift-links-cqrs

Conversation

@rob-ghost

@rob-ghost rob-ghost commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Problem

The gift-links service bundled several unrelated concerns into one file — logging actions, mapping database rows, generating tokens, and the core read/write logic — which blurred the boundaries and made the service harder to follow.

Solution

Kept a single service but gave each concern its own home: action logging is now an injected dependency, row mapping and token generation moved out, and the schema definition was renamed for clarity. The slower database-backed service-level tests were replaced with faster unit tests plus end-to-end coverage of the not-found and concurrency behaviours.

@coderabbitai

coderabbitai Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
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.
Title check ✅ Passed The title matches the refactor theme and highlights extracted side effects, a central part of the change.
Description check ✅ Passed The description is clearly about the gift-links refactor and extracted concerns, so it is related to the changeset.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/gift-links-cqrs

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 Jun 29, 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 8897359

Command Status Duration Result
nx run ghost:test:ci:integration ✅ Succeeded 2m 47s View ↗
nx run ghost:test:integration ✅ Succeeded 2m 49s View ↗
nx run ghost:test:legacy ✅ Succeeded 2m 50s View ↗
nx run ghost:test:e2e ✅ Succeeded 2m 27s View ↗
nx run-many --target=build --projects=tag:publi... ✅ Succeeded <1s View ↗
nx run-many -t test:unit -p ghost ✅ Succeeded 29s View ↗
nx run @tryghost/admin:build ✅ Succeeded 9s View ↗
nx run-many -t lint -p ghost ✅ Succeeded 33s View ↗
Additional runs (2) ✅ Succeeded ... View ↗

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


☁️ Nx Cloud last updated this comment at 2026-07-07 08:00:05 UTC

@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/gift-links/commands.ts`:
- Around line 19-26: The `GiftLinksCommands.ensure()` flow is not atomic: it
checks `post.giftLinks` before the write path, while `mint()` merges on
`post_id`, so concurrent calls can mint different tokens and return one that is
later overwritten. Move the “no gift link exists” check into the transaction
used by `mint()`/the ensure path, and use an insert-or-lock strategy so only one
call can create the association. Make `ensure()` return the live association
created or observed in that transaction, and call `recordAction({context, verb:
'add', subject: postId})` only when this invocation actually created the link.
🪄 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: c8a01828-04e2-4700-9f03-55892801828d

📥 Commits

Reviewing files that changed from the base of the PR and between 03bed4a and f4a20bc.

📒 Files selected for processing (19)
  • ghost/core/core/frontend/services/proxy.js
  • ghost/core/core/frontend/services/routing/controllers/entry/gift-links.ts
  • ghost/core/core/server/api/endpoints/gift-links.ts
  • ghost/core/core/server/api/endpoints/utils/serializers/output/gift-links.ts
  • ghost/core/core/server/services/gift-links/actions.ts
  • ghost/core/core/server/services/gift-links/codec.ts
  • ghost/core/core/server/services/gift-links/commands.ts
  • ghost/core/core/server/services/gift-links/index.ts
  • ghost/core/core/server/services/gift-links/models.ts
  • ghost/core/core/server/services/gift-links/queries.ts
  • ghost/core/core/server/services/gift-links/schema.ts
  • ghost/core/core/server/services/gift-links/serializers.ts
  • ghost/core/core/server/services/gift-links/service.ts
  • ghost/core/test/e2e-api/admin/gift-links.test.ts
  • ghost/core/test/e2e-frontend/gift-links-toast-override.test.ts
  • ghost/core/test/e2e-frontend/gift-links.test.ts
  • ghost/core/test/integration/services/gift-links.test.ts
  • ghost/core/test/unit/server/services/gift-links/actions.test.ts
  • ghost/core/test/unit/server/services/gift-links/gift-link-token.test.ts
💤 Files with no reviewable changes (4)
  • ghost/core/test/integration/services/gift-links.test.ts
  • ghost/core/core/server/services/gift-links/serializers.ts
  • ghost/core/core/server/services/gift-links/schema.ts
  • ghost/core/core/server/services/gift-links/service.ts

Comment thread ghost/core/core/server/services/gift-links/service.ts
@rob-ghost
rob-ghost force-pushed the refactor/gift-links-cqrs branch from f4a20bc to cf704c4 Compare June 29, 2026 22:53
@rob-ghost rob-ghost self-assigned this Jun 29, 2026
@rob-ghost
rob-ghost force-pushed the refactor/gift-links-cqrs branch from cf704c4 to 69523d0 Compare July 2, 2026 21:21
@rob-ghost rob-ghost changed the title Changed gift-links service to split CQRS queries and commands Extracted gift-links side effects into ports Jul 2, 2026
@rob-ghost
rob-ghost marked this pull request as ready for review July 2, 2026 21:45
@rob-ghost rob-ghost added the ok to merge for me You can merge this on my behalf if you want. label Jul 2, 2026
Kept a single GiftLinksService but pulled the action-log write out into
actions.ts (injected as a port rather than reaching for the model), moved
the DB row<->domain codec into codec.ts, renamed database.ts to schema.ts,
and moved token generation onto the model. Replaced the DB-backed
service-level integration suite with unit tests plus HTTP-level e2e
coverage for the not-found and concurrency cases.
@rob-ghost
rob-ghost force-pushed the refactor/gift-links-cqrs branch from 69523d0 to ae7e092 Compare July 3, 2026 23:36
@cursor

cursor Bot commented Jul 3, 2026

Copy link
Copy Markdown

Bugbot is not enabled for this team, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

ref #28977

- the ports refactor swapped the integration suite for e2e + unit tests, leaving several service-level behaviours unpinned: revocation of a replaced token, idempotent ensure, removeAll's cross-post scope, the DB-enforced one-live-link invariant, and best-effort action recording as init() composes it
- these need a real database but no HTTP, so they are pinned at the integration layer instead of growing the e2e suites
- also strengthened the concurrent-ensures e2e assertion so last-writer-wins is actually asserted: the surviving live token must be one a caller received
@cursor

cursor Bot commented Jul 7, 2026

Copy link
Copy Markdown

Bugbot is not enabled for this team, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

@jonatansberg
jonatansberg merged commit ed6063a into main Jul 7, 2026
43 checks passed
@jonatansberg
jonatansberg deleted the refactor/gift-links-cqrs branch July 7, 2026 08:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ok to merge for me You can merge this on my behalf if you want.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants