From 5588e3b3e8d7552a22d6ec34fe466001be5fc9a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Fri, 7 Jun 2024 14:53:00 +0200 Subject: [PATCH] Add settled_at to zaps, scope by settlement status --- app/controllers/webhooks_controller.rb | 5 +++-- app/models/zap.rb | 3 +++ db/migrate/20240607123654_add_settled_at_to_zaps.rb | 9 +++++++++ db/schema.rb | 3 ++- spec/requests/webhooks_spec.rb | 8 ++++++-- 5 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20240607123654_add_settled_at_to_zaps.rb diff --git a/app/controllers/webhooks_controller.rb b/app/controllers/webhooks_controller.rb index 0b0fcde..27635e3 100644 --- a/app/controllers/webhooks_controller.rb +++ b/app/controllers/webhooks_controller.rb @@ -8,12 +8,13 @@ class WebhooksController < ApplicationController @user = User.find_by!(ln_account: @payload[:user_login]) if @zap = @user.zaps.find_by(payment_request: @payload[:payment_request]) + settled_at = Time.parse(@payload[:settled_at]) zap_receipt = NostrManager::CreateZapReceipt.call( zap: @zap, - paid_at: Time.parse(@payload[:settled_at]).to_i, + paid_at: settled_at.to_i, preimage: @payload[:preimage] ) - @zap.update! receipt: zap_receipt.to_h + @zap.update! settled_at: settled_at, receipt: zap_receipt.to_h NostrManager::PublishZapReceipt.call(zap: @zap) end diff --git a/app/models/zap.rb b/app/models/zap.rb index ef839a2..a522447 100644 --- a/app/models/zap.rb +++ b/app/models/zap.rb @@ -1,6 +1,9 @@ class Zap < ApplicationRecord belongs_to :user + scope :settled, -> { where.not(settled_at: nil) } + scope :unpaid, -> { where(settled_at: nil) } + def request_event nostr_event_from_hash(request) end diff --git a/db/migrate/20240607123654_add_settled_at_to_zaps.rb b/db/migrate/20240607123654_add_settled_at_to_zaps.rb new file mode 100644 index 0000000..ff76975 --- /dev/null +++ b/db/migrate/20240607123654_add_settled_at_to_zaps.rb @@ -0,0 +1,9 @@ +class AddSettledAtToZaps < ActiveRecord::Migration[7.1] + def change + add_column :zaps, :settled_at, :datetime, default: nil + + Zap.where.not(receipt: nil).each do |zap| + zap.update! settled_at: zap.receipt_event.created_at + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 8400be3..90a60e6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_04_22_171653) do +ActiveRecord::Schema[7.1].define(version: 2024_06_07_123654) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false @@ -144,6 +144,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_04_22_171653) do t.bigint "amount" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.datetime "settled_at" t.index ["user_id"], name: "index_zaps_on_user_id" end diff --git a/spec/requests/webhooks_spec.rb b/spec/requests/webhooks_spec.rb index 2851bb2..5a1d12f 100644 --- a/spec/requests/webhooks_spec.rb +++ b/spec/requests/webhooks_spec.rb @@ -167,10 +167,14 @@ RSpec.describe "Webhooks", type: :request do expect(response).to have_http_status(:ok) end + it "adds the settlement date/time to the zap record" do + post "/webhooks/lndhub", params: payload.to_json + expect(user.zaps.first.settled_at.to_i).to eq(1673428978) + end + it "creates and adds a zap receipt to the zap record" do post "/webhooks/lndhub", params: payload.to_json - zap = user.zaps.first - expect(zap.receipt).not_to be_nil + expect(user.zaps.first.receipt).not_to be_nil end it "publishes the zap receipt" do