Skip to content

Commit

Permalink
PROD-30973 - Implement 'user cancels enrolment to event' event for th…
Browse files Browse the repository at this point in the history
  • Loading branch information
nkoporec authored and ribel committed Nov 6, 2024
1 parent c48f9ae commit c7553e4
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
26 changes: 26 additions & 0 deletions modules/social_features/social_event/social_event.module
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,32 @@ function social_event_entity_insert(EntityInterface $entity): void {
}
}

/**
* Implements hook_entity_delete().
*/
function social_event_entity_delete(EntityInterface $entity): void {
if ($entity instanceof EventEnrollment) {
// Invite or user requests statuses.
$invite_or_request_statuses = [
EventEnrollmentInterface::REQUEST_OR_INVITE_DECLINED,
EventEnrollmentInterface::INVITE_INVALID_OR_EXPIRED,
EventEnrollmentInterface::INVITE_PENDING_REPLY,
];

if (!$entity->get('field_request_or_invite_status')->isEmpty() &&
in_array(
(int) $entity->get('field_request_or_invite_status')->getString(),
$invite_or_request_statuses
)
) {
return;
}

\Drupal::service('social_event.eda_event_enrollment_handler')
->eventEnrollmentCancel($entity);
}
}

/**
* Implements hook_block_view_alter().
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ public function eventEnrollmentCreate(EventEnrollmentInterface $event_enrollment
$this->dispatch($topic_name, $event_type, $event_enrollment);
}

/**
* Cancels an event enrollment.
*/
public function eventEnrollmentCancel(EventEnrollmentInterface $event_enrollment): void {
$event_type = 'com.getopensocial.event_enrollment.cancel';
$topic_name = 'com.getopensocial.event_enrollment.cancel';
$this->dispatch($topic_name, $event_type, $event_enrollment);
}

/**
* Transforms a EventEnrollment into a CloudEvent.
*
Expand Down Expand Up @@ -141,6 +150,29 @@ public function fromEntity(EventEnrollmentInterface $event_enrollment, string $e
];
}

// Get enrollee data.
$enrollee = $event_enrollment->getAccountEntity();
$enrollee_data = [];
if ($enrollee && $enrollee->id() != 0) {
$enrollee_data = [
'id' => (string) $enrollee->uuid(),
'displayName' => (string) $enrollee->getDisplayName(),
'email' => "",
'href' => Href::fromEntity($enrollee),
];
}
else {
$first_name = $event_enrollment->get('field_first_name')->value;
$last_name = $event_enrollment->get('field_last_name')->value;
$email = $event_enrollment->get('field_email')->value;
$enrollee_data = [
'id' => NULL,
'displayName' => $first_name . " " . $last_name,
'email' => $email,
'href' => NULL,
];
}

return new CloudEvent(
id: $this->uuid->generate(),
source: $this->source,
Expand Down Expand Up @@ -179,7 +211,7 @@ public function fromEntity(EventEnrollmentInterface $event_enrollment, string $e
],
'actor' => [
'application' => $actor_application ? Application::fromId($actor_application) : NULL,
'user' => $actor_user ? User::fromEntity($actor_user) : NULL,
'user' => $enrollee_data,
],
],
dataContentType: 'application/json',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,27 @@ public function testEventEnrollmentCreate(): void {
$handler->eventEnrollmentCreate($this->eventEnrollment);
}

/**
* Tests the eventEnrollmentCreate method.
*
* @covers ::eventEnrollmentCreate
*/
public function testEventEnrollmentCancel(): void {
// Create the handler instance.
$handler = $this->getMockedHandler();

// Expect dispatch to be called with specific parameters.
$this->dispatcher->expects($this->once())
->method('dispatch')
->with(
$this->equalTo('com.getopensocial.event_enrollment.cancel'),
$this->isInstanceOf(CloudEvent::class)
);

// Call the eventEnrollmentCancel method.
$handler->eventEnrollmentCancel($this->eventEnrollment);
}

/**
* Tests the fromEntity method.
*
Expand Down

0 comments on commit c7553e4

Please sign in to comment.