Skip to content

Commit 2bfcc07

Browse files
committed
feat(coupons): add lifecycle webhooks
coupon.created/updated/deleted webhook event names already existed (added for activity logs), but nothing ever fired them -- sibling catalog resources Plan/BillableMetric/Feature all fire webhooks on create/update/destroy, Coupon didn't. Adds Webhooks::Coupons::{Created,Updated,Deleted}Service mirroring the existing BillableMetrics webhook services, registers them in SendWebhookJob::WEBHOOK_SERVICES, and wires the SendWebhookJob calls into Coupons::{Create,Update,Destroy}Service.
1 parent a24f3ab commit 2bfcc07

14 files changed

Lines changed: 164 additions & 0 deletions

app/jobs/send_webhook_job.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ class SendWebhookJob < ApplicationJob
2121
"billable_metric.created" => Webhooks::BillableMetrics::CreatedService,
2222
"billable_metric.updated" => Webhooks::BillableMetrics::UpdatedService,
2323
"billable_metric.deleted" => Webhooks::BillableMetrics::DeletedService,
24+
"coupon.created" => Webhooks::Coupons::CreatedService,
25+
"coupon.updated" => Webhooks::Coupons::UpdatedService,
26+
"coupon.deleted" => Webhooks::Coupons::DeletedService,
2427
"dunning_campaign.finished" => Webhooks::DunningCampaigns::FinishedService,
2528
"invoice.created" => Webhooks::Invoices::CreatedService,
2629
"invoice.one_off_created" => Webhooks::Invoices::OneOffCreatedService,

app/services/coupons/create_service.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def call
6363
end
6464

6565
result.coupon = coupon
66+
SendWebhookJob.perform_after_commit("coupon.created", coupon)
6667
result
6768
rescue ActiveRecord::RecordInvalid => e
6869
result.record_validation_failure!(record: e.record)

app/services/coupons/destroy_service.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def call
2727
end
2828

2929
result.coupon = coupon
30+
SendWebhookJob.perform_after_commit("coupon.deleted", coupon)
3031
result
3132
end
3233

app/services/coupons/update_service.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def call
7171
end
7272

7373
result.coupon = coupon
74+
SendWebhookJob.perform_after_commit("coupon.updated", coupon)
7475
result
7576
rescue ActiveRecord::RecordInvalid => e
7677
result.record_validation_failure!(record: e.record)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
module Webhooks
4+
module Coupons
5+
class CreatedService < Webhooks::BaseService
6+
private
7+
8+
def object_serializer
9+
::V1::CouponSerializer.new(
10+
object,
11+
root_name: "coupon"
12+
)
13+
end
14+
15+
def webhook_type
16+
"coupon.created"
17+
end
18+
19+
def object_type
20+
"coupon"
21+
end
22+
end
23+
end
24+
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
module Webhooks
4+
module Coupons
5+
class DeletedService < Webhooks::BaseService
6+
private
7+
8+
def object_serializer
9+
::V1::CouponSerializer.new(
10+
object,
11+
root_name: "coupon"
12+
)
13+
end
14+
15+
def webhook_type
16+
"coupon.deleted"
17+
end
18+
19+
def object_type
20+
"coupon"
21+
end
22+
end
23+
end
24+
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
module Webhooks
4+
module Coupons
5+
class UpdatedService < Webhooks::BaseService
6+
private
7+
8+
def object_serializer
9+
::V1::CouponSerializer.new(
10+
object,
11+
root_name: "coupon"
12+
)
13+
end
14+
15+
def webhook_type
16+
"coupon.updated"
17+
end
18+
19+
def object_type
20+
"coupon"
21+
end
22+
end
23+
end
24+
end

spec/jobs/send_webhook_job_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,22 @@
741741
Webhooks::BillableMetrics::DeletedService
742742
end
743743

744+
context "with coupon webhooks" do
745+
let(:object) { create(:coupon, organization:) }
746+
747+
it_behaves_like "a webhook service",
748+
"coupon.created",
749+
Webhooks::Coupons::CreatedService
750+
751+
it_behaves_like "a webhook service",
752+
"coupon.updated",
753+
Webhooks::Coupons::UpdatedService
754+
755+
it_behaves_like "a webhook service",
756+
"coupon.deleted",
757+
Webhooks::Coupons::DeletedService
758+
end
759+
744760
context "with quote webhooks" do
745761
let(:object) { create(:quote_version, organization:) }
746762

spec/services/coupons/create_service_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
expect(Utils::ActivityLog).to have_produced("coupon.created").after_commit.with(coupon)
3939
end
4040

41+
it "enqueues a coupon.created webhook" do
42+
result = create_service.call
43+
44+
expect(SendWebhookJob).to have_been_enqueued.with("coupon.created", result.coupon)
45+
end
46+
4147
context "with code already used by a deleted coupon" do
4248
it "creates an coupon with the same code" do
4349
create(:coupon, :deleted, organization:, code: coupon_code)

spec/services/coupons/destroy_service_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535
expect(Utils::ActivityLog).to have_produced("coupon.deleted").after_commit.with(coupon)
3636
end
3737

38+
it "enqueues a coupon.deleted webhook" do
39+
result = destroy_service.call
40+
41+
expect(SendWebhookJob).to have_been_enqueued.with("coupon.deleted", result.coupon)
42+
end
43+
3844
context "with applied coupons" do
3945
it "terminates applied coupons" do
4046
applied_coupon = create(:applied_coupon, coupon:)

0 commit comments

Comments
 (0)