-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add insider discounts * Prepare stripe for 100% off coupons * Remove typo
- Loading branch information
Showing
8 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
app/commands/user/generate_bootcamp_affiliate_coupon_code.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class User::GenerateBootcampAffiliateCouponCode | ||
include Mandate | ||
|
||
initialize_with :user | ||
|
||
def call | ||
# Easy cheap guard | ||
return if user_data.bootcamp_affiliate_coupon_code.present? | ||
|
||
# Now things get expensive with Stripe call and lock below | ||
code = generate_coupon_code | ||
user_data.with_lock do | ||
return if user_data.bootcamp_affiliate_coupon_code.present? | ||
|
||
user_data.update!(bootcamp_affiliate_coupon_code: code) | ||
end | ||
end | ||
|
||
private | ||
delegate :data, to: :user, prefix: true | ||
|
||
memoize | ||
def generate_coupon_code | ||
promo_code = Stripe::PromotionCode.create( | ||
coupon: COUPON_ID, | ||
metadata: { | ||
user_id: user.id | ||
} | ||
) | ||
promo_code.code | ||
end | ||
|
||
COUPON_ID = "NRp5SOVV".freeze | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
class User::GenerateBootcampFreeCouponCode | ||
include Mandate | ||
|
||
initialize_with :user | ||
|
||
def call | ||
# Easy cheap guard | ||
return if user_data.bootcamp_free_coupon_code.present? | ||
|
||
# Now things get expensive with Stripe call and lock below | ||
code = generate_coupon_code | ||
user_data.with_lock do | ||
return if user_data.bootcamp_free_coupon_code.present? | ||
|
||
user_data.update!(bootcamp_free_coupon_code: code) | ||
end | ||
end | ||
|
||
private | ||
delegate :data, to: :user, prefix: true | ||
|
||
memoize | ||
def generate_coupon_code | ||
promo_code = Stripe::PromotionCode.create( | ||
coupon: COUPON_ID, | ||
max_redemptions: 1, | ||
metadata: { | ||
user_id: user.id | ||
} | ||
) | ||
promo_code.code | ||
end | ||
|
||
COUPON_ID = "OarhoKrd".freeze | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
require 'stripe' | ||
|
||
Stripe.api_key = Exercism.secrets.stripe_secret_key | ||
Stripe.api_version = '2024-10-28.acacia' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class AddInsiderDiscounts < ActiveRecord::Migration[7.0] | ||
def change | ||
return if Rails.env.production? | ||
|
||
add_column :user_data, :bootcamp_affiliate_coupon_code, :string, null: true | ||
add_column :user_data, :bootcamp_free_coupon_code, :string, null: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
test/models/user/generate_bootcamp_affiliate_coupon_code_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require "test_helper" | ||
|
||
class User::GenerateBootcampAffiliateCouponCodeTest < ActiveSupport::TestCase | ||
test "creates correctly" do | ||
user = create :user | ||
code = SecureRandom.hex(6) | ||
Stripe::PromotionCode.expects(:create).with( | ||
coupon: "NRp5SOVV", metadata: { user_id: user.id } | ||
).returns(OpenStruct.new(code:)) | ||
|
||
User::GenerateBootcampAffiliateCouponCode.(user) | ||
|
||
assert_equal code, user.bootcamp_affiliate_coupon_code | ||
end | ||
|
||
test "immutable" do | ||
code = SecureRandom.hex(6) | ||
user = create :user, bootcamp_affiliate_coupon_code: code | ||
Stripe::PromotionCode.expects(:create).never | ||
|
||
User::GenerateBootcampAffiliateCouponCode.(user) | ||
|
||
assert_equal code, user.bootcamp_affiliate_coupon_code | ||
end | ||
end |
27 changes: 27 additions & 0 deletions
27
test/models/user/generate_bootcamp_free_coupon_code_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require "test_helper" | ||
|
||
class User::GenerateBootcampFreeCouponCodeTest < ActiveSupport::TestCase | ||
test "creates correctly" do | ||
user = create :user | ||
code = SecureRandom.hex(6) | ||
Stripe::PromotionCode.expects(:create).with( | ||
coupon: "OarhoKrd", | ||
max_redemptions: 1, | ||
metadata: { user_id: user.id } | ||
).returns(OpenStruct.new(code:)) | ||
|
||
User::GenerateBootcampFreeCouponCode.(user) | ||
|
||
assert_equal code, user.bootcamp_free_coupon_code | ||
end | ||
|
||
test "immutable" do | ||
code = SecureRandom.hex(6) | ||
user = create :user, bootcamp_free_coupon_code: code | ||
Stripe::PromotionCode.expects(:create).never | ||
|
||
User::GenerateBootcampFreeCouponCode.(user) | ||
|
||
assert_equal code, user.bootcamp_free_coupon_code | ||
end | ||
end |