Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
StanBarrows committed Dec 7, 2023
1 parent 100b825 commit 56e67d5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 27 deletions.
13 changes: 12 additions & 1 deletion src/DTO/DocumentIndex/PrepareDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static function guess(string $name, mixed $value): mixed
};
}

public static function makeContent(Collection $indexes): array
public static function makeFields(Collection $indexes): array
{
return [
'Fields' => $indexes
Expand All @@ -28,4 +28,15 @@ public static function makeContent(Collection $indexes): array
->values(),
];
}

public static function makeField(Collection $indexes, bool $forceUpdate = false): array
{
return [
'Field' => $indexes
->map(fn (IndexTextDTO|IndexDateDTO|IndexDateTimeDTO|IndexNumericDTO|IndexDecimalDTO|IndexTableDTO $index) => $index->values())
->filter()
->values(),
'ForceUpdate' => $forceUpdate,
];
}
}
2 changes: 1 addition & 1 deletion src/Requests/Document/PostDocumentRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function defaultBody(): array
$body = [];

if ($this->indexes) {
$indexContent = json_encode(PrepareDTO::makeContent($this->indexes));
$indexContent = json_encode(PrepareDTO::makeFields($this->indexes));
$body[] = new MultipartValue(name: 'document', value: $indexContent, filename: 'index.json');
}

Expand Down
27 changes: 9 additions & 18 deletions src/Requests/Document/PutDocumentFieldsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace CodebarAg\DocuWare\Requests\Document;

use CodebarAg\DocuWare\DTO\DocumentIndex\PrepareDTO;
use CodebarAg\DocuWare\Exceptions\UnableToUpdateFields;
use CodebarAg\DocuWare\Responses\Document\PutDocumentFieldsResponse;
use Illuminate\Support\Collection;
use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
Expand All @@ -22,7 +24,7 @@ class PutDocumentFieldsRequest extends Request implements HasBody
public function __construct(
protected readonly string $fileCabinetId,
protected readonly string $documentId,
protected readonly array $values,
protected readonly ?Collection $indexes = null,
protected readonly bool $forceUpdate = false,
) {
}
Expand All @@ -34,27 +36,16 @@ public function resolveEndpoint(): string

public function defaultBody(): array
{
throw_unless(count($this->values) > 0, UnableToUpdateFields::noValuesProvided());
$body = [];

$fields = [];

foreach ($this->values as $key => $value) {
throw_unless($value, UnableToUpdateFields::noValuesProvidedForField($key));
$fields[] = [
'FieldName' => $key,
'Item' => $value,
];
if ($this->indexes) {
$bodyEncode = json_encode(PrepareDTO::makeField($this->indexes));
$bodyDecode = json_decode($bodyEncode, true);
$body = $bodyDecode;
}

$content = [
'Field' => $fields,
];

if ($this->forceUpdate) {
$content['ForceUpdate'] = true;
}
return $body;

return $content;
}

public function createDtoFromResponse(Response $response): mixed
Expand Down
14 changes: 8 additions & 6 deletions tests/Feature/Requests/Document/PutDocumentFieldsRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use CodebarAg\DocuWare\Connectors\DocuWareStaticConnector;
use CodebarAg\DocuWare\DTO\Config;
use CodebarAg\DocuWare\DTO\DocumentIndex\IndexTextDTO;
use CodebarAg\DocuWare\Events\DocuWareResponseLog;
use CodebarAg\DocuWare\Requests\Document\DeleteDocumentRequest;
use CodebarAg\DocuWare\Requests\Document\PostDocumentRequest;
Expand Down Expand Up @@ -41,7 +42,9 @@
$response = $this->connector->send(new PutDocumentFieldsRequest(
$fileCabinetId,
$document->id,
['UUID' => $newValue]
collect([
IndexTextDTO::make('UUID', $newValue),
])
))->dto();

$this->assertSame('laravel-docuware', $response['UUID']);
Expand All @@ -57,10 +60,6 @@
Event::fake();

$fileCabinetId = config('laravel-docuware.tests.file_cabinet_id');
$values = [
'UUID' => 'laravel-docuware',
'DOCUMENT_LABEL' => 'laravel-docuware-2',
];

$document = $this->connector->send(new PostDocumentRequest(
$fileCabinetId,
Expand All @@ -71,7 +70,10 @@
$response = $this->connector->send(new PutDocumentFieldsRequest(
$fileCabinetId,
$document->id,
$values,
collect([
IndexTextDTO::make('UUID', 'laravel-docuware'),
IndexTextDTO::make('DOCUMENT_LABEL', 'laravel-docuware-2'),
]),
true
))->dto();

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/DTO/DocumentIndex/PrepareDTOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

$indexes = collect([PrepareDTO::guess($name, $value)]);

expect(PrepareDTO::makeContent($indexes))
expect(PrepareDTO::makeFields($indexes))
->toBeArray();

})->group('dto');

0 comments on commit 56e67d5

Please sign in to comment.