From 848dbb1618f73c867cc433630523bcf760f2d3b7 Mon Sep 17 00:00:00 2001 From: Helena <37183360+helenarebane@users.noreply.github.com> Date: Fri, 4 Jul 2025 14:33:23 +0300 Subject: [PATCH] move product_id to medusa_product_id, make all product relations one-to-one (#30) Co-authored-by: Helena --- lib/services/medipost.service.ts | 2 +- ...119_rename_medreport_product_to_medusa.sql | 342 ++++++++++++++++++ 2 files changed, 343 insertions(+), 1 deletion(-) create mode 100644 supabase/migrations/20250703151119_rename_medreport_product_to_medusa.sql diff --git a/lib/services/medipost.service.ts b/lib/services/medipost.service.ts index 355d7d7..e4cf7ee 100644 --- a/lib/services/medipost.service.ts +++ b/lib/services/medipost.service.ts @@ -29,10 +29,10 @@ import { UuringuGrupp, } from '@/lib/types/medipost'; import { toArray } from '@/lib/utils'; -import { Tables } from '@/supabase/database.types'; import axios from 'axios'; import { XMLParser } from 'fast-xml-parser'; import { uniqBy } from 'lodash'; +import { Tables } from '@kit/supabase/database'; const BASE_URL = process.env.MEDIPOST_URL!; const USER = process.env.MEDIPOST_USER!; diff --git a/supabase/migrations/20250703151119_rename_medreport_product_to_medusa.sql b/supabase/migrations/20250703151119_rename_medreport_product_to_medusa.sql new file mode 100644 index 0000000..f572ab3 --- /dev/null +++ b/supabase/migrations/20250703151119_rename_medreport_product_to_medusa.sql @@ -0,0 +1,342 @@ +drop trigger if exists "check_not_already_tied_to_connected_online" on "public"."medreport_products_analyses_relations"; + +drop trigger if exists "check_not_already_tied_to_analysis" on "public"."medreport_products_external_services_relations"; + +drop policy "read_all" on "public"."medreport_product_groups"; + +drop policy "Enable read access for all users" on "public"."medreport_products_analyses_relations"; + +revoke delete on table "public"."medreport_product_groups" from "anon"; + +revoke insert on table "public"."medreport_product_groups" from "anon"; + +revoke references on table "public"."medreport_product_groups" from "anon"; + +revoke select on table "public"."medreport_product_groups" from "anon"; + +revoke trigger on table "public"."medreport_product_groups" from "anon"; + +revoke truncate on table "public"."medreport_product_groups" from "anon"; + +revoke update on table "public"."medreport_product_groups" from "anon"; + +revoke delete on table "public"."medreport_product_groups" from "authenticated"; + +revoke insert on table "public"."medreport_product_groups" from "authenticated"; + +revoke references on table "public"."medreport_product_groups" from "authenticated"; + +revoke select on table "public"."medreport_product_groups" from "authenticated"; + +revoke trigger on table "public"."medreport_product_groups" from "authenticated"; + +revoke truncate on table "public"."medreport_product_groups" from "authenticated"; + +revoke update on table "public"."medreport_product_groups" from "authenticated"; + +revoke delete on table "public"."medreport_product_groups" from "service_role"; + +revoke insert on table "public"."medreport_product_groups" from "service_role"; + +revoke references on table "public"."medreport_product_groups" from "service_role"; + +revoke select on table "public"."medreport_product_groups" from "service_role"; + +revoke trigger on table "public"."medreport_product_groups" from "service_role"; + +revoke truncate on table "public"."medreport_product_groups" from "service_role"; + +revoke update on table "public"."medreport_product_groups" from "service_role"; + +revoke delete on table "public"."medreport_products_analyses_relations" from "anon"; + +revoke insert on table "public"."medreport_products_analyses_relations" from "anon"; + +revoke references on table "public"."medreport_products_analyses_relations" from "anon"; + +revoke select on table "public"."medreport_products_analyses_relations" from "anon"; + +revoke trigger on table "public"."medreport_products_analyses_relations" from "anon"; + +revoke truncate on table "public"."medreport_products_analyses_relations" from "anon"; + +revoke update on table "public"."medreport_products_analyses_relations" from "anon"; + +revoke delete on table "public"."medreport_products_analyses_relations" from "authenticated"; + +revoke insert on table "public"."medreport_products_analyses_relations" from "authenticated"; + +revoke references on table "public"."medreport_products_analyses_relations" from "authenticated"; + +revoke select on table "public"."medreport_products_analyses_relations" from "authenticated"; + +revoke trigger on table "public"."medreport_products_analyses_relations" from "authenticated"; + +revoke truncate on table "public"."medreport_products_analyses_relations" from "authenticated"; + +revoke update on table "public"."medreport_products_analyses_relations" from "authenticated"; + +revoke delete on table "public"."medreport_products_analyses_relations" from "service_role"; + +revoke insert on table "public"."medreport_products_analyses_relations" from "service_role"; + +revoke references on table "public"."medreport_products_analyses_relations" from "service_role"; + +revoke select on table "public"."medreport_products_analyses_relations" from "service_role"; + +revoke trigger on table "public"."medreport_products_analyses_relations" from "service_role"; + +revoke truncate on table "public"."medreport_products_analyses_relations" from "service_role"; + +revoke update on table "public"."medreport_products_analyses_relations" from "service_role"; + +revoke delete on table "public"."medreport_products_external_services_relations" from "anon"; + +revoke insert on table "public"."medreport_products_external_services_relations" from "anon"; + +revoke references on table "public"."medreport_products_external_services_relations" from "anon"; + +revoke select on table "public"."medreport_products_external_services_relations" from "anon"; + +revoke trigger on table "public"."medreport_products_external_services_relations" from "anon"; + +revoke truncate on table "public"."medreport_products_external_services_relations" from "anon"; + +revoke update on table "public"."medreport_products_external_services_relations" from "anon"; + +revoke delete on table "public"."medreport_products_external_services_relations" from "authenticated"; + +revoke insert on table "public"."medreport_products_external_services_relations" from "authenticated"; + +revoke references on table "public"."medreport_products_external_services_relations" from "authenticated"; + +revoke select on table "public"."medreport_products_external_services_relations" from "authenticated"; + +revoke trigger on table "public"."medreport_products_external_services_relations" from "authenticated"; + +revoke truncate on table "public"."medreport_products_external_services_relations" from "authenticated"; + +revoke update on table "public"."medreport_products_external_services_relations" from "authenticated"; + +revoke delete on table "public"."medreport_products_external_services_relations" from "service_role"; + +revoke insert on table "public"."medreport_products_external_services_relations" from "service_role"; + +revoke references on table "public"."medreport_products_external_services_relations" from "service_role"; + +revoke select on table "public"."medreport_products_external_services_relations" from "service_role"; + +revoke trigger on table "public"."medreport_products_external_services_relations" from "service_role"; + +revoke truncate on table "public"."medreport_products_external_services_relations" from "service_role"; + +revoke update on table "public"."medreport_products_external_services_relations" from "service_role"; + +alter table "public"."medreport_product_groups" drop constraint "medreport_product_groups_name_key"; + +alter table "public"."medreport_products" drop constraint "medreport_products_product_groups_id_fkey"; + +alter table "public"."medreport_products_analyses_relations" drop constraint "medreport_products_analyses_analysis_element_id_fkey"; + +alter table "public"."medreport_products_analyses_relations" drop constraint "medreport_products_analyses_analysis_element_id_key"; + +alter table "public"."medreport_products_analyses_relations" drop constraint "medreport_products_analyses_analysis_id_fkey"; + +alter table "public"."medreport_products_analyses_relations" drop constraint "medreport_products_analyses_analysis_id_key"; + +alter table "public"."medreport_products_analyses_relations" drop constraint "medreport_products_analyses_product_id_fkey"; + +alter table "public"."medreport_products_analyses_relations" drop constraint "product_can_be_tied_to_only_one_analysis_item"; + +alter table "public"."medreport_products_analyses_relations" drop constraint "product_can_be_tied_to_only_one_external_item"; + +alter table "public"."medreport_products_external_services_relations" drop constraint "medreport_products_connected_online_services_id_fkey"; + +alter table "public"."medreport_products_external_services_relations" drop constraint "medreport_products_connected_online_services_id_key"; + +alter table "public"."medreport_products_external_services_relations" drop constraint "medreport_products_connected_online_services_product_id_fkey"; + +alter table "public"."medreport_product_groups" drop constraint "medreport_product_groups_pkey"; + +alter table "public"."medreport_products_analyses_relations" drop constraint "medreport_products_analyses_pkey"; + +alter table "public"."medreport_products_external_services_relations" drop constraint "medreport_products_connected_online_services_pkey"; + +drop index if exists "public"."medreport_product_groups_name_key"; + +drop index if exists "public"."medreport_product_groups_pkey"; + +drop index if exists "public"."medreport_products_analyses_pkey"; + +drop index if exists "public"."medreport_products_analyses_analysis_element_id_key"; + +drop index if exists "public"."medreport_products_analyses_analysis_id_key"; + +drop index if exists "public"."medreport_products_connected_online_services_id_key"; + +drop index if exists "public"."medreport_products_connected_online_services_pkey"; + +drop table "public"."medreport_product_groups"; + +drop table "public"."medreport_products_analyses_relations"; + +drop table "public"."medreport_products_external_services_relations"; + +create table "public"."medusa_products_analyses_relations" ( + "medusa_product_id" bigint not null, + "analysis_element_id" bigint, + "analysis_id" bigint +); + + +alter table "public"."medusa_products_analyses_relations" enable row level security; + +create table "public"."medusa_products_external_services_relations" ( + "medusa_product_id" bigint not null, + "connected_online_service_id" bigint not null +); + + +alter table "public"."medusa_products_external_services_relations" enable row level security; + +CREATE UNIQUE INDEX medusa_products_analyses_relations_pkey ON public.medusa_products_analyses_relations USING btree (medusa_product_id); + +CREATE UNIQUE INDEX medusa_products_external_services_relations_product_id_key ON public.medusa_products_external_services_relations USING btree (medusa_product_id); + +CREATE UNIQUE INDEX medreport_products_analyses_analysis_element_id_key ON public.medusa_products_analyses_relations USING btree (analysis_element_id); + +CREATE UNIQUE INDEX medreport_products_analyses_analysis_id_key ON public.medusa_products_analyses_relations USING btree (analysis_id); + +CREATE UNIQUE INDEX medreport_products_connected_online_services_id_key ON public.medusa_products_external_services_relations USING btree (connected_online_service_id); + +CREATE UNIQUE INDEX medreport_products_connected_online_services_pkey ON public.medusa_products_external_services_relations USING btree (connected_online_service_id); + +alter table "public"."medusa_products_analyses_relations" add constraint "medusa_products_analyses_relations_pkey" PRIMARY KEY using index "medusa_products_analyses_relations_pkey"; + +alter table "public"."medusa_products_external_services_relations" add constraint "medreport_products_connected_online_services_pkey" PRIMARY KEY using index "medreport_products_connected_online_services_pkey"; + +alter table "public"."medusa_products_analyses_relations" add constraint "medreport_products_analyses_analysis_element_id_fkey" FOREIGN KEY (analysis_element_id) REFERENCES analysis_elements(id) ON UPDATE CASCADE ON DELETE CASCADE not valid; + +alter table "public"."medusa_products_analyses_relations" validate constraint "medreport_products_analyses_analysis_element_id_fkey"; + +alter table "public"."medusa_products_analyses_relations" add constraint "medreport_products_analyses_analysis_element_id_key" UNIQUE using index "medreport_products_analyses_analysis_element_id_key"; + +alter table "public"."medusa_products_analyses_relations" add constraint "medreport_products_analyses_analysis_id_fkey" FOREIGN KEY (analysis_id) REFERENCES analyses(id) ON UPDATE CASCADE ON DELETE CASCADE not valid; + +alter table "public"."medusa_products_analyses_relations" validate constraint "medreport_products_analyses_analysis_id_fkey"; + +alter table "public"."medusa_products_analyses_relations" add constraint "medreport_products_analyses_analysis_id_key" UNIQUE using index "medreport_products_analyses_analysis_id_key"; + +alter table "public"."medusa_products_analyses_relations" add constraint "medreport_products_analyses_product_id_fkey" FOREIGN KEY (medusa_product_id) REFERENCES medreport_products(id) ON UPDATE CASCADE ON DELETE CASCADE not valid; + +alter table "public"."medusa_products_analyses_relations" validate constraint "medreport_products_analyses_product_id_fkey"; + +alter table "public"."medusa_products_analyses_relations" add constraint "product_can_be_tied_to_only_one_analysis_item" CHECK (((analysis_id IS NULL) OR (analysis_element_id IS NULL))) not valid; + +alter table "public"."medusa_products_analyses_relations" validate constraint "product_can_be_tied_to_only_one_analysis_item"; + +alter table "public"."medusa_products_analyses_relations" add constraint "product_can_be_tied_to_only_one_external_item" CHECK (((analysis_id IS NULL) OR (analysis_element_id IS NULL))) not valid; + +alter table "public"."medusa_products_analyses_relations" validate constraint "product_can_be_tied_to_only_one_external_item"; + +alter table "public"."medusa_products_external_services_relations" add constraint "medreport_products_connected_online_services_id_fkey" FOREIGN KEY (connected_online_service_id) REFERENCES connected_online_services(id) ON UPDATE CASCADE ON DELETE CASCADE not valid; + +alter table "public"."medusa_products_external_services_relations" validate constraint "medreport_products_connected_online_services_id_fkey"; + +alter table "public"."medusa_products_external_services_relations" add constraint "medreport_products_connected_online_services_id_key" UNIQUE using index "medreport_products_connected_online_services_id_key"; + +alter table "public"."medusa_products_external_services_relations" add constraint "medreport_products_connected_online_services_product_id_fkey" FOREIGN KEY (medusa_product_id) REFERENCES medreport_products(id) ON UPDATE CASCADE ON DELETE CASCADE not valid; + +alter table "public"."medusa_products_external_services_relations" validate constraint "medreport_products_connected_online_services_product_id_fkey"; + +alter table "public"."medusa_products_external_services_relations" add constraint "medusa_products_external_services_relations_product_id_key" UNIQUE using index "medusa_products_external_services_relations_product_id_key"; + +grant delete on table "public"."medusa_products_analyses_relations" to "service_role"; + +grant insert on table "public"."medusa_products_analyses_relations" to "service_role"; + +grant references on table "public"."medusa_products_analyses_relations" to "service_role"; + +grant select on table "public"."medusa_products_analyses_relations" to "service_role"; + +grant trigger on table "public"."medusa_products_analyses_relations" to "service_role"; + +grant truncate on table "public"."medusa_products_analyses_relations" to "service_role"; + +grant update on table "public"."medusa_products_analyses_relations" to "service_role"; + +grant delete on table "public"."medusa_products_external_services_relations" to "service_role"; + +grant insert on table "public"."medusa_products_external_services_relations" to "service_role"; + +grant references on table "public"."medusa_products_external_services_relations" to "service_role"; + +grant select on table "public"."medusa_products_external_services_relations" to "service_role"; + +grant trigger on table "public"."medusa_products_external_services_relations" to "service_role"; + +grant truncate on table "public"."medusa_products_external_services_relations" to "service_role"; + +grant update on table "public"."medusa_products_external_services_relations" to "service_role"; + +CREATE TRIGGER check_not_already_tied_to_connected_online BEFORE INSERT OR UPDATE ON public.medusa_products_analyses_relations FOR EACH ROW EXECUTE FUNCTION check_tied_to_connected_online(); + +CREATE TRIGGER check_not_already_tied_to_analysis BEFORE INSERT OR UPDATE ON public.medusa_products_external_services_relations FOR EACH ROW EXECUTE FUNCTION check_tied_to_analysis_item(); + +revoke delete on table "public"."medreport_products" from "anon"; + +revoke insert on table "public"."medreport_products" from "anon"; + +revoke references on table "public"."medreport_products" from "anon"; + +revoke select on table "public"."medreport_products" from "anon"; + +revoke trigger on table "public"."medreport_products" from "anon"; + +revoke truncate on table "public"."medreport_products" from "anon"; + +revoke update on table "public"."medreport_products" from "anon"; + +revoke delete on table "public"."medreport_products" from "authenticated"; + +revoke insert on table "public"."medreport_products" from "authenticated"; + +revoke references on table "public"."medreport_products" from "authenticated"; + +revoke select on table "public"."medreport_products" from "authenticated"; + +revoke trigger on table "public"."medreport_products" from "authenticated"; + +revoke truncate on table "public"."medreport_products" from "authenticated"; + +revoke update on table "public"."medreport_products" from "authenticated"; + +revoke delete on table "public"."medreport_products" from "service_role"; + +revoke insert on table "public"."medreport_products" from "service_role"; + +revoke references on table "public"."medreport_products" from "service_role"; + +revoke select on table "public"."medreport_products" from "service_role"; + +revoke trigger on table "public"."medreport_products" from "service_role"; + +revoke truncate on table "public"."medreport_products" from "service_role"; + +revoke update on table "public"."medreport_products" from "service_role"; + +alter table "public"."medreport_products" drop constraint "medreport_products_name_key"; + +alter table "public"."medusa_products_analyses_relations" drop constraint "medreport_products_analyses_product_id_fkey"; + +alter table "public"."medusa_products_external_services_relations" drop constraint "medreport_products_connected_online_services_product_id_fkey"; + +alter table "public"."medreport_products" drop constraint "medreport_products_pkey"; + +drop index if exists "public"."medreport_products_name_key"; + +drop index if exists "public"."medreport_products_pkey"; + +drop table "public"."medreport_products"; + +