-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add image fees during item creation/update
* we calculate image fees during item creation and update now * function imageFees returns queries which deduct fees from user and mark images as paid + fees * queries need to be run inside same transaction as item creation/update
- Loading branch information
Showing
5 changed files
with
123 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
-- function to manually deduct fees from user, for example for images fees | ||
CREATE OR REPLACE FUNCTION user_fee(user_id INTEGER, item_id INTEGER, cost_msats BIGINT) | ||
RETURNS users | ||
LANGUAGE plpgsql | ||
AS $$ | ||
DECLARE | ||
user users; | ||
user_msats BIGINT; | ||
BEGIN | ||
PERFORM ASSERT_SERIALIZED(); | ||
|
||
SELECT msats INTO user_msats FROM users WHERE id = user_id; | ||
IF cost_msats > user_msats THEN | ||
RAISE EXCEPTION 'SN_INSUFFICIENT_FUNDS'; | ||
END IF; | ||
|
||
UPDATE users SET msats = msats - cost_msats WHERE id = user_id RETURNING * INTO user; | ||
|
||
INSERT INTO "ItemAct" (msats, "itemId", "userId", act) | ||
VALUES (cost_msats, item_id, user_id, 'FEE'); | ||
|
||
RETURN user; | ||
END; | ||
$$; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters