1# frozen_string_literal: true
 2
 3require "forwardable"
 4require "ruby-bandwidth-iris"
 5Faraday.default_adapter = :em_synchrony
 6
 7class BandwidthTnReservationRepo
 8	FOUR_HOURS = 60 * 60 * 4
 9
10	def initialize(redis: REDIS)
11		@redis = redis
12	end
13
14	def ensure(customer, tel)
15		REDIS.exists(key(customer, tel)).then do |exists|
16			unless exists == 1
17				reservation_id = BandwidthIris::TnReservation.create(tel).reservation_id
18				REDIS.setex(key(customer, tel), FOUR_HOURS, reservation_id)
19			end
20		end
21	end
22
23	def get(customer, tel)
24		REDIS.get(key(customer, tel))
25	end
26
27	def key(customer, tel)
28		"jmp_customer_tn_reservation_#{tel}-#{customer.customer_id}"
29	end
30end