Made "record sent automation email" code more consistent#29399
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (6)
💤 Files with no reviewable changes (1)
🚧 Files skipped from review as they are similar to previous changes (4)
WalkthroughThe Suggested reviewers: 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
|
| Command | Status | Duration | Result |
|---|---|---|---|
nx run ghost:test:ci:integration |
✅ Succeeded | 2m 12s | View ↗ |
nx run ghost:test:integration |
✅ Succeeded | 2m 42s | View ↗ |
nx run ghost:test:legacy |
✅ Succeeded | 3m 8s | View ↗ |
nx run ghost:test:e2e |
✅ Succeeded | 2m 30s | View ↗ |
nx run @tryghost/admin:build |
✅ Succeeded | 5s | View ↗ |
nx run-many -t test:unit -p ghost |
✅ Succeeded | 4s | View ↗ |
nx run ghost-monorepo:lint:boundaries |
✅ Succeeded | <1s | View ↗ |
nx run-many -t lint -p ghost,ghost-monorepo |
✅ Succeeded | 1s | View ↗ |
nx run-many --target=build --projects=tag:publi... |
✅ Succeeded | <1s | View ↗ |
💡 Verify your cache is correct by running tasks in a sandbox. Read docs ↗
☁️ Nx Cloud last updated this comment at 2026-07-20 18:51:28 UTC
| return await knex.transaction(trx => retryStep(trx, step, retryAt)); | ||
| }, | ||
|
|
||
| async recordEmailSent(options): Promise<void> { |
There was a problem hiding this comment.
Here's a diff showing what actually changed:
@@ -1,5 +1,7 @@
-await knex.transaction(async (transacting: Knex.Transaction) => {
- await AutomatedEmailRecipient.add({
+await knex.transaction(async (trx) => {
+ const now = toDatabaseDate(new Date());
+ await trx("automated_email_recipients").insert({
+ id: ObjectId().toHexString(),
member_id: options.memberId,
member_uuid: options.memberUuid,
member_email: options.memberEmail,
@@ -9,12 +11,14 @@
? { mailgun_message_id: options.mailgunMessageId }
: {}),
track_opens: options.trackOpens,
- }, { transacting });
+ created_at: now,
+ updated_at: now,
+ });
- await transacting("automation_action_revisions")
+ await trx("automation_action_revisions")
.where("id", options.automationActionRevisionId)
.update({
- email_sent_count: transacting.raw("COALESCE(??, 0) + ?", [
+ email_sent_count: trx.raw("COALESCE(??, 0) + ?", [
"email_sent_count",
1,
]),| }); | ||
|
|
||
| describe('recordEmailSent', function () { | ||
| it('records the recipient and increments the action revision count', async function () { |
There was a problem hiding this comment.
Here's a diff showing what actually changed in this test:
-it("records the recipient and increments the action revision count in one transaction", async function () {
- const emailSentCountExpression = Symbol("email_sent_count_expression");
- const updateRevision = sinon.stub().resolves();
- const whereRevision = sinon.stub().returns({ update: updateRevision });
- const transacting = sinon.stub().withArgs("automation_action_revisions")
- .returns({ where: whereRevision });
- transacting.raw = sinon.stub().returns(emailSentCountExpression);
- const transaction = sinon.stub(db.knex, "transaction").callsFake(
- async (callback) => {
- return await callback(transacting);
- },
- );
- const addRecipient = sinon.stub(AutomatedEmailRecipient, "add").resolves();
+it("records the recipient and increments the action revision count", async function () {
+ const revision = await knex("automation_action_revisions").select("id")
+ .first();
+ assert(revision);
- await automationsApi.recordEmailSent({
- automationActionRevisionId: "revision-id",
+ await repo.recordEmailSent({
+ automationActionRevisionId: revision.id,
mailgunMessageId: "mailgun-message-id",
memberEmail: "member@example.com",
memberId: "member-id",
@@ -22,22 +13,28 @@
trackOpens: true,
});
- sinon.assert.calledOnce(transaction);
- sinon.assert.calledOnceWithExactly(addRecipient, {
+ const recipient = await knex("automated_email_recipients").first();
+ assert.deepEqual(recipient, {
+ id: recipient.id,
+ automation_action_revision_id: revision.id,
member_id: "member-id",
member_uuid: "00000000-0000-4000-8000-000000000001",
member_email: "member@example.com",
member_name: "Test Member",
- automation_action_revision_id: "revision-id",
mailgun_message_id: "mailgun-message-id",
- track_opens: true,
- }, { transacting });
- sinon.assert.calledOnceWithExactly(whereRevision, "id", "revision-id");
- sinon.assert.calledOnceWithExactly(transacting.raw, "COALESCE(??, 0) + ?", [
- "email_sent_count",
- 1,
- ]);
- sinon.assert.calledOnceWithExactly(updateRevision, {
- email_sent_count: emailSentCountExpression,
+ delivered_at: null,
+ opened_at: null,
+ track_opens: 1,
+ created_at: recipient.created_at,
+ updated_at: recipient.updated_at,
});
+ assert(ObjectId.isValid(recipient.id));
+ assert.equal(typeof recipient.created_at, "string");
+ assert.equal(recipient.updated_at, recipient.created_at);
+
+ const updatedRevision = await knex("automation_action_revisions")
+ .select("email_sent_count")
+ .where("id", revision.id)
+ .first();
+ assert.equal(updatedRevision.email_sent_count, 1);
});
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #29399 +/- ##
==========================================
- Coverage 74.53% 74.50% -0.04%
==========================================
Files 1602 1602
Lines 140399 140406 +7
Branches 17060 17057 -3
==========================================
- Hits 104646 104609 -37
- Misses 34733 34743 +10
- Partials 1020 1054 +34
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
closes https://linear.app/ghost/issue/NY-1459 `automationsApi.recordEmailSent` was different from the others: it hit the database directly. Now it's part of the automations repository just like the rest. This change should have no user impact.
91c589d to
95900d3
Compare

closes https://linear.app/ghost/issue/NY-1459
automationsApi.recordEmailSentwas different from the others: it hit the database directly. Now it's part of the automations repository just like the rest.This cleanup should have no user impact.