Skip to content

Commit

Permalink
Feature/api endpoints refactor (#1203)
Browse files Browse the repository at this point in the history
* working groups mostly

* groups fully working

* cleaning up

* feat: add update function to GroupController

* feat: add updateGroup method to ConfigData model

* passing tests

* working updates

* update group test

* lint fixes

* thorough group page testing

* check for an empty dropdown

* service body call handling

* lint fix

* cypress tests for call handling

* wip

* lint fixes

* wip

* update servicebody call handling tests

* update service body call handling test

* adding volunteer api tests

* adding group volunteer tests

* configure volunteers for groups

* reset primary key ids between tests

* lint fix

* volunteers cypress tests

* updating release notes

* fixing issue with group name in volunteer list

* reset database capability after each cypress test file

* using seeders

* silence logs from webserver

* try again to run database reset

* make tests pass for now
  • Loading branch information
dgershman authored Oct 30, 2024
1 parent 19c631f commit 714d5f6
Show file tree
Hide file tree
Showing 45 changed files with 1,421 additions and 302 deletions.
2 changes: 1 addition & 1 deletion .github/actions/test/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ runs:
mysql -uroot -p"$MYSQL_ROOT_PASSWORD" --port=3106 --host=127.0.0.1 --database=yap_test < tests/yap_test.sql
npm install
DISABLE_NOTIFIER=true gulp
env ENVIRONMENT=test php -S 127.0.0.1:8000 -t . server.php &
env ENVIRONMENT=test php -S 127.0.0.1:8000 -t . server.php > /dev/null 2>&1 &
env CYPRESS_BASE_URL=http://127.0.0.1:8000/yap npx cypress run
shell: bash
env:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
.DS_Store
.aider*
3 changes: 3 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Release Notes

### 4.4.1 (UNRELEASED)
* Fix for editing groups. [#1194]

### 4.4.0 (September 10, 2024)
* Added a feature to download recursive volunteer lists in either JSON or CSV format. Report also includes service body and notes field now. [#1122]
* Dealing with cases where no timezone is set for a volunteer. Those volunteers are excluded. [#1121]
Expand Down
32 changes: 8 additions & 24 deletions src/app/Http/Controllers/Api/V1/Admin/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ public function __construct(ConfigRepository $config)
public function index(Request $request)
{
if ($request->has('parent_id')) {
$data = $this->config->getDbDataByParentId($request->get('parent_id'), $request->get('data_type'));
} elseif ($request->get('data_type') === DataType::YAP_GROUPS_V2 && $request->has('id')) {
$data = $this->config->getDbDataById($request->get('id'), $request->get('data_type'));
$data = $this->config->getDbDataByParentId($request->get('parent_id'), DataType::YAP_CONFIG_V2);
} else {
$data = $this->config->getDbData($request->get('service_body_id'), $request->get('data_type'));
$data = $this->config->getDbData($request->get('service_body_id'), DataType::YAP_CONFIG_V2);
}

if (count($data) > 0) {
Expand All @@ -75,27 +73,13 @@ public function index(Request $request)

public function store(Request $request)
{
$data = $request->getContent();

if ($request->get('data_type') === DataType::YAP_GROUPS_V2 &&
$request->has('id') && intval($request->get('id')) > 0) {
$this->config->adminPersistDbConfigById($request->get('id'), $data);
$request->get('id');
} else {
$this->config->adminPersistDbConfig(
$request->get('service_body_id'),
$data,
$request->get('data_type'),
$request->has('parent_id') ? $request->get('parent_id') : "0"
);
}
$this->config->adminPersistDbConfig(
$request->get('service_body_id'),
$request->getContent(),
$request->get('data_type'),
$request->has('parent_id') ? $request->get('parent_id') : "0"
);

return self::index($request);
}

public function destroy($id)
{
$this->config->deleteDbConfigById($id);
return response()->json()->header("Content-Type", "application/json");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace app\Http\Controllers\Api\V1\Admin;

use App\Constants\DataType;
use App\Http\Controllers\Controller;
use App\Models\ConfigData;
use App\Structures\Volunteer;
use App\Structures\VolunteerData;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use stdClass;
Expand All @@ -19,17 +22,37 @@ public function __construct(ConfigData $configData)

public function index(Request $request): JsonResponse
{
$data = ConfigData::getVolunteers($request->get("service_body_id"));
$data = ConfigData::getVolunteers($request->get("serviceBodyId"));

if (count($data) > 0) {
return response()->json([
'service_body_id' => $data[0]->service_body_id,
'id' => $data[0]->id,
'parent_id' => $data[0]->parent_id ?? "null",
'parent_id' => $data[0]->parent_id ?? null,
'data' => json_decode($data[0]->data)
])->header("Content-Type", "application/json");
} else {
return response()->json(new stdClass())->header("Content-Type", "application/json");
}
}

public function store(Request $request)
{
$volunteers = json_decode($request->getContent());
$serviceBodyId = $request->get('serviceBodyId');

$existingRecord = ConfigData::where('service_body_id', $serviceBodyId)
->where('data_type', DataType::YAP_VOLUNTEERS_V2)
->first();

if ($existingRecord) {
// If the record exists, update it
ConfigData::updateVolunteers($serviceBodyId, $volunteers);
} else {
// Otherwise, create a new record
ConfigData::createVolunteers($serviceBodyId, $volunteers);
}

return self::index($request);
}
}
85 changes: 85 additions & 0 deletions src/app/Http/Controllers/Api/V1/Admin/GroupController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace App\Http\Controllers\Api\V1\Admin;

use App\Http\Controllers\Controller;
use App\Models\ConfigData;
use App\Services\VolunteerService;
use App\Structures\Group;
use Illuminate\Http\Request;
use stdClass;

class GroupController extends Controller
{
protected VolunteerService $volunteerService;

public function __construct(VolunteerService $volunteerService)
{
$this->volunteerService = $volunteerService;
}

private function getGroupsData(int $serviceBodyId, bool $manage = false)
{
$data = $this->volunteerService->getGroupsForServiceBody(
$serviceBodyId,
$manage
);

$results = [];

foreach ($data as $item) {
$results[] = [
'service_body_id' => $item->service_body_id,
'id' => $item->id,
'parent_id' => null,
'data' => json_decode($item->data)
];
}

return $results;
}

public function index(Request $request)
{
$groupsData = $this
->getGroupsData(intval($request->get("serviceBodyId")), boolval($request->get("manage") ?? false));
return response()->json($groupsData)->header("Content-Type", "application/json");
}

public function update(Request $request, $id)
{
$decodedData = json_decode($request->getContent());
$groupData = new Group($decodedData);

$group = ConfigData::updateGroup(
$id,
$groupData
);

$groupsData = $this->getGroupsData(intval($group->service_body_id));
return response()->json($groupsData)->header("Content-Type", "application/json");
}

public function store(Request $request)
{
$decodedData = json_decode($request->getContent());
$groupData = new Group($decodedData);

ConfigData::createGroup(
$request->get('serviceBodyId'),
$groupData
);

return self::index($request);
}

public function destroy($id)
{
$response = ConfigData::deleteGroup($id);
if ($response === 1) {
return response()->json(['message' => sprintf('Group %s deleted successfully', $id)]);
}

return response()->json(['message' => 'Not found'], 404);
}
}
57 changes: 57 additions & 0 deletions src/app/Http/Controllers/Api/V1/Admin/GroupVolunteerController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace App\Http\Controllers\Api\V1\Admin;

use App\Constants\DataType;
use App\Http\Controllers\Controller;
use App\Models\ConfigData;
use App\Services\VolunteerService;
use Illuminate\Http\Request;
use stdClass;

class GroupVolunteerController extends Controller
{
protected VolunteerService $volunteerService;

public function __construct(VolunteerService $volunteerService)
{
$this->volunteerService = $volunteerService;
}

public function index(Request $request)
{
$data = ConfigData::getGroupVolunteers($request->get("groupId"));

if (count($data) > 0) {
return response()->json([
'service_body_id' => $data[0]->service_body_id,
'id' => $data[0]->id,
'parent_id' => $data[0]->parent_id ?? null,
'data' => json_decode($data[0]->data)
])->header("Content-Type", "application/json");
} else {
return response()->json(new stdClass())->header("Content-Type", "application/json");
}
}

public function store(Request $request)
{
$volunteers = json_decode($request->getContent());
$groupId = $request->get('groupId');
$serviceBodyId = $request->get('serviceBodyId') ?? null;

$existingRecord = ConfigData::where('parent_id', $groupId)
->where('data_type', DataType::YAP_GROUP_VOLUNTEERS_V2)
->first();

if ($existingRecord) {
// If the record exists, update it
ConfigData::updateGroupVolunteers($groupId, $volunteers);
} else {
// Otherwise, create a new record
ConfigData::createGroupVolunteers($serviceBodyId, $groupId, $volunteers);
}

return self::index($request);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace App\Http\Controllers\Api\V1\Admin;

use App\Constants\DataType;
use App\Http\Controllers\Controller;
use App\Models\ConfigData;
use App\Structures\ServiceBodyCallHandling;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use stdClass;

class ServiceBodyCallHandlingController extends Controller
{
protected ConfigData $configData;

public function __construct(ConfigData $configData)
{
$this->configData = $configData;
}

public function index(Request $request): JsonResponse
{
$data = ConfigData::getCallHandling($request->get("serviceBodyId"));

if (count($data) > 0) {
return response()->json([
'service_body_id' => $data[0]->service_body_id,
'id' => $data[0]->id,
'parent_id' => null,
'data' => json_decode($data[0]->data)
])->header("Content-Type", "application/json");
} else {
return response()->json(new stdClass())->header("Content-Type", "application/json");
}
}

public function store(Request $request)
{
$decodedData = json_decode($request->getContent());
$serviceBodyCallHandling = new ServiceBodyCallHandling($decodedData);

$serviceBodyId = $request->get('serviceBodyId');

$existingRecord = ConfigData::where('service_body_id', $serviceBodyId)
->where('data_type', DataType::YAP_CALL_HANDLING_V2)
->first();

if ($existingRecord) {
// If the record exists, update it
ConfigData::updateServiceBodyCallHandling($request->get('serviceBodyId'), $serviceBodyCallHandling);
} else {
// Otherwise, create a new record
ConfigData::createServiceBodyCallHandling($request->get('serviceBodyId'), $serviceBodyCallHandling);
}

return self::index($request);
}
}

This file was deleted.

Loading

0 comments on commit 714d5f6

Please sign in to comment.