B2B-51: add medreport product tables, relations and constraints (#19)

* B2B-51: add medreport product tables, relations and constraints

* change table names

* sync data based on env

---------

Co-authored-by: Helena <helena@Helenas-MacBook-Pro.local>
This commit is contained in:
Helena
2025-06-19 16:40:22 +03:00
committed by GitHub
parent 538a17031a
commit ea540a4aeb
2 changed files with 246 additions and 10 deletions

View File

@@ -8,6 +8,8 @@ async function syncData() {
config({ path: `.env.${process.env.NODE_ENV}` });
}
const isProd = process.env.NODE_ENV === 'production';
const baseUrl = process.env.CONNECTED_ONLINE_URL;
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
const supabaseServiceRoleKey =
@@ -26,15 +28,12 @@ async function syncData() {
});
try {
const response = await axios.post(
`${baseUrl}/Search_Load`,
{
const response = await axios.post(`${baseUrl}/Search_Load`, {
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
param: "{'Value':'|et|-1'}", // get all available services in Estonian
},
);
});
const responseData: {
Value: any;
@@ -59,8 +58,20 @@ async function syncData() {
});
}
const clinics = responseData.Data.T_Lic;
const services = responseData.Data.T_Service;
let clinics;
let services;
// Filter out "Dentas Demo OÜ" in prod or only sync "Dentas Demo OÜ" in any other environment
if (isProd) {
clinics = responseData.Data.T_Lic.filter((clinic) => clinic.ID !== 2);
services = responseData.Data.T_Service.filter(
(service) => service.ClinicID !== 2,
);
} else {
clinics = responseData.Data.T_Lic.filter((clinic) => clinic.ID === 2);
services = responseData.Data.T_Service.filter(
(service) => service.ClinicID === 2,
);
}
const mappedClinics = clinics.map((clinic) => {
return {

View File

@@ -0,0 +1,225 @@
create table "public"."medreport_product_groups" (
"id" bigint generated by default as identity not null,
"name" text not null,
"created_at" timestamp with time zone not null default now(),
"updated_at" timestamp with time zone
);
create table "public"."medreport_products" (
"id" bigint generated by default as identity not null,
"name" text not null,
"product_group_id" bigint,
"created_at" timestamp with time zone not null default now(),
"updated_at" timestamp with time zone default now()
);
alter table "public"."medreport_products" enable row level security;
create table "public"."medreport_products_analyses_relations" (
"product_id" bigint not null,
"analysis_element_id" bigint,
"analysis_id" bigint
);
alter table "public"."medreport_product_groups" enable row level security;
alter table "public"."medreport_products_analyses_relations" enable row level security;
CREATE UNIQUE INDEX medreport_product_groups_name_key ON public.medreport_product_groups USING btree (name);
CREATE UNIQUE INDEX medreport_product_groups_pkey ON public.medreport_product_groups USING btree (id);
alter table "public"."medreport_product_groups" add constraint "medreport_product_groups_pkey" PRIMARY KEY using index "medreport_product_groups_pkey";
alter table "public"."medreport_product_groups" add constraint "medreport_product_groups_name_key" UNIQUE using index "medreport_product_groups_name_key";
alter table "public"."medreport_products" add constraint "medreport_products_product_groups_id_fkey" FOREIGN KEY (product_group_id) REFERENCES medreport_product_groups(id) ON UPDATE CASCADE ON DELETE CASCADE not valid;
alter table "public"."medreport_products" validate constraint "medreport_products_product_groups_id_fkey";
grant select on table "public"."medreport_product_groups" to "anon";
grant select on table "public"."medreport_product_groups" to "authenticated";
grant delete on table "public"."medreport_product_groups" to "service_role";
grant insert on table "public"."medreport_product_groups" to "service_role";
grant references on table "public"."medreport_product_groups" to "service_role";
grant select on table "public"."medreport_product_groups" to "service_role";
grant trigger on table "public"."medreport_product_groups" to "service_role";
grant truncate on table "public"."medreport_product_groups" to "service_role";
grant update on table "public"."medreport_product_groups" to "service_role";
CREATE UNIQUE INDEX medreport_products_analyses_analysis_element_id_key ON public.medreport_products_analyses_relations USING btree (analysis_element_id);
CREATE UNIQUE INDEX medreport_products_analyses_analysis_id_key ON public.medreport_products_analyses_relations USING btree (analysis_id);
CREATE UNIQUE INDEX medreport_products_analyses_pkey ON public.medreport_products_analyses_relations USING btree (product_id);
CREATE UNIQUE INDEX medreport_products_name_key ON public.medreport_products USING btree (name);
CREATE UNIQUE INDEX medreport_products_pkey ON public.medreport_products USING btree (id);
alter table "public"."medreport_products" add constraint "medreport_products_pkey" PRIMARY KEY using index "medreport_products_pkey";
alter table "public"."medreport_products_analyses_relations" add constraint "medreport_products_analyses_pkey" PRIMARY KEY using index "medreport_products_analyses_pkey";
alter table "public"."medreport_products" add constraint "medreport_products_name_key" UNIQUE using index "medreport_products_name_key";
alter table "public"."medreport_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"."medreport_products_analyses_relations" validate constraint "medreport_products_analyses_analysis_element_id_fkey";
alter table "public"."medreport_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"."medreport_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"."medreport_products_analyses_relations" validate constraint "medreport_products_analyses_analysis_id_fkey";
alter table "public"."medreport_products_analyses_relations" add constraint "medreport_products_analyses_analysis_id_key" UNIQUE using index "medreport_products_analyses_analysis_id_key";
alter table "public"."medreport_products_analyses_relations" add constraint "medreport_products_analyses_product_id_fkey" FOREIGN KEY (product_id) REFERENCES medreport_products(id) ON UPDATE CASCADE ON DELETE CASCADE not valid;
alter table "public"."medreport_products_analyses_relations" validate constraint "medreport_products_analyses_product_id_fkey";
alter table "public"."medreport_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"."medreport_products_analyses_relations" validate constraint "product_can_be_tied_to_only_one_external_item";
grant select on table "public"."medreport_products" to "anon";
grant select on table "public"."medreport_products" to "authenticated";
grant delete on table "public"."medreport_products" to "service_role";
grant insert on table "public"."medreport_products" to "service_role";
grant references on table "public"."medreport_products" to "service_role";
grant select on table "public"."medreport_products" to "service_role";
grant trigger on table "public"."medreport_products" to "service_role";
grant truncate on table "public"."medreport_products" to "service_role";
grant update on table "public"."medreport_products" to "service_role";
grant select on table "public"."medreport_products_analyses_relations" to "anon";
grant select on table "public"."medreport_products_analyses_relations" to "authenticated";
grant delete on table "public"."medreport_products_analyses_relations" to "service_role";
grant insert on table "public"."medreport_products_analyses_relations" to "service_role";
grant references on table "public"."medreport_products_analyses_relations" to "service_role";
grant select on table "public"."medreport_products_analyses_relations" to "service_role";
grant trigger on table "public"."medreport_products_analyses_relations" to "service_role";
grant truncate on table "public"."medreport_products_analyses_relations" to "service_role";
grant update on table "public"."medreport_products_analyses_relations" to "service_role";
create policy "Enable read access for all users"
on "public"."medreport_products_analyses_relations"
as permissive
for select
to public
using (true);
ALTER TABLE medreport_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);
create table "public"."medreport_products_external_services_relations" (
"product_id" bigint not null,
"connected_online_service_id" bigint not null
);
alter table "public"."medreport_products_external_services_relations" enable row level security;
CREATE UNIQUE INDEX medreport_products_connected_online_services_id_key ON public.medreport_products_external_services_relations USING btree (connected_online_service_id);
CREATE UNIQUE INDEX medreport_products_connected_online_services_pkey ON public.medreport_products_external_services_relations USING btree (connected_online_service_id);
alter table "public"."medreport_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"."medreport_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"."medreport_products_external_services_relations" validate constraint "medreport_products_connected_online_services_id_fkey";
alter table "public"."medreport_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"."medreport_products_external_services_relations" add constraint "medreport_products_connected_online_services_product_id_fkey" FOREIGN KEY (product_id) REFERENCES medreport_products(id) ON UPDATE CASCADE ON DELETE CASCADE not valid;
alter table "public"."medreport_products_external_services_relations" validate constraint "medreport_products_connected_online_services_product_id_fkey";
grant select on table "public"."medreport_products_external_services_relations" to "anon";
grant select on table "public"."medreport_products_external_services_relations" to "authenticated";
grant delete on table "public"."medreport_products_external_services_relations" to "service_role";
grant insert on table "public"."medreport_products_external_services_relations" to "service_role";
grant references on table "public"."medreport_products_external_services_relations" to "service_role";
grant select on table "public"."medreport_products_external_services_relations" to "service_role";
grant trigger on table "public"."medreport_products_external_services_relations" to "service_role";
grant truncate on table "public"."medreport_products_external_services_relations" to "service_role";
grant update on table "public"."medreport_products_external_services_relations" to "service_role";
CREATE OR REPLACE FUNCTION check_tied_to_connected_online()
RETURNS TRIGGER AS $$
BEGIN
IF EXISTS (
SELECT 1
FROM medreport_products_external_services_relations
WHERE product_id = NEW.product_id
) THEN
RAISE EXCEPTION 'Value "%" already exists in medreport_products_external_services_relations', NEW.product_id;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION check_tied_to_analysis_item()
RETURNS TRIGGER AS $$
BEGIN
IF EXISTS (
SELECT 1
FROM medreport_products_analyses_relations
WHERE product_id = NEW.product_id
) THEN
RAISE EXCEPTION 'Value "%" already exists in medreport_products_analyses_relations', NEW.product_id;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER check_not_already_tied_to_connected_online BEFORE INSERT OR UPDATE ON public.medreport_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.medreport_products_external_services_relations FOR EACH ROW EXECUTE FUNCTION check_tied_to_analysis_item();
create policy "read_all"
on "public"."medreport_product_groups"
as permissive
for select
to public
using (true);