add earned sats to items

This commit is contained in:
keyan 2023-05-08 18:10:41 -05:00
parent fbd746b023
commit d948a653c9
2 changed files with 21 additions and 1 deletions

View File

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

View File

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