2022-02-28 20:09:21 +00:00
|
|
|
ALTER TABLE "Item" RENAME COLUMN "noSatsAt" TO "statusUpdatedAt";
|
|
|
|
|
2022-02-25 17:34:09 +00:00
|
|
|
-- 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
|
2022-02-28 20:09:21 +00:00
|
|
|
SELECT ("maxBid" * 5 / 216), "userId", status INTO bid, user_id, item_status FROM "Item" WHERE id = item_id;
|
2022-02-25 17:34:09 +00:00
|
|
|
SELECT msats INTO user_msats FROM users WHERE id = user_id;
|
|
|
|
|
|
|
|
-- check if user wallet has enough sats
|
|
|
|
IF bid > user_msats THEN
|
2022-02-28 20:09:21 +00:00
|
|
|
-- if not, set status = NOSATS and statusUpdatedAt to now_utc if not already set
|
2022-02-25 17:34:09 +00:00
|
|
|
IF item_status <> 'NOSATS' THEN
|
2022-02-28 20:09:21 +00:00
|
|
|
UPDATE "Item" SET status = 'NOSATS', "statusUpdatedAt" = now_utc() WHERE id = item_id;
|
2022-02-25 17:34:09 +00:00
|
|
|
END IF;
|
|
|
|
ELSE
|
|
|
|
-- if so, deduct from user
|
|
|
|
UPDATE users SET msats = msats - bid WHERE id = user_id;
|
2022-02-28 20:09:21 +00:00
|
|
|
-- update item status = ACTIVE and statusUpdatedAt = null if NOSATS
|
2022-02-25 17:34:09 +00:00
|
|
|
IF item_status = 'NOSATS' THEN
|
2022-02-28 20:09:21 +00:00
|
|
|
UPDATE "Item" SET status = 'ACTIVE', "statusUpdatedAt" = now_utc() WHERE id = item_id;
|
2022-02-25 17:34:09 +00:00
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
END;
|
|
|
|
$$ LANGUAGE plpgsql;
|