record sats spent on auction items

This commit is contained in:
keyan 2022-03-16 11:35:51 -05:00
parent 4a43a9b864
commit 8c513e93c8
3 changed files with 40 additions and 1 deletions

View File

@ -60,7 +60,7 @@ export function ItemJob ({ item, rank, children }) {
</a> </a>
</div> </div>
<div className={`${styles.other}`}> <div className={`${styles.other}`}>
<span>{formatSats(item.maxBid)} sats</span> <span>{formatSats(item.maxBid)} sats per min</span>
<span> \ </span> <span> \ </span>
<Link href={`/items/${item.id}`} passHref> <Link href={`/items/${item.id}`} passHref>
<a className='text-reset'>{item.ncomments} comments</a> <a className='text-reset'>{item.ncomments} comments</a>

View File

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

View File

@ -176,6 +176,7 @@ enum ItemActType {
VOTE VOTE
BOOST BOOST
TIP TIP
STREAM
} }
model ItemAct { model ItemAct {