-- Deploy numerus:edit_payment_account_bank to pg
-- requires: roles
-- requires: schema_numerus
-- requires: payment_account
-- requires: payment_account_bank
-- requires: extension_iban

begin;

set search_path to numerus, public;

create or replace function edit_payment_account_bank(account_slug uuid, new_name text, new_iban iban) returns uuid as
$$
declare
	account_id int;
begin
	update payment_account
	set name = new_name
	where slug = account_slug
	  and payment_account_type = 'bank'
	returning payment_account_id into account_id
	;

	if account_id is null then
		return null;
	end if;

	update payment_account_bank
	set iban = new_iban
	where payment_account_id = account_id
	;

	return account_slug;
end
$$
	language plpgsql
;

revoke execute on function edit_payment_account_bank(uuid, text, iban) from public;
grant execute on function edit_payment_account_bank(uuid, text, iban) to invoicer;
grant execute on function edit_payment_account_bank(uuid, text, iban) to admin;

commit;
