Skip to content

Commit

Permalink
feat: UHF-10693: Add copyable list for application delete function + …
Browse files Browse the repository at this point in the history
…new Event for sending to integration. (#1576)
  • Loading branch information
jiisuominen authored Nov 25, 2024
1 parent 89f6c49 commit d6eb844
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 259 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,12 @@ public function buildForm(array $form, FormStateInterface $form_state): array {

// Build the application listing form elements.
if ($uuid || $businessId) {
$this->buildApplicationList($uuid, $businessId, $appEnv, $type, $status, $form_state, $form);
try {
$this->buildApplicationList($uuid, $businessId, $appEnv, $type, $status, $form_state, $form);
}
catch (\Throwable $e) {
$this->messenger()->addError($e->getMessage());
}
}

$form['actions']['delete_selected'] = [
Expand Down Expand Up @@ -365,6 +370,8 @@ private function deleteDraftDocuments(array $documents): void {
* The form state.
* @param array $form
* The form.
*
* @throws \Drupal\helfi_helsinki_profiili\TokenExpiredException
*/
private function buildApplicationList(
mixed $uuid,
Expand All @@ -384,6 +391,13 @@ private function buildApplicationList(
return;
}

// Extract the transaction_id values and join them into a string.
$transactionIds = array_map(function ($document) {
return $document->getTransactionId();
}, $documents);

$applicationNumberList = implode(',', $transactionIds);

$form_state->setStorage(['documents' => $documents]);
$documentsByType = $this->sortDocuments($documents);

Expand Down Expand Up @@ -413,6 +427,15 @@ private function buildApplicationList(
];
}
}

// Add the application numbers list to a form element.
$form['appData']['application_numbers'] = [
'#type' => 'textarea',
'#title' => $this->t('Application numbers in copyable list.'),
'#value' => $applicationNumberList,
'#description' => $this->t('Total number of results: @count', ['@count' => count($transactionIds)]),
'#disabled' => TRUE,
];
}
catch (AtvDocumentNotFoundException | AtvFailedToConnectException | GuzzleException $e) {
$this->messenger()->addError('Failed fetching applications.');
Expand Down
24 changes: 22 additions & 2 deletions public/modules/custom/grants_events/src/EventsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\grants_handler\DebuggableTrait;
use Drupal\grants_metadata\AtvSchema;
use Drupal\helfi_atv\AtvDocument;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Ramsey\Uuid\Uuid;
Expand Down Expand Up @@ -67,6 +68,7 @@ class EventsService {
'MESSAGE_READ' => 'MESSAGE_READ',
'MESSAGE_RESEND' => 'MESSAGE_RESEND',
'HANDLER_ATT_OK' => 'HANDLER_ATT_OK',
'HANDLER_SEND_INTEGRATION' => 'HANDLER_SEND_INTEGRATION',
'HANDLER_ATT_DELETE' => 'HANDLER_ATT_DELETE',
'HANDLER_RESEND_APP' => 'HANDLER_RESEND_APP',
'HANDLER_APP_COPIED' => 'HANDLER_APP_COPIED',
Expand Down Expand Up @@ -139,7 +141,7 @@ public function logEvent(
): ?array {

if (empty($eventData)) {
$eventData = self::getEventData($eventType, $applicationNumber, $eventDescription, $eventTarget);
$eventData = $this->getEventData($eventType, $applicationNumber, $eventDescription, $eventTarget);
}

$eventDataJson = Json::encode($eventData);
Expand Down Expand Up @@ -212,7 +214,7 @@ public function filterEvents(array $events, string $typeKey): array {
* @param string $eventDescription
* Event description.
* @param string $eventTarget
* Eent target.
* Event target.
*
* @return array
* Event data in array.
Expand Down Expand Up @@ -245,4 +247,22 @@ public function getEventData(string $eventType, string $applicationNumber, strin
return $eventData;
}

/**
* Add new event to application document.
*
* @param \Drupal\helfi_atv\AtvDocument $document
* Document to be updated.
* @param array $eventData
* Event data to be added.
*/
public function addNewEventForApplication(AtvDocument &$document, array $eventData): void {
$documentContent = $document->getContent();
$documentEvents = $documentContent['events'] ?? [];
$documentEvents[] = $eventData;
$documentContent['events'] = $documentEvents;

$document->setContent($documentContent);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ services:
'@helfi_helsinki_profiili.userdata',
'@grants_attachments.attachment_fixer_service',
'@current_user',
'@database'
'@database',
'@grants_events.events_service'
]

grants_handler.application_access_handler:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\grants_attachments\AttachmentFixerService;
use Drupal\grants_events\EventsService;
use Drupal\grants_metadata\AtvSchema;
use Drupal\helfi_atv\AtvDocument;
use Drupal\helfi_atv\AtvService;
Expand Down Expand Up @@ -83,6 +84,7 @@ public function __construct(
private readonly AttachmentFixerService $attachmentFixerService,
private readonly AccountInterface $currentUser,
private readonly Connection $database,
private readonly EventsService $eventsService,
) {
$this->logger = $this->loggerChannelFactory->get('application_uploader_service');

Expand Down Expand Up @@ -185,6 +187,7 @@ public function handleApplicationUploadToAtv(
* @throws \Drupal\helfi_atv\AtvFailedToConnectException
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Drupal\helfi_helsinki_profiili\TokenExpiredException
* @throws \Drupal\grants_events\EventException
*/
public function handleApplicationUploadViaIntegration(
TypedDataInterface $applicationData,
Expand All @@ -199,6 +202,25 @@ public function handleApplicationUploadViaIntegration(
* for some reason.
*/
$updatedDocumentFromAtv = $this->handleApplicationUploadToAtv($applicationData, $applicationNumber, $submittedFormData);

// Create new saveid before sending data to integration,
// so we can add it to event data.
$newSaveId = $this->logSubmissionSaveid(
NULL,
$applicationNumber,
$this->helfiHelsinkiProfiiliUserdata->getUserData()
);

// Add new event for sending it to integration.
$this->eventsService->addNewEventForApplication(
$updatedDocumentFromAtv,
$this->eventsService->getEventData(
$this->eventsService->getEventTypes()['HANDLER_SEND_INTEGRATION'],
$applicationNumber,
'Send application to integration.',
$newSaveId
));

$myJSON = Json::encode($updatedDocumentFromAtv->getContent());

// No matter what the debug value is, we do NOT log json in PROD.
Expand Down Expand Up @@ -232,12 +254,8 @@ public function handleApplicationUploadViaIntegration(
// Set application number to meta as well to enable better searches.
$headers['X-hki-applicationNumber'] = $applicationNumber;

// Set new saveid and save it to db.
$headers['X-hki-saveId'] = $this->logSubmissionSaveid(
NULL,
$applicationNumber,
$this->helfiHelsinkiProfiiliUserdata->getUserData()
);
// Set new saveid to header.
$headers['X-hki-saveId'] = $newSaveId;

$res = $this->httpClient->post($this->endpoint, [
'auth' => [
Expand Down Expand Up @@ -335,14 +353,13 @@ public function logSubmissionSaveid(
'saveid' => $saveId,
'uid' => $this->currentUser->id(),
'user_uuid' => $userData['sub'] ?? '',
'timestamp' => (string) (new Time)->getRequestTime(),
'timestamp' => (string) (new Time())->getRequestTime(),
];

$query = $this->database->insert(ApplicationHelpers::TABLE, $fields);
$query->fields($fields)->execute();

return $saveId;

}

}
Loading

0 comments on commit d6eb844

Please sign in to comment.