Skip to content

Commit

Permalink
Préavis – Ajouter la boîte de façade aux destinataires des emails de …
Browse files Browse the repository at this point in the history
…préavis (#3678)

## Linked issues

- Resolve #3596
  • Loading branch information
VincentAntoine authored Sep 24, 2024
2 parents 6f39a69 + 085eb28 commit 639a8b5
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE public.facade_areas_subdivided
ADD COLUMN email_address text;
3 changes: 3 additions & 0 deletions datascience/src/pipeline/entities/pnos.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class PnoToRender:
catch_onboard: List[dict]
port_locode: str
port_name: str
facade: str
predicted_arrival_datetime_utc: datetime
predicted_landing_datetime_utc: datetime
trip_gears: List[dict]
Expand Down Expand Up @@ -113,6 +114,7 @@ class PreRenderedPno:
catch_onboard: pd.DataFrame
port_locode: str
port_name: str
facade: str
predicted_arrival_datetime_utc: datetime
predicted_landing_datetime_utc: datetime
trip_gears: List[FishingGear]
Expand Down Expand Up @@ -204,6 +206,7 @@ class RenderedPno:
is_being_sent: bool
trip_segments: list
port_locode: str
facade: str
source: PnoSource
html_for_pdf: Optional[str] = None
pdf_document: Optional[bytes] = None
Expand Down
22 changes: 22 additions & 0 deletions datascience/src/pipeline/flows/distribute_pnos.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ def extract_pnos_to_generate(
return (pnos, generation_needed)


@task(checkpoint=False)
def extract_facade_email_addresses() -> dict:
df = extract(
db_name="monitorfish_remote",
query_filepath="monitorfish/facade_email_addresses.sql",
)
return df.set_index("facade").email_address.to_dict()


@task(checkpoint=False)
def to_pnos_to_render(pnos: pd.DataFrame) -> List[PnoToRender]:
records = pnos.to_dict(orient="records")
Expand Down Expand Up @@ -247,6 +256,7 @@ def format_sr_list(sr_list: list) -> str:
catch_onboard=sum_by_species,
port_locode=pno.port_locode,
port_name=pno.port_name,
facade=pno.facade,
predicted_arrival_datetime_utc=pno.predicted_arrival_datetime_utc,
predicted_landing_datetime_utc=pno.predicted_landing_datetime_utc,
trip_gears=trip_gears,
Expand Down Expand Up @@ -474,6 +484,7 @@ def format_nullable_datetime(d: datetime, format: str = date_format):
is_being_sent=pno.is_being_sent,
trip_segments=pno.trip_segments,
port_locode=pno.port_locode,
facade=pno.facade,
source=pno.source,
html_for_pdf=html_for_pdf,
pdf_document=pdf,
Expand Down Expand Up @@ -536,6 +547,7 @@ def attribute_addressees(
units_targeting_vessels: pd.DataFrame,
units_ports_and_segments_subscriptions: pd.DataFrame,
control_units_contacts: pd.DataFrame,
facade_email_addresses: dict,
) -> RenderedPno:
"""
Returns a copy of the input `RenderedPno`'s with its `control_unit_ids`,
Expand All @@ -560,6 +572,9 @@ def attribute_addressees(
`unit_subscribed_segments`
control_units_contacts (pd.DataFrame): DataFrame with columns
`control_unit_id`, `emails` and `phone_numbers`
facade_email_addresses (dict): dictionnary with facade names as keys and FMC
facade email addresses as values. The email address of the corresponding
facade will be added to addressees in PNO emails.
Returns:
RenderedPno: copy of the input `pno_to_distribute` with addressees added
Expand Down Expand Up @@ -622,6 +637,10 @@ def attribute_addressees(
)
)
)
if emails:
facade_email_address = facade_email_addresses.get(pno_to_distribute.facade)
if facade_email_address:
emails.append(facade_email_address)

phone_numbers = sorted(
set(
Expand Down Expand Up @@ -995,11 +1014,14 @@ def make_manual_prior_notifications_statement(

with case(distribution_needed, True):
# Distribute PNOs
facade_email_addresses = extract_facade_email_addresses()

pnos_with_addressees = attribute_addressees.map(
pnos_to_distribute,
unmapped(units_targeting_vessels),
unmapped(units_ports_and_segments_subscriptions),
control_units_contacts=unmapped(control_units_contacts),
facade_email_addresses=unmapped(facade_email_addresses),
)

email = create_email.map(
Expand Down
1 change: 1 addition & 0 deletions datascience/src/pipeline/queries/cross/facade_areas.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
SELECT
facade_cnsp AS facade,
email_address,
ST_SubDivide(geometry) AS geometry
FROM prod.facade_areas
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ facades_polygons_without_holes AS (
SELECT
id,
prod.facade_areas_unextended.facade,
prod.facade_areas_unextended.email_address,
ST_Difference(ST_MakePolygon(ST_ExteriorRing((ST_Dump(geom)).geom)), other_facades_geom) AS geom
FROM prod.facade_areas_unextended
JOIN other_unexpanded_facades
Expand All @@ -32,9 +33,10 @@ facades_multipolygons AS (
SELECT
id,
facade,
email_address,
ST_Multi(ST_Union(geom)) AS geom
FROM facades_polygons_without_holes
GROUP BY id, facade
GROUP BY id, facade, email_address
),

all_facades AS (
Expand All @@ -45,6 +47,7 @@ all_facades AS (
expanded_facades AS (
SELECT
facade,
email_address,
ST_Difference(
ST_Buffer(geom, 0.1),
ST_Difference(
Expand All @@ -69,6 +72,7 @@ expanded_facades_without_overlap AS (
SELECT
ef.facade AS facade_cnsp,
CASE WHEN ef.facade = 'Corse' THEN 'MED' ELSE ef.facade END AS facade_cacem,
email_address,
ST_Difference(
ef.expanded_geometry,
other_expanded_facades_geom
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SELECT DISTINCT ON (facade)
facade,
email_address
FROM facade_areas_subdivided
ORDER BY facade
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ acknowledged_deleted_messages AS (
r.value->'catchOnboard' AS catch_onboard,
r.value->>'port' AS port_locode,
p.port_name,
p.facade,
(r.value->>'predictedArrivalDatetimeUtc')::TIMESTAMPTZ AT TIME ZONE 'UTC' AS predicted_arrival_datetime_utc,
(r.value->>'predictedLandingDatetimeUtc')::TIMESTAMPTZ AT TIME ZONE 'UTC' AS predicted_landing_datetime_utc,
r.trip_gears,
Expand Down Expand Up @@ -109,6 +110,7 @@ UNION ALL
r.value->'catchOnboard' AS catch_onboard,
r.value->>'port' AS port_locode,
p.port_name,
p.facade,
(r.value->>'predictedArrivalDatetimeUtc')::TIMESTAMPTZ AT TIME ZONE 'UTC' AS predicted_arrival_datetime_utc,
(r.value->>'predictedLandingDatetimeUtc')::TIMESTAMPTZ AT TIME ZONE 'UTC' AS predicted_landing_datetime_utc,
(CASE WHEN jsonb_typeof(r.trip_gears) = 'array' THEN r.trip_gears ELSE '[]'::jsonb END) AS trip_gears,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ DELETE FROM public.ports;

INSERT INTO public.ports (
country_code_iso2, region, locode, port_name, is_active, facade) VALUES
( 'FR', '56', 'FAKE', 'Fake Port initially active', true, NULL),
( 'FR', '56', 'FAKE', 'Fake Port initially active', true, 'NAMO'),
( 'FR', '56', 'PNO_PORT', 'Fake Port with PNO', false, NULL),
( 'FR', '56', 'LAN_PORT', 'Fake Port with LAN', false, NULL),
( 'FR', '999', 'FRCQF', 'Somewhere over the rainbow', false, NULL),
( 'FR', '999', 'FRBES', 'Somewhere over the hill', false, NULL),
( 'FR', '56', 'LAN_PORT', 'Fake Port with LAN', false, 'NAMO'),
( 'FR', '999', 'FRCQF', 'Somewhere over the rainbow', false, 'NAMO'),
( 'FR', '999', 'FRBES', 'Somewhere over the hill', false, 'SA'),
( 'FR', '999', 'FRDPE', 'Somewhere over the clouds', false, NULL),
( 'FR', '999', 'FRDKK', 'Somewhere over the swell', false, NULL),
( 'FR', '999', 'FRLEH', 'Somewhere over the ocean', false, NULL),
( 'FR', '999', 'FRZJZ', 'Somewhere over the top', false, NULL),
( 'FR', '999', 'FRDKK', 'Somewhere over the swell', false, 'NAMO'),
( 'FR', '999', 'FRLEH', 'Somewhere over the ocean', false, 'SA'),
( 'FR', '999', 'FRZJZ', 'Somewhere over the top', false, 'SA'),
( 'FR', '999', 'GBPHD', 'Port with facade', false, 'MEMN');
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
DELETE FROM facade_areas_subdivided;

INSERT INTO facade_areas_subdivided (
facade, geometry
facade, email_address, geometry
) VALUES
('NAMO', ST_Polygon('LINESTRING(10.0 45.0, -10.0 45.0, -10.0 0.0, 10.0 0.0, 10.0 45.0)'::geometry, 4326)),
('SA', ST_Polygon('LINESTRING(10.0 45.0, -10.0 45.0, -10.0 50.0, 10.0 50.0, 10.0 45.0)'::geometry, 4326));
( 'NAMO', 'namo@email', ST_Polygon('LINESTRING(10.0 45.0, -10.0 45.0, -10.0 0.0, 10.0 0.0, 10.0 45.0)'::geometry, 4326)),
( 'SA', 'sa@email', ST_Polygon('LINESTRING(10.0 45.0, -10.0 45.0, -10.0 50.0, 10.0 50.0, 10.0 45.0)'::geometry, 4326));
Loading

0 comments on commit 639a8b5

Please sign in to comment.