data-dwh-dbt-project/models/intermediate/resolutions/int_resolutions__incidents.sql
2025-02-25 15:25:37 +01:00

150 lines
4.1 KiB
SQL

with
stg_resolutions__incidents as (
select * from {{ ref("stg_resolutions__incidents") }}
),
logs_timeline as (
select
i.id_incident,
(
select (comment ->> 'CreatedDate')::timestamp
from jsonb_array_elements(i.comment_logs::jsonb) comment
order by (comment ->> 'CreatedDate')::timestamp
limit 1
) as first_comment_date,
(
select (comment ->> 'CreatedDate')::timestamp
from jsonb_array_elements(i.comment_logs::jsonb) comment
order by (comment ->> 'CreatedDate')::timestamp desc
limit 1
) as last_comment_date,
(
select count(*) from jsonb_array_elements(i.comment_logs::jsonb) comment
) as comment_count,
(
select count(*)
from jsonb_array_elements(i.damage_report_items::jsonb) items
) as number_damaged_items,
from stg_resolutions__incidents i
),
damage_reported_items as (
select
i.id_incident,
jsonb_array_elements(i.documents -> 'DamageReport' -> 'Items') as item
from stg_resolutions__incidents i
)
damage_report_amounts as (
select
id_incident,
sum((corrective_measures ->> 'Amount')::numeric) as asked_repair_amount,
sum(
(corrective_measures ->> 'SettlementAmount')::numeric
) as accepted_amount,
from
parsed_data,
jsonb_array_elements(item -> 'CorrectiveMeasures') as corrective_measures
group by id_incident
)
select
-- Basic Incident Details
i.id_incident,
id_user,
id_verification,
current_status_name,
is_submission_complete,
current_agent_name,
read_only_for_customers,
status_history_logs,
document_version,
task_execution_logs,
created_at_date,
created_date_date,
updated_at_date,
updated_date_date,
-- Resolution Details
first_comment_date,
last_comment_date,
-- Host Details
id_user_host,
host_user_claim_logs,
host_account_name,
-- Host Contact Details
host_email,
host_last_name,
host_first_name,
host_phone_code,
host_phone_number,
host_phone_number_with_code,
-- Guest Details
id_user_guest,
guest_email,
guest_last_name,
guest_first_name,
guest_phone_code,
guest_phone_number,
guest_phone_number_with_code,
-- Guest Deposit Details
is_guest_deposit_retained,
is_guest_deposit_collected,
deposit_retained_amount_in_txn_currency,
deposit_retained_currency,
-- Guest Involvements
has_guest_contributed_to_cost,
has_host_taken_preventative_steps,
guest_contribution_amount_in_txn_currency,
guest_contribution_currency,
guest_contacted_about_damage,
guest_contacted_evidence_files,
guest_preventative_steps_details,
-- Accommodation Details
id_accommodation,
accommodation_url,
accommodation_name,
are_pets_allowed,
-- Booking Details
check_in_at_utc,
check_in_date_utc,
check_out_at_utc,
check_out_date_utc,
id_booking,
booking_status,
id_reservation,
booking_details,
number_of_guests,
booking_services,
booking_protection,
booking_platform_used,
booking_platform_reference,
-- Damage Report
before_damage_evidence,
original_invoice_evidence,
damage_incident_details,
has_confirmed_loss,
damage_report_items,
-- Calculator
protection_name,
was_overriden,
waiver_or_deposit_name,
guest_verification_status,
lower_protection_limit_usd,
upper_protection_limit_usd,
calculated_payout_amount_in_txn_currency,
calculated_payout_currency,
calculated_payout_amount_in_usd,
calculated_guest_charge_amount_in_txn_currency,
calculated_guest_charge_currency,
calculated_guest_charge_amount_in_usd
from stg_resolutions__incidents i
left join comments_timeline ct on i.id_incident = ct.id_incident