Skip to content

Commit ca6caf9

Browse files
committed
feat: update role filtering to use created_at instead of created_at_range
1 parent 7ac3fcc commit ca6caf9

File tree

5 files changed

+68
-22
lines changed

5 files changed

+68
-22
lines changed

contexts/Authorization/Application/DTOs/Role/GetRoleListDTO.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static function fromRequest(array $data): self
2626
$merged['id'] ?? null,
2727
$merged['label'] ?? null,
2828
$merged['status'] ?? null,
29-
$merged['created_at_range'] ?? null,
29+
$merged['created_at'] ?? null,
3030
$merged['current_page'] ?? 1,
3131
$merged['per_page'] ?? 10,
3232
self::normalizeAndFilterSorting($merged)
@@ -88,13 +88,8 @@ private static function convertFiltersToCriteria(array $filters): array
8888
{
8989
return collect($filters)->mapWithKeys(function ($filter) {
9090
$key = $filter['id'];
91-
if ($key === 'created_at_range') {
92-
$value = json_decode($filter['value'], true);
93-
} else {
94-
$value = $filter['value'];
95-
}
9691

97-
return [$key => $value];
92+
return [$key => $filter['value']];
9893
})->toArray();
9994
}
10095

@@ -109,7 +104,7 @@ public function toCriteria(): array
109104
'id' => $this->id,
110105
'label' => $this->label,
111106
'status' => $this->status,
112-
'created_at_range' => $this->createdAtRange,
107+
'created_at' => $this->createdAtRange,
113108
];
114109
}
115110
}

contexts/Authorization/Infrastructure/Records/RoleRecord.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ public function scopeSearch(Builder $query, array $criteria = [])
8080
$query->where('status', $criteria['status']);
8181
});
8282

83-
$query->when(isset($criteria['created_at_range']), function ($query) use ($criteria) {
84-
[$start, $end] = $criteria['created_at_range'];
83+
$query->when(isset($criteria['created_at']), function ($query) use ($criteria) {
84+
[$start, $end] = $criteria['created_at'];
8585
$query->whereBetween('created_at', [$start, $end]);
8686
});
8787

contexts/Authorization/Presentation/Requests/Role/GetRoleListRequest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public function rules(): array
1414
'id' => ['integer', 'gt:0'],
1515
'label' => ['string', 'max:255'],
1616
'status' => ['string', 'in:suspended,active'],
17-
'created_at_range' => ['array', 'size:2'],
18-
'created_at_range.*' => ['date'],
17+
'created_at' => ['array', 'size:2'],
18+
'created_at.*' => ['date_format:Y-m-d'],
1919
'current_page' => ['integer', 'gt:0'],
2020
'per_page' => ['integer', 'gt:0'],
2121
...$this->filtersRule(),

contexts/Authorization/Tests/Feature/Infrastructure/Persistence/RolePersistenceTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@
187187
$result = $rolePersistence->paginate(1, 10, ['label' => 'Django']);
188188
expect($result->total())->toBe(0);
189189

190-
// Test search by created_at_range criteria
190+
// Test search by created_at criteria
191191
$role4 = $this->roleFactory->create(
192192
RoleId::null(),
193193
'Past Role',
@@ -196,7 +196,7 @@
196196
$rolePersistence->create($role4);
197197

198198
$result = $rolePersistence->paginate(1, 10, [
199-
'created_at_range' => ['2021-01-01', '2021-01-02'],
199+
'created_at' => ['2021-01-01', '2021-01-02'],
200200
]);
201201

202202
expect($result->total())->toBe(1); // Should find the role created on 2021-01-01

contexts/Authorization/Tests/Feature/RoleTest.php

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
$response->assertStatus(201);
2727
});
2828

29-
it('can get a role', function () {
29+
it('can get a role via api', function () {
3030
$response = $this->postJson('roles', [
3131
'label' => 'My Role',
3232
'status' => 'active',
@@ -48,13 +48,13 @@
4848
]);
4949
});
5050

51-
it('can not get a role that does not exist', function () {
51+
it('can not get a role that does not exist via api', function () {
5252
$response = $this->get('roles/1');
5353

5454
$response->assertStatus(404);
5555
});
5656

57-
it('can get a list of roles', function () {
57+
it('can get a list of roles via api', function () {
5858
$response = $this->postJson('roles', [
5959
'label' => 'My Role',
6060
'status' => 'active',
@@ -67,7 +67,7 @@
6767
$response->assertStatus(200);
6868
});
6969

70-
it('can get a list of roles with sorting', function () {
70+
it('can get a list of roles with sorting via api', function () {
7171
$initialCount = RoleRecord::count();
7272

7373
RoleRecord::factory(3)->create();
@@ -82,7 +82,7 @@
8282
expect($responseIds)->toBe($sortedIds);
8383
});
8484

85-
it('can search for roles', function () {
85+
it('can search for roles via api', function () {
8686
RoleRecord::factory()->create([
8787
'label' => 'My Role',
8888
'status' => RoleRecord::mapStatusToRecord(RoleStatus::active()),
@@ -114,10 +114,61 @@
114114
],
115115
],
116116
]);
117+
});
118+
119+
it('can search for roles with created_at via api', function () {
120+
RoleRecord::factory(3)->create([
121+
'status' => RoleRecord::mapStatusToRecord(RoleStatus::active()),
122+
'created_at' => now()->subDays(7),
123+
]);
124+
RoleRecord::factory()->create([
125+
'label' => 'Role1',
126+
'status' => RoleRecord::mapStatusToRecord(RoleStatus::active()),
127+
'created_at' => now()->subDays(5),
128+
]);
129+
RoleRecord::factory()->create([
130+
'label' => 'Role2',
131+
'status' => RoleRecord::mapStatusToRecord(RoleStatus::active()),
132+
'created_at' => now()->subDays(2),
133+
]);
134+
135+
RoleRecord::factory(3)->create([
136+
'status' => RoleRecord::mapStatusToRecord(RoleStatus::active()),
137+
'created_at' => now()->subDays(1),
138+
]);
139+
140+
$response = $this->get('roles?filters=[{"id":"created_at","value":["'.now()->subDays(5)->format('Y-m-d').'","'.now()->subDays(1)->format('Y-m-d').'"]}]');
141+
142+
$response->assertStatus(200);
143+
$response->assertJsonCount(2, 'data');
144+
$response->assertJson([
145+
'data' => [
146+
[
147+
'label' => 'Role2',
148+
],
149+
[
150+
'label' => 'Role1',
151+
],
152+
],
153+
]);
117154

155+
$response = $this->get('roles?created_at[]='.now()->subDays(5)->format('Y-m-d').'&created_at[]='.now()->subDays(1)->format('Y-m-d'));
156+
157+
$response->assertStatus(200);
158+
$response->assertJsonCount(2, 'data');
159+
$response->assertJson([
160+
'data' => [
161+
[
162+
'label' => 'Role2',
163+
],
164+
[
165+
'label' => 'Role1',
166+
],
167+
],
168+
]);
118169
});
119170

120-
it('can update a role', function () {
171+
it('can update a role via api', function () {
121172
$response = $this->postJson('roles', [
122173
'label' => 'My Role',
123174
'status' => 'active',
@@ -141,7 +192,7 @@
141192
]);
142193
});
143194

144-
it('can suspend a role', function () {
195+
it('can suspend a role via api', function () {
145196
$response = $this->postJson('roles', [
146197
'label' => 'My Role',
147198
'status' => 'active',
@@ -156,7 +207,7 @@
156207
$response->assertStatus(200);
157208
});
158209

159-
it('can delete a role', function () {
210+
it('can delete a role via api', function () {
160211
$response = $this->postJson('roles', [
161212
'label' => 'My Role',
162213
'status' => 'active',

0 commit comments

Comments
 (0)