Skip to content

Commit

Permalink
233 improve the search of inspection (#258)
Browse files Browse the repository at this point in the history
* Issue #233: Build doc

* Issue #233: `search_inspection` added

* Issue #233: Search modules tests passing

* Issue #233: datastore search_inspection test

* Issue #233: fix markdown lint

* Issue #233: Formatting

* Issue #233: Fix markdown lint

* Issue #233: fix high level doc
  • Loading branch information
Francois-Werbrouck authored Feb 27, 2025
1 parent b32757d commit d13a2c3
Show file tree
Hide file tree
Showing 10 changed files with 1,011 additions and 29 deletions.
86 changes: 86 additions & 0 deletions fertiscan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from azure.storage.blob import ContainerClient
from dotenv import load_dotenv
from psycopg import Cursor
from datetime import datetime

import datastore
import datastore.db.queries.picture as picture
Expand Down Expand Up @@ -600,3 +601,88 @@ def get_user_analysis_by_verified(cursor: Cursor, user_id: UUID, verified: bool)
if not user.is_a_user_id(cursor=cursor, user_id=user_id):
raise user.UserNotFoundError(f"User not found based on the given id: {user_id}")
return inspection.get_all_user_inspection_filter_verified(cursor, user_id, verified)


def search_inspection(
cursor: Cursor,
fertilizer_name: str,
reg_number: str,
lot_number: str,
inspector_name: str,
lower_bound_date: datetime,
upper_bound_date: datetime,
organization_name: str,
organization_address: str,
organization_phone: str,
):
"""
This function search all the verified inspection based on the given parameters
Parameters:
- cursor (Cursor): The cursor object to interact with the database.
- fertilizer_name (str): The name of the fertilizer.
- reg_number (str): The registration number of the fertilizer.
- lot_number (str): The lot number of the fertilizer.
- inspector_name (str): The name of the inspector. (Not used at the moment)
- lower_bound_date (str): The lower bound date of the inspection.
- upper_bound_date (str): The upper bound date of the inspection.
- organization_name (str): The name of the organization.
- organization_address (str): The address of the organization.
- organization_phone (str): The phone number of the organization.
Returns:
- List of inspection tuple.
[
inspection.id,
inspection.verified
inspection.upload_date,
inspection.updated_at,
inspection.inspector_id
inspection.label_info_id,
inspection.container_id,
inspection.folder_id,
inspection.inspection_comment,
inspection.verified_date,
label_info.product_name,
organization_info.id, (main_contact_id)
organization_info.name,
organization_info.phone_number,
organization_info.address,
label_info.is_minimal,
label_info.record_keeping,
registration_number.identifiers, (list of reg numbers)
]
"""
label_ids = []
# search based on Organization info
if (
organization_name is not None
or organization_address is not None
or organization_phone is not None
):
orgs = organization.search_organization_information(
cursor=cursor,
name=organization_name,
address=organization_address,
phone_number=organization_phone,
website=None,
)
if org is not None and len(orgs) > 0:
for org in orgs:
label_ids.append(org[1])
# search based on registration number
if reg_number is not None and reg_number.strip() == "":
reg_result = registration_number.search_registration_number(
cursor=cursor, registration_number=reg_number
)
if reg_result is not None and len(reg_result) > 0:
for reg in reg_result:
label_ids.append(reg[1])
return inspection.search_inspection(
cursor=cursor,
fertilizer_name=fertilizer_name,
lower_bound_date=lower_bound_date,
upper_bound_date=upper_bound_date,
lot_number=lot_number,
label_ids=label_ids,
inspector_name=inspector_name
)
123 changes: 123 additions & 0 deletions fertiscan/db/queries/fertilizer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
from psycopg.rows import dict_row
from psycopg.sql import SQL

from datetime import date as Date

from fertiscan.db.queries.errors import (
FertilizerUpsertError,
FertilizerQueryError,
handle_query_errors,
)

Expand Down Expand Up @@ -53,3 +56,123 @@ def upsert_fertilizer(cursor: Cursor, name: str, reg_number:str, org_owner_id:UU
"""
cursor.execute(query,(name,reg_number,org_owner_id,latest_inspection_id,))
return cursor.fetchone()[0]


def search_fertilizer(cursor: Cursor, fertilizer_name:str, registration_number: str, lower_bound_date: Date, upper_bound_date: Date, lot_number:str, org_ids:list[UUID]):
"""
Find all inspections where the organization is listed as main contact.
Parameters:
- cursor (Cursor): Database cursor
- org_ids (list[UUID]): List of organization IDs to search for
Returns:
- list: List of tuples containing inspection data
"""
query = """
SELECT
i.id as inspection_id,
i.verified as verified,
i.upload_date as upload_date,
i.updated_at as last_updated_at,
i.inspector_id as inspector_id,
i.label_info_id as label_info_id,
i.container_id as container_id,
i.picture_set_id as foldeer_id,
i.inspection_comment as inspection_comment,
i.verified_date as verified_date,
f.id as fertilizer_id,
f.name as fertilizer_name,
f.registration_number as registration_number,
f.main_contact_id as organization,
o.name as organization_name,
o.phone_number as organization_phone_number,
o.address as organization_email,
l.lot_number as lot_number,
l.title_is_minimal as is_minimal_guaranteed_analysis,
l.record_keeping as is_record_keeping
FROM
fertilizer f
JOIN
inspection i ON f.latest_inspection_id = i.id
LEFT JOIN
organization o ON f.main_contact_id = o.id
LEFT JOIN
label_information l ON i.label_info_id = l.id
"""
first = True
# check if all parameters are not none
if ((org_ids is None or len(org_ids) < 1) and
(fertilizer_name is None or fertilizer_name.strip() == "") and
(registration_number is None or registration_number.strip() == "") and
(lower_bound_date is None) and
(upper_bound_date is None) and
(lot_number is None or lot_number.strip() == "")
):
raise FertilizerQueryError("No search parameters provided, please provide at least one search parameter.")
if org_ids is not None and len(org_ids) > 0:
query += "WHERE o.id = ANY(%s)"
if first:
first = False
if fertilizer_name is not None and fertilizer_name.strip() != "":
if first:
query += "WHERE "
else:
query += "AND "
query += "f.name = %s"
first = False
if registration_number is not None and registration_number.strip() != "":
if first:
query += "WHERE "
else:
query += "AND "
query += "f.registration_number = %s"
first = False
if lower_bound_date is not None:
if first:
query += "WHERE "
else:
query += "AND "
query += "DATE(i.upload_date) >= DATE(%s)"
first = False
if upper_bound_date is not None:
if first:
query += "WHERE "
else:
query += "AND "
query += "DATE(i.upload_date) <= DATE(%s)"
first = False
if lot_number is not None and lot_number.strip() != "":
if first:
query += "WHERE "
else:
query += "AND "
query += "l.lot_number = %s"
first = False

# Aggregate the Registration Numbers
query += """
GROUP BY
i.id,
i.verified,
i.upload_date,
i.updated_at,
i.inspector_id,
i.label_info_id,
i.container_id,
i.picture_set_id,
i.inspection_comment,
i.verified_date,
l.product_name,
o.id,
o.name,
o.phone_number,
o.address,
l.lot_number,
l.title_is_minimal,
l.record_keeping
"""
query += ";"

cursor.execute(query, (org_ids,))
return cursor.fetchall()
Loading

0 comments on commit d13a2c3

Please sign in to comment.