campingmontagut/deploy/marshal_payment.sql
jordi fita mas b2ee4dfea3 Add marshal_payment and unmarshal_booking functions
The idea is that we will marshal the payment, send it to the campsite’s
instance by email, and then unmarshal it as a booking, that way we can
have a one way replication from the internal to the public instance with
a way back to send the payments.

For testing purposes, i just create the booking in the same instance.

Had to change the booking relation’s permissions to allow insert from
a guest, much like for payments, because the notification from Redsys
comes as a guest connection.  I need this even with all the
marshal/unmarshal shenanigans because not everyone will have an internal
instance, thus need to allow bookings from guest connections.
2024-04-29 20:59:22 +02:00

68 lines
1.5 KiB
PL/PgSQL

-- Deploy camper:marshal_payment to pg
-- requires: roles
-- requires: schema_camper
-- requires: payment
-- requires: payment_customer
-- requires: payment_option
-- requires: payment__acsi_card
-- requires: payment_customer__-acsi_card
begin;
set search_path to camper, public;
create or replace function marshal_payment(pid integer) returns jsonb as
$$
select to_jsonb(ctx)
from (
select company_id
, campsite_type_id
, arrival_date
, departure_date
, subtotal_nights
, number_adults
, subtotal_adults
, number_teenagers
, subtotal_teenagers
, number_children
, subtotal_children
, number_dogs
, subtotal_dogs
, subtotal_tourist_tax
, total
, currency_code
, zone_preferences
, acsi_card
, full_name
, address
, postal_code
, city
, country_code
, email
, phone
, lang_tag
, (
select array_agg(to_jsonb(o))
from (
select campsite_type_option_id
, units
, subtotal
from payment_option
where payment_option.payment_id = payment.payment_id
) o
) as options
from payment
join payment_customer using (payment_id)
where payment_id = pid
) as ctx;
$$
language sql
;
revoke execute on function marshal_payment(integer) from public;
grant execute on function marshal_payment(integer) to guest;
grant execute on function marshal_payment(integer) to employee;
grant execute on function marshal_payment(integer) to admin;
commit;