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

Filter validation refactoring #232

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -717,8 +717,17 @@ public function preValidatePromoCode($summit_id, $promo_code_val)

if (is_null($filter)) $filter = new Filter();

if (!$filter->hasFilter('ticket_type_id'))
return $this->error412("Type ID is required.");

if (!$filter->hasFilter('ticket_type_qty'))
return $this->error412("Quantity is required.");

if (!$filter->hasFilter('ticket_type_subtype'))
return $this->error412("Subtype is required.");

$filter->validate([
'ticket_type_id' => 'required|integer',
'ticket_type_id' => 'integer',
'ticket_type_qty' => [
'required',
'integer',
Expand All @@ -729,11 +738,16 @@ function ($attribute, $value, $fail) use ($filter) {
}
},
],
'ticket_type_subtype' => 'required|string|in:'.join(",", SummitTicketType::SubTypes),
'ticket_type_subtype' => 'string|in:'.join(",", SummitTicketType::SubTypes),
]);

$this->promo_code_service
->preValidatePromoCode($summit, $this->resource_server_context->getCurrentUser(), $promo_code_val, $filter);
$this->promo_code_service->preValidatePromoCode(
$summit,
$this->resource_server_context->getCurrentUser(),
$promo_code_val,
intval($filter->getUniqueFilter('ticket_type_id')->getValue()),
$filter->getUniqueFilter('ticket_type_subtype')->getValue(),
intval($filter->getUniqueFilter('ticket_type_qty')->getValue()));

return $this->ok();
});
Expand Down
7 changes: 5 additions & 2 deletions app/Services/Model/ISummitPromoCodeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,12 @@ public function removePromoCodeSpeaker(SummitRegistrationPromoCode $promo_code,
* @param Summit $summit
* @param Member $owner
* @param string $promo_code_value
* @param Filter $filter
* @param int $ticket_type_id
* @param string $ticket_type_subtype
* @param int $qty
* @return void
* @throws \Exception
*/
public function preValidatePromoCode(Summit $summit, Member $owner, string $promo_code_value, Filter $filter):void;
public function preValidatePromoCode(
Summit $summit, Member $owner, string $promo_code_value, int $ticket_type_id, string $ticket_type_subtype, int $qty):void;
}
15 changes: 6 additions & 9 deletions app/Services/Model/Imp/SummitPromoCodeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -756,32 +756,29 @@ public function removePromoCodeSpeaker(SummitRegistrationPromoCode $promo_code,
* @param Summit $summit
* @param Member $owner
* @param string $promo_code_value
* @param Filter $filter
* @param int $ticket_type_id
* @param string $ticket_type_subtype
* @param int $qty
* @return void
* @throws \Exception
*/
public function preValidatePromoCode(Summit $summit, Member $owner, string $promo_code_value, Filter $filter):void
public function preValidatePromoCode(
Summit $summit, Member $owner, string $promo_code_value, int $ticket_type_id, string $ticket_type_subtype, int $qty):void
{
$this->tx_service->transaction(function () use ($summit, $owner, $promo_code_value, $filter) {

$ticket_type_id = intval($filter->getUniqueFilter('ticket_type_id')->getValue());
$this->tx_service->transaction(function () use ($summit, $owner, $promo_code_value, $ticket_type_id, $ticket_type_subtype, $qty) {

$ticket_type = $summit->getTicketTypeById($ticket_type_id);
if (is_null($ticket_type)) {
throw new EntityNotFoundException(sprintf("Ticket Type %s not found on summit %s.", $ticket_type_id, $summit->getId()));
}

$ticket_type_subtype = $filter->getUniqueFilter('ticket_type_subtype')->getValue();
$qty = intval($filter->getUniqueFilter('ticket_type_qty')->getValue());

$validator = PromoCodeValidationStrategyFactory::createStrategy($ticket_type, $ticket_type_subtype, $qty, $owner);

$promo_code = $this->repository->getByValueExclusiveLock($summit, $promo_code_value);

if (!$promo_code instanceof SummitRegistrationPromoCode || $promo_code->getSummitId() != $summit->getId() || !$validator->isValid($promo_code)){
throw new ValidationException(sprintf('The Promo Code "%s" is not a valid code.', $promo_code_value));
}

});
}
}