diff --git a/app/Notifications/MissingOrderReminderNotification.php b/app/Notifications/MissingOrderReminderNotification.php index 71e5e929..02381c5b 100644 --- a/app/Notifications/MissingOrderReminderNotification.php +++ b/app/Notifications/MissingOrderReminderNotification.php @@ -71,7 +71,9 @@ class MissingOrderReminderNotification extends Notification implements ShouldQue $icsGenerator = app(IcsGeneratorService::class); $schedulingService = app(PickupSchedulingService::class); - $cutoffDate = $schedulingService->getPeriodForPickupDate($this->pickupDay)->getStartDate(); + $cutoffDate = $schedulingService->getPeriodForPickupDate($this->pickupDay) + ->getStartDate() + ->setTimezone('Europe/Paris'); $closureTime = $cutoffDate->format('G') . 'h'; $reminderDay = $cutoffDate->translatedFormat('l'); diff --git a/domain/Services/PickupSchedulingService.php b/domain/Services/PickupSchedulingService.php index 7367c59d..4fe2a588 100644 --- a/domain/Services/PickupSchedulingService.php +++ b/domain/Services/PickupSchedulingService.php @@ -22,19 +22,26 @@ class PickupSchedulingService ) { } - private const PICKUP_DAY = WeekDay::Friday; + protected const WeekDay PICKUP_DAY = WeekDay::Friday; - private const CUTOFF_WEEK_OFFSET = 0; + protected const int CUTOFF_WEEK_OFFSET = 0; - private const CUTOFF_DAY = WeekDay::Monday; + protected const WeekDay CUTOFF_DAY = WeekDay::Monday; - private const CUTOFF_HOUR = 14; + protected const int CUTOFF_HOUR = 14; + + protected const string CUTOFF_TIME_ZONE = 'Europe/Paris'; public function getPickupDay(): WeekDay { return self::PICKUP_DAY; } + public function getCutoffTimezone(): string + { + return self::CUTOFF_TIME_ZONE; + } + public function currentCutoff(): CarbonInterface { $nextPickup = $this->nextPickup(); @@ -44,16 +51,19 @@ class PickupSchedulingService public function nextPickup(): CarbonInterface { - $now = $this->getNow(); + $systemTz = $this->getNow()->getTimezone(); + $now = $this->getNow()->setTimezone(self::CUTOFF_TIME_ZONE); $startOfThisDay = $now->copy()->startOfDay(); if ($startOfThisDay->isDayOfWeek(self::PICKUP_DAY->value) && $now->hour < self::CUTOFF_HOUR) { - return $startOfThisDay->addHours(self::CUTOFF_HOUR); + return $startOfThisDay->addHours(self::CUTOFF_HOUR) + ->setTimezone($systemTz); } return $startOfThisDay ->next(self::PICKUP_DAY->value) - ->addHours(self::CUTOFF_HOUR); + ->addHours(self::CUTOFF_HOUR) + ->setTimezone($systemTz); } public function getPeriodFromPickUpDay(): CarbonPeriod @@ -105,7 +115,7 @@ class PickupSchedulingService public function getTimeSlotsFor(Shop $shop, \DateTimeInterface $until): array { $todayDate = $this->getNow()->startOfDay(); - $untilDayDate = CarbonImmutable::createFromInterface($until)->startOfDay(); + $untilDayDate = CarbonImmutable::createFromInterface($until)->setTimezone(self::CUTOFF_TIME_ZONE)->startOfDay(); $coveredPeriod = CarbonPeriod::create($todayDate, $untilDayDate); if ($untilDayDate->lessThanOrEqualTo($todayDate)) { @@ -176,10 +186,13 @@ class PickupSchedulingService protected function getDefaultDeadline(CarbonInterface $pickupDate): CarbonImmutable { + $systemTz = $this->getNow()->getTimezone(); $deadline = CarbonImmutable::createFromInterface($pickupDate) + ->setTimezone(self::CUTOFF_TIME_ZONE) ->startOfWeek(CarbonInterface::MONDAY) ->addDays(self::CUTOFF_DAY->value - 1) ->addHours(self::CUTOFF_HOUR); + $deadline = $deadline->setTimezone($systemTz); return $this->getCutoffWeekOffset() !== 0 ? $deadline->addWeeks($this->getCutoffWeekOffset()) diff --git a/tests/Unit/Domain/Services/PickupSchedulingServiceTest.php b/tests/Unit/Domain/Services/PickupSchedulingServiceTest.php index da9b5199..83d014f2 100644 --- a/tests/Unit/Domain/Services/PickupSchedulingServiceTest.php +++ b/tests/Unit/Domain/Services/PickupSchedulingServiceTest.php @@ -342,9 +342,12 @@ describe('Pickup Scheduling Service', function (): void { parent::__construct($repo); } + #[\Override] protected function getNow(): CarbonImmutable { - return $this->now; + return $this->now->timezoneName === 'UTC' + ? $this->now->shiftTimezone(self::CUTOFF_TIME_ZONE) + : $this->now->setTimezone(self::CUTOFF_TIME_ZONE); } }; @@ -371,6 +374,29 @@ describe('Pickup Scheduling Service', function (): void { ->and($editable[1]->passedDeadline)->toBeFalse(); }); + it('enforces cutoff at 14h Paris time in winter (13h UTC)', function (): void { + $cutoffTimezone = new PickupSchedulingService(\Mockery::mock(PickupExceptionRepository::class)); + // GIVEN: Monday 2 Nov 2026 14:00 Paris (13:00 UTC) vs 14:01 Paris (13:01 UTC) + $nowAtCutoff = CarbonImmutable::parse('2026-11-02 14:00:00', $cutoffTimezone->getCutoffTimezone()); + $nowPastCutoff = CarbonImmutable::parse('2026-11-02 14:01:00', $cutoffTimezone->getCutoffTimezone()); + $shop = createTestShop(1); + $until = $nowAtCutoff->addWeeks(2); + + $serviceAtCutoff = createServiceWithExceptions($nowAtCutoff); + $servicePastCutoff = createServiceWithExceptions($nowPastCutoff); + + // WHEN + $slotsAtCutoff = $serviceAtCutoff->getTimeSlotsFor($shop, $until); + $slotsPastCutoff = $servicePastCutoff->getTimeSlotsFor($shop, $until); + + // THEN + $targetFriday = '2026-11-06'; + $hasFridayAtCutoff = collect($slotsAtCutoff)->contains(fn (PickupTimeSlot $slot) => Date::createFromInterface($slot->getAt())->isSameDay($targetFriday)); + $hasFridayPastCutoff = collect($slotsPastCutoff)->contains(fn (PickupTimeSlot $slot) => Date::createFromInterface($slot->getAt())->isSameDay($targetFriday)); + + expect($hasFridayAtCutoff)->toBeTrue() + ->and($hasFridayPastCutoff)->toBeFalse(); + }); }); /** @@ -394,18 +420,20 @@ function createServiceWithExceptions(CarbonImmutable $now, array $exceptions = [ #[\Override] protected function getNow(): CarbonImmutable { - return $this->now; + return $this->now->timezoneName === 'UTC' + ? $this->now->shiftTimezone(self::CUTOFF_TIME_ZONE) + : $this->now->setTimezone(self::CUTOFF_TIME_ZONE); } }; } function createExceptionPivot(string $date, ?string $replacementDate = null, ?string $orderDeadlineAt = null): ShopPickupOverride { - static $idCounter = 1; - - $exception = new PickupExceptionOnDate( + static $idCounter = 1; + $pickupSchedulingService = new PickupSchedulingService(\Mockery::mock(PickupExceptionRepository::class)); + $exception = new PickupExceptionOnDate( id: new PickupExceptionId(Uuid::uuid7()), - date: CarbonImmutable::parse($date), + date: CarbonImmutable::parse($date, $pickupSchedulingService->getCutoffTimezone()), label: 'Test Exception', repeatable: false, repeatFrequency: RepeatFrequency::YEARLY, @@ -414,7 +442,7 @@ function createExceptionPivot(string $date, ?string $replacementDate = null, ?st return new ShopPickupOverride( exception: $exception, - replacementDate: $replacementDate ? CarbonImmutable::parse($replacementDate) : null, - orderDeadlineAt: $orderDeadlineAt ? CarbonImmutable::parse($orderDeadlineAt) : null + replacementDate: $replacementDate ? CarbonImmutable::parse($replacementDate, $pickupSchedulingService->getCutoffTimezone()) : null, + orderDeadlineAt: $orderDeadlineAt ? CarbonImmutable::parse($orderDeadlineAt, $pickupSchedulingService->getCutoffTimezone()) : null ); }