From d948a653c94e099895394e66ff240bc161f3e337 Mon Sep 17 00:00:00 2001 From: keyan Date: Mon, 8 May 2023 18:10:41 -0500 Subject: [PATCH] add earned sats to items --- api/resolvers/notifications.js | 2 +- .../20230508223532_earn_add/migration.sql | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 prisma/migrations/20230508223532_earn_add/migration.sql diff --git a/api/resolvers/notifications.js b/api/resolvers/notifications.js index 7ec0ae6e..3ef9d8b3 100644 --- a/api/resolvers/notifications.js +++ b/api/resolvers/notifications.js @@ -106,7 +106,7 @@ export default { if (meFull.noteItemSats) { queries.push( `(SELECT "Item".id::TEXT, MAX("ItemAct".created_at) AS "sortTime", - floor(sum("ItemAct".msats)/1000) as "earnedSats", 'Votification' AS type + MAX("Item".msats/1000) as "earnedSats", 'Votification' AS type FROM "Item" JOIN "ItemAct" ON "ItemAct"."itemId" = "Item".id WHERE "ItemAct"."userId" <> $1 diff --git a/prisma/migrations/20230508223532_earn_add/migration.sql b/prisma/migrations/20230508223532_earn_add/migration.sql new file mode 100644 index 00000000..22f05206 --- /dev/null +++ b/prisma/migrations/20230508223532_earn_add/migration.sql @@ -0,0 +1,20 @@ +CREATE OR REPLACE FUNCTION earn(user_id INTEGER, earn_msats BIGINT, created_at TIMESTAMP(3), + type "EarnType", type_id INTEGER, rank INTEGER) +RETURNS void AS $$ +DECLARE +BEGIN + PERFORM ASSERT_SERIALIZED(); + -- insert into earn + INSERT INTO "Earn" (msats, "userId", created_at, type, "typeId", rank) + VALUES (earn_msats, user_id, created_at, type, type_id, rank); + + -- give the user the sats + UPDATE users + SET msats = msats + earn_msats, "stackedMsats" = "stackedMsats" + earn_msats + WHERE id = user_id; + + IF type = 'POST' OR type = 'COMMENT' THEN + PERFORM sats_after_tip(type_id, NULL, earn_msats); + END IF; +END; +$$ LANGUAGE plpgsql; \ No newline at end of file