From e084e9e89fef5670f40f365edb04f9a989faf656 Mon Sep 17 00:00:00 2001 From: keyan Date: Wed, 19 Jan 2022 17:00:20 -0600 Subject: [PATCH] force upvote on variable tip --- components/upvote.js | 4 +- .../migration.sql | 45 +++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 prisma/migrations/20220119221313_item_act_var_tip_bug/migration.sql diff --git a/components/upvote.js b/components/upvote.js index 337c88cf..070f9f6d 100644 --- a/components/upvote.js +++ b/components/upvote.js @@ -244,9 +244,9 @@ export default function UpVote ({ item, className }) { `${styles.upvote} ${className || ''} ${noSelfTips ? styles.noSelfTips : ''} - ${item?.meVote ? styles.voted : ''}` + ${item?.meSats ? styles.voted : ''}` } - style={item?.meVote + style={item?.meSats ? { fill: color, filter: `drop-shadow(0 0 6px ${color}90)` diff --git a/prisma/migrations/20220119221313_item_act_var_tip_bug/migration.sql b/prisma/migrations/20220119221313_item_act_var_tip_bug/migration.sql new file mode 100644 index 00000000..8896503e --- /dev/null +++ b/prisma/migrations/20220119221313_item_act_var_tip_bug/migration.sql @@ -0,0 +1,45 @@ +CREATE OR REPLACE FUNCTION item_act(item_id INTEGER, user_id INTEGER, act "ItemActType", act_sats INTEGER) +RETURNS INTEGER +LANGUAGE plpgsql +AS $$ +DECLARE + user_sats INTEGER; +BEGIN + PERFORM ASSERT_SERIALIZED(); + + SELECT (msats / 1000) INTO user_sats FROM users WHERE id = user_id; + IF act_sats > user_sats THEN + RAISE EXCEPTION 'SN_INSUFFICIENT_FUNDS'; + END IF; + + -- deduct sats from actor + UPDATE users SET msats = msats - (act_sats * 1000) WHERE id = user_id; + + IF act = 'BOOST' THEN + INSERT INTO "ItemAct" (sats, "itemId", "userId", act, created_at, updated_at) + VALUES (act_sats, item_id, user_id, 'BOOST', now_utc(), now_utc()); + ELSE + -- add sats to actee + UPDATE users SET msats = msats + (act_sats * 1000) WHERE id = (SELECT "userId" FROM "Item" WHERE id = item_id); + + -- if they have already voted, this is a tip + IF EXISTS (SELECT 1 FROM "ItemAct" WHERE "itemId" = item_id AND "userId" = user_id AND "ItemAct".act = 'VOTE') THEN + INSERT INTO "ItemAct" (sats, "itemId", "userId", act, created_at, updated_at) + VALUES (act_sats, item_id, user_id, 'TIP', now_utc(), now_utc()); + ELSE + -- else this is a vote with a possible extra tip + INSERT INTO "ItemAct" (sats, "itemId", "userId", act, created_at, updated_at) + VALUES (1, item_id, user_id, 'VOTE', now_utc(), now_utc()); + act_sats := act_sats - 1; + + -- if we have sats left after vote, leave them as a tip + IF act_sats > 0 THEN + INSERT INTO "ItemAct" (sats, "itemId", "userId", act, created_at, updated_at) + VALUES (act_sats, item_id, user_id, 'TIP', now_utc(), now_utc()); + END IF; + END IF; + END IF; + + RETURN act_sats; +END; +$$; \ No newline at end of file