~singpolyma/sgx-jmp

sgx-jmp/lib/transaction.rb -rw-r--r-- 1.8 KiB
b8aa8ed3Stephen Paul Weber Merge branch 'feature_flags' 2 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# frozen_string_literal: true

require "bigdecimal"
require "time"
require "value_semantics/monkey_patched"

class Transaction
	value_semantics do
		customer_id String
		transaction_id String
		created_at Time, coerce: ->(x) { Time.parse(x.to_s) }
		settled_after Time, coerce: ->(x) { Time.parse(x.to_s) }
		amount BigDecimal, coerce: ->(x) { BigDecimal(x, 4) }
		note String
		bonus_eligible? Bool(), default: true
	end

	def insert
		EM.promise_fiber do
			DB.transaction do
				insert_tx
				insert_bonus
			end
		end
	end

	def delete
		ids = [@transaction_id, "bonus_for_#{@transaction_id}"]
		DB.query_defer(<<~SQL, ids)
			DELETE FROM transactions WHERE transaction_id IN ($1, $2)
		SQL
	end

	def exists?
		DB.query_one(<<~SQL, @transaction_id).then { |r| r[:count] == 1 }
			SELECT COUNT(1) FROM transactions
			WHERE transaction_id = $1
		SQL
	end

	def total
		amount + bonus
	end

	def bonus
		return BigDecimal(0) unless bonus_eligible? && amount > 15

		amount *
			case amount
			when (15..29.99)
				0.01
			when (30..139.99)
				0.03
			else
				0.05
			end
	end

	def to_s
		plus = " + #{'%.4f' % bonus} bonus"
		"$#{'%.2f' % amount}#{plus if bonus.positive?}"
	end

protected

	def insert_tx
		params = [
			@customer_id, @transaction_id, @created_at, @settled_after, @amount, @note
		]
		DB.exec(<<~SQL, params)
			INSERT INTO transactions
				(customer_id, transaction_id, created_at, settled_after, amount, note)
			VALUES
				($1, $2, $3, $4, $5, $6)
		SQL
	end

	def insert_bonus
		return if bonus <= 0

		params = [
			@customer_id, "bonus_for_#{@transaction_id}", @created_at,
			@settled_after, bonus, "#{@note} bonus"
		]
		DB.exec(<<~SQL, params)
			INSERT INTO transactions
				(customer_id, transaction_id, created_at, settled_after, amount, note)
			VALUES
				($1, $2, $3, $4, $5, $6)
		SQL
	end
end