~singpolyma/sgx-jmp

917096eb4400f3e6247d719f6bd75759ef93be78 — Stephen Paul Weber 1 year, 9 months ago 97df926
Only allow one credit card transaction per customer per day
2 files changed, 16 insertions(+), 1 deletions(-)

M lib/transaction.rb
M test/test_transaction.rb
M lib/transaction.rb => lib/transaction.rb +3 -1
@@ 18,9 18,11 @@ class Transaction

	def self.resolve_payment_method(customer, payment_method)
		EMPromise.all([
			ExpiringLock.new("jmp_customer_credit_card_lock-#{customer.customer_id}")
				.with(els: -> { raise "Too many transactions today" }) { nil },
			customer.declines,
			payment_method || customer.payment_methods.then(&:default_payment_method)
		]).then do |(declines, selected_method)|
		]).then do |(_, declines, selected_method)|
			raise "Declined" if declines >= 2
			raise "No valid payment method on file" unless selected_method


M test/test_transaction.rb => test/test_transaction.rb +13 -0
@@ 7,6 7,7 @@ require "transaction"
Transaction::DB = Minitest::Mock.new
Transaction::BRAINTREE = Minitest::Mock.new
Transaction::REDIS = Minitest::Mock.new
ExpiringLock::REDIS = Minitest::Mock.new

class TransactionTest < Minitest::Test
	FAKE_BRAINTREE_TRANSACTION =


@@ 18,6 19,11 @@ class TransactionTest < Minitest::Test
		)

	def test_sale_fails
		ExpiringLock::REDIS.expect(
			:set,
			EMPromise.resolve("OK"),
			["jmp_customer_credit_card_lock-test", Time, "EX", 86400, "NX"]
		)
		CustomerFinancials::REDIS.expect(
			:get,
			EMPromise.resolve("1"),


@@ 50,10 56,16 @@ class TransactionTest < Minitest::Test
			).sync
		end
		assert_mock CustomerFinancials::REDIS
		assert_mock ExpiringLock::REDIS
	end
	em :test_sale_fails

	def test_sale
		ExpiringLock::REDIS.expect(
			:set,
			EMPromise.resolve("OK"),
			["jmp_customer_credit_card_lock-test", Time, "EX", 86400, "NX"]
		)
		CustomerFinancials::REDIS.expect(
			:get,
			EMPromise.resolve("1"),


@@ 83,6 95,7 @@ class TransactionTest < Minitest::Test
		).sync
		assert_kind_of Transaction, result
		assert_mock CustomerFinancials::REDIS
		assert_mock ExpiringLock::REDIS
	end
	em :test_sale