nostream/migrations/20230220_002700_fix_unit_confirm_invoice_func.js
Ricardo Arturo Cabral Mejía fd3294929a
fix: confirm invoice function ambiguous unit variable (#221)
* fix: dont crash when SECRET is not set

* docs: add semisol to contributors

* docs: improve readme

* docs: add payment info to readme

* docs: add zebedee_api_key to configuration.md

* fix: confirm_invoice unit var

* chore: remove unused code

* chore: improve error logging for payments

* chore: use  instead of changeme

* chore: fix typo

* chore: improve get invoice status ctrl

* fix: csp bug

* chore: remove rate limits

* chore: improve invoice page logging

* chore: prevent root with start_local

* chore: revert to redis 4.5.1
2023-02-20 09:15:45 -08:00

63 lines
2.2 KiB
JavaScript

exports.up = function (knex) {
return knex.schema
.raw('DROP FUNCTION confirm_invoice(invoice_id UUID, amount_received BIGINT, confirmation_date TIMESTAMP WITHOUT TIME ZONE)')
.raw('DROP FUNCTION confirm_invoice(invoice_id TEXT, amount_received BIGINT, confirmation_date TIMESTAMP WITHOUT TIME ZONE)')
.raw(`CREATE OR REPLACE FUNCTION confirm_invoice(invoice_id TEXT, amount_received BIGINT, confirmation_date TIMESTAMP WITHOUT TIME ZONE)
RETURNS INTEGER
LANGUAGE plpgsql
AS $$
DECLARE
payee BYTEA;
confirmed_date TIMESTAMP WITHOUT TIME ZONE;
invoice_unit TEXT;
BEGIN
PERFORM ASSERT_SERIALIZED();
SELECT "pubkey", "confirmed_at", "unit" INTO payee, confirmed_date, invoice_unit FROM "invoices" WHERE id = invoice_id;
IF confirmed_date IS NULL THEN
UPDATE invoices
SET
"confirmed_at" = confirmation_date,
"amount_paid" = amount_received,
"updated_at" = now_utc()
WHERE id = invoice_id;
IF invoice_unit = 'sats' THEN
UPDATE users SET balance = balance + amount_received * 1000 WHERE "pubkey" = payee;
ELSIF invoice_unit = 'msats' THEN
UPDATE users SET balance = balance + amount_received WHERE "pubkey" = payee;
ELSIF invoice_unit = 'btc' THEN
UPDATE users SET balance = balance + amount_received * 100000000 * 1000 WHERE "pubkey" = payee;
END IF;
END IF;
RETURN 0;
END;
$$;`)
}
exports.down = function (knex) {
return knex.schema
.raw(`CREATE OR REPLACE FUNCTION confirm_invoice(invoice_id TEXT, amount_received BIGINT, confirmation_date TIMESTAMP WITHOUT TIME ZONE)
RETURNS INTEGER
LANGUAGE plpgsql
AS $$
DECLARE
payee BYTEA;
confirmed_date TIMESTAMP WITHOUT TIME ZONE;
BEGIN
PERFORM ASSERT_SERIALIZED();
SELECT "pubkey", "confirmed_at" INTO payee, confirmed_date FROM "invoices" WHERE id = invoice_id;
IF confirmed_date IS NULL THEN
UPDATE invoices
SET
"confirmed_at" = confirmation_date,
"amount_paid" = amount_received,
"updated_at" = now_utc()
WHERE id = invoice_id;
UPDATE users SET balance = balance + amount_received WHERE "pubkey" = payee;
END IF;
RETURN 0;
END;
$$;`)
}