Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FZ-122 initial version of meetings tquery #185

Merged
merged 18 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions app/Http/Controllers/Facility/MeetingTqueryController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Http\Controllers\Facility;

use App\Http\Controllers\ApiController;
use App\Http\Permissions\Permission;
use App\Http\Permissions\PermissionDescribe;
use App\Tquery\Engine\TqService;
use App\Tquery\OpenApi\OpenApiGet;
use App\Tquery\OpenApi\OpenApiPost;
use App\Tquery\Tables\MeetingTquery;
use App\Utils\OpenApi\FacilityParameter;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;

class MeetingTqueryController extends ApiController
{
protected function initPermissions(): void
{
$this->permissionOneOf(Permission::globalAdmin);
}

private function getTqService(): TqService
{
return App::make(MeetingTquery::class, ['facility' => $this->getFacilityOrFail()]);
}

#[OpenApiGet(
path: '/api/v1/facility/{facility}/meeting/tquery',
permissions: new PermissionDescribe(Permission::facilityAdmin),
summary: 'Facility meetings tquery',
tag: 'Facility meeting',
parameters: [new FacilityParameter()],
)]
public function get(): JsonResponse
{
return new JsonResponse($this->getTqService()->getConfigArray());
}

#[OpenApiPost(
path: '/api/v1/facility/{facility}/meeting/tquery',
permissions: new PermissionDescribe(Permission::facilityAdmin),
summary: 'Facility meetings tquery',
tag: 'Facility meeting',
parameters: [new FacilityParameter()],
)]
public function post(
Request $request,
): JsonResponse {
return new JsonResponse($this->getTqService()->query($request));
}
}
19 changes: 19 additions & 0 deletions app/Models/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
use App\Models\Enums\AttributeType;
use App\Models\QueryBuilders\AttributeBuilder;
use App\Models\Traits\BaseModel;
use App\Tquery\Config\TqDataTypeEnum;
use App\Tquery\Config\TqDictDef;
use Illuminate\Database\Eloquent\Model;

