From 8c513e93c8f07ec03d8ce4dd915d521ceea3a580 Mon Sep 17 00:00:00 2001 From: keyan Date: Wed, 16 Mar 2022 11:35:51 -0500 Subject: [PATCH] record sats spent on auction items --- components/item.js | 2 +- .../migration.sql | 38 +++++++++++++++++++ prisma/schema.prisma | 1 + 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 prisma/migrations/20220315171015_item_act_stream/migration.sql diff --git a/components/item.js b/components/item.js index e14dccdd..d49efc50 100644 --- a/components/item.js +++ b/components/item.js @@ -60,7 +60,7 @@ export function ItemJob ({ item, rank, children }) {
- {formatSats(item.maxBid)} sats + {formatSats(item.maxBid)} sats per min \ {item.ncomments} comments diff --git a/prisma/migrations/20220315171015_item_act_stream/migration.sql b/prisma/migrations/20220315171015_item_act_stream/migration.sql new file mode 100644 index 00000000..24390bf4 --- /dev/null +++ b/prisma/migrations/20220315171015_item_act_stream/migration.sql @@ -0,0 +1,38 @@ +-- AlterEnum +ALTER TYPE "ItemActType" ADD VALUE 'STREAM'; + +-- charge the user for the auction item +CREATE OR REPLACE FUNCTION run_auction(item_id INTEGER) RETURNS void AS $$ + DECLARE + bid INTEGER; + user_id INTEGER; + user_msats INTEGER; + item_status "Status"; + BEGIN + PERFORM ASSERT_SERIALIZED(); + + -- extract data we need + SELECT "maxBid" * 1000, "userId", status INTO bid, user_id, item_status FROM "Item" WHERE id = item_id; + SELECT msats INTO user_msats FROM users WHERE id = user_id; + + -- check if user wallet has enough sats + IF bid > user_msats THEN + -- if not, set status = NOSATS and statusUpdatedAt to now_utc if not already set + IF item_status <> 'NOSATS' THEN + UPDATE "Item" SET status = 'NOSATS', "statusUpdatedAt" = now_utc() WHERE id = item_id; + END IF; + ELSE + -- if so, deduct from user + UPDATE users SET msats = msats - bid WHERE id = user_id; + + -- create an item act + INSERT INTO "ItemAct" (sats, "itemId", "userId", act, created_at, updated_at) + VALUES (bid / 1000, item_id, user_id, 'STREAM', now_utc(), now_utc()); + + -- update item status = ACTIVE and statusUpdatedAt = null if NOSATS + IF item_status = 'NOSATS' THEN + UPDATE "Item" SET status = 'ACTIVE', "statusUpdatedAt" = now_utc() WHERE id = item_id; + END IF; + END IF; + END; +$$ LANGUAGE plpgsql; \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 375535da..98d044ed 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -176,6 +176,7 @@ enum ItemActType { VOTE BOOST TIP + STREAM } model ItemAct {