From 00218273682fc0d8519df994e61d6f8b7bf89e15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oriol=20Roqu=C3=A9=20Paniagua?= Date: Tue, 10 Jun 2025 14:38:35 +0000 Subject: [PATCH] Merged PR 5429: Rework Booking Source in int_core__bookings # Description This PR creates a proper Booking Source in `int_core__bookings` table. This mostly refers to ensure that whenever we do data requests tickets on this regards we have a proper logic in place (as last time we had issues because of this). Logic follows Gus recommendation on applying: ``` case when upper(coalesce(bsrc.booking_source, 'UNKNOWN')) = 'UNKNOWN' then b.raw_booking_source else bsrc.booking_source end ``` where b. is Booking table and bsrc. is Booking Source table. Things to note: * A new `stg_core__booking_source` table is created, full-refreshed from the backend (less than 10 records) * I added a unique test for `stg_core__booking` that was not in there. I don't document the rest of the model though. * I remove the previous `id_booking_source` as it wasn't used and the logic in place should be far more accurate. * I do NOT remove the existing `verification_request_booking_source`. I don't remember what's this but I see this is in use specially on Guest Insights (CSAT) and the legacy Truvi reporting (Bookings, PMS, etc.). Not the scope of this ticket to change it. * I don't do further propagation, but potentially, this could be extended to New Dash Booking Summary as this source is being shown in the dashboard itself. # Checklist - [X] The edited models and dependants run properly with production data. - [X] The edited models are sufficiently documented. - [X] The edited models contain PK tests, and I've ran and passed them. - [X] I have checked for DRY opportunities with other models and docs. - [X] I've picked the right materialization for the affected models. # Other - [ ] Check if a full-refresh is required after this PR is merged. Related work items: #30415 --- .../intermediate/core/int_core__bookings.sql | 10 +++++++++- models/intermediate/core/schema.yml | 19 ++++++++++++++----- models/staging/core/_core_sources.yml | 2 ++ models/staging/core/schema.yml | 18 ++++++++++++++++++ models/staging/core/stg_core__booking.sql | 1 + .../staging/core/stg_core__booking_source.sql | 12 ++++++++++++ 6 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 models/staging/core/stg_core__booking_source.sql diff --git a/models/intermediate/core/int_core__bookings.sql b/models/intermediate/core/int_core__bookings.sql index 70ec201..cad61b1 100644 --- a/models/intermediate/core/int_core__bookings.sql +++ b/models/intermediate/core/int_core__bookings.sql @@ -2,6 +2,7 @@ with stg_core__booking as (select * from {{ ref("stg_core__booking") }}), stg_core__booking_state as (select * from {{ ref("stg_core__booking_state") }}), + stg_core__booking_source as (select * from {{ ref("stg_core__booking_source") }}), int_core__duplicate_bookings as ( select id_booking, is_duplicate_booking, is_duplicating_booking_with_id from {{ ref("int_core__duplicate_bookings") }} @@ -23,7 +24,13 @@ select b.id_user_host, b.id_integration, b.id_accommodation, - b.id_booking_source, + upper( + case + when upper(coalesce(bsrc.booking_source, 'UNKNOWN')) = 'UNKNOWN' + then b.raw_booking_source + else bsrc.booking_source + end + ) as booking_source, b.id_verification_request, vrbs.verification_request_booking_source, coalesce(db.is_duplicate_booking, false) as is_duplicate_booking, @@ -54,6 +61,7 @@ from stg_core__booking b -- which in turn ensures the removal of test accounts. inner join int_core__user_host uh on b.id_user_host = uh.id_user_host left join stg_core__booking_state bs on b.id_booking_state = bs.id_booking_state +left join stg_core__booking_source bsrc on b.id_booking_source = bsrc.id_booking_source left join int_core__duplicate_bookings db on b.id_booking = db.id_booking left join int_core__booking_charge_events bce on b.id_booking = bce.id_booking left join diff --git a/models/intermediate/core/schema.yml b/models/intermediate/core/schema.yml index 7bbb1e9..907a87a 100644 --- a/models/intermediate/core/schema.yml +++ b/models/intermediate/core/schema.yml @@ -1263,13 +1263,22 @@ models: data_type: bigint description: "Id value for the verification request, there can be more than 1 record for each verification request since they can be associated to more than 1 booking" + - name: booking_source + data_type: character varying + description: | + The source of the booking according to Booking data. + This should be preferred over the verification_request_booking_source. + - name: verification_request_booking_source data_type: text - description: Source type of host of the booking, this could be either; - - PMS - - OSL - - API/MANUAL - - null (bookings without verification request) + description: | + Source type of host of the booking according to the Verification Request. + Prefer booking_source over this field. + This could be either; + - PMS + - OSL + - API/MANUAL + - null (bookings without verification request) data_tests: - accepted_values: values: diff --git a/models/staging/core/_core_sources.yml b/models/staging/core/_core_sources.yml index 1abc912..ea440cb 100644 --- a/models/staging/core/_core_sources.yml +++ b/models/staging/core/_core_sources.yml @@ -283,6 +283,8 @@ sources: identifier: VerificationRequestGuestProductToPayment - name: UserProductBundleToGuestProduct identifier: UserProductBundleToGuestProduct + - name: BookingSource + identifier: BookingSource - name: guest_product schema: sync_guest_product diff --git a/models/staging/core/schema.yml b/models/staging/core/schema.yml index 91b705f..e9cad40 100644 --- a/models/staging/core/schema.yml +++ b/models/staging/core/schema.yml @@ -114,6 +114,24 @@ models: - unique - not_null + - name: stg_core__booking + columns: + - name: id_booking + data_tests: + - unique + - not_null + + - name: stg_core__booking_source + columns: + - name: id_booking_source + data_tests: + - unique + - not_null + - name: booking_source + data_tests: + - unique + - not_null + - name: stg_core__accommodation_to_user columns: - name: id_accommodation_to_user diff --git a/models/staging/core/stg_core__booking.sql b/models/staging/core/stg_core__booking.sql index a1ffac2..8349d85 100644 --- a/models/staging/core/stg_core__booking.sql +++ b/models/staging/core/stg_core__booking.sql @@ -21,6 +21,7 @@ with {{ adapter.quote("IntegrationId") }} as id_integration, {{ adapter.quote("AccommodationId") }} as id_accommodation, {{ adapter.quote("BookingSourceId") }} as id_booking_source, + {{ adapter.quote("RawBookingSource") }} as raw_booking_source, {{ adapter.quote("VerificationRequestId") }} as id_verification_request, {{ adapter.quote("BookingStateId") }} as id_booking_state, {{ adapter.quote("CheckIn") }} as check_in_at_utc, diff --git a/models/staging/core/stg_core__booking_source.sql b/models/staging/core/stg_core__booking_source.sql new file mode 100644 index 0000000..72fda81 --- /dev/null +++ b/models/staging/core/stg_core__booking_source.sql @@ -0,0 +1,12 @@ +with + raw_booking_source as (select * from {{ source("core", "BookingSource") }}), + stg_core__booking_source as ( + select + {{ adapter.quote("Id") }} as id_booking_source, + upper({{ adapter.quote("FullName") }}) as booking_source, + {{ adapter.quote("_airbyte_extracted_at") }} as dwh_extracted_at_utc + + from raw_booking_source + ) +select * +from stg_core__booking_source