camper/deploy/ready_payment.sql

57 lines
2.1 KiB
MySQL
Raw Normal View History

-- Deploy camper:ready_payment to pg
-- requires: roles
-- requires: schema_camper
-- requires: payment
-- requires: payment_customer
-- requires: country_code
-- requires: email
-- requires: extension_pg_libphonenumber
begin;
set search_path to camper, public;
create or replace function ready_payment(payment_slug uuid, customer_name text, customer_address text, customer_post_code text, customer_city text, customer_country_code country_code, customer_email email, customer_phone text, customer_lang_tag text, customer_acsi_card boolean) returns integer as
$$
declare
pid integer;
begin
update payment
set payment_status = 'pending'
, updated_at = current_timestamp
where slug = payment_slug
and payment_status = 'draft'
returning payment_id into pid
;
if pid is null then
raise check_violation using message = 'insert or update on table "payment" violates check constraint "payment_is_draft"';
end if;
insert into payment_customer (payment_id, full_name, address, postal_code, city, country_code, email, phone, acsi_card, lang_tag)
values (pid, customer_name, customer_address, customer_post_code, customer_city, customer_country_code, customer_email, parse_packed_phone_number(customer_phone, customer_country_code), customer_acsi_card, customer_lang_tag)
on conflict (payment_id) do update
set full_name = excluded.full_name
, address = excluded.address
, postal_code = excluded.postal_code
, city = excluded.city
, country_code = excluded.country_code
, email = excluded.email
, phone = excluded.phone
, acsi_card = excluded.acsi_card
, lang_tag = excluded.lang_tag
;
return pid;
end;
$$
language plpgsql
;
revoke execute on function ready_payment(uuid, text, text, text, text, country_code, email, text, text, boolean) from public;
grant execute on function ready_payment(uuid, text, text, text, text, country_code, email, text, text, boolean) to guest;
grant execute on function ready_payment(uuid, text, text, text, text, country_code, email, text, text, boolean) to employee;
grant execute on function ready_payment(uuid, text, text, text, text, country_code, email, text, text, boolean) to admin;
commit;