Skip to content

Commit

Permalink
#232 - delete benefits report (#366)
Browse files Browse the repository at this point in the history
* #232 - wip

* #232 - wip

* #354 - cr fixes

* #232 - fix

* #232 - fix
  • Loading branch information
EwelinaSkrzypacz authored Oct 26, 2023
1 parent 8e3e751 commit e3dcec1
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 19 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ test:

fix:
@docker compose --file ${DOCKER_COMPOSE_FILE} exec --user "${CURRENT_USER_ID}:${CURRENT_USER_GROUP_ID}" ${DOCKER_COMPOSE_APP_CONTAINER} bash -c 'composer csf'
@docker compose --file ${DOCKER_COMPOSE_FILE} exec --user "${CURRENT_USER_ID}:${CURRENT_USER_GROUP_ID}" ${DOCKER_COMPOSE_APP_CONTAINER} npm run lintf

queue:
@docker compose --file ${DOCKER_COMPOSE_FILE} exec --user "${CURRENT_USER_ID}:${CURRENT_USER_GROUP_ID}" ${DOCKER_COMPOSE_APP_CONTAINER} php artisan queue:work
Expand Down
2 changes: 1 addition & 1 deletion app/Domain/UserBenefitsRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function getAssignedBenefits(User $user): array
{
/* @var BenefitsReport $assignedBenefits */
$assignedBenefits = BenefitsReport::query()
->whereKey(1)
->withoutGlobalScope("withoutAssignedBenefitReport")
->first();

$allBenefits = Benefit::query()->get();
Expand Down
5 changes: 5 additions & 0 deletions app/Eloquent/Models/BenefitsReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public function scopeSearch(Builder $query, ?string $text): Builder
->where("name", "ILIKE", "%{$text}%");
}

protected static function booted(): void
{
static::addGlobalScope("withoutAssignedBenefitReport", fn(Builder $builder): Builder => $builder->whereNotNull("committed_at"));
}

protected static function newFactory(): BenefitsReportFactory
{
return BenefitsReportFactory::new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function index(): Response
->get();

$assignedBenefits = BenefitsReport::query()
->whereKey(1)
->withoutGlobalScope("withoutAssignedBenefitReport")
->first();

return inertia("AssignedBenefits/AssignedBenefits", [
Expand Down Expand Up @@ -65,7 +65,7 @@ public function update(AssignedBenefitsRequest $request): RedirectResponse

/** @var BenefitsReport $assignedBenefits */
$assignedBenefits = BenefitsReport::query()
->whereKey(1)
->withoutGlobalScope("withoutAssignedBenefitReport")
->first();

$data = Arr::where(
Expand Down
16 changes: 14 additions & 2 deletions app/Infrastructure/Http/Controllers/BenefitsReportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public function index(Request $request): Response
$benefitsReports = BenefitsReport::query()
->search($searchText)
->orderBy("committed_at", "desc")
->whereKeyNot(1)
->paginate()
->withQueryString();

Expand Down Expand Up @@ -80,7 +79,7 @@ public function store(BenefitsReportRequest $request): RedirectResponse

/** @var BenefitsReport $assignedBenefits */
$assignedBenefits = BenefitsReport::query()
->whereKey(1)
->withoutGlobalScope("withoutAssignedBenefitReport")
->first();

$data = $users->map(fn(User $user): array => [
Expand Down Expand Up @@ -139,4 +138,17 @@ public function download(Request $request, BenefitsReport $benefitsReport): Bina

return Excel::download($timesheet, $filename);
}

public function destroy(BenefitsReport $benefitsReport): RedirectResponse
{
$this->authorize("manageBenefits");

$benefitsReport->delete();

return redirect()
->back()
->with("success", __("Benefits report :name deleted.", [
"name" => $benefitsReport->name,
]));
}
}
1 change: 1 addition & 0 deletions lang/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
"Benefit :name created.": "Benefit :name utworzony.",
"Benefit :name deleted.": "Benefit :name usunięty.",
"Benefits report :name created.": "Raport :name utworzony.",
"Benefits report :name deleted.": "Raport :name usunięty.",
":sender leaves key no :key in the office": ":sender zostawia klucz nr :key w biurze",
":recipient takes key no :key from the office": ":recipient zabiera klucz nr :key z biura",
"today": "dzisiaj",
Expand Down
57 changes: 45 additions & 12 deletions resources/js/Pages/BenefitsReport/Index.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<script setup>
import { ChevronRightIcon } from '@heroicons/vue/24/solid'
import { EllipsisVerticalIcon, TrashIcon } from '@heroicons/vue/24/solid'
import Pagination from '@/Shared/Pagination.vue'
import EmptyState from '@/Shared/Feedbacks/EmptyState.vue'
import { reactive, watch } from 'vue'
import { MagnifyingGlassIcon } from '@heroicons/vue/24/outline'
import { debounce } from 'lodash'
import { Inertia } from '@inertiajs/inertia'
import { Menu, MenuButton, MenuItem, MenuItems } from '@headlessui/vue'
const props = defineProps({
benefitsReports: Object,
Expand Down Expand Up @@ -71,12 +72,9 @@ watch(form, debounce(() => {
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-100">
<InertiaLink
<tr
v-for="benefitsReport in benefitsReports.data"
:key="benefitsReport.id"
:href="`/benefits-reports/${benefitsReport.id}`"
as="tr"
class="relative hover:bg-blumilk-25"
>
<td class="p-4 text-sm text-gray-500 whitespace-nowrap">
<InertiaLink
Expand All @@ -89,15 +87,50 @@ watch(form, debounce(() => {
<td class="p-4 text-sm text-gray-500 whitespace-nowrap">
{{ benefitsReport.createdAt }}
</td>
<td class="p-4 text-sm text-gray-500 whitespace-nowrap">
<InertiaLink
:href="`/benefits-reports/${benefitsReport.id}`"
class="flex justify-around focus:outline-blumilk-500"
<td class="p-4 text-sm text-right text-gray-500 whitespace-nowrap">
<Menu
as="div"
class="inline-block relative text-left"
>
<ChevronRightIcon class="block w-6 h-6 fill-blumilk-500" />
</InertiaLink>
<MenuButton
class="flex items-center text-gray-400 hover:text-gray-600 rounded-full focus:outline-none focus:ring-2 focus:ring-blumilk-500 focus:ring-offset-2 focus:ring-offset-gray-100"
>
<EllipsisVerticalIcon class="w-5 h-5" />
</MenuButton>

<transition
enter-active-class="transition ease-out duration-100"
enter-from-class="transform opacity-0 scale-95"
enter-to-class="transform opacity-100 scale-100"
leave-active-class="transition ease-in duration-75"
leave-from-class="transform opacity-100 scale-100"
leave-to-class="transform opacity-0 scale-95"
>
<MenuItems
class="absolute right-0 z-10 mt-2 w-56 bg-white rounded-md focus:outline-none ring-1 ring-black ring-opacity-5 shadow-lg origin-top-right"
>
<div class="py-1">
<MenuItem
v-slot="{ active }"
class="flex"
>
<InertiaLink
as="button"
method="delete"
preserve-scroll
:href="`/benefits-reports/${benefitsReport.id}`"
:class="[active ? 'bg-gray-100 text-gray-900' : 'text-gray-700', 'block w-full text-left font-medium px-4 py-2 text-sm']"
>
<TrashIcon class="mr-2 w-5 h-5 text-red-500" />
Usuń
</InertiaLink>
</MenuItem>
</div>
</MenuItems>
</transition>
</Menu>
</td>
</InertiaLink>
</tr>
<tr v-if="! benefitsReports.data.length">
<td
colspan="100%"
Expand Down
4 changes: 4 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
Route::post("/benefits-reports", [BenefitsReportController::class, "store"])
->name("benefits-reports.store");

Route::delete("/benefits-reports/{benefitsReport}", [BenefitsReportController::class, "destroy"])
->name("benefits-reports.delete")
->whereNumber("report");

Route::get("/benefits-reports/{benefitsReport}", [BenefitsReportController::class, "show"])
->name("benefits-reports.show")
->whereNumber("report");
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/AssignedBenefitsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testAdminCanSeeAssignedBenefits(): void

/** @var BenefitsReport $assignedBenefits */
$assignedBenefits = BenefitsReport::query()
->whereKey(1)
->withoutGlobalScope("withoutAssignedBenefitReport")
->first();

$assignedBenefits->refresh();
Expand Down Expand Up @@ -54,7 +54,7 @@ public function testAdminCanEditAssignedBenefits(): void

/** @var BenefitsReport $assignedBenefits */
$assignedBenefits = BenefitsReport::query()
->whereKey(1)
->withoutGlobalScope("withoutAssignedBenefitReport")
->first();

$assignedBenefits->refresh();
Expand Down
33 changes: 33 additions & 0 deletions tests/Feature/BenefitsReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,37 @@ public function testAdminCanDownloadBenefitsReport(): void
->get("/benefits-reports/{$benefitsReport->id}/download?users[]={$firstUser->id}&users[]={$secondUser->id}")
->assertDownload($expectedFilename);
}

public function testAdminCanDeleteBenefitsReport(): void
{
$admin = User::factory()->admin()->create();

Benefit::factory(4)->create();
User::factory(4)->create();

/** @var BenefitsReport $benefitsReport */
$benefitsReport = BenefitsReport::factory()->create([
"name" => "Test BenefitsReport",
]);

$this->actingAs($admin)
->delete("/benefits-reports/{$benefitsReport->id}")
->assertRedirect();
}

public function testAdminCannotDeleteBenefitsReportWithNullCommittedAtDate(): void
{
$admin = User::factory()->admin()->create();

Benefit::factory(4)->create();
User::factory(4)->create();

$firstBenefitReport = BenefitsReport::query()
->withoutGlobalScope("withoutAssignedBenefitReport")
->first();

$this->actingAs($admin)
->delete("/benefits-reports/{$firstBenefitReport->id}")
->assertStatus(404);
}
}

0 comments on commit e3dcec1

Please sign in to comment.