From c49239a7cf7e69108c84fd72a9dd78f65fad976a Mon Sep 17 00:00:00 2001 From: Joaquin Ossa Date: Tue, 25 Feb 2025 09:13:41 +0100 Subject: [PATCH] Final changes --- .../int_resolutions__incidents.sql | 47 +++++++++++++++---- models/intermediate/resolutions/schema.yml | 20 ++++++-- 2 files changed, 54 insertions(+), 13 deletions(-) diff --git a/models/intermediate/resolutions/int_resolutions__incidents.sql b/models/intermediate/resolutions/int_resolutions__incidents.sql index 94b0ec8..c72441c 100644 --- a/models/intermediate/resolutions/int_resolutions__incidents.sql +++ b/models/intermediate/resolutions/int_resolutions__incidents.sql @@ -2,6 +2,7 @@ with stg_resolutions__incidents as ( select * from {{ ref("stg_resolutions__incidents") }} ), + int_core__user_host as (select * from {{ ref("int_core__user_host") }}), int_daily_currency_exchange_rates as ( select * from {{ ref("int_daily_currency_exchange_rates") }} ), @@ -37,26 +38,50 @@ with ) as last_status_at_utc from stg_resolutions__incidents ), + -- The host can submit any number of damaged items in the damage report. + -- Each damaged item can have different currencies, so we need to convert + -- the amount to the host's currency. damage_reported_items as ( - select id_incident, jsonb_array_elements(damage_report_items::jsonb) as item, - from stg_resolutions__incidents - where damage_report_items is not null + select + i.id_incident, + jsonb_array_elements(i.damage_report_items::jsonb) as item, + uh.account_currency_iso4217 as host_currency, + i.created_date_utc + from stg_resolutions__incidents i + left join int_core__user_host uh on i.id_user_host = uh.id_user_host + where i.damage_report_items is not null ), damage_report as ( select dri.id_incident, + dri.host_currency, count(distinct dri.item ->> 'Id') as number_damaged_items, sum( - coalesce((cm.corrective_measure ->> 'OriginalAmount')::numeric, 0) + coalesce( + ((dri.item ->> 'OriginalAmount')::numeric * cer.rate)::decimal( + 19, 4 + ), + 0 + ) ) as original_value_amount_in_txn_currency, sum( - coalesce((cm.corrective_measure ->> 'Amount')::numeric, 0) + coalesce( + ((cm.corrective_measure ->> 'Amount')::numeric * cer.rate)::decimal( + 19, 4 + ), + 0 + ) ) as asked_repair_amount_in_txn_currency, sum( - coalesce((cm.corrective_measure ->> 'SettlementAmount')::numeric, 0) + coalesce( + ( + (cm.corrective_measure ->> 'SettlementAmount')::numeric + * cer.rate + )::decimal(19, 4), + 0 + ) ) as accepted_amount_in_txn_currency from damage_reported_items dri - left join int_daily_currency_exchange_rates cer left join lateral jsonb_array_elements( case @@ -66,7 +91,12 @@ with end ) as cm(corrective_measure) on true - group by dri.id_incident + left join + int_daily_currency_exchange_rates cer + on dri.created_date_utc = cer.rate_date_utc + and (cm.corrective_measure ->> 'Currency') = cer.from_currency + and dri.host_currency = cer.to_currency + group by dri.id_incident, dri.host_currency ) select @@ -101,6 +131,7 @@ select dr.original_value_amount_in_txn_currency, dr.asked_repair_amount_in_txn_currency, dr.accepted_amount_in_txn_currency, + dr.host_currency, -- Resolutions calculator i.protection_name, diff --git a/models/intermediate/resolutions/schema.yml b/models/intermediate/resolutions/schema.yml index b86a506..3615617 100644 --- a/models/intermediate/resolutions/schema.yml +++ b/models/intermediate/resolutions/schema.yml @@ -69,7 +69,7 @@ models: - name: comment_count data_type: bigint - description: "Number of comments on the incident." + description: "Number of comments submitted on the incident." - name: first_status_at_utc data_type: timestamp @@ -118,6 +118,15 @@ models: min_value: 0 strictly: false + - name: host_currency + data_type: text + description: "Host or local currency" + data_tests: + - not_null: + where: "original_value_amount_in_txn_currency > 0 + or asked_repair_amount_in_txn_currency > 0 + or accepted_amount_in_txn_currency > 0" + - name: was_overriden data_type: boolean description: | @@ -185,8 +194,7 @@ models: - name: id_user_host data_type: text description: | - "Unique Id that identifies the host. Can be from SH or can - be external from the client." + "Unique Id that identifies the host." data_tests: - not_null - relationships: @@ -271,11 +279,13 @@ models: - name: has_guest_contributed_to_cost data_type: boolean - description: "If the guest has agreed to contribute to the cost when talking to the host" + description: "If the guest has agreed to contribute to the cost + when talking to the host" - name: has_host_taken_preventative_steps data_type: boolean - description: "If the host has taken any action to prevent the damage or further damage once identified" + description: "If the host has taken any action to prevent the damage + or further damage once identified" - name: guest_contribution_amount_in_txn_currency data_type: numeric