-- Deploy numerus:add_payment_account_card to pg
-- requires: roles
-- requires: schema_numerus
-- requires: payment_account
-- requires: payment_account_card

begin;

set search_path to numerus, public;

create or replace function add_payment_account_card(company integer, name text, four_digits text, exp_date date) returns uuid as
$$
declare
	account_id integer;
	account_slug uuid;
begin
	insert into payment_account (company_id, payment_account_type, name)
	select company, 'card', add_payment_account_card.name
	returning payment_account_id, slug into account_id, account_slug;

	insert into payment_account_card (payment_account_id, last_four_digits, expiration_date)
	values (account_id, four_digits, exp_date);

	return account_slug;
end
$$
	language plpgsql
;

revoke execute on function add_payment_account_card(integer, text, text, date) from public;
grant execute on function add_payment_account_card(integer, text, text, date) to invoicer;
grant execute on function add_payment_account_card(integer, text, text, date) to admin;

commit;
