2023-02-14 11:39:54 +00:00
|
|
|
-- Deploy numerus:edit_product to pg
|
|
|
|
-- requires: schema_numerus
|
|
|
|
-- requires: product
|
|
|
|
-- requires: product_tax
|
|
|
|
-- requires: parse_price
|
|
|
|
-- requires: company
|
|
|
|
-- requires: currency
|
2023-03-26 11:51:57 +00:00
|
|
|
-- requires: tag_name
|
|
|
|
-- requires: tag_product
|
2023-02-14 11:39:54 +00:00
|
|
|
|
|
|
|
begin;
|
|
|
|
|
|
|
|
set search_path to numerus, public;
|
|
|
|
|
2023-03-26 11:51:57 +00:00
|
|
|
create or replace function edit_product(slug uuid, name text, description text, price text, taxes integer[], tags tag_name[]) returns boolean
|
2023-02-14 11:39:54 +00:00
|
|
|
as $$
|
|
|
|
declare
|
|
|
|
pid integer;
|
2023-03-26 11:51:57 +00:00
|
|
|
company integer;
|
2023-02-14 11:39:54 +00:00
|
|
|
begin
|
|
|
|
update product
|
|
|
|
set name = edit_product.name
|
|
|
|
, description = edit_product.description
|
|
|
|
, price = parse_price(edit_product.price, decimal_digits)
|
|
|
|
from company
|
|
|
|
join currency using (currency_code)
|
|
|
|
where product.company_id = company.company_id
|
|
|
|
and product.slug = edit_product.slug
|
2023-03-26 11:51:57 +00:00
|
|
|
returning product_id, product.company_id
|
|
|
|
into pid, company;
|
2023-02-14 11:39:54 +00:00
|
|
|
|
|
|
|
if pid is null then
|
|
|
|
return false;
|
|
|
|
end if;
|
|
|
|
|
|
|
|
delete from product_tax where product_id = pid;
|
|
|
|
|
|
|
|
insert into product_tax(product_id, tax_id)
|
|
|
|
select pid, tax_id
|
|
|
|
from unnest(taxes) as tax(tax_id);
|
|
|
|
|
2023-03-26 11:51:57 +00:00
|
|
|
perform tag_product(company, pid, tags);
|
|
|
|
|
2023-02-14 11:39:54 +00:00
|
|
|
return true;
|
|
|
|
end;
|
|
|
|
$$ language plpgsql;
|
|
|
|
|
2023-03-26 11:51:57 +00:00
|
|
|
revoke execute on function edit_product(uuid, text, text, text, integer[], tag_name[]) from public;
|
|
|
|
grant execute on function edit_product(uuid, text, text, text, integer[], tag_name[]) to invoicer;
|
|
|
|
grant execute on function edit_product(uuid, text, text, text, integer[], tag_name[]) to admin;
|
2023-02-14 11:39:54 +00:00
|
|
|
|
|
|
|
commit;
|