only queue autowithdraw if a wallet is enabled

This commit is contained in:
k00b 2024-08-29 16:13:16 -05:00
parent b6e4f97668
commit 6dedda577b
1 changed files with 23 additions and 0 deletions

View File

@ -0,0 +1,23 @@
CREATE OR REPLACE FUNCTION user_auto_withdraw() RETURNS TRIGGER AS $$
DECLARE
BEGIN
INSERT INTO pgboss.job (name, data)
SELECT 'autoWithdraw', jsonb_build_object('id', NEW.id)
-- only if there isn't already a pending job for this user
WHERE NOT EXISTS (
SELECT *
FROM pgboss.job
WHERE name = 'autoWithdraw'
AND data->>'id' = NEW.id::TEXT
AND state = 'created'
)
-- and they have an attached wallet (currently all are received only)
AND EXISTS (
SELECT *
FROM "Wallet"
WHERE "userId" = NEW.id
AND enabled = TRUE
);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;