Merge pull request 'Zap model improvements' (#195) from chore/zap_model_improvements into master
All checks were successful
continuous-integration/drone/push Build is passing

Reviewed-on: #195
Reviewed-by: bumi <bumi@noreply.kosmos.org>
This commit is contained in:
Râu Cao 2024-06-09 11:16:34 +00:00
commit f0d5457ec1
5 changed files with 23 additions and 5 deletions

View File

@ -8,12 +8,13 @@ class WebhooksController < ApplicationController
@user = User.find_by!(ln_account: @payload[:user_login]) @user = User.find_by!(ln_account: @payload[:user_login])
if @zap = @user.zaps.find_by(payment_request: @payload[:payment_request]) if @zap = @user.zaps.find_by(payment_request: @payload[:payment_request])
settled_at = Time.parse(@payload[:settled_at])
zap_receipt = NostrManager::CreateZapReceipt.call( zap_receipt = NostrManager::CreateZapReceipt.call(
zap: @zap, zap: @zap,
paid_at: Time.parse(@payload[:settled_at]).to_i, paid_at: settled_at.to_i,
preimage: @payload[:preimage] 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) NostrManager::PublishZapReceipt.call(zap: @zap)
end end

View File

@ -1,6 +1,9 @@
class Zap < ApplicationRecord class Zap < ApplicationRecord
belongs_to :user belongs_to :user
scope :settled, -> { where.not(settled_at: nil) }
scope :unpaid, -> { where(settled_at: nil) }
def request_event def request_event
nostr_event_from_hash(request) nostr_event_from_hash(request)
end end

View File

@ -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

View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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| create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false t.string "name", null: false
t.string "record_type", 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.bigint "amount"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.datetime "settled_at"
t.index ["user_id"], name: "index_zaps_on_user_id" t.index ["user_id"], name: "index_zaps_on_user_id"
end end

View File

@ -167,10 +167,14 @@ RSpec.describe "Webhooks", type: :request do
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
end 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 it "creates and adds a zap receipt to the zap record" do
post "/webhooks/lndhub", params: payload.to_json post "/webhooks/lndhub", params: payload.to_json
zap = user.zaps.first expect(user.zaps.first.receipt).not_to be_nil
expect(zap.receipt).not_to be_nil
end end
it "publishes the zap receipt" do it "publishes the zap receipt" do