-- Deploy camper:redsys_encrypt to pg
-- requires: roles
-- requires: schema_camper
-- requires: zero_pad
-- requires: redsys
-- requires: extension_pgcrypto

begin;

set search_path to camper, public;

create or replace function redsys_encrypt(company_id integer, data bytea) returns bytea as
$$
	select public.encrypt_iv(zero_pad(data, 8), encrypt_key, '\x0000000000000000'::bytea, '3des-cbc/pad:none')
	from redsys
	where redsys.company_id = redsys_encrypt.company_id;
$$
	language sql
	stable
	security definer
	set search_path = camper, pg_temp;
;

revoke execute on function redsys_encrypt(integer, bytea) from public;
grant execute on function redsys_encrypt(integer, bytea) to guest;
grant execute on function redsys_encrypt(integer, bytea) to employee;
grant execute on function redsys_encrypt(integer, bytea) to admin;

commit;