numerus/deploy/add_product.sql

40 lines
1.1 KiB
MySQL
Raw Normal View History

-- Deploy numerus:add_product to pg
-- requires: schema_numerus
-- requires: product
-- requires: product_tax
-- requires: parse_price
-- requires: company
-- requires: currency
begin;
set search_path to numerus, public;
create or replace function add_product(company_id integer, name text, description text, price text, taxes integer[]) returns uuid
as $$
declare
pid integer;
pslug uuid;
begin
insert into product (company_id, name, description, price)
select add_product.company_id, add_product.name, add_product.description, parse_price(add_product.price, decimal_digits)
from company
join currency using (currency_code)
where company.company_id = add_product.company_id
returning product_id, slug
into pid, pslug;
insert into product_tax (product_id, tax_id)
select pid, tax_id
from unnest(taxes) as tax(tax_id);
return pslug;
end;
$$ language plpgsql;
revoke execute on function add_product(integer, text, text, text, integer[]) from public;
grant execute on function add_product(integer, text, text, text, integer[]) to invoicer;
grant execute on function add_product(integer, text, text, text, integer[]) to admin;
commit;