Skip to content

Commit

Permalink
Fixed the reports download and ropa generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslanbaidan committed Sep 3, 2024
1 parent 3a0c013 commit eeb8aa2
Show file tree
Hide file tree
Showing 17 changed files with 106 additions and 100 deletions.
4 changes: 2 additions & 2 deletions src/Controller/ApiAnrInstancesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ public function get($id)
$anr = $this->getRequest()->getAttribute('anr');

if ($this->params()->fromQuery('csv', false)) {
return $this->prepareCsvDataResponse(
return $this->prepareCsvExportResponse(
$this->anrInstanceRiskOpService->getOperationalRisksInCsv($anr, (int)$id, $this->parseParams())
);
}

if ($this->params()->fromQuery('csvInfoInst', false)) {
return $this->prepareCsvDataResponse(
return $this->prepareCsvExportResponse(
$this->anrInstanceRiskService->getInstanceRisksInCsv($anr, (int)$id, $this->parseParams())
);
}
Expand Down
42 changes: 5 additions & 37 deletions src/Controller/ApiAnrRecordsExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
use Monarc\Core\Controller\Handler\AbstractRestfulControllerRequestHandler;
use Monarc\Core\Controller\Handler\ControllerRequestResponseHandlerTrait;
use Monarc\Core\Exception\Exception;
use Monarc\FrontOffice\Export\Controller\Traits\ExportResponseControllerTrait;
use Monarc\FrontOffice\Service\AnrRecordService;

class ApiAnrRecordsExportController extends AbstractRestfulControllerRequestHandler
{
use ControllerRequestResponseHandlerTrait;
use ExportResponseControllerTrait;

public function __construct(private AnrRecordService $anrRecordService)
{
Expand All @@ -24,54 +26,20 @@ public function create($data)
{
if (!empty($data['id'])) {
$output = $this->anrRecordService->export($data);
$filename = empty($data['filename']) ? $data['id'] : $data['filename'];

if (empty($data['password'])) {
$contentType = 'application/json; charset=utf-8';
$extension = '.json';
} else {
$contentType = 'text/plain; charset=utf-8';
$extension = '.bin';
}

$this->getResponse()
->getHeaders()
->clearHeaders()
->addHeaderLine('Content-Type', $contentType)
->addHeaderLine('Content-Disposition', 'attachment; filename="' .
(empty($data['filename']) ? $data['id'] : $data['filename']) . $extension . '"');

$this->getResponse()
->setContent($output);

return $this->getResponse();
return $this->prepareJsonExportResponse($filename, $output, !empty($data['password']));
}

if ($data['export'] === "All") {
if (empty($data['password'])) {
$contentType = 'application/json; charset=utf-8';
$extension = '.json';
} else {
$contentType = 'text/plain; charset=utf-8';
$extension = '.bin';
}
$anrId = (int)$this->params()->fromRoute('anrid');
if (empty($anrId)) {
throw new Exception('Anr id missing', 412);
}
$data['anr'] = $anrId;
$data['filename'] = "records_list";
$output = $this->anrRecordService->exportAll($data);

$this->getResponse()
->getHeaders()
->clearHeaders()
->addHeaderLine('Content-Type', $contentType)
->addHeaderLine('Content-Disposition', 'attachment; filename="' .
(empty($data['filename']) ? $data['id'] : $data['filename']) . $extension . '"');

$this->getResponse()->setContent($output);

return $this->getResponse();
return $this->prepareJsonExportResponse('records_list', $output, !empty($data['password']));
}

throw new Exception('Record to export is required', 412);
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/ApiAnrRisksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function get($id)
$id = $id === null ? null : (int)$id;

if ($this->params()->fromQuery('csv', false)) {
return $this->prepareCsvDataResponse(
return $this->prepareCsvExportResponse(
$this->anrInstanceRiskService->getInstanceRisksInCsv($anr, $id, $params)
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/ApiAnrRisksOpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function get($id)
$id = $id === null ? null : (int)$id;

if ($this->params()->fromQuery('csv', false)) {
return $this->prepareCsvDataResponse(
return $this->prepareCsvExportResponse(
$this->anrInstanceRiskOpService->getOperationalRisksInCsv($anr, $id, $params)
);
}
Expand Down
18 changes: 3 additions & 15 deletions src/Controller/ApiDeliveriesModelsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
use Monarc\Core\Controller\Handler\ControllerRequestResponseHandlerTrait;
use Monarc\Core\Exception\Exception;
use Monarc\Core\Service\DeliveriesModelsService;
use Monarc\FrontOffice\Export\Controller\Traits\ExportResponseControllerTrait;

class ApiDeliveriesModelsController extends AbstractController
{
use ControllerRequestResponseHandlerTrait;
use ExportResponseControllerTrait;

public function __construct(DeliveriesModelsService $deliveriesModelsService)
{
Expand Down Expand Up @@ -85,23 +87,9 @@ public function get($id)

$fileContents = file_get_contents($currentPath);
if ($fileContents !== false) {
$response = $this->getResponse();
$response->setContent($fileContents);

$headers = $response->getHeaders();
$headers->clearHeaders()
->addHeaderLine(
'Content-Type',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
)
->addHeaderLine('Content-Disposition', 'attachment; filename="' . utf8_decode($name) . '"')
->addHeaderLine('Content-Length', strlen($fileContents));

return $this->response;
return $this->prepareWordExportResponse($name, $fileContents);
}
throw new Exception('Document template not found');
}
throw new Exception('Document template not found');
}

throw new Exception('Document template not found');
Expand Down
34 changes: 23 additions & 11 deletions src/Entity/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Record extends AbstractEntity
protected $label;

/**
* @var array
* @var string
*
* @ORM\Column(name="purposes", type="text", nullable=true)
*/
Expand Down Expand Up @@ -187,20 +187,32 @@ public function setlabel($label)
$this->label = $label;
}

/**
* @return array
*/
public function getPurposes()
public function getlabel(): string
{
return $this->purposes;
return (string)$this->label;
}
/**
* @param string $purposes
* @return Record
*/
public function setPurposes($purposes)

public function getPurposes(): string
{
return (string)$this->purposes;
}

public function setPurposes(string $purposes)
{
$this->purposes = $purposes;

return $this;
}

public function getSecMeasures(): string
{
return (string)$this->secMeasures;
}

public function setSecMeasures(string $secMeasures): self
{
$this->secMeasures = $secMeasures;

return $this;
}

Expand Down
12 changes: 11 additions & 1 deletion src/Entity/RecordActor.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class RecordActor extends AbstractEntity
protected $label;

/**
* @var array
* @var string
*
* @ORM\Column(name="contact", type="string", length=255, nullable=true)
*/
Expand Down Expand Up @@ -109,4 +109,14 @@ public function setContact($contact)
$this->contact = $contact;
return $this;
}

public function getLabel(): string
{
return (string)$this->label;
}

public function getContact(): string
{
return (string)$this->contact;
}
}
8 changes: 7 additions & 1 deletion src/Entity/RecordRecipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ class RecordRecipient extends AbstractEntity
protected $label;

/**
* 0 for internal recipients and 1 for external recipients.
*
* @var bool
*
* @ORM\Column(name="type", type="string", length=255, nullable=true)
*/
// 0 for internal recipients and 1 for external recipients
protected $type;

/**
Expand Down Expand Up @@ -102,4 +103,9 @@ public function setAnr($anr): self

return $this;
}

public function getType(): int
{
return (int)$this->type;
}
}
2 changes: 1 addition & 1 deletion src/Export/Controller/ApiAnrExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public function create($data)
$anr = $this->getRequest()->getAttribute('anr');
$result = $this->anrExportService->export($anr, $data);

return $this->prepareExportResponse($result['filename'], $result['content'], !empty($data['password']));
return $this->prepareJsonExportResponse($result['filename'], $result['content'], !empty($data['password']));
}
}
2 changes: 1 addition & 1 deletion src/Export/Controller/ApiAnrInstancesExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public function create($data)
$anr = $this->getRequest()->getAttribute('anr');
$result = $this->anrInstanceExportService->export($anr, $data);

return $this->prepareExportResponse($result['filename'], $result['content'], !empty($data['password']));
return $this->prepareJsonExportResponse($result['filename'], $result['content'], !empty($data['password']));
}
}
2 changes: 1 addition & 1 deletion src/Export/Controller/ApiAnrObjectsExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public function create($data)
$anr = $this->getRequest()->getAttribute('anr');
$result = $this->anrObjectExportService->export($anr, $data);

return $this->prepareExportResponse($result['filename'], $result['content'], !empty($data['password']));
return $this->prepareJsonExportResponse($result['filename'], $result['content'], !empty($data['password']));
}
}
17 changes: 15 additions & 2 deletions src/Export/Controller/Traits/ExportResponseControllerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

trait ExportResponseControllerTrait
{
private function prepareExportResponse(string $filename, string $output, bool $isEncrypted): ResponseInterface
private function prepareJsonExportResponse(string $filename, string $output, bool $isEncrypted): ResponseInterface
{
$contentType = 'application/json; charset=utf-8';
$extension = '.json';
Expand All @@ -32,7 +32,7 @@ private function prepareExportResponse(string $filename, string $output, bool $i
]);
}

private function prepareCsvDataResponse(string $output): ResponseInterface
private function prepareCsvExportResponse(string $output): ResponseInterface
{
$stream = fopen('php://memory', 'rb+');
fwrite($stream, $output);
Expand All @@ -43,4 +43,17 @@ private function prepareCsvDataResponse(string $output): ResponseInterface
'Content-Length' => strlen($output),
]);
}

private function prepareWordExportResponse(string $filename, string $output): ResponseInterface
{
$stream = fopen('php://memory', 'rb+');
fwrite($stream, $output);
rewind($stream);

return new Response($stream, 200, [
'Content-Type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'Content-Length' => strlen($output),
'Content-Disposition' => 'attachment; filename="' . utf8_decode($filename) . '"',
]);
}
}
6 changes: 4 additions & 2 deletions src/Middleware/AnrValidationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,12 @@ private function validateAnrStatusAndGetResponseIfInvalid(
['anrId' => $anr->getId()]
);
if ($importCronTask !== null && $importCronTask->getStatus() === Entity\CronTask::STATUS_IN_PROGRESS) {
$timeDiff = $importCronTask->getUpdatedAt()->diff(new DateTime());
$timeDiff = $importCronTask->getUpdatedAt() !== null
? $importCronTask->getUpdatedAt()->diff(new DateTime())
: $importCronTask->getCreatedAt()->diff(new DateTime());
$instancesNumber = $this->instanceTable->countByAnrIdFromDate(
$anr->getId(),
$importCronTask->getUpdatedAt()
$importCronTask->getUpdatedAt() ?? $importCronTask->getCreatedAt()
);
$result['importStatus'] = [
'executionTime' => $timeDiff->h . ' hours ' . $timeDiff->i . ' min ' . $timeDiff->s . ' sec',
Expand Down
11 changes: 11 additions & 0 deletions src/Model/Table/RecordTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Monarc\FrontOffice\Model\Table;

use Doctrine\ORM\EntityNotFoundException;
use Monarc\Core\Model\Table\AbstractEntityTable;
use Monarc\Core\Service\ConnectedUserService;
use Monarc\FrontOffice\Entity\Anr;
Expand All @@ -20,6 +21,16 @@ public function __construct(DbCli $dbService, ConnectedUserService $connectedUse
parent::__construct($dbService, Record::class, $connectedUserService);
}

public function findById(int $id): Record
{
$record = $this->getRepository()->find($id);
if ($record === null) {
throw EntityNotFoundException::fromClassNameAndIdentifier(\get_class($this), [$id]);
}

return $record;
}

/**
* @return Record[]
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Service/AnrRecordService.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function duplicateRecord($recordId, $newLabel)
$newRecord = new Record($entity);
$newRecord->setId(null);
$newRecord->resetUpdatedAtValue();
$newRecord->setLabel($newLabel);
$newRecord->setLabel((string)$newLabel);
$id = $this->get('table')->save($newRecord);
if ($entity->getProcessors()) {
foreach ($entity->getProcessors() as $p) {
Expand Down
6 changes: 4 additions & 2 deletions src/Service/AnrService.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,12 @@ private function getPreparedAnrData(
['anrId' => $anr->getId()]
);
if ($importCronTask !== null && $importCronTask->getStatus() === Entity\CronTask::STATUS_IN_PROGRESS) {
$timeDiff = $importCronTask->getUpdatedAt()->diff(new DateTime());
$timeDiff = $importCronTask->getUpdatedAt() !== null
? $importCronTask->getUpdatedAt()->diff(new DateTime())
: $importCronTask->getCreatedAt()->diff(new DateTime());
$instancesNumber = $this->instanceTable->countByAnrIdFromDate(
$anr->getId(),
$importCronTask->getUpdatedAt()
$importCronTask->getUpdatedAt() ?? $importCronTask->getCreatedAt()
);
$anrData['importStatus'] = [
'executionTime' => $timeDiff->h . ' hours ' . $timeDiff->i . ' min ' . $timeDiff->s . ' sec',
Expand Down
Loading

0 comments on commit eeb8aa2

Please sign in to comment.