From e3013075ccaca6da010c44e6d34e1812046d2313 Mon Sep 17 00:00:00 2001 From: Aleksey Sabilin Date: Thu, 23 Jul 2026 13:20:33 +0300 Subject: [PATCH] feat: booking JSON user_nickname/email for owner UI. Refs EventHub/EventHubBack#56 --- src/handlers/handler_booking_by_id.erl | 15 +--------- src/handlers/handler_bookings.erl | 15 +--------- src/handlers/handler_user_bookings.erl | 15 +--------- src/handlers/handler_utils.erl | 39 ++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 42 deletions(-) mode change 100644 => 100755 src/handlers/handler_user_bookings.erl diff --git a/src/handlers/handler_booking_by_id.erl b/src/handlers/handler_booking_by_id.erl index c23e1db..9d50b7f 100755 --- a/src/handlers/handler_booking_by_id.erl +++ b/src/handlers/handler_booking_by_id.erl @@ -181,17 +181,4 @@ cancel_booking(Req) -> %% Учитывает все поля из records.hrl. -spec booking_to_json(#booking{}) -> map(). booking_to_json(Booking) -> - #{ - id => Booking#booking.id, - event_id => Booking#booking.event_id, - user_id => Booking#booking.user_id, - status => Booking#booking.status, - notes => Booking#booking.notes, - reminder_sent => Booking#booking.reminder_sent, - confirmed_at => case Booking#booking.confirmed_at of - undefined -> null; - Dt -> handler_utils:datetime_to_iso8601(Dt) - end, - created_at => handler_utils:datetime_to_iso8601(Booking#booking.created_at), - updated_at => handler_utils:datetime_to_iso8601(Booking#booking.updated_at) - }. \ No newline at end of file + handler_utils:booking_to_json(Booking). \ No newline at end of file diff --git a/src/handlers/handler_bookings.erl b/src/handlers/handler_bookings.erl index 4eaebef..2680e57 100755 --- a/src/handlers/handler_bookings.erl +++ b/src/handlers/handler_bookings.erl @@ -167,17 +167,4 @@ list_bookings(Req) -> %% @private Формирует JSON-представление записи #booking{}. -spec booking_to_json(#booking{}) -> map(). booking_to_json(Booking) -> - #{ - id => Booking#booking.id, - event_id => Booking#booking.event_id, - user_id => Booking#booking.user_id, - status => Booking#booking.status, - notes => Booking#booking.notes, - reminder_sent => Booking#booking.reminder_sent, - confirmed_at => case Booking#booking.confirmed_at of - undefined -> null; - Dt -> handler_utils:datetime_to_iso8601(Dt) - end, - created_at => handler_utils:datetime_to_iso8601(Booking#booking.created_at), - updated_at => handler_utils:datetime_to_iso8601(Booking#booking.updated_at) - }. \ No newline at end of file + handler_utils:booking_to_json(Booking). \ No newline at end of file diff --git a/src/handlers/handler_user_bookings.erl b/src/handlers/handler_user_bookings.erl old mode 100644 new mode 100755 index 2a179f1..a7c1b99 --- a/src/handlers/handler_user_bookings.erl +++ b/src/handlers/handler_user_bookings.erl @@ -89,17 +89,4 @@ list_user_bookings(Req) -> %% @private Формирует JSON-представление записи #booking{}. -spec booking_to_json(#booking{}) -> map(). booking_to_json(Booking) -> - #{ - id => Booking#booking.id, - event_id => Booking#booking.event_id, - user_id => Booking#booking.user_id, - status => Booking#booking.status, - notes => Booking#booking.notes, - reminder_sent => Booking#booking.reminder_sent, - confirmed_at => case Booking#booking.confirmed_at of - undefined -> null; - Dt -> handler_utils:datetime_to_iso8601(Dt) - end, - created_at => handler_utils:datetime_to_iso8601(Booking#booking.created_at), - updated_at => handler_utils:datetime_to_iso8601(Booking#booking.updated_at) - }. \ No newline at end of file + handler_utils:booking_to_json(Booking). \ No newline at end of file diff --git a/src/handlers/handler_utils.erl b/src/handlers/handler_utils.erl index bcde83f..3d9ba93 100755 --- a/src/handlers/handler_utils.erl +++ b/src/handlers/handler_utils.erl @@ -26,6 +26,7 @@ ticket_to_json/1, calendar_to_json/1, subscription_to_json/1, + booking_to_json/1, trails_for_crud/4, is_superadmin/1, pagination_headers/2, @@ -473,6 +474,44 @@ subscription_to_json(Subscription) -> updated_at => datetime_to_iso8601(Subscription#subscription.updated_at) }. +%% @doc Booking JSON with optional booker nickname/email for owner UI. +-spec booking_to_json(#booking{}) -> map(). +booking_to_json(Booking) -> + Base = #{ + id => Booking#booking.id, + event_id => Booking#booking.event_id, + user_id => Booking#booking.user_id, + status => Booking#booking.status, + notes => Booking#booking.notes, + reminder_sent => Booking#booking.reminder_sent, + confirmed_at => case Booking#booking.confirmed_at of + undefined -> null; + Dt -> datetime_to_iso8601(Dt) + end, + created_at => datetime_to_iso8601(Booking#booking.created_at), + updated_at => datetime_to_iso8601(Booking#booking.updated_at) + }, + maps:merge(Base, booking_user_fields(Booking#booking.user_id)). + +%% @private +booking_user_fields(UserId) when is_binary(UserId) -> + case core_user:get_by_id(UserId) of + {ok, #user{nickname = Nick, email = Email}} -> + #{ + user_nickname => empty_to_null(Nick), + user_email => empty_to_null(Email) + }; + _ -> + #{user_nickname => null, user_email => null} + end; +booking_user_fields(_) -> + #{user_nickname => null, user_email => null}. + +empty_to_null(undefined) -> null; +empty_to_null(null) -> null; +empty_to_null(<<>>) -> null; +empty_to_null(V) -> V. + %%%=================================================================== %%% Вспомогательные внутренние функции %%%===================================================================