From 5b89c97b001fee032b46d3896c6d76afae17ef08 Mon Sep 17 00:00:00 2001 From: jordi fita mas Date: Mon, 15 Jul 2024 23:41:47 +0200 Subject: [PATCH] Add operating_dates to campsite type table We forgot that different accommodation types are not always operating on the whole season calendar, thus we need a specific date for each type. Someday i will add the field in the administration panel, but for now i will have to add them by hand, as people are starting to book plots on dates that are not operating. --- deploy/campsite_type__operating_dates.sql | 12 ++++++++++++ pkg/booking/public.go | 6 +++--- revert/campsite_type__operating_dates.sql | 9 +++++++++ sqitch.plan | 1 + test/campsite_type.sql | 8 +++++++- verify/campsite_type__operating_dates.sql | 10 ++++++++++ 6 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 deploy/campsite_type__operating_dates.sql create mode 100644 revert/campsite_type__operating_dates.sql create mode 100644 verify/campsite_type__operating_dates.sql diff --git a/deploy/campsite_type__operating_dates.sql b/deploy/campsite_type__operating_dates.sql new file mode 100644 index 0000000..6739da4 --- /dev/null +++ b/deploy/campsite_type__operating_dates.sql @@ -0,0 +1,12 @@ +-- Deploy camper:campsite_type__operating_dates to pg +-- requires: campsite_type + +begin; + +set search_path to camper, public; + +alter table campsite_type + add column operating_dates daterange not null default 'empty' +; + +commit; diff --git a/pkg/booking/public.go b/pkg/booking/public.go index 6d73ff2..d28b918 100644 --- a/pkg/booking/public.go +++ b/pkg/booking/public.go @@ -245,14 +245,14 @@ func NewDateFields(ctx context.Context, conn *database.Conn, campsiteType string row := conn.QueryRow(ctx, ` select lower(bookable_nights), upper(bookable_nights) - 1, - greatest(min(lower(season_range)), current_timestamp::date), - max(upper(season_range)) + greatest(min(lower(season_range)), lower(operating_dates), current_timestamp::date), + least(max(upper(season_range)), upper(operating_dates)) from campsite_type join campsite_type_cost using (campsite_type_id) join season_calendar using (season_id) where campsite_type.slug = $1 and season_range >> daterange(date_trunc('year', current_timestamp)::date, date_trunc('year', current_timestamp)::date + 1) - group by bookable_nights; + group by bookable_nights, operating_dates `, campsiteType) f := &DateFields{ ArrivalDate: &bookingDateInput{ diff --git a/revert/campsite_type__operating_dates.sql b/revert/campsite_type__operating_dates.sql new file mode 100644 index 0000000..c64268d --- /dev/null +++ b/revert/campsite_type__operating_dates.sql @@ -0,0 +1,9 @@ +-- Revert camper:campsite_type__operating_dates from pg + +begin; + +alter table camper.campsite_type + drop column if exists operating_dates +; + +commit; diff --git a/sqitch.plan b/sqitch.plan index 6101d68..999bb2e 100644 --- a/sqitch.plan +++ b/sqitch.plan @@ -333,3 +333,4 @@ booking_invoice [roles schema_camper booking invoice] 2024-04-28T19:45:05Z jordi marshal_payment [roles schema_camper payment payment_customer payment_option payment__acsi_card payment_customer__-acsi_card] 2024-04-29T17:11:59Z jordi fita mas # Add function to marshal a payment unmarshal_booking [roles schema_camper booking booking_option extension_pg_libphonenumber] 2024-04-29T17:20:38Z jordi fita mas # Add function to unmarshal a booking cancel_booking [roles schema_camper booking booking_campsite] 2024-05-03T14:27:31Z jordi fita mas # Add function to cancel a booking +campsite_type__operating_dates [campsite_type] 2024-07-15T21:27:19Z jordi fita mas # Add operating_dates field to campsite_type diff --git a/test/campsite_type.sql b/test/campsite_type.sql index 83ad93c..0293b08 100644 --- a/test/campsite_type.sql +++ b/test/campsite_type.sql @@ -5,7 +5,7 @@ reset client_min_messages; begin; -select plan(108); +select plan(113); set search_path to camper, public; @@ -118,6 +118,12 @@ select col_not_null('campsite_type', 'active'); select col_has_default('campsite_type', 'active'); select col_default_is('campsite_type', 'active', 'true'); +select has_column('campsite_type', 'operating_dates'); +select col_type_is('campsite_type', 'operating_dates', 'daterange'); +select col_not_null('campsite_type', 'operating_dates'); +select col_has_default('campsite_type', 'operating_dates'); +select col_default_is('campsite_type', 'operating_dates', 'empty'); + select has_column('campsite_type', 'position'); select col_type_is('campsite_type', 'position', 'integer'); select col_not_null('campsite_type', 'position'); diff --git a/verify/campsite_type__operating_dates.sql b/verify/campsite_type__operating_dates.sql new file mode 100644 index 0000000..427a4be --- /dev/null +++ b/verify/campsite_type__operating_dates.sql @@ -0,0 +1,10 @@ +-- Verify camper:campsite_type__operating_dates on pg + +begin; + +select operating_dates +from camper.campsite_type +where false +; + +rollback;