Skip to content

Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️

Chore/move utc handling to be (#1082)

Dave Earleyfed042d0560c
diff --git a/backend/app/Services/Application/Handlers/Message/SendMessageHandler.php b/backend/app/Services/Application/Handlers/Message/SendMessageHandler.php
index 01a7e526a..a6621eda6 100644
--- a/backend/app/Services/Application/Handlers/Message/SendMessageHandler.php
+++ b/backend/app/Services/Application/Handlers/Message/SendMessageHandler.php
@@ -8,10 +8,12 @@ use HiEvents\DomainObjects\MessageDomainObject;
use HiEvents\DomainObjects\Status\MessageStatus;
use HiEvents\Exceptions\AccountNotVerifiedException;
use HiEvents\Exceptions\MessagingTierLimitExceededException;
+use HiEvents\Helper\DateHelper;
use HiEvents\Jobs\Event\SendMessagesJob;
use HiEvents\Jobs\Message\MessagePendingReviewJob;
use HiEvents\Repository\Interfaces\AccountRepositoryInterface;
use HiEvents\Repository\Interfaces\AttendeeRepositoryInterface;
+use HiEvents\Repository\Interfaces\EventRepositoryInterface;
use HiEvents\Repository\Interfaces\MessageRepositoryInterface;
use HiEvents\Repository\Interfaces\OrderRepositoryInterface;
use HiEvents\Repository\Interfaces\ProductRepositoryInterface;
@@ -29,6 +31,7 @@ class SendMessageHandler
private readonly ProductRepositoryInterface $productRepository,
private readonly MessageRepositoryInterface $messageRepository,
private readonly AccountRepositoryInterface $accountRepository,
+ private readonly EventRepositoryInterface $eventRepository,
private readonly HtmlPurifierService $purifier,
private readonly Repository $config,
private readonly MessagingEligibilityService $eligibilityService,
@@ -75,6 +78,12 @@ class SendMessageHandler
$isScheduled = $messageData->scheduled_at !== null && !$messageData->is_test;
+ $event = $this->eventRepository->findById($messageData->event_id);
+
+ $scheduledAtUtc = $messageData->scheduled_at
+ ? DateHelper::convertToUTC($messageData->scheduled_at, $event->getTimezone())
+ : null;
+
if ($eligibilityFailure !== null) {
$status = MessageStatus::PENDING_REVIEW;
} elseif ($isScheduled) {
@@ -94,7 +103,7 @@ class SendMessageHandler
'sent_at' => $isScheduled ? null : Carbon::now()->toDateTimeString(),
'sent_by_user_id' => $messageData->sent_by_user_id,
'status' => $status->name,
- 'scheduled_at' => $messageData->scheduled_at,
+ 'scheduled_at' => $scheduledAtUtc,
'eligibility_failures' => $eligibilityFailure?->getFailureValues(),
'send_data' => [
'is_test' => $messageData->is_test,
diff --git a/backend/tests/Unit/Services/Application/Handlers/Message/SendMessageHandlerScheduledTest.php b/backend/tests/Unit/Services/Application/Handlers/Message/SendMessageHandlerScheduledTest.php
index e5fe760a5..0b4c2010d 100644
--- a/backend/tests/Unit/Services/Application/Handlers/Message/SendMessageHandlerScheduledTest.php
+++ b/backend/tests/Unit/Services/Application/Handlers/Message/SendMessageHandlerScheduledTest.php
@@ -6,6 +6,7 @@ use Carbon\Carbon;
use HiEvents\DomainObjects\AccountDomainObject;
use HiEvents\DomainObjects\AttendeeDomainObject;
use HiEvents\DomainObjects\Enums\MessageTypeEnum;
+use HiEvents\DomainObjects\EventDomainObject;
use HiEvents\DomainObjects\MessageDomainObject;
use HiEvents\DomainObjects\OrderDomainObject;
use HiEvents\DomainObjects\ProductDomainObject;
@@ -13,6 +14,7 @@ use HiEvents\DomainObjects\Status\MessageStatus;
use HiEvents\Jobs\Event\SendMessagesJob;
use HiEvents\Repository\Interfaces\AccountRepositoryInterface;
use HiEvents\Repository\Interfaces\AttendeeRepositoryInterface;
+use HiEvents\Repository\Interfaces\EventRepositoryInterface;
use HiEvents\Repository\Interfaces\MessageRepositoryInterface;
use HiEvents\Repository\Interfaces\OrderRepositoryInterface;
use HiEvents\Repository\Interfaces\ProductRepositoryInterface;
@@ -32,6 +34,7 @@ class SendMessageHandlerScheduledTest extends TestCase
private ProductRepositoryInterface $productRepository;
private MessageRepositoryInterface $messageRepository;
private AccountRepositoryInterface $accountRepository;
+ private EventRepositoryInterface $eventRepository;
private HtmlPurifierService $purifier;
private Repository $config;
private MessagingEligibilityService $eligibilityService;
@@ -46,6 +49,7 @@ class SendMessageHandlerScheduledTest extends TestCase
$this->productRepository = m::mock(ProductRepositoryInterface::class);
$this->messageRepository = m::mock(MessageRepositoryInterface::class);
$this->accountRepository = m::mock(AccountRepositoryInterface::class);
+ $this->eventRepository = m::mock(EventRepositoryInterface::class);
$this->purifier = m::mock(HtmlPurifierService::class);
$this->config = m::mock(Repository::class);
$this->eligibilityService = m::mock(MessagingEligibilityService::class);
@@ -56,6 +60,7 @@ class SendMessageHandlerScheduledTest extends TestCase
$this->productRepository,
$this->messageRepository,
$this->accountRepository,
+ $this->eventRepository,
$this->purifier,
$this->config,
$this->eligibilityService
@@ -68,7 +73,11 @@ class SendMessageHandlerScheduledTest extends TestCase
$account->shouldReceive('getAccountVerifiedAt')->andReturn(Carbon::now());
$account->shouldReceive('getIsManuallyVerified')->andReturn(true);
+ $event = m::mock(EventDomainObject::class);
+ $event->shouldReceive('getTimezone')->andReturn('America/New_York');
+
$this->accountRepository->shouldReceive('findById')->with(1)->andReturn($account);
+ $this->eventRepository->shouldReceive('findById')->with(101)->andReturn($event);
$this->config->shouldReceive('get')->with('app.saas_mode_enabled')->andReturn(false);
$this->eligibilityService->shouldReceive('checkTierLimits')->andReturn(null);
@@ -129,7 +138,7 @@ class SendMessageHandlerScheduledTest extends TestCase
order_statuses: [],
attendee_ids: [10],
product_ids: [20],
- scheduled_at: Carbon::now()->addHour()->toIso8601String(),
+ scheduled_at: Carbon::now('America/New_York')->addHour()->format('Y-m-d\TH:i'),
);
$result = $this->handler->handle($dto);
@@ -216,7 +225,7 @@ class SendMessageHandlerScheduledTest extends TestCase
order_statuses: [],
attendee_ids: [10],
product_ids: [20],
- scheduled_at: Carbon::now()->addHour()->toIso8601String(),
+ scheduled_at: Carbon::now('America/New_York')->addHour()->format('Y-m-d\TH:i'),
);
$result = $this->handler->handle($dto);
diff --git a/backend/tests/Unit/Services/Application/Handlers/Message/SendMessageHandlerTest.php b/backend/tests/Unit/Services/Application/Handlers/Message/SendMessageHandlerTest.php
index 8376eb838..4f9cccded 100644
--- a/backend/tests/Unit/Services/Application/Handlers/Message/SendMessageHandlerTest.php
+++ b/backend/tests/Unit/Services/Application/Handlers/Message/SendMessageHandlerTest.php
@@ -6,6 +6,7 @@ use Carbon\Carbon;
use HiEvents\DomainObjects\AccountDomainObject;
use HiEvents\DomainObjects\AttendeeDomainObject;
use HiEvents\DomainObjects\Enums\MessageTypeEnum;
+use HiEvents\DomainObjects\EventDomainObject;
use HiEvents\DomainObjects\MessageDomainObject;
use HiEvents\DomainObjects\OrderDomainObject;
use HiEvents\DomainObjects\ProductDomainObject;
@@ -13,6 +14,7 @@ use HiEvents\Exceptions\AccountNotVerifiedException;
use HiEvents\Jobs\Event\SendMessagesJob;
use HiEvents\Repository\Interfaces\AccountRepositoryInterface;
use HiEvents\Repository\Interfaces\AttendeeRepositoryInterface;
+use HiEvents\Repository\Interfaces\EventRepositoryInterface;
use HiEvents\Repository\Interfaces\MessageRepositoryInterface;
use HiEvents\Repository\Interfaces\OrderRepositoryInterface;
use HiEvents\Repository\Interfaces\ProductRepositoryInterface;
@@ -35,6 +37,7 @@ class SendMessageHandlerTest extends TestCase
private HtmlPurifierService $purifier;
private Repository $config;
private MessagingEligibilityService $eligibilityService;
+ private EventRepositoryInterface $eventRepository;
private SendMessageHandler $handler;
@@ -50,16 +53,18 @@ class SendMessageHandlerTest extends TestCase
$this->purifier = m::mock(HtmlPurifierService::class);
$this->config = m::mock(Repository::class);
$this->eligibilityService = m::mock(MessagingEligibilityService::class);
+ $this->eventRepository = m::mock(EventRepositoryInterface::class);
$this->handler = new SendMessageHandler(
- $this->orderRepository,
- $this->attendeeRepository,
- $this->productRepository,
- $this->messageRepository,
- $this->accountRepository,
- $this->purifier,
- $this->config,
- $this->eligibilityService
+ orderRepository: $this->orderRepository,
+ attendeeRepository: $this->attendeeRepository,
+ productRepository: $this->productRepository,
+ messageRepository: $this->messageRepository,
+ accountRepository: $this->accountRepository,
+ eventRepository: $this->eventRepository,
+ purifier: $this->purifier,
+ config: $this->config,
+ eligibilityService: $this->eligibilityService
);
}
@@ -74,8 +79,8 @@ class SendMessageHandlerTest extends TestCase
is_test: false,
send_copy_to_current_user: false,
sent_by_user_id: 1,
- order_statuses: [],
order_id: null,
+ order_statuses: [],
attendee_ids: [],
product_ids: []
);
@@ -101,8 +106,8 @@ class SendMessageHandlerTest extends TestCase
is_test: false,
send_copy_to_current_user: false,
sent_by_user_id: 1,
- order_statuses: [],
order_id: null,
+ order_statuses: [],
attendee_ids: [],
product_ids: []
);
@@ -131,12 +136,16 @@ class SendMessageHandlerTest extends TestCase
is_test: false,
send_copy_to_current_user: false,
sent_by_user_id: 99,
- order_statuses: [],
order_id: 5,
+ order_statuses: [],
attendee_ids: [10],
product_ids: [20],
);
+ $event = m::mock(EventDomainObject::class);
+ $event->shouldReceive('getTimezone')->andReturn('UTC');
+ $this->eventRepository->shouldReceive('findById')->with(101)->andReturn($event);
+
$account = m::mock(AccountDomainObject::class);
$account->shouldReceive('getAccountVerifiedAt')->andReturn(Carbon::now());
$account->shouldReceive('getIsManuallyVerified')->andReturn(true);
diff --git a/frontend/src/components/modals/SendMessageModal/index.tsx b/frontend/src/components/modals/SendMessageModal/index.tsx
index 33153085d..f33cc8f5f 100644
--- a/frontend/src/components/modals/SendMessageModal/index.tsx
+++ b/frontend/src/components/modals/SendMessageModal/index.tsx
@@ -37,7 +37,7 @@ import {useEffect, useMemo, useState} from "react";
import {useGetAccount} from "../../../queries/useGetAccount.ts";
import {StripeConnectButton} from "../../common/StripeConnectButton";
import {getConfig} from "../../../utilites/config";
-import {formatDate, utcToTz} from "../../../utilites/dates.ts";
+import {utcToTz} from "../../../utilites/dates.ts";
import dayjs from "dayjs";
import classes from "./SendMessageModal.module.scss";
@@ -106,22 +106,22 @@ const getSchedulePresets = (event: Event) => {
const startDate = dayjs.utc(event.start_date);
const endDate = event.end_date ? dayjs.utc(event.end_date) : null;
- const presets: { value: string; label: string; date: dayjs.Dayjs }[] = [
- {value: '1_week_before', label: t`1 week before event`, date: startDate.subtract(1, 'week')},
- {value: '1_day_before', label: t`1 day before event`, date: startDate.subtract(1, 'day')},
- {value: '1_hour_before', label: t`1 hour before event`, date: startDate.subtract(1, 'hour')},
- {value: '1_day_after_start', label: t`1 day after start date`, date: startDate.add(1, 'day')},
+ const presets: { value: string; label: string; utcDate: dayjs.Dayjs }[] = [
+ {value: '1_week_before', label: t`1 week before event`, utcDate: startDate.subtract(1, 'week')},
+ {value: '1_day_before', label: t`1 day before event`, utcDate: startDate.subtract(1, 'day')},
+ {value: '1_hour_before', label: t`1 hour before event`, utcDate: startDate.subtract(1, 'hour')},
+ {value: '1_day_after_start', label: t`1 day after start date`, utcDate: startDate.add(1, 'day')},
];
if (endDate) {
presets.push({
value: '1_day_after_end',
label: t`1 day after end date`,
- date: endDate.add(1, 'day'),
+ utcDate: endDate.add(1, 'day'),
});
}
- return presets.filter(p => p.date.isAfter(now));
+ return presets.filter(p => p.utcDate.isAfter(now));
};
export const SendMessageModal = (props: EventMessageModalProps) => {
@@ -142,9 +142,9 @@ export const SendMessageModal = (props: EventMessageModalProps) => {
const presets = useMemo(() => event ? getSchedulePresets(event) : [], [event]);
- const resolvedPresetDate = useMemo(() => {
+ const resolvedPreset = useMemo(() => {
if (!selectedPreset || selectedPreset === CUSTOM_PRESET) return null;
- return presets.find(p => p.value === selectedPreset)?.date ?? null;
+ return presets.find(p => p.value === selectedPreset) ?? null;
}, [selectedPreset, presets]);
const sendMessageMutation = useSendEventMessage();
@@ -170,7 +170,7 @@ export const SendMessageModal = (props: EventMessageModalProps) => {
if (!isScheduled) return null;
if (selectedPreset && selectedPreset !== CUSTOM_PRESET) return null;
if (!value) return t`The scheduled time is required`;
- if (event && dayjs.tz(value, event.timezone).isBefore(dayjs.utc())) return t`The scheduled time must be in the future`;
+ if (event && dayjs.tz(value, event.timezone).isBefore(dayjs())) return t`The scheduled time must be in the future`;
return null;
},
}
@@ -180,10 +180,8 @@ export const SendMessageModal = (props: EventMessageModalProps) => {
setTierLimitError(null);
const submitData = {...values};
if (isScheduled) {
- if (selectedPreset && selectedPreset !== CUSTOM_PRESET && resolvedPresetDate) {
- submitData.scheduled_at = resolvedPresetDate.toISOString();
- } else if (submitData.scheduled_at && event) {
- submitData.scheduled_at = dayjs.tz(submitData.scheduled_at, event.timezone).utc().toISOString();
+ if (selectedPreset && selectedPreset !== CUSTOM_PRESET && resolvedPreset && event) {
+ submitData.scheduled_at = resolvedPreset.utcDate.tz(event.timezone).format('YYYY-MM-DDTHH:mm');
}
} else {
delete submitData.scheduled_at;
@@ -416,17 +414,17 @@ export const SendMessageModal = (props: EventMessageModalProps) => {
{...form.getInputProps('scheduled_at')}
/>
)}
- {resolvedPresetDate && event && (
+ {resolvedPreset && event && (
<div className={classes.scheduledConfirmation}>
<div className={classes.scheduledConfirmationIcon}>
<IconClock size={20}/>
</div>
<div className={classes.scheduledConfirmationText}>
<span className={classes.scheduledConfirmationDate}>
- {formatDate(resolvedPresetDate.toISOString(), 'dddd, MMMM D, YYYY', event.timezone)}
+ {resolvedPreset.utcDate.tz(event.timezone).format('dddd, MMMM D, YYYY')}
</span>
<span className={classes.scheduledConfirmationTime}>
- {formatDate(resolvedPresetDate.toISOString(), 'h:mm A', event.timezone)}
+ {resolvedPreset.utcDate.tz(event.timezone).format('h:mm A')}
{' '}<span className={classes.scheduledConfirmationTz}>{event.timezone}</span>
</span>
</div>