getContainer()->getPermissionsService()->currentUserCanWrite(Entities::APPOINTMENTS)) { throw new AccessDeniedException('You are not allowed to add appointment'); } $result = new CommandResult(); $this->checkMandatoryFields($command); /** @var AppointmentRepository $appointmentRepo */ $appointmentRepo = $this->container->get('domain.booking.appointment.repository'); /** @var AppointmentApplicationService $appointmentAS */ $appointmentAS = $this->container->get('application.booking.appointment.service'); /** @var ServiceRepository $serviceRepository */ $serviceRepository = $this->container->get('domain.bookable.service.repository'); $appointmentData = $command->getFields(); $service = $serviceRepository->getProviderServiceWithExtras( $appointmentData['serviceId'], $appointmentData['providerId'] ); $appointment = $appointmentAS->build($appointmentData, $service); if (!$appointment instanceof Appointment) { $result->setResult(CommandResult::RESULT_ERROR); $result->setMessage('Could not create new appointment'); return $result; } if (!$appointmentAS->canBeBooked($appointment, false)) { $result->setResult(CommandResult::RESULT_ERROR); $result->setMessage(FrontendStrings::getCommonStrings()['time_slot_unavailable']); $result->setData([ 'timeSlotUnavailable' => true ]); return $result; } $appointmentRepo->beginTransaction(); try { $appointmentAS->add($appointment, $service, $command->getField('payment')); } catch (QueryExecutionException $e) { $appointmentRepo->rollback(); throw $e; } $result->setResult(CommandResult::RESULT_SUCCESS); $result->setMessage('Successfully added new appointment'); $result->setData( [ Entities::APPOINTMENT => $appointment->toArray() ] ); $appointmentRepo->commit(); return $result; } }