Completes task 03. The resource calendar page edits weekly shifts, records leave and
maintenance, and previews two weeks of availability with a Persian reason for every
empty day — showing the raw server key ("outside_branch_hours") to a user would have
been a meaningless message. The preview is labelled raw on the page itself, because
booked appointments are not subtracted yet and mistaking it for bookable time leads
to overbooking.
The interval algebra moved to src/Shared/Time/TimeInterval.php with twelve unit
tests: tasks 05 and 06 need the same union/intersect/subtract, and a second
implementation is how two subtly different definitions of "overlap" get born. The
half-open [start, end) contract is what makes a shift ending at 13:00 and one
starting at 13:00 not overlap.
AvailabilityQueryCountTest locks the query count flat: one day and ninety days cost
exactly the same number of queries. Without it the first refactor can put a query
inside the day loop and a 90-day response quietly becomes hundreds of queries —
something only production would reveal.
app:resource:calendar:backfill derives shifts from existing WeeklySchedule sessions,
so the resources created in task 02 are not left with empty calendars. It skips any
resource a user has already configured, which is also what makes it idempotent. The
weekly schedule itself is untouched: this is a copy, not a migration.
Also added --replace to the holiday import. upsert keys on the date, so a row written
with a *wrong* date can never correct itself — re-running just creates the right row
beside the wrong one. That is exactly what happened after fixing the Jalali
conversion bug, and it was caught while capturing real responses for the docs.
Deferred with reasons recorded in the checklist: seasonal shift validity (two
nullable columns can be added later without backfill, so "needed from day one" does
not hold), and a Jalali date picker in the exception form.
1154 tests / 3229 assertions. phpstan at its 14-error baseline, none in touched
files. tsc clean, vitest 622 tests.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
153 lines
5.9 KiB
PHP
153 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace App\Resource\Service;
|
|
|
|
use App\Representation\Service\JalaliDateService;
|
|
use App\Resource\Entity\NationalHoliday;
|
|
use App\Resource\Entity\TenantHolidayOverride;
|
|
use App\Resource\Repository\NationalHolidayRepository;
|
|
use App\Resource\Repository\TenantHolidayOverrideRepository;
|
|
use App\Shared\Constant\ErrorCodes;
|
|
use App\Shared\Exception\AppException;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
/**
|
|
* تعطیلات رسمی کشور و استثناهای هر محیط رویشان.
|
|
*
|
|
* تبدیل شمسی از {@see JalaliDateService} میآید و اینجا دوباره پیاده نمیشود — آن
|
|
* سرویس از فاز نمایندگی موجود است و درست کار میکند. (نامِ فضایش
|
|
* `App\Representation` است که برای مصرف مشترک جای درستی نیست؛ جابهجا کردنش یک
|
|
* تغییر مکانیکی در کل کدبیس است و به این تسک ربطی ندارد.)
|
|
*/
|
|
final class HolidayService
|
|
{
|
|
/** تهران مرجع «روز» است: تعطیل رسمی کشوری است، نه محلیِ شعبه. */
|
|
public const NATIONAL_TIMEZONE = 'Asia/Tehran';
|
|
|
|
public function __construct(
|
|
private readonly NationalHolidayRepository $holidays,
|
|
private readonly TenantHolidayOverrideRepository $overrides,
|
|
private readonly JalaliDateService $jalali,
|
|
private readonly EntityManagerInterface $em,
|
|
) {}
|
|
|
|
/** نیمهشبِ یک تاریخ شمسی به وقت تهران. */
|
|
public function jalaliToMidnight(int $jy, int $jm, int $jd): int
|
|
{
|
|
[$gy, $gm, $gd] = $this->jalali->jalaliToGregorian($jy, $jm, $jd);
|
|
|
|
return (new \DateTimeImmutable(
|
|
sprintf('%04d-%02d-%02d 00:00:00', $gy, $gm, $gd),
|
|
new \DateTimeZone(self::NATIONAL_TIMEZONE),
|
|
))->getTimestamp();
|
|
}
|
|
|
|
/**
|
|
* یک تعطیل رسمی را ثبت یا بهروز میکند. idempotent: تکیهگاهش `date` است که
|
|
* یکتاست، پس import دوباره ردیف تکراری نمیسازد.
|
|
*
|
|
* خودش flush میکند و این عمدی است: سپردنِ flush به فراخوان یعنی اگر او
|
|
* EntityManager دیگری در دست داشته باشد، persist روی یکی و flush روی دیگری
|
|
* میافتد و **هیچ ردیفی نوشته نمیشود، بیهیچ خطایی**. همین در تستها اتفاق افتاد،
|
|
* چون هر درخواست HTTP کرنل را از نو میسازد و سرویس از کانتینر تازه میآید.
|
|
*/
|
|
public function upsertNational(int $jy, int $jm, int $jd, string $title): NationalHoliday
|
|
{
|
|
$date = $this->jalaliToMidnight($jy, $jm, $jd);
|
|
$existing = $this->holidays->findByDate($date);
|
|
|
|
if ($existing !== null) {
|
|
$existing->setTitle($title);
|
|
$this->em->flush();
|
|
|
|
return $existing;
|
|
}
|
|
|
|
$holiday = new NationalHoliday(
|
|
$date,
|
|
sprintf('%04d-%02d-%02d', $jy, $jm, $jd),
|
|
$jy,
|
|
$title,
|
|
);
|
|
|
|
$this->em->persist($holiday);
|
|
$this->em->flush();
|
|
|
|
return $holiday;
|
|
}
|
|
|
|
/**
|
|
* همهٔ تعطیلات یک سال را پاک میکند — تنها راه تصحیحِ ردیفی که با تاریخ غلط
|
|
* نوشته شده، چون upsert روی `date` کلید میخورد و ردیف غلط را نمیشناسد.
|
|
*/
|
|
public function purgeYear(int $jalaliYear): int
|
|
{
|
|
return (int) $this->em->createQuery(
|
|
'DELETE FROM App\Resource\Entity\NationalHoliday h WHERE h.jalaliYear = :year'
|
|
)->setParameter('year', $jalaliYear)->execute();
|
|
}
|
|
|
|
/** @return NationalHoliday[] */
|
|
public function forYear(int $jalaliYear): array
|
|
{
|
|
return $this->holidays->findForYear($jalaliYear);
|
|
}
|
|
|
|
/**
|
|
* استثنای محیط. روی همان تاریخ دوباره فرستادن، همان ردیف را عوض میکند — وگرنه
|
|
* کلید یکتا با خطای خام دیتابیس میشکست.
|
|
*/
|
|
public function setOverride(string $entityType, int $entityId, int $date, bool $isWorking, ?string $note): TenantHolidayOverride
|
|
{
|
|
$midnight = $this->midnightTehran($date);
|
|
$existing = $this->overrides->findForDate($entityType, $entityId, $midnight);
|
|
|
|
if ($existing !== null) {
|
|
$existing->setWorking($isWorking)->setNote($note);
|
|
$this->em->flush();
|
|
|
|
return $existing;
|
|
}
|
|
|
|
$override = new TenantHolidayOverride($entityType, $entityId, $midnight, $isWorking);
|
|
$override->setNote($note);
|
|
|
|
$this->em->persist($override);
|
|
$this->em->flush();
|
|
|
|
return $override;
|
|
}
|
|
|
|
public function deleteOverride(TenantHolidayOverride $override): void
|
|
{
|
|
$this->em->remove($override);
|
|
$this->em->flush();
|
|
}
|
|
|
|
/** @return TenantHolidayOverride[] */
|
|
public function overridesForYear(string $entityType, int $entityId, int $jalaliYear): array
|
|
{
|
|
$from = $this->jalaliToMidnight($jalaliYear, 1, 1);
|
|
$to = $this->jalaliToMidnight($jalaliYear + 1, 1, 1);
|
|
|
|
return array_values($this->overrides->mapForRange($entityType, $entityId, $from, $to));
|
|
}
|
|
|
|
public function assertJalaliYear(mixed $value): int
|
|
{
|
|
if (!is_numeric($value) || (int) $value < 1300 || (int) $value > 1500) {
|
|
throw new AppException(ErrorCodes::ERR_VALIDATION_001, 'سال شمسی نامعتبر است', 422, 'year');
|
|
}
|
|
|
|
return (int) $value;
|
|
}
|
|
|
|
private function midnightTehran(int $timestamp): int
|
|
{
|
|
return (new \DateTimeImmutable('@' . $timestamp))
|
|
->setTimezone(new \DateTimeZone(self::NATIONAL_TIMEZONE))
|
|
->setTime(0, 0)
|
|
->getTimestamp();
|
|
}
|
|
}
|