/**
* @property ?string facility_id
* @property AttributeTable table
* @property string name
* @property string api_name
* @property AttributeType type
Expand Down Expand Up @@ -45,4 +48,20 @@ class Attribute extends Model
'is_multi_value' => 'boolean',
'requirement_level' => AttributeRequirementLevel::class,
];

public function getTqueryDataType(): TqDataTypeEnum|TqDictDef
{
$nullable = $this->requirement_level->isNullable();
$type = match ($this->type) {
AttributeType::Bool => $nullable ? TqDataTypeEnum::bool_nullable : TqDataTypeEnum::bool,
AttributeType::Date => $nullable ? TqDataTypeEnum::date_nullable : TqDataTypeEnum::date,
AttributeType::Datetime => $nullable ? TqDataTypeEnum::datetime_nullable : TqDataTypeEnum::datetime,
AttributeType::Int => $nullable ? TqDataTypeEnum::int_nullable : TqDataTypeEnum::int,
AttributeType::String => $nullable ? TqDataTypeEnum::string_nullable : TqDataTypeEnum::string,
AttributeType::Users, AttributeType::Clients, AttributeType::Attributes => $nullable ?
TqDataTypeEnum::uuid_nullable : TqDataTypeEnum::uuid,
AttributeType::Dict => $nullable ? TqDataTypeEnum::dict_nullable : TqDataTypeEnum::dict,
};
return $type->isDict() ? (new TqDictDef($type, $this->dictionary_id)) : $type;
}
}
5 changes: 5 additions & 0 deletions app/Models/Enums/AttributeRequirementLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ enum AttributeRequirementLevel: string
case Recommended = 'recommended';
case Optional = 'optional';
case Empty = 'empty';

public function isNullable(): bool
{
return $this !== self::Required;
}
}
2 changes: 1 addition & 1 deletion app/Models/Meeting.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected static function fieldValidator(string $field): string|array
new MemberExistsRule(AttendanceType::Client),
]),
'resources.*' => Valid::array(keys: ['resource_dict_id']),
'resources.*.resource_dict_id' =>Valid::dict(
'resources.*.resource_dict_id' => Valid::dict(
DictionaryUuidEnum::MeetingResource,
[new UniqueWithMemoryRule('resource')],
),
Expand Down
7 changes: 7 additions & 0 deletions app/Models/UuidEnum/AttributeUuidEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace App\Models\UuidEnum;

interface AttributeUuidEnum
{
}
10 changes: 10 additions & 0 deletions app/Models/UuidEnum/MeetingAttributeUuidEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Models\UuidEnum;

enum MeetingAttributeUuidEnum: string implements AttributeUuidEnum
{
case Type = '5f7d5e66-03f9-4bcd-a726-fde82cf98d6f';
case Category = '8111626d-130c-454d-b0c0-9fda9ab9917a';
case Status = 'e443e2c2-82fc-41d3-8fda-fe374e5329d3';
}
2 changes: 1 addition & 1 deletion app/Models/UuidEnum/PositionAttributeUuidEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Models\UuidEnum;

enum PositionAttributeUuidEnum: string
enum PositionAttributeUuidEnum: string implements AttributeUuidEnum
{
case Category = 'c9ab3795-0012-4cfe-8100-a7bb1dd9755b';
case DurationMinutes = '540e2b26-1330-42ae-8209-55cf22bb3638';
Expand Down
1 change: 1 addition & 0 deletions app/Rules/PositionInDictionaryRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

final class PositionInDictionaryRule extends AbstractRule
{
// todo private $facilityId - check dictionary position for given facility
public function __construct(
private readonly string $dictionaryId,
) {
Expand Down
1 change: 1 addition & 0 deletions app/Rules/Valid.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ private static function base(bool $sometimes, bool $nullable, array $rules, arra
{
return array_merge(
$sometimes ? ['sometimes'] : [],
// todo: consider: ['present', new self($nullable, ['bail', ...$rules]), ...$additionalRules],
['present', new self($nullable, array_merge(['bail'], $rules, $additionalRules))],
);
}
Expand Down
11 changes: 7 additions & 4 deletions app/Tquery/Config/TqColumnConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Tquery\Config;

use App\Models\Attribute;
use App\Tquery\Engine\TqBuilder;
use App\Tquery\Engine\TqFilterGenerator;
use App\Tquery\Engine\TqRendererGenerator;
Expand All @@ -23,10 +24,12 @@ public function __construct(
private string|Closure $columnOrQuery,
public ?TqTableAliasEnum $table,
public string $columnAlias,
?Closure $selector = null,
?Closure $filter = null,
?Closure $sorter = null,
?Closure $renderer = null,
public ?string $dictionaryId,
public ?Attribute $attribute,
?Closure $selector,
?Closure $filter,
?Closure $sorter,
?Closure $renderer,
) {
$this->selector = $selector ?? TqSelectGenerator::getSelect($this);
$this->filter = $filter ?? TqFilterGenerator::getFilter($this);
Expand Down
39 changes: 32 additions & 7 deletions app/Tquery/Config/TqConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
namespace App\Tquery\Config;

use App\Exceptions\FatalExceptionFactory;
use App\Models\Attribute;
use App\Models\UuidEnum\AttributeUuidEnum;
use BackedEnum;
use Closure;
use Illuminate\Support\Str;

final class TqConfig
{
/** @var array<string, TqColumnConfig> */
public array $columns = [];
private const COUNT_COLUMN = '_count';
private const string COUNT_COLUMN = '_count';

private ?array $filterableColumns = null;

Expand Down Expand Up @@ -48,7 +51,7 @@ public function getSortableColumns(bool $distinct): array
}

public function addSimple(
TqDataTypeEnum $type,
TqDataTypeEnum|TqDictDef $type,
string $columnName,
?string $columnAlias = null,
): void {
Expand All @@ -60,8 +63,20 @@ public function addSimple(
);
}

public function addAttribute(string|(AttributeUuidEnum&BackedEnum) $attribute): void
{
$attributeModel = Attribute::query()->findOrFail(is_string($attribute) ? $attribute : $attribute->value);
$this->addColumn(
type: $attributeModel->getTqueryDataType(),
columnOrQuery: $attributeModel->api_name,
table: null,
columnAlias: Str::camel($attributeModel->api_name),
attribute: $attributeModel,
);
}

public function addJoined(
TqDataTypeEnum $type,
TqDataTypeEnum|TqDictDef $type,
TqTableAliasEnum $table,
string $columnName,
?string $columnAlias = null,
Expand All @@ -75,7 +90,7 @@ public function addJoined(
}

public function addQuery(
TqDataTypeEnum $type,
TqDataTypeEnum|TqDictDef $type,
Closure $columnOrQuery,
string $columnAlias,
?Closure $filter = null,
Expand Down Expand Up @@ -104,24 +119,34 @@ public function addCount(): void
}

private function addColumn(
TqDataTypeEnum $type,
TqDataTypeEnum|TqDictDef $type,
string|Closure $columnOrQuery,
?TqTableAliasEnum $table,
string $columnAlias,
?Attribute $attribute = null,
?Closure $selector = null,
?Closure $filter = null,
?Closure $sorter = null,
?Closure $renderer = null,
): void {
if (array_key_exists($columnAlias, $this->columns)) {
if (
(($type instanceof TqDataTypeEnum) && $type->isDict())
|| array_key_exists($columnAlias, $this->columns)
) {
throw FatalExceptionFactory::tquery();
}
[$dataType, $dictionaryId] = ($type instanceof TqDataTypeEnum)
? [$type, $attribute?->dictionary_id] : [$type->dataType, $type->dictionaryId];
$this->filterableColumns = null;
$this->columns[$columnAlias] = new TqColumnConfig(
config: $this,
type: $type,
type: $dataType,
columnOrQuery: $columnOrQuery,
table: $table,
columnAlias: $columnAlias,
dictionaryId: $dictionaryId,
attribute: $attribute,
selector: $selector,
filter: $filter,
sorter: $sorter,
renderer: $renderer,
Expand Down
62 changes: 35 additions & 27 deletions app/Tquery/Config/TqDataTypeEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum TqDataTypeEnum
case int;
case string;
case uuid;
case dict;
case text;
// nullable
case bool_nullable;
Expand All @@ -24,7 +25,12 @@ enum TqDataTypeEnum
case int_nullable;
case string_nullable;
case uuid_nullable;
case dict_nullable;
case text_nullable;
//list
case dict_list;
case uuid_list;
case list;
// additional
case count;
case is_null;
Expand All @@ -34,7 +40,24 @@ public function isNullable(): bool
{
return match ($this) {
self::bool_nullable, self::date_nullable, self::datetime_nullable, self::int_nullable,
self::string_nullable, self::uuid_nullable, self::text_nullable => true,
self::string_nullable, self::uuid_nullable, self::dict_nullable, self::text_nullable,
self::dict_list, self::uuid_list, self::list => true, // list have "null" operator
default => false,
};
}

public function isDict(): bool
{
return match ($this) {
self::dict, self::dict_nullable, self::dict_list => true,
default => false,
};
}

public function isList(): bool
{
return match ($this) {
self::dict_list, self::uuid_list, self::list => true,
default => false,
};
}
Expand Down Expand Up @@ -69,7 +92,7 @@ public function notNullBaseType(): self
public function isSortable(): bool
{
return match ($this->notNullBaseType()) {
self::uuid, self::text => false,
self::uuid, self::text, self::dict_list, self::uuid_list, self::list => false,
default => true,
};
}
Expand Down Expand Up @@ -101,14 +124,16 @@ public function operators(): array
...TqFilterOperator::CMP,
...TqFilterOperator::LIKE,
],
self::uuid => [TqFilterOperator::eq, TqFilterOperator::in],
self::uuid, self::dict => [TqFilterOperator::eq, TqFilterOperator::in],
self::text => TqFilterOperator::LIKE,
default => FatalExceptionFactory::tquery(),
self::dict_list, self::uuid_list, => [TqFilterOperator::eq, ...TqFilterOperator::LIST_COLUMN],
self::list => [],
default => FatalExceptionFactory::tquery()->throw(),
}
);
}

public function filterValueValidator(TqFilterOperator $operator): array
public function filterValueValidator(TqColumnConfig $column, TqFilterOperator $operator): array
{
if (in_array($operator, TqFilterOperator::LIKE, true)) {
return Valid::string($operator === TqFilterOperator::regexp ? [new RegexpIsValidRule()] : []);
Expand All @@ -118,28 +143,11 @@ public function filterValueValidator(TqFilterOperator $operator): array
self::date => Valid::date(),
self::datetime => Valid::datetime(),
self::int => Valid::int(),
self::string, self::text => in_array($operator, TqFilterOperator::TRIMMED, true)
? Valid::trimmed() : Valid::string(),
self::uuid => Valid::uuid(),
default => FatalExceptionFactory::tquery(),
self::string, self::text => in_array($operator, TqFilterOperator::LIKE, true)
? Valid::string() : Valid::trimmed(),
self::uuid, self::uuid_list => Valid::uuid(),
self::dict, self::dict_list => Valid::dict($column->dictionaryId),
default => FatalExceptionFactory::tquery()->throw(),
};
}

public function filterValuePrepare(
TqFilterOperator $operator,
bool|int|string|array $value,
): bool|int|string|array {
if ($this->notNullBaseType() === self::datetime) {
return DateHelper::zuluToDbString($value);
}
if (in_array($operator, [TqFilterOperator::pv, TqFilterOperator::vp, TqFilterOperator::pvp])) {
if (!is_string($value)) {
throw FatalExceptionFactory::tquery();
}
return (($operator === TqFilterOperator::pv || $operator === TqFilterOperator::pvp) ? '%' : '')
. str_replace(['\\', '%', '_'], ['\\\\', '\\%', '\\_'], $value)
. (($operator === TqFilterOperator::vp || $operator === TqFilterOperator::pvp) ? '%' : '');
}
return $value;
}
}
Loading