Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ZMSKVR-129 ZMSKVR-140): fix invalid appointment timestamps array and too many appointments with email at location #895

Merged
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpmd.rules.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

<rule ref="rulesets/codesize.xml/TooManyPublicMethods">
<properties>
<property name="maxmethods" value="20" />
<property name="maxmethods" value="25" />
<property name="ignorepattern" value="(^(set|get))i" />
</properties>
</rule>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class AvailableAppointments extends Entity implements JsonSerializable
{
public static $schema = 'citizenapi/availableAppointments.json';
/** @var array */
/** @var array */
public array $appointmentTimestamps = [];
public function __construct(array $appointmentTimestamps = [])
{
Expand Down
52 changes: 0 additions & 52 deletions zmscitizenapi/src/Zmscitizenapi/Models/ProcessFreeSlots.php

This file was deleted.

11 changes: 10 additions & 1 deletion zmscitizenapi/src/Zmscitizenapi/Models/ThinnedScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class ThinnedScope extends Entity implements JsonSerializable
/** @var string|null */
public ?string $emailFrom;
/** @var bool|null */
public ?bool $emailRequired;
/** @var bool|null */
public ?bool $telephoneActivated;
/** @var bool|null */
public ?bool $telephoneRequired;
Expand All @@ -34,12 +36,13 @@ class ThinnedScope extends Entity implements JsonSerializable
public ?bool $captchaActivatedRequired;
/** @var string|null */
public ?string $displayInfo;
public function __construct(int $id = 0, ?ThinnedProvider $provider = null, ?string $shortName = null, ?string $emailFrom = null, ?bool $telephoneActivated = null, ?bool $telephoneRequired = null, ?bool $customTextfieldActivated = null, ?bool $customTextfieldRequired = null, ?string $customTextfieldLabel = null, ?bool $captchaActivatedRequired = null, ?string $displayInfo = null)
public function __construct(int $id = 0, ?ThinnedProvider $provider = null, ?string $shortName = null, ?string $emailFrom = null, ?bool $emailRequired = null, ?bool $telephoneActivated = null, ?bool $telephoneRequired = null, ?bool $customTextfieldActivated = null, ?bool $customTextfieldRequired = null, ?string $customTextfieldLabel = null, ?bool $captchaActivatedRequired = null, ?string $displayInfo = null)
{
$this->id = $id;
$this->provider = $provider;
$this->shortName = $shortName;
$this->emailFrom = $emailFrom;
$this->emailRequired = $emailRequired;
$this->telephoneActivated = $telephoneActivated;
$this->telephoneRequired = $telephoneRequired;
$this->customTextfieldActivated = $customTextfieldActivated;
Expand Down Expand Up @@ -72,6 +75,11 @@ public function getEmailFrom(): ?string
return $this->emailFrom;
}

public function getEmailRequired(): ?bool
{
return $this->emailRequired;
}

public function getTelephoneActivated(): ?bool
{
return $this->telephoneActivated;
Expand Down Expand Up @@ -114,6 +122,7 @@ public function toArray(): array
'provider' => $this->provider,
'shortName' => $this->shortName,
'emailFrom' => $this->emailFrom,
'emailRequired' => $this->emailRequired,
'telephoneActivated' => $this->telephoneActivated,
'telephoneRequired' => $this->telephoneRequired,
'customTextfieldActivated' => $this->customTextfieldActivated,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,52 @@ class AppointmentUpdateService
public function processUpdate(array $body): ThinnedProcess|array
{
$clientData = $this->extractClientData($body);

$errors = $this->validateClientData($clientData);
if (!empty($errors['errors'])) {
return $errors;
}

$reservedProcess = $this->getReservedProcess($clientData->processId, $clientData->authKey);
if (is_array($reservedProcess) && !empty($reservedProcess['errors'])) {
if (!($reservedProcess instanceof ThinnedProcess)) {
return $reservedProcess;
}

$updatedProcess = $this->updateProcessWithClientData($reservedProcess, $clientData);
return $this->saveProcessUpdate($updatedProcess);
}

private function validateClientData(object $data): array
{
$allErrors = [];

$basicErrors = ValidationService::validateAppointmentUpdateAuth($data->processId, $data->authKey);
if (!empty($basicErrors['errors'])) {
$allErrors = array_merge($allErrors, $basicErrors['errors']);
}

$reservedProcess = null;
if (is_int($data->processId) && is_string($data->authKey)) {
$reservedProcess = $this->getReservedProcess($data->processId, $data->authKey);
if (is_array($reservedProcess) && !empty($reservedProcess['errors'])) {
$allErrors = array_merge($allErrors, $reservedProcess['errors']);
}
}

$fieldErrors = ValidationService::validateAppointmentUpdateFields(
$data->familyName,
$data->email,
$data->telephone,
$data->customTextfield,
$reservedProcess instanceof ThinnedProcess ? $reservedProcess->scope : null
);
if (!empty($fieldErrors['errors'])) {
$allErrors = array_merge($allErrors, $fieldErrors['errors']);
}

return ['errors' => $allErrors];
}

private function extractClientData(array $body): object
{
return (object) [
Expand All @@ -44,11 +76,6 @@ private function extractClientData(array $body): object
];
}

private function validateClientData(object $data): array
{
return ValidationService::validateUpdateAppointmentInputs($data->processId, $data->authKey, $data->familyName, $data->email, $data->telephone, $data->customTextfield);
}

private function getReservedProcess(int $processId, string $authKey): ThinnedProcess|array
{
return ZmsApiFacadeService::getThinnedProcessById($processId, $authKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public static function mapOfficesWithScope(ProviderList $providerList, bool $sho
provider: isset($providerScope->provider) ? $providerScope->provider : null,
shortName: isset($providerScope->shortName) ? $providerScope->shortName : null,
emailFrom: isset($providerScope->emailFrom) ? $providerScope->emailFrom : null,
emailRequired: isset($providerScope->emailRequired) ? (bool) $providerScope->emailRequired : null,
telephoneActivated: isset($providerScope->telephoneActivated) ? (bool) $providerScope->telephoneActivated : null,
telephoneRequired: isset($providerScope->telephoneRequired) ? (bool) $providerScope->telephoneRequired : null,
customTextfieldActivated: isset($providerScope->customTextfieldActivated) ? (bool) $providerScope->customTextfieldActivated : null,
Expand Down Expand Up @@ -190,6 +191,7 @@ public static function scopeToThinnedScope(Scope $scope): ThinnedScope
provider: $thinnedProvider,
shortName: $scope->shortName ?? null,
emailFrom: (string) $scope->getEmailFrom() ?? null,
emailRequired: isset($scope->data['emailRequired']) ? (bool) $scope->data['emailRequired'] : null,
telephoneActivated: isset($scope->data['telephoneActivated']) ? (bool) $scope->data['telephoneActivated'] : null,
telephoneRequired: isset($scope->data['telephoneRequired']) ? (bool) $scope->data['telephoneRequired'] : null,
customTextfieldActivated: isset($scope->data['customTextfieldActivated']) ? (bool) $scope->data['customTextfieldActivated'] : null,
Expand Down Expand Up @@ -282,6 +284,7 @@ public static function thinnedProcessToProcess(ThinnedProcess $thinnedProcess):
$scope->preferences = [
'client' => [
'emailFrom' => $thinnedProcess->scope->getEmailFrom() ?? null,
'emailRequired' => $thinnedProcess->scope->getEmailRequired() ?? false,
'telephoneActivated' => $thinnedProcess->scope->getTelephoneActivated() ?? false,
'telephoneRequired' => $thinnedProcess->scope->getTelephoneRequired() ?? false,
'customTextfieldActivated' => $thinnedProcess->scope->getCustomTextfieldActivated() ?? false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace BO\Zmscitizenapi\Services\Core;

use BO\Zmscitizenapi\Localization\ErrorMessages;
use BO\Zmscitizenapi\Middleware\LanguageMiddleware;
use BO\Zmscitizenapi\Models\ThinnedScope;
use BO\Zmscitizenapi\Services\Core\ZmsApiFacadeService;
use BO\Zmsentities\Process;
use BO\Zmsentities\Collection\ProcessList;
Expand Down Expand Up @@ -187,34 +187,75 @@ public static function validatePostAppointmentReserve(?int $officeId, ?array $se
return ['errors' => $errors];
}

public static function validateUpdateAppointmentInputs(?int $processId, ?string $authKey, ?string $familyName, ?string $email, ?string $telephone, ?string $customTextfield): array
public static function validateAppointmentUpdateAuth(?int $processId, ?string $authKey): array
{
$errors = [];
if (!self::isValidProcessId($processId)) {
$errors[] = self::getError('invalidProcessId');
}

if (!self::isValidAuthKey($authKey)) {
$errors[] = self::getError('invalidAuthKey');
}
return ['errors' => $errors];
}

public static function validateAppointmentUpdateFields(
?string $familyName,
?string $email,
?string $telephone,
?string $customTextfield,
?ThinnedScope $scope
): array {
$errors = [];

self::validateFamilyNameField($familyName, $errors);
self::validateEmailField($email, $scope, $errors);
self::validateTelephoneField($telephone, $scope, $errors);
self::validateCustomTextField($customTextfield, $scope, $errors);

return ['errors' => $errors];
}

private static function validateFamilyNameField(?string $familyName, array &$errors): void
{
if (!self::isValidFamilyName($familyName)) {
$errors[] = self::getError('invalidFamilyName');
}
}

if (!self::isValidEmail($email)) {
private static function validateEmailField(?string $email, ?ThinnedScope $scope, array &$errors): void
{
if ($scope && $scope->emailRequired && !self::isValidEmail($email)) {
$errors[] = self::getError('invalidEmail');
}
}

private static function validateTelephoneField(?string $telephone, ?ThinnedScope $scope, array &$errors): void
{
if (!$scope || !$scope->telephoneActivated) {
return;
}

if (!self::isValidTelephone($telephone)) {
if (
($scope->telephoneRequired && !self::isValidTelephone($telephone)) ||
($telephone !== null && !self::isValidTelephone($telephone))
) {
$errors[] = self::getError('invalidTelephone');
}
}

if (!self::isValidCustomTextfield($customTextfield)) {
$errors[] = self::getError('invalidCustomTextfield');
private static function validateCustomTextField(?string $customTextfield, ?ThinnedScope $scope, array &$errors): void
{
if (!$scope || !$scope->customTextfieldActivated) {
return;
}

return ['errors' => $errors];
if (
($scope->customTextfieldRequired && !self::isValidCustomTextfield($customTextfield)) ||
($customTextfield !== null && !self::isValidCustomTextfield($customTextfield))
) {
$errors[] = self::getError('invalidCustomTextfield');
}
}

public static function validateGetScopeById(?int $scopeId): array
Expand Down
Loading
Loading