From 19d883ccd76d01dccca41dfe1f92f776eb1d4a8a Mon Sep 17 00:00:00 2001 From: Joaquin Ossa Date: Fri, 20 Dec 2024 14:03:58 +0100 Subject: [PATCH] Simplify PR --- .../int_core__screen_and_protect_users.sql | 5 +- models/intermediate/core/schema.yml | 10 + .../int_screen_and_protect__invoice.sql | 185 ------------------ ...een_and_protect__verification_requests.sql | 12 +- .../screen_and_protect/schema.yml | 12 +- .../reporting/screen_and_protect/schema.yml | 9 +- ...een_and_protect__verification_requests.sql | 11 +- models/staging/screen_and_protect/schema.yml | 11 +- ...een_and_protect__verification_requests.sql | 6 +- 9 files changed, 52 insertions(+), 209 deletions(-) delete mode 100644 models/intermediate/screen_and_protect/int_screen_and_protect__invoice.sql diff --git a/models/intermediate/core/int_core__screen_and_protect_users.sql b/models/intermediate/core/int_core__screen_and_protect_users.sql index 0e4a069..32671dc 100644 --- a/models/intermediate/core/int_core__screen_and_protect_users.sql +++ b/models/intermediate/core/int_core__screen_and_protect_users.sql @@ -2,10 +2,12 @@ with stg_core__apim_user as (select * from {{ ref("stg_core__apim_user") }}), - stg_core__apim_user_type as (select * from {{ ref("stg_core__apim_user_type") }}) + stg_core__apim_user_type as (select * from {{ ref("stg_core__apim_user_type") }}), + stg_core__user as (select * from {{ ref("stg_core__user") }}) select au.id_apim_user, au.id_apim_user_type, + u.id_account_currency as id_currency, au.json_document_user_data ->> 'DealId' as id_deal, au.json_document_user_data ->> 'AccountType' as account_type, au.json_document_user_data ->> 'ClientMarkup' as client_markup, @@ -42,3 +44,4 @@ inner join stg_core__apim_user_type aut on au.id_apim_user_type = aut.id_apim_user_type and upper(aut.user_type_name) = '{{ api_name }}' +inner join stg_core__user u on au.id_apim_user = u.id_user diff --git a/models/intermediate/core/schema.yml b/models/intermediate/core/schema.yml index 6d24dbe..91c4ff5 100644 --- a/models/intermediate/core/schema.yml +++ b/models/intermediate/core/schema.yml @@ -4590,6 +4590,16 @@ models: data_type: bigint description: "Identifier of the type of user." + - name: id_currency + data_type: bigint + description: "Id of the currency, works as a foreign key to the + currency table" + tests: + - not_null + - relationships: + to: ref('stg_core__currency') + field: id_currency + - name: id_deal data_type: text description: "" diff --git a/models/intermediate/screen_and_protect/int_screen_and_protect__invoice.sql b/models/intermediate/screen_and_protect/int_screen_and_protect__invoice.sql deleted file mode 100644 index c1edd20..0000000 --- a/models/intermediate/screen_and_protect/int_screen_and_protect__invoice.sql +++ /dev/null @@ -1,185 +0,0 @@ -{% set rejected_status = "REJECTED" %} -{% set approved_flagged_status = ("APPROVED", "FLAGGED") %} -{% set basic_protection = "BASIC PROTECTION" %} -{% set damage_waiver = "DAMAGE WAIVER" %} -{% set screen_and_protect = "SCREEN & PROTECT" %} -{% set standalone_protection = "STANDALONE PROTECTION" %} -{% set cancel_rejected_fee = 0.25 %} -{% set long_stay_booking = 30 %} - -with - int_screen_and_protect__verification_requests as ( - select * from {{ ref("int_screen_and_protect__verification_requests") }} - ), - stg_core__basic_protection as ( - select * from {{ ref("stg_core__basic_protection") }} - ), - stg_core__damage_waiver as (select * from {{ ref("stg_core__damage_waiver") }}), - stg_core__screen_and_protect as ( - select * from {{ ref("stg_core__screen_and_protect") }} - ), - stg_core__standalone_protection as ( - select * from {{ ref("stg_core__standalone_protection") }} - ), - active_discount_timeframe as ( - select distinct - vr.id_user_partner, - date_trunc('month', vr.checkout_date_utc) as verification_month, - case - when - date_trunc('month', vr.checkout_date_utc) - between vr.monthly_general_discount_start_date_utc - and vr.monthly_general_discount_end_date_utc - then 1 - else 0 - end as is_monthly_general_discount_active, - case - when - date_trunc('month', vr.checkout_date_utc) - between vr.monthly_volume_discount_start_date_utc - and vr.monthly_volume_discount_end_date_utc - then 1 - else 0 - end as is_monthly_volume_discount_on, - vr.threshold_approved_booking_volume, - case - when - date_trunc('month', vr.checkout_date_utc) - >= vr.price_increase_start_date_utc - then 1 - else 0 - end as is_price_increase_active - from int_screen_and_protect__verification_requests vr - where upper(vr.verification_status) in {{ approved_flagged_status }} - ), - approved_verifications as ( - select - vr.id_user_partner, - date_trunc('month', vr.checkout_date_utc) as verification_month, - count(*) as monthly_verifications - from int_screen_and_protect__verification_requests vr - where upper(vr.verification_status) in {{ approved_flagged_status }} - group by vr.id_user_partner, date_trunc('month', vr.checkout_date_utc) - ), - active_discounts as ( - select - av.id_user_partner, - av.verification_month, - av.monthly_verifications, - adt.is_monthly_general_discount_active, - case - when - av.monthly_verifications >= adt.threshold_approved_booking_volume - and adt.is_monthly_volume_discount_on = 1 - then 1 - else 0 - end as is_monthly_volume_discount_active, - adt.is_price_increase_active - from approved_verifications av - left join - active_discount_timeframe adt - on av.id_user_partner = adt.id_user_partner - and av.verification_month = adt.verification_month - ) -select - vr.id_verification, - vr.id_booking, - vr.id_user_partner, - vr.id_accommodation, - vr.is_protected, - vr.protection_type, - vr.verification_status, - case - when - vr.number_of_nights > {{ long_stay_booking }} - and upper(vr.verification_status) in {{ approved_flagged_status }} - and vr.is_protected is true - then - bp.long_stay_fee_in_local_currency - * vr.number_of_nights - * (1 + vr.price_increase / 100 * ad.is_price_increase_active) - * ( - 1 - - vr.monthly_volume_discount - / 100 - * ad.is_monthly_volume_discount_active - ) - * ( - 1 - - vr.monthly_general_discount - / 100 - * ad.is_monthly_general_discount_active - + vr.price_increase / 100 * ad.is_price_increase_active - ) - when - upper(vr.verification_status) in {{ approved_flagged_status }} - and vr.is_protected is true - then - bp.short_stay_fee_in_local_currency - * vr.number_of_nights - * (1 + vr.price_increase / 100 * ad.is_price_increase_active) - * ( - 1 - - vr.monthly_volume_discount - / 100 - * ad.is_monthly_volume_discount_active - ) - * ( - 1 - - vr.monthly_general_discount - / 100 - * ad.is_monthly_general_discount_active - + vr.price_increase / 100 * ad.is_price_increase_active - ) - else 0 - end as booking_fee_in_local_currency, - case - when - upper(vr.verification_status) = '{{ rejected_status }}' - or vr.is_cancelled = true - or vr.is_protected is false - then {{ cancel_rejected_fee }} - else 0 - end as cancel_or_rejected_fee_in_local_currency, - case - when - upper(vr.verification_status) = '{{ rejected_status }}' - or vr.is_cancelled = true - then creation_date_utc - else checkout_date_utc - end as invoice_date_utc, - vr.id_currency, - vr.checkin_date_utc, - vr.checkout_date_utc, - vr.number_of_nights, - vr.is_cancelled, - vr.cancelled_at_utc, - vr.cancelled_date_utc, - vr.user_email, - vr.company_name, - vr.property_manager_name, - vr.property_manager_email, - vr.listing_name, - vr.listing_address, - vr.listing_town, - vr.listing_country, - vr.listing_postcode, - vr.pets_allowed, - vr.status_updated_at_utc, - vr.status_updated_date_utc, - vr.updated_at_utc, - vr.updated_date_utc, - vr.creation_at_utc, - vr.creation_date_utc, - vr.cosmos_created_date_utc -from int_screen_and_protect__verification_requests vr -inner join - stg_core__basic_protection bp - on vr.id_currency = bp.id_currency - and vr.protection_basic_amount_in_local_currency - = bp.protection_basic_amount_in_local_currency -inner join - active_discounts ad - on vr.id_user_partner = ad.id_user_partner - and date_trunc('month', vr.checkout_date_utc) = ad.verification_month -where upper(vr.protection_type) = '{{ basic_protection }}' diff --git a/models/intermediate/screen_and_protect/int_screen_and_protect__verification_requests.sql b/models/intermediate/screen_and_protect/int_screen_and_protect__verification_requests.sql index a1e3fdb..0a87d8a 100644 --- a/models/intermediate/screen_and_protect/int_screen_and_protect__verification_requests.sql +++ b/models/intermediate/screen_and_protect/int_screen_and_protect__verification_requests.sql @@ -4,19 +4,18 @@ with ), int_core__screen_and_protect_users as ( select * from {{ ref("int_core__screen_and_protect_users") }} - ), - stg_core__user as (select * from {{ ref("stg_core__user") }}) + ) select vr.id_verification, vr.id_booking, vr.id_user_partner, vr.id_accommodation, - u.id_account_currency as id_currency, + spu.id_currency, spu.is_protected, vr.protection_type, - vr.protection_starting_level as protection_starting_level_in_local_currency, - vr.protection_basic_amount as protection_basic_amount_in_local_currency, - vr.protection_extended_amount as protection_extended_amount_in_local_currency, + vr.protection_starting_amount_in_local_currency, + vr.protection_basic_amount_in_local_currency, + vr.protection_extended_amount_in_local_currency, vr.pet_protection, vr.verification_status, vr.verification_status_reason, @@ -63,4 +62,3 @@ select from stg_screen_and_protect__verification_requests vr inner join int_core__screen_and_protect_users spu on vr.id_user_partner = spu.id_apim_user -inner join stg_core__user u on spu.id_apim_user = u.id_user diff --git a/models/intermediate/screen_and_protect/schema.yml b/models/intermediate/screen_and_protect/schema.yml index 4917710..041451b 100644 --- a/models/intermediate/screen_and_protect/schema.yml +++ b/models/intermediate/screen_and_protect/schema.yml @@ -35,6 +35,16 @@ models: data_type: text description: Identifier for the accommodation related to the booking. + - name: id_currency + data_type: bigint + description: "Id of the currency, works as a foreign key to the + currency table" + tests: + - not_null + - relationships: + to: ref('stg_core__currency') + field: id_currency + - name: is_protected data_type: boolean description: | @@ -55,7 +65,7 @@ models: - "DAMAGE WAIVER" - "PET PROTECTION" - - name: protection_starting_level_in_local_currency + - name: protection_starting_amount_in_local_currency data_type: numeric description: Field used for protection type "STANDALONE PROTECTION" to indicate the starting level of protection. diff --git a/models/reporting/screen_and_protect/schema.yml b/models/reporting/screen_and_protect/schema.yml index 7817753..9979e50 100644 --- a/models/reporting/screen_and_protect/schema.yml +++ b/models/reporting/screen_and_protect/schema.yml @@ -50,30 +50,33 @@ models: - "DAMAGE WAIVER" - "PET PROTECTION" - - name: protection_starting_level + - name: protection_starting_amount_in_local_currency data_type: numeric description: Field used for protection type "STANDALONE PROTECTION" to indicate the starting level of protection. + In local currency. tests: - dbt_expectations.expect_column_values_to_be_between: min_value: 0 max_value: 100000 strictly: true - - name: protection_basic_amount + - name: protection_basic_amount_in_local_currency data_type: numeric description: Field used for protection type "SCREEN & PROTECT", "BASIC PROTECTION" or "DAMAGE WAIVER" to show the basic amount of protection. + In local currency. tests: - dbt_expectations.expect_column_values_to_be_between: min_value: 0 max_value: 100000 strictly: true - - name: protection_extended_amount + - name: protection_extended_amount_in_local_currency data_type: numeric description: Field used for protection type "SCREEN & PROTECT", or "STANDALONE PROTECTION" to show the basic amount of protection. + In local currency. tests: - dbt_expectations.expect_column_values_to_be_between: min_value: 0 diff --git a/models/reporting/screen_and_protect/screen_and_protect__verification_requests.sql b/models/reporting/screen_and_protect/screen_and_protect__verification_requests.sql index ef45209..aed3acd 100644 --- a/models/reporting/screen_and_protect/screen_and_protect__verification_requests.sql +++ b/models/reporting/screen_and_protect/screen_and_protect__verification_requests.sql @@ -3,17 +3,18 @@ with select * from {{ ref("int_screen_and_protect__verification_requests") }} ) select - -- note that these ids are not the same as the ones found in Core DWH - -- they are completely unrelated id_verification as id_verification, id_booking as id_booking, id_user_partner as id_user_partner, id_accommodation as id_accommodation, is_protected as is_protected, protection_type as protection_type, - protection_starting_level as protection_starting_level, - protection_basic_amount as protection_basic_amount, - protection_extended_amount as protection_extended_amount, + protection_starting_amount_in_local_currency + as protection_starting_amount_in_local_currency, + protection_basic_amount_in_local_currency + as protection_basic_amount_in_local_currency, + protection_extended_amount_in_local_currency + as protection_extended_amount_in_local_currency, pet_protection as pet_protection, verification_status as verification_status, verification_status_reason as verification_status_reason, diff --git a/models/staging/screen_and_protect/schema.yml b/models/staging/screen_and_protect/schema.yml index c7cf271..76a1091 100644 --- a/models/staging/screen_and_protect/schema.yml +++ b/models/staging/screen_and_protect/schema.yml @@ -43,30 +43,33 @@ models: - "DAMAGE WAIVER" - "PET PROTECTION" - - name: protection_starting_level + - name: protection_starting_amount_in_local_currency data_type: numeric description: Field used for protection type "STANDALONE PROTECTION" to - indicate the starting level of protection. + indicate the starting amount of protection. + In local currency. tests: - dbt_expectations.expect_column_values_to_be_between: min_value: 0 max_value: 100000 strictly: true - - name: protection_basic_amount + - name: protection_basic_amount_in_local_currency data_type: numeric description: Field used for protection type "SCREEN & PROTECT", "BASIC PROTECTION" or "DAMAGE WAIVER" to show the basic amount of protection. + In local currency. tests: - dbt_expectations.expect_column_values_to_be_between: min_value: 0 max_value: 100000 strictly: true - - name: protection_extended_amount + - name: protection_extended_amount_in_local_currency data_type: numeric description: Field used for protection type "SCREEN & PROTECT", or "STANDALONE PROTECTION" to show the basic amount of protection. + In local currency. tests: - dbt_expectations.expect_column_values_to_be_between: min_value: 0 diff --git a/models/staging/screen_and_protect/stg_screen_and_protect__verification_requests.sql b/models/staging/screen_and_protect/stg_screen_and_protect__verification_requests.sql index cbaf7a2..5f1536a 100644 --- a/models/staging/screen_and_protect/stg_screen_and_protect__verification_requests.sql +++ b/models/staging/screen_and_protect/stg_screen_and_protect__verification_requests.sql @@ -22,7 +22,7 @@ with then null else {{ adapter.quote("documents") }} ->> 'ProtectionStartingLevel' end as decimal(19, 4) - ) as protection_starting_level, + ) as protection_starting_amount_in_local_currency, cast( case when @@ -31,7 +31,7 @@ with then null else {{ adapter.quote("documents") }} ->> 'ProtectionBasicAmount' end as decimal(19, 4) - ) as protection_basic_amount, + ) as protection_basic_amount_in_local_currency, cast( case when @@ -40,7 +40,7 @@ with then null else {{ adapter.quote("documents") }} ->> 'ProtectionExtendedAmount' end as decimal(19, 4) - ) as protection_extended_amount, + ) as protection_extended_amount_in_local_currency, ({{ adapter.quote("documents") }} ->> 'PetProtection')::boolean as pet_protection, upper({{ adapter.quote("documents") }} ->> 'Status') as verification_status,