Skip to content

Commit

Permalink
Issue #212: Update SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
Francois-Werbrouck committed Nov 18, 2024
1 parent 0edb01a commit c42785b
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ END IF;
RETURNING id INTO location_id;
END IF;
END IF;
INSERT INTO organization_information (name,website,phone_number,location_id)
INSERT INTO organization_information (name,website,phone_number,location_id,edited)
VALUES (
name,
website,
phone_number,
location_id
location_id,
edited
)
RETURNING id INTO organization_id;

Expand Down
107 changes: 65 additions & 42 deletions fertiscan/db/bytebase/new_inspection_function.sql
Original file line number Diff line number Diff line change
Expand Up @@ -45,51 +45,51 @@ DECLARE
BEGIN

-- COMPANY
name_string := input_json->'company'->>'name';
address_string := input_json->'company'->>'address';
website_string := input_json->'company'->>'website';
phone_number_string := input_json->'company'->>'phone_number';
IF COALESCE(name_string,
address_string,
website_string,
phone_number_string,
'') <> ''
THEN
company_id := "fertiscan_0.0.17".new_organization_info_located(
input_json->'company'->>'name',
input_json->'company'->>'address',
input_json->'company'->>'website',
input_json->'company'->>'phone_number',
FALSE
);

-- Update input_json with company_id
input_json := jsonb_set(input_json, '{company,id}', to_jsonb(company_id));
END IF;
-- name_string := input_json->'company'->>'name';
-- address_string := input_json->'company'->>'address';
-- website_string := input_json->'company'->>'website';
-- phone_number_string := input_json->'company'->>'phone_number';
-- IF COALESCE(name_string,
-- address_string,
-- website_string,
-- phone_number_string,
-- '') <> ''
-- THEN
-- company_id := "fertiscan_0.0.17".new_organization_info_located(
-- input_json->'company'->>'name',
-- input_json->'company'->>'address',
-- input_json->'company'->>'website',
-- input_json->'company'->>'phone_number',
-- FALSE
-- );
--
-- -- Update input_json with company_id
-- input_json := jsonb_set(input_json, '{company,id}', to_jsonb(company_id));
-- END IF;
-- COMPANY END

-- MANUFACTURER
name_string := input_json->'manufacturer'->>'name';
address_string := input_json->'manufacturer'->>'address';
website_string := input_json->'manufacturer'->>'website';
phone_number_string := input_json->'manufacturer'->>'phone_number';
-- Check if any of the manufacturer fields are not null
IF COALESCE(name_string,
address_string,
website_string,
phone_number_string,
'') <> ''
THEN
manufacturer_id := "fertiscan_0.0.17".new_organization_info_located(
input_json->'manufacturer'->>'name',
input_json->'manufacturer'->>'address',
input_json->'manufacturer'->>'website',
input_json->'manufacturer'->>'phone_number',
FALSE
);
-- Update input_json with company_id
input_json := jsonb_set(input_json, '{manufacturer,id}', to_jsonb(manufacturer_id));
end if;
-- name_string := input_json->'manufacturer'->>'name';
-- address_string := input_json->'manufacturer'->>'address';
-- website_string := input_json->'manufacturer'->>'website';
-- phone_number_string := input_json->'manufacturer'->>'phone_number';
-- -- Check if any of the manufacturer fields are not null
-- IF COALESCE(name_string,
-- address_string,
-- website_string,
-- phone_number_string,
-- '') <> ''
-- THEN
-- manufacturer_id := "fertiscan_0.0.17".new_organization_info_located(
-- input_json->'manufacturer'->>'name',
-- input_json->'manufacturer'->>'address',
-- input_json->'manufacturer'->>'website',
-- input_json->'manufacturer'->>'phone_number',
-- FALSE
-- );
-- -- Update input_json with company_id
-- input_json := jsonb_set(input_json, '{manufacturer,id}', to_jsonb(manufacturer_id));
-- end if;
-- Manufacturer end

-- LABEL INFORMATION
Expand Down Expand Up @@ -352,6 +352,29 @@ BEGIN

-- REGISTRATION NUMBER END

-- ORGANIZATIONS INFO

FOR record in SELECT * FROM jsonb_array_elements(input_json->'organizations')
LOOP
-- Check if any of the fields are not null
IF COALESCE(record->>'name',
record->>'address',
record->>'website',
record->>'phone_number',
'') <> ''
THEN
-- Insert the new organization info
organization_id := "fertiscan_0.0.17".new_organization_info_located(
record->>'name',
record->>'address',
record->>'website',
record->>'phone_number',
FALSE
);
END IF;
END LOOP;
-- ORGANIZATIONS INFO END

-- INSPECTION
INSERT INTO "fertiscan_0.0.17".inspection (
inspector_id, label_info_id, sample_id, picture_set_id, inspection_comment
Expand Down
6 changes: 2 additions & 4 deletions fertiscan/db/bytebase/schema_0.0.17.sql
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ IF (EXISTS (SELECT 1 FROM information_schema.schemata WHERE schema_name = 'ferti
"phone_number" text,
"location_id" uuid REFERENCES "fertiscan_0.0.17".location(id),
"edited" boolean DEFAULT false,
"label_id" uuid REFERENCES "fertiscan_0.0.17".label_information(id),
CONSTRAINT check_not_all_null CHECK (
(name IS NOT NULL)::integer +
(website IS NOT NULL)::integer +
Expand Down Expand Up @@ -108,10 +109,7 @@ IF (EXISTS (SELECT 1 FROM information_schema.schemata WHERE schema_name = 'ferti
"k" float,
"guaranteed_title_en" text,
"guaranteed_title_fr" text,
"title_is_minimal" boolean,
"company_info_id" uuid REFERENCES "fertiscan_0.0.17".organization_information(id),
"manufacturer_info_id" uuid REFERENCES "fertiscan_0.0.17".organization_information(id),
"record_keeping" boolean default null
"title_is_minimal" boolean
);

CREATE TABLE "fertiscan_0.0.17"."label_dimension" (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

-- Function to upsert organization information
CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".upsert_organization_info(input_org_info jsonb, label_info_id uuid)
RETURNS uuid AS $$
DECLARE
record jsonb;
address_str TEXT;
BEGIN

-- loop each orgs in the input_org_info
for record in SELECT * FROM jsonb_array_elements(input_org_info)
loop
if record->>'id' IS NULL THEN
new_organization_info_located(
record->>'name',
record->>'address',
record->>'website',
record->>'phone_number',
TRUE
);
else
-- UPDATE THE LOCATION
address_str := input_org_info->>'address';

-- CHECK IF ADRESS IS NULL
IF address_str IS NULL or COALESCE(address_str,'')='' THEN
RAISE WARNING 'Address should not be null';
ELSE
-- Check if organization location exists by address
SELECT id INTO location_id
FROM location
WHERE location.address ILIKE address_str
LIMIT 1;
-- Use upsert_location to insert or update the location
location_id := upsert_location(location_id, address_str);
END IF;
-- UPDATE THE ORGANIZATION INFORMATION
UPDATE organization_information SET
"name" = record->>'name',
"website" = record->>'website',
"phone_number" = record->>'phone_number',
"location_id" = location_id,
"edited" = (record->>'edited')::boolean
WHERE "id" = record->>'id';
end if;
END LOOP;
END;
$$ LANGUAGE plpgsql;

0 comments on commit c42785b

Please sign in to comment.