# frozen_string_literal: true require "value_semantics/monkey_patched" require_relative "trust_level" class TrustLevelRepo value_semantics do db Anything(), default: LazyObject.new { DB } redis Anything(), default: LazyObject.new { REDIS } end def find(customer) EMPromise.all([ redis.get("jmp_customer_trust_level-#{customer.customer_id}"), fetch_settled_amount(customer.customer_id) ]).then do |(manual, row)| TrustLevel.for( manual: manual, plan_name: customer.plan_name, **row ) end end protected def fetch_settled_amount(customer_id) db.query_one(<<~SQL, customer_id, default: {}) SELECT COALESCE(SUM(amount), 0) AS settled_amount FROM transactions WHERE customer_id=$1 AND settled_after < LOCALTIMESTAMP AND amount > 0 SQL end end