diff --git a/.github/actions/test/action.yml b/.github/actions/test/action.yml index 73a93ba81..ac8ea24d3 100644 --- a/.github/actions/test/action.yml +++ b/.github/actions/test/action.yml @@ -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: diff --git a/.gitignore b/.gitignore index 090a1f02d..e674a38dd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea .DS_Store +.aider* diff --git a/RELEASENOTES.md b/RELEASENOTES.md index b8581b16f..6bd3c8340 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -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] diff --git a/src/app/Http/Controllers/Api/V1/Admin/ConfigController.php b/src/app/Http/Controllers/Api/V1/Admin/ConfigController.php index 2c7483034..4f7efeb74 100644 --- a/src/app/Http/Controllers/Api/V1/Admin/ConfigController.php +++ b/src/app/Http/Controllers/Api/V1/Admin/ConfigController.php @@ -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) { @@ -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"); - } } diff --git a/src/app/Http/Controllers/Api/V1/Admin/ConfigureVolunteersController.php b/src/app/Http/Controllers/Api/V1/Admin/ConfigureVolunteersController.php index b8b749784..b73c8c7d4 100644 --- a/src/app/Http/Controllers/Api/V1/Admin/ConfigureVolunteersController.php +++ b/src/app/Http/Controllers/Api/V1/Admin/ConfigureVolunteersController.php @@ -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; @@ -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); + } } diff --git a/src/app/Http/Controllers/Api/V1/Admin/GroupController.php b/src/app/Http/Controllers/Api/V1/Admin/GroupController.php new file mode 100644 index 000000000..39661b7a6 --- /dev/null +++ b/src/app/Http/Controllers/Api/V1/Admin/GroupController.php @@ -0,0 +1,85 @@ +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); + } +} diff --git a/src/app/Http/Controllers/Api/V1/Admin/GroupVolunteerController.php b/src/app/Http/Controllers/Api/V1/Admin/GroupVolunteerController.php new file mode 100644 index 000000000..5583d0a7c --- /dev/null +++ b/src/app/Http/Controllers/Api/V1/Admin/GroupVolunteerController.php @@ -0,0 +1,57 @@ +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); + } +} diff --git a/src/app/Http/Controllers/Api/V1/Admin/ServiceBodyCallHandlingController.php b/src/app/Http/Controllers/Api/V1/Admin/ServiceBodyCallHandlingController.php new file mode 100644 index 000000000..57a0d8e0c --- /dev/null +++ b/src/app/Http/Controllers/Api/V1/Admin/ServiceBodyCallHandlingController.php @@ -0,0 +1,59 @@ +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); + } +} diff --git a/src/app/Http/Controllers/Api/V1/Admin/VolunteerGroupsController.php b/src/app/Http/Controllers/Api/V1/Admin/VolunteerGroupsController.php deleted file mode 100644 index a6ff5117e..000000000 --- a/src/app/Http/Controllers/Api/V1/Admin/VolunteerGroupsController.php +++ /dev/null @@ -1,27 +0,0 @@ -volunteerService = $volunteerService; - } - - public function index(Request $request) - { - return response() - ->json($this->volunteerService->getGroupsForServiceBody( - $request->get("service_body_id"), - $request->get("manage") - )) - ->header("Content-Type", "application/json"); - } -} diff --git a/src/app/Models/ConfigData.php b/src/app/Models/ConfigData.php index c7ec2014e..a82083a6f 100644 --- a/src/app/Models/ConfigData.php +++ b/src/app/Models/ConfigData.php @@ -4,7 +4,7 @@ use App\Constants\DataType; use App\Constants\Status; -use App\Structures\GroupData; +use App\Structures\Group; use App\Structures\ServiceBodyCallHandling; use App\Structures\VolunteerData; use Illuminate\Database\Eloquent\Collection; @@ -17,14 +17,13 @@ class ConfigData extends Model public $timestamps = false; protected $fillable = ["service_body_id", "data", "data_type", "parent_id", "status"]; - public static function createCallHandling( + public static function createServiceBodyCallHandling( int $serviceBodyId, - int $parentServiceBodyId, ServiceBodyCallHandling $serviceBodyCallHandlingData ) : void { self::create([ "service_body_id"=>$serviceBodyId, - "parent_id"=>$parentServiceBodyId, + "parent_id"=>0, "data"=>json_encode([$serviceBodyCallHandlingData]), "data_type"=>DataType::YAP_CALL_HANDLING_V2 ]); @@ -43,14 +42,22 @@ public static function createServiceBodyConfiguration( ]); } + public static function updateServiceBodyCallHandling( + int $serviceBodyId, + ServiceBodyCallHandling $volunteerDataArray + ) : void { + self::where('service_body_id', $serviceBodyId) + ->where('data_type', DataType::YAP_CALL_HANDLING_V2) + ->update(['data' => json_encode([$volunteerDataArray])]); + } + public static function createGroup( int $serviceBodyId, - int $parentServiceBodyId, - object $serviceBodyConfiguration + Group $serviceBodyConfiguration ) : int { return self::create([ "service_body_id"=>$serviceBodyId, - "parent_id"=>$parentServiceBodyId, + "parent_id"=>null, "data"=>json_encode([$serviceBodyConfiguration]), "data_type"=>DataType::YAP_GROUPS_V2 ])->id; @@ -72,16 +79,35 @@ public static function addGroupToVolunteers( public static function createGroupVolunteers( int $serviceBodyId, int $groupId, - VolunteerData $volunteerConfiguration + array $volunteerDataArray ) : void { self::create([ "service_body_id"=>$serviceBodyId, "parent_id"=>$groupId, - "data"=>json_encode([$volunteerConfiguration]), + "data"=>json_encode($volunteerDataArray), "data_type"=>DataType::YAP_GROUP_VOLUNTEERS_V2 ]); } + public static function updateGroupVolunteers( + int $groupId, + array $volunteerDataArray + ) : void { + self::where('parent_id', $groupId) + ->where('data_type', DataType::YAP_GROUP_VOLUNTEERS_V2) + ->update(['data' => json_encode($volunteerDataArray)]); + } + + public static function getGroupVolunteers( + int $groupId + ) : Collection { + return ConfigData::select(['data','service_body_id','id','parent_id']) + ->where('parent_id', $groupId) + ->where('data_type', DataType::YAP_GROUP_VOLUNTEERS_V2) + ->whereRaw('IFNULL(`status`, 0) <> ?', [Status::DELETED]) + ->get(); + } + public static function createVolunteer( int $serviceBodyId, int $parentServiceBodyId, @@ -124,14 +150,47 @@ public static function getCallHandling(int $serviceBodyId): Collection public static function createVolunteers( int $serviceBodyId, - int $parentServiceBodyId, array $volunteerDataArray ) : void { self::create([ "service_body_id"=>$serviceBodyId, - "parent_id"=>$parentServiceBodyId, + "parent_id"=>0, "data"=>json_encode($volunteerDataArray), "data_type"=>DataType::YAP_VOLUNTEERS_V2 ]); } + + public static function updateVolunteers( + int $serviceBodyId, + array $volunteerDataArray + ) : void { + self::where('service_body_id', $serviceBodyId) + ->where('data_type', DataType::YAP_VOLUNTEERS_V2) + ->update(['data' => json_encode($volunteerDataArray)]); + } + + public static function deleteGroup($id) : int + { + $group = self::select(['id']) + ->where('id', $id) + ->where('data_type', DataType::YAP_GROUPS_V2); + + if ($group) { + return $group->delete(); + } + + return false; + } + + public static function updateGroup($id, Group $groupData) : ConfigData + { + self::where('id', $id) + ->where('data_type', DataType::YAP_GROUPS_V2) + ->update(['data' => json_encode([$groupData])]); + + return self::select(['service_body_id']) + ->where('id', $id) + ->where('data_type', DataType::YAP_GROUPS_V2) + ->first(); + } } diff --git a/src/app/Repositories/ConfigRepository.php b/src/app/Repositories/ConfigRepository.php index 4fb55cb20..ca7bfd81c 100644 --- a/src/app/Repositories/ConfigRepository.php +++ b/src/app/Repositories/ConfigRepository.php @@ -16,15 +16,6 @@ public function getDbDataByParentId($parent_id, $data_type): array ); } - public function getDbDataById($id, $data_type): array - { - return DB::select( - 'SELECT `data`,`service_body_id`,`id`,`parent_id` - FROM `config` WHERE `id`=? AND `data_type`=? AND IFNULL(`status`,0)<>?', - [$id, $data_type, Status::DELETED] - ); - } - public function getDbData($service_body_id, $data_type): array { return DB::select( @@ -43,18 +34,6 @@ public function getAllDbData($data_type): array ); } - public function adminPersistDbConfigById($id, $data) - { - return DB::update("UPDATE `config` SET `data`=? WHERE `id`=?", [ - $data, $id - ]); - } - - public function deleteDbConfigById($id) - { - return DB::update("UPDATE `config` SET `status`=1 WHERE `id`=?", [$id]); - } - public function adminPersistDbConfig($service_body_id, $data, $data_type, $parent_id = 0) { $current_data_check = (isset($parent_id) && $parent_id > 0 diff --git a/src/app/Services/SettingsService.php b/src/app/Services/SettingsService.php index 8744361f2..6729ee19d 100644 --- a/src/app/Services/SettingsService.php +++ b/src/app/Services/SettingsService.php @@ -11,7 +11,7 @@ class SettingsService { - private string $version = "4.4.0"; + private string $version = "4.4.1"; private array $allowlist = [ 'announce_servicebody_volunteer_routing' => ['description' => '/helpline/announce_servicebody_volunteer_routing' , 'default' => false, 'overridable' => true, 'hidden' => false], 'blocklist' => ['description' => '/general/blocklist' , 'default' => '', 'overridable' => true, 'hidden' => false], diff --git a/src/app/Services/VolunteerService.php b/src/app/Services/VolunteerService.php index 724ade2c7..b17bf69ba 100644 --- a/src/app/Services/VolunteerService.php +++ b/src/app/Services/VolunteerService.php @@ -207,7 +207,7 @@ public function getGroupsForServiceBody($service_body_id, $manage = false) return $final_groups; } - private function getGroupVolunteers($group_id) + public function getGroupVolunteers($group_id) { $groupData = $this->configRepository->getDbDataByParentId($group_id, DataType::YAP_GROUP_VOLUNTEERS_V2); return isset($groupData[0]->data) ? json_decode($groupData[0]->data) : array(); diff --git a/src/app/Structures/Group.php b/src/app/Structures/Group.php new file mode 100644 index 000000000..5e8a97507 --- /dev/null +++ b/src/app/Structures/Group.php @@ -0,0 +1,23 @@ + $value) { + $this->$property = $value; + } + } else { + // Optionally, set default values here + $this->group_name = ""; + $this->group_shared_service_bodies = []; + } + } +} diff --git a/src/app/Structures/ServiceBodyCallHandling.php b/src/app/Structures/ServiceBodyCallHandling.php index 372ec8fc1..9e049dc2b 100644 --- a/src/app/Structures/ServiceBodyCallHandling.php +++ b/src/app/Structures/ServiceBodyCallHandling.php @@ -5,7 +5,7 @@ use App\Constants\CycleAlgorithm; use App\Constants\SpecialPhoneNumber; -class ServiceBodyCallHandling +class ServiceBodyCallHandling extends Structure { public $service_body_id; public $service_body_name; @@ -24,7 +24,7 @@ class ServiceBodyCallHandling public $gender_routing_enabled = false; public $call_strategy = CycleAlgorithm::LINEAR_LOOP_FOREVER; public $primary_contact_number_enabled = false; - public $primary_contact; // TODO: need to kill this off + public $primary_contact; public $primary_contact_number = SpecialPhoneNumber::UNKNOWN; public $primary_contact_email_enabled = false; public $primary_contact_email; @@ -34,4 +34,14 @@ class ServiceBodyCallHandling public $sms_strategy = CycleAlgorithm::RANDOM_LOOP_FOREVER; public $override_en_US_greeting; public $override_en_US_voicemail_greeting; + + public function __construct($serviceBodyCallHandling = null) + { + if ($serviceBodyCallHandling) { + // Dynamically assign all properties from the passed group object + foreach (get_object_vars($serviceBodyCallHandling) as $property => $value) { + $this->$property = $value; + } + } + } } diff --git a/src/app/Structures/Structure.php b/src/app/Structures/Structure.php new file mode 100644 index 000000000..04c492561 --- /dev/null +++ b/src/app/Structures/Structure.php @@ -0,0 +1,11 @@ + $value) { + $this->$property = $value; + } + } else { + // Optionally, set default values here + $this->volunteer_name = ""; + $this->volunteer_phone_number = ""; + $this->volunteer_responder = false; + $this->volunteer_languages = []; + $this->volunteer_notes = ""; + $this->volunteer_enabled = false; + $this->volunteer_shift_schedule = ""; + } + } + public function get247Schedule() { $shiftTz = "America/New_York"; diff --git a/src/cypress.config.js b/src/cypress.config.js index 97f47c412..71066d687 100644 --- a/src/cypress.config.js +++ b/src/cypress.config.js @@ -6,4 +6,7 @@ module.exports = defineConfig({ // implement node event listeners here }, }, + env: { + apiUrl: 'http://localhost:8000/api' + } }); diff --git a/src/cypress/e2e/authentication/login.cy.js b/src/cypress/e2e/authentication/login.cy.js index 15b3b648b..b08586b92 100644 --- a/src/cypress/e2e/authentication/login.cy.js +++ b/src/cypress/e2e/authentication/login.cy.js @@ -1,7 +1,7 @@ describe('Login', () => { - beforeEach(() => { - + before(() => { + cy.resetDatabase(); }); it('Login with BMLT creds', () => { diff --git a/src/cypress/e2e/callHandling/callHandling.cy.js b/src/cypress/e2e/callHandling/callHandling.cy.js new file mode 100644 index 000000000..57a0421f0 --- /dev/null +++ b/src/cypress/e2e/callHandling/callHandling.cy.js @@ -0,0 +1,71 @@ +describe('Service Body Call Handling', () => { + + before(() => { + cy.resetDatabase(); + }); + + it('Save service body', () => { + cy + .login() + .get('.navbar-nav') + .contains('Service Bodies') + .click() + .get('#service-bodies-table tr:contains(\'Brooklyn\')') + .should('contain', 'Brooklyn Area Service (1006)') + .get("[onclick=\"openServiceBodyCallHandling(1005);\"]") + .click() + .wait(1000) + .get("#serviceBodyCallHandling_1005 > .modal-dialog > .modal-content > .modal-body > #serviceBodyCallHandlingForm > :nth-child(4) > #forced_caller_id") + .focus() + .clear() + .invoke("val", "123") + .wait(500) + .get("#serviceBodyCallHandling_1005 > .modal-dialog > .modal-content > .modal-footer > .btn-primary") + .click() + + cy + .login() + .get('.navbar-nav') + .contains('Service Bodies') + .click() + .get("[onclick=\"openServiceBodyCallHandling(1005);\"]") + .click() + .wait(1000) + .get("#serviceBodyCallHandling_1005 > .modal-dialog > .modal-content > .modal-body > #serviceBodyCallHandlingForm > :nth-child(4) > #forced_caller_id") + .focus() + .should('have.value', '123'); + }) + + + it('Update service body', () => { + cy + .login() + .get('.navbar-nav') + .contains('Service Bodies') + .click() + .get('#service-bodies-table tr:contains(\'Brooklyn\')') + .should('contain', 'Brooklyn Area Service (1006)') + .get("[onclick=\"openServiceBodyCallHandling(1005);\"]") + .click() + .wait(1000) + .get("#serviceBodyCallHandling_1005 > .modal-dialog > .modal-content > .modal-body > #serviceBodyCallHandlingForm > :nth-child(4) > #forced_caller_id") + .focus() + .clear() + .invoke("val", "123abc") + .wait(500) + .get("#serviceBodyCallHandling_1005 > .modal-dialog > .modal-content > .modal-footer > .btn-primary") + .click() + + cy + .login() + .get('.navbar-nav') + .contains('Service Bodies') + .click() + .get("[onclick=\"openServiceBodyCallHandling(1005);\"]") + .click() + .wait(1000) + .get("#serviceBodyCallHandling_1005 > .modal-dialog > .modal-content > .modal-body > #serviceBodyCallHandlingForm > :nth-child(4) > #forced_caller_id") + .focus() + .should('have.value', '123abc'); + }) +}) diff --git a/src/cypress/e2e/groups/groups.cy.js b/src/cypress/e2e/groups/groups.cy.js new file mode 100644 index 000000000..a4c9a240d --- /dev/null +++ b/src/cypress/e2e/groups/groups.cy.js @@ -0,0 +1,195 @@ +describe('Groups', () => { + + before(() => { + cy.resetDatabase(); + }); + + it('Add a new group', () => { + cy + .login() + .get('.navbar-nav') + .contains('Groups') + .click() + .get('#service_body_id option:selected') + .should('have.text', '-= Select A Service Body =-') + + cy.get('#service_body_id') + .select(1) + + cy.get("#addGroupButton").click() + + cy.get("#group_name").type("testgroup1"); + + cy.wait(500); + + // Click to close the modal + cy.get('#addGroupDialog > .modal-dialog > .modal-content > .modal-footer > .btn-primary').click(); + + // Use a wait to ensure transition ends (if necessary) + cy.wait(500); // Adjust time based on your modal’s transition duration + + // Ensure the modal is hidden + cy.get('#addGroupDialog').should('not.be.visible'); + }) + + it('Edit a group', () => { + cy + .login() + .get('.navbar-nav') + .contains('Groups') + .click() + .get('#service_body_id option:selected') + .should('have.text', '-= Select A Service Body =-') + + cy.get('#service_body_id') + .select(1) + + cy.get('#group_id') + .select(1) + + cy.get("#editGroupButton").click() + + cy.get("#group_name") + .clear() + .invoke("val", "testgroup1-modified"); + + cy.wait(500); + + // Click to close the modal + cy.get('#addGroupDialog > .modal-dialog > .modal-content > .modal-footer > .btn-primary').click(); + + // Use a wait to ensure transition ends (if necessary) + cy.wait(500); // Adjust time based on your modal’s transition duration + + // Ensure the modal is hidden + cy + .get('#addGroupDialog') + .should('not.be.visible') + .wait(1000) + .get("#group_id") + .select(1) + .get('#group_id option:selected') + .should('have.text', 'testgroup1-modified'); + }) + + it('Add volunteers to a group', () => { + cy + .login() + .get('.navbar-nav') + .contains('Groups') + .click() + .get('#service_body_id option:selected') + .should('have.text', '-= Select A Service Body =-') + + cy.get('#service_body_id') + .select(1) + + cy.get('#group_id') + .select(1) + + cy + .get("#add-volunteer").click() + + cy.wait(500); + + cy + .get('#volunteerCard_1 > #volunteersForm > .card-header > .form-group > .volunteer-name-text > #volunteer_name') + .invoke('val', 'danny g') + .get('#volunteerCard_1 > #volunteersForm > .card-header > .form-group > .expand-button > .btn') + .click() + .wait(500) + .get('#volunteerCard_1 > #volunteersForm > .card-body > :nth-child(1) > #volunteer_phone_number') + .invoke('val', '9735558811') + .get('#volunteerCard_1 > #volunteersForm > .card-footer > #volunteerCardFooter > .form-check > #volunteer_enabled') + .click() + .get('#save-volunteers') + .click() + + cy.wait(2000) + }) + + it('Get volunteers from a group', () => { + cy + .login() + .get('.navbar-nav') + .contains('Groups') + .click() + .get('#service_body_id option:selected') + .should('have.text', '-= Select A Service Body =-') + + cy.get('#service_body_id') + .select(1) + + cy.get('#group_id') + .select(1) + + cy.wait(1000); + + cy.get('#volunteerCard_1 > #volunteersForm > .card-header > .form-group > .volunteer-name-text > #volunteer_name') + .should('have.value', 'danny g') + }) + + it('Add group to volunteers', () => { + cy + .login() + .get('.navbar-nav') + .contains('Service Bodies') + .click() + .get('#service-bodies-table tr:contains(\'Brooklyn\')') + .should('contain', 'Brooklyn Area Service (1006)') + .get("[onclick=\"openServiceBodyCallHandling(1005);\"]") + .click() + .wait(1000) + .get('#serviceBodyCallHandling_1005 > .modal-dialog > .modal-content > .modal-body > #serviceBodyCallHandlingForm > #volunteer_routing') + .select('Volunteers') + .wait(500) + .get("#serviceBodyCallHandling_1005 > .modal-dialog > .modal-content > .modal-footer > .btn-primary") + .click() + .wait(1000) + + cy + .login() + .get('.navbar-nav') + .contains('Volunteers') + .click() + .wait(1000) + .get("#include-group") + .click() + .wait(1000) + .get("#selected_group_id") + .select(1) + .get('#includeGroupDialog > .modal-dialog > .modal-content > .modal-footer > .btn-primary') + .click() + .wait(1000) + .get('#volunteerCard_1 > #volunteersForm > .card-footer > #groupCardFooter > .form-check > #group_enabled') + .click() + .get('#volunteerCard_1 > #volunteersForm > .card-header > #group_name') + .should('has.text', 'testgroup1-modified') + }) + + it('Delete a group', () => { + cy + .login() + .get('.navbar-nav') + .contains('Groups') + .click() + .get('#service_body_id option:selected') + .should('have.text', '-= Select A Service Body =-') + + cy.get('#service_body_id') + .select(1) + + cy.get('#group_id') + .select(1) + + cy.get("#deleteGroupButton").click() + + cy.wait(500); + + cy.get('#service_body_id') + .select(1) + + cy.get("#group_id") + .should('be.empty'); + }) +}) diff --git a/src/cypress/e2e/menubar/menubar.cy.js b/src/cypress/e2e/menubar/menubar.cy.js index 2fd932144..1ea8f5ec8 100644 --- a/src/cypress/e2e/menubar/menubar.cy.js +++ b/src/cypress/e2e/menubar/menubar.cy.js @@ -1,7 +1,7 @@ describe('Menubar', () => { - beforeEach(() => { - + before(() => { + cy.resetDatabase(); }); it('Click Reports', () => { @@ -10,6 +10,7 @@ describe('Menubar', () => { .get('.navbar-nav') .contains('Reports') .click() + .wait(1000) .get('#service_body_id option:selected') .should('have.text', '-= Select A Service Body =-'); }) @@ -20,6 +21,7 @@ describe('Menubar', () => { .get('.navbar-nav') .contains('Service Bodies') .click() + .wait(1000) .get('#service-bodies-table tr:contains(\'Brooklyn\')') .should('contain', 'Brooklyn Area Service (1006)'); }) @@ -30,8 +32,9 @@ describe('Menubar', () => { .get('.navbar-nav') .contains('Schedules') .click() + .wait(1000) .get('#service_body_id option:selected') - .should('have.text', '-= Select A Service Body =-'); + .should('have.text', 'Bronx Area Service (1005) / Greater New York Region (1)'); }) it('Click Settings', () => { @@ -40,6 +43,7 @@ describe('Menubar', () => { .get('.navbar-nav') .contains('Settings') .click() + .wait(1000) .get('#settingsTable') .should('contain', 'https://latest.aws.bmlt.app/main_server'); }) @@ -50,8 +54,9 @@ describe('Menubar', () => { .get('.navbar-nav') .contains('Volunteer') .click() + .wait(1000) .get('#service_body_id option:selected') - .should('have.text', '-= Select A Service Body =-'); + .should('have.text', 'Bronx Area Service (1005) / Greater New York Region (1)'); }) it('Click Groups', () => { @@ -60,6 +65,7 @@ describe('Menubar', () => { .get('.navbar-nav') .contains('Groups') .click() + .wait(1000) .get('#service_body_id option:selected') .should('have.text', '-= Select A Service Body =-'); }) diff --git a/src/cypress/e2e/users/users.cy.js b/src/cypress/e2e/users/users.cy.js index 9a44cb7a7..30d0f471b 100644 --- a/src/cypress/e2e/users/users.cy.js +++ b/src/cypress/e2e/users/users.cy.js @@ -1,7 +1,7 @@ describe('Menubar', () => { - beforeEach(() => { - + before(() => { + cy.resetDatabase(); }); it('Click Users', () => { diff --git a/src/cypress/e2e/volunteers/volunteers.cy.js b/src/cypress/e2e/volunteers/volunteers.cy.js new file mode 100644 index 000000000..7904de803 --- /dev/null +++ b/src/cypress/e2e/volunteers/volunteers.cy.js @@ -0,0 +1,99 @@ +describe('Volunteers', () => { + + before(() => { + cy.resetDatabase(); + }); + + it('Save Volunteers', () => { + cy + .login() + .get('.navbar-nav') + .contains('Service Bodies') + .click() + .get('#service-bodies-table tr:contains(\'Brooklyn\')') + .should('contain', 'Brooklyn Area Service (1006)') + .get("[onclick=\"openServiceBodyCallHandling(1005);\"]") + .click() + .wait(1000) + .get('#serviceBodyCallHandling_1005 > .modal-dialog > .modal-content > .modal-body > #serviceBodyCallHandlingForm > #volunteer_routing') + .select('Volunteers') + .wait(500) + .get("#serviceBodyCallHandling_1005 > .modal-dialog > .modal-content > .modal-footer > .btn-primary") + .click() + .wait(1000) + + cy + .login() + .get('.navbar-nav') + .contains('Volunteers') + .click() + + cy + .get("#add-volunteer").click() + + cy + .get('#volunteerCard_1 > #volunteersForm > .card-header > .form-group > .volunteer-name-text > #volunteer_name') + .invoke('val', 'danny g') + .get('#volunteerCard_1 > #volunteersForm > .card-header > .form-group > .expand-button > .btn') + .click() + .wait(500) + .get('#volunteerCard_1 > #volunteersForm > .card-body > :nth-child(1) > #volunteer_phone_number') + .invoke('val', '9735558811') + .get('#volunteerCard_1 > #volunteersForm > .card-footer > #volunteerCardFooter > .form-check > #volunteer_enabled') + .click() + .get('#save-volunteers') + .click() + + cy.wait(2000) + }); + + it('Check Volunteer', () => { + cy + .login() + .get('.navbar-nav') + .contains('Volunteers') + .click() + + cy + .get('#volunteerCard_1 > #volunteersForm > .card-header > .form-group > .expand-button > .btn') + .click() + .wait(500) + .get('#volunteerCard_1 > #volunteersForm > .card-header > .form-group > .volunteer-name-text > #volunteer_name') + .should('have.value', 'danny g') + + cy.wait(2000) + }); + + it('Update Volunteer', () => { + cy + .login() + .get('.navbar-nav') + .contains('Volunteers') + .click() + + cy + .get('#volunteerCard_1 > #volunteersForm > .card-header > .form-group > .volunteer-name-text > #volunteer_name') + .invoke('val', 'danny g2') + .get('#volunteerCard_1 > #volunteersForm > .card-header > .form-group > .expand-button > .btn') + .click() + .get('#save-volunteers') + .click() + + cy.wait(2000) + + cy + .login() + .get('.navbar-nav') + .contains('Volunteers') + .click() + + cy + .get('#volunteerCard_1 > #volunteersForm > .card-header > .form-group > .expand-button > .btn') + .click() + .wait(500) + .get('#volunteerCard_1 > #volunteersForm > .card-header > .form-group > .volunteer-name-text > #volunteer_name') + .should('have.value', 'danny g2') + + cy.wait(2000) + }); +}); diff --git a/src/cypress/screenshots/.gitignore b/src/cypress/screenshots/.gitignore deleted file mode 100644 index e33609d25..000000000 --- a/src/cypress/screenshots/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.png diff --git a/src/cypress/support/commands.js b/src/cypress/support/commands.js index bbdf8f2c9..96d52bac1 100644 --- a/src/cypress/support/commands.js +++ b/src/cypress/support/commands.js @@ -6,3 +6,7 @@ Cypress.Commands.add('login', (username = "gnyr_admin", password = "CoreysGorySt cy.get("#inputPassword").type(password); cy.get("#authenticateButton").contains("Authenticate").click(); }) + +Cypress.Commands.add('resetDatabase', () => { + cy.request('POST', `${Cypress.env('apiUrl')}/resetDatabase`); +}); diff --git a/src/database/seeders/DatabaseSeeder.php b/src/database/seeders/DatabaseSeeder.php index 57b73b54d..501051545 100644 --- a/src/database/seeders/DatabaseSeeder.php +++ b/src/database/seeders/DatabaseSeeder.php @@ -11,8 +11,10 @@ class DatabaseSeeder extends Seeder * * @return void */ - public function run() + public function run() : void { - // \App\Models\User::factory(10)->create(); + if (getenv("ENVIRONMENT") === "test") { + $this->call(TestEnvironmentSeeder::class); + } } } diff --git a/src/database/seeders/TestEnvironmentSeeder.php b/src/database/seeders/TestEnvironmentSeeder.php new file mode 100644 index 000000000..811f6e113 --- /dev/null +++ b/src/database/seeders/TestEnvironmentSeeder.php @@ -0,0 +1,19 @@ +', { value: data[i].id, - text: JSON.parse(data[i].data)[0]['group_name'] + text: data[i].data[0]['group_name'] })); } @@ -759,7 +801,7 @@ function onGroupServiceBodyChange(callback) function loadGroups(service_body_id, callback) { if (groups === undefined) { - $.getJSON("../api/v1/volunteers/groups?service_body_id=" + service_body_id, function (data) { + $.getJSON(`../api/v1/groups?serviceBodyId=${service_body_id}`, function (data) { groups = data; callback(data) }); @@ -772,7 +814,7 @@ function getGroupForId(service_body_id, group_id, callback) { loadGroups(service_body_id, function (data) { for (item of data) { - if (item['id'] === group_id) { + if (item['id'] === parseInt(group_id)) { callback(item); } } @@ -958,14 +1000,14 @@ function deleteUserHandling($username) function showGroupsModal() { - spinnerDialog(true, "Retrieving Groups...", function () { + spinnerDialog(true, "Retrieving Groups...", function () { loadGroups($("#service_body_id").val(), function (data) { if (!$.isEmptyObject(data)) { $("#selected_group_id").find("option").remove(); $("#selected_group_id").append(new Option("-= Select a Group =-", 0, true, true)); - for (item of data) { - var group_info = JSON.parse(item['data']) - $("#selected_group_id").append(new Option(group_info[0]['group_name'], item['id'], false, false)); + for (let item of data) { + var group_info = item['data'][0] + $("#selected_group_id").append(new Option(group_info['group_name'], item['id'], false, false)); } spinnerDialog(false); @@ -1014,7 +1056,7 @@ function includeGroup(groupData) getGroupForId($("#service_body_id").val(), groupData['group_id'], function (data) { if (data !== null) { - var groupInfo = JSON.parse(data['data']); + var groupInfo = data['data']; groupCardTemplate.find("#group_name").html(groupInfo[0]['group_name']) } @@ -1238,7 +1280,7 @@ function openServiceBodyConfigure(service_body_id) spinnerDialog(true, "Retrieving Service Body Configuration...", function () { var serviceBodyConfiguration = $("#serviceBodyConfiguration_" + service_body_id); var serviceBodyFields = $("#serviceBodyConfigurationFields"); - loadFromAdminApi(null, service_body_id, '_YAP_CONFIG_V2_', function (data) { + $.getJSON("../api/v1/config?service_body_id=" + service_body_id, function (data) { if (!$.isEmptyObject(data)) { clearServiceBodyFields(service_body_id); var dataSet = data['data'][0]; @@ -1287,7 +1329,7 @@ function openServiceBodyCallHandling(service_body_id) { spinnerDialog(true, "Retrieving Service Body Call Handling...", function () { var serviceBodyCallHandling = $("#serviceBodyCallHandling_" + service_body_id); - loadFromAdminApi(null, service_body_id, '_YAP_CALL_HANDLING_V2_', function (data) { + $.getJSON(`../api/v1/callHandling?serviceBodyId=${service_body_id}`, function (data) { if (!$.isEmptyObject(data)) { var dataSet = data['data'][0]; for (var key in dataSet) { @@ -1361,7 +1403,7 @@ function groupsPage() clearVolunteerCards(); if (parseInt($(this).val()) > 0) { spinnerDialog(true, "Retrieving Group Volunteers...", function () { - loadGroupVolunteers($("#group_id").val(), $("#service_body_id").val(), function () { + loadGroupVolunteers($("#group_id").val(), function () { $("#editGroupButton").show(); $("#deleteGroupButton").show(); spinnerDialog(false); @@ -1405,7 +1447,7 @@ function deleteGroup() $.ajax({ async: false, type: "DELETE", - url: "../api/v1/config/" + $("#group_id").val(), + url: "../api/v1/groups/" + $("#group_id").val(), contentType: "application/json", complete: function () { spinnerDialog(false); @@ -1425,7 +1467,7 @@ function editGroup() $("#group_name").val($("#group_id option:selected").text()); for (var group of groups) { if (group['id'] === $("#group_id").val()) { - var data = JSON.parse(group.data)[0]; + var data = group[0]; if (data.hasOwnProperty("group_shared_service_bodies")) { $("#group_shared_service_bodies").val(data['group_shared_service_bodies']); break; @@ -1440,7 +1482,7 @@ function editGroup() function confirmGroup() { - if ($("#group_name").val() == "") { + if ($("#group_name").val() === "") { $("#group_dialog_message").html("A name is required."); return false; } @@ -1453,12 +1495,12 @@ function confirmGroup() dataObj[formItem["name"]] = $("#groupEditor").find("#" + formItem["name"]).val(); } - saveToAdminApi( - $("#service_body_id").val(), - [dataObj], - '_YAP_GROUPS_V2_', - 0, - $("#groupEditorHeader").text().indexOf("Add") !== 0 ? $("#group_id").val() : 0, + let isUpdate = $("#groupEditorHeader").text().indexOf("Add") !== 0 + + saveGroups( + isUpdate ? $("#group_id").val() : $("#service_body_id").val(), + dataObj, + isUpdate ? 'PUT' : 'POST', function (xhr, status) { var alert = $("#service_body_saved_alert"); if (xhr.responseText === "{}" || xhr.status !== 200) { diff --git a/src/resources/views/admin/groups.blade.php b/src/resources/views/admin/groups.blade.php index 2770d09ff..5bb47eb43 100644 --- a/src/resources/views/admin/groups.blade.php +++ b/src/resources/views/admin/groups.blade.php @@ -45,8 +45,8 @@ diff --git a/src/routes/api.php b/src/routes/api.php index ee47d0962..6154b7cae 100644 --- a/src/routes/api.php +++ b/src/routes/api.php @@ -1,6 +1,7 @@ ['authForAdminPortal'] ], function () { Route::get('/openapi.json', [SwaggerController::class, 'openapi'])->name('openapi'); - Route::resource('config', 'ConfigController')->only(['index', 'store', 'destroy']); - Route::resource('volunteers', 'ConfigureVolunteersController')->only(['index', 'store', 'destroy', 'update']); + Route::resource('config', 'ConfigController')->only(['index', 'store']); + Route::resource('volunteers', 'ConfigureVolunteersController')->only(['index', 'store']); + Route::resource('callHandling', 'ServiceBodyCallHandlingController')->only(['index', 'store']); Route::resource('users', 'UserController')->only(['index', 'show', 'store', 'destroy', 'update']); + Route::resource('groups', 'GroupController')->only(['index', 'store', 'destroy', 'update']); + Route::resource('groups/volunteers', 'GroupVolunteerController')->only(['index', 'store']); Route::resource('volunteers/schedule', 'VolunteerScheduleController')->only(['index']); Route::resource('volunteers/download', 'VolunteerDownloadController')->only(['index']); - Route::resource('volunteers/groups', 'VolunteerGroupsController')->only(['index']); Route::resource('reports/cdr', 'CdrController')->only(['index']); Route::resource('reports/mapmetrics', 'MapMetricController')->only(['index']); Route::resource('reports/metrics', 'MetricController')->only(['index']); Route::resource('rootServer/servicebodies', 'RootServerServiceBodiesController')->only(['index']); Route::resource('events/status', 'EventStatusController')->only(['index', 'store']); }); + +if (getenv('ENVIRONMENT') == "test") { + Route::post('/resetDatabase', function () { + Artisan::call('migrate:fresh --seed'); + return response()->json(['status' => 'database reset']); + }); +} diff --git a/src/tests/Feature/AdminAuthenticateTest.php b/src/tests/Feature/AdminAuthenticateTest.php index 28c9cda27..438dd23d4 100644 --- a/src/tests/Feature/AdminAuthenticateTest.php +++ b/src/tests/Feature/AdminAuthenticateTest.php @@ -1,6 +1,11 @@ assertHeader("Location", 'http://localhost/admin/home') ->assertHeader("Content-Type", "text/html; charset=utf-8"); }); + +// TODO: this test should be removed after we load data solely with React +test('get service body call handling (legacy)', function () { + $_SESSION['auth_mechanism'] = AuthMechanism::V2; + $_SESSION['auth_is_admin'] = true; + $_SESSION['username'] = "admin"; + + $serviceBodyCallHandling = new ServiceBodyCallHandling(); + $serviceBodyCallHandling->volunteer_routing_enabled = true; + $serviceBodyCallHandling->volunteer_routing = VolunteerRoutingType::VOLUNTEERS; + + ConfigData::createServiceBodyCallHandling(1, $serviceBodyCallHandling); + + $response = $this->get('/admin/volunteers'); + $response + ->assertStatus(200) + ->assertHeader("Content-Type", "text/html; charset=UTF-8"); +}); diff --git a/src/tests/Feature/ConfigApiTest.php b/src/tests/Feature/ConfigApiTest.php index bd3ec5b25..a13582934 100644 --- a/src/tests/Feature/ConfigApiTest.php +++ b/src/tests/Feature/ConfigApiTest.php @@ -63,27 +63,6 @@ ->assertJson([]); }); -test('get groups', function () { - $_SESSION['auth_mechanism'] = AuthMechanism::V2; - $groupData = ["group_name"=>"test", "group_shared_service_bodies"=>["1060"]]; - ConfigData::createGroup( - $this->serviceBodyId, - $this->parentServiceBodyId, - (object)$groupData - ); - - $this->call('GET', '/api/v1/config', [ - "id" => 3, - "data_type" => DataType::YAP_GROUPS_V2 - ])->assertStatus(200) - ->assertHeader("Content-Type", "application/json") - ->assertJson([ - "id"=>3, - "service_body_id"=>$this->serviceBodyId, - "parent_id"=>$this->parentServiceBodyId, - "data"=>[$groupData] - ]); -}); test('get by parent id', function () { $_SESSION['auth_mechanism'] = AuthMechanism::V2; @@ -101,31 +80,13 @@ ])->assertStatus(200) ->assertHeader("Content-Type", "application/json") ->assertJson([ - "id"=>4, + "id"=>1, "service_body_id"=>$this->serviceBodyId, "parent_id"=>$this->parentServiceBodyId, "data"=>[$configData] ]); }); -test('save group', function () { - $_SESSION['auth_mechanism'] = AuthMechanism::V2; - $groupData = [["group_name"=>"test", "group_shared_service_bodies"=>["1060"]]]; - $response = $this->call('POST', '/api/v1/config', [ - "service_body_id" => $this->serviceBodyId, - "parent_id" => $this->parentServiceBodyId, - "data_type" => DataType::YAP_GROUPS_V2, - ], content: json_encode($groupData)); - $response->assertStatus(200) - ->assertHeader("Content-Type", "application/json") - ->assertJson([ - "id"=>5, - "service_body_id"=>$this->serviceBodyId, - "parent_id"=>$this->parentServiceBodyId, - "data"=>$groupData - ]); -}); - test('save config', function () { $_SESSION['auth_mechanism'] = AuthMechanism::V2; app()->instance(RootServerService::class, $this->rootServerMocks->getService()); @@ -139,7 +100,7 @@ $response->assertStatus(200) ->assertHeader("Content-Type", "application/json") ->assertJson([ - "id"=>6, + "id"=>1, "service_body_id"=>$this->serviceBodyId, "parent_id"=>$this->parentServiceBodyId, "data"=>$serviceBodyConfigData @@ -164,21 +125,13 @@ $response->assertStatus(200) ->assertHeader("Content-Type", "application/json") ->assertJson([ - "id"=>7, + "id"=>1, "service_body_id"=>$this->serviceBodyId, "parent_id"=>$this->parentServiceBodyId, "data"=>$serviceBodyConfigData ]); }); -test('delete group', function () { - $_SESSION['auth_mechanism'] = AuthMechanism::V2; - $response = $this->call('DELETE', sprintf('/api/v1/config/%s', $this->id)); - $response->assertStatus(200) - ->assertHeader("Content-Type", "application/json") - ->assertJson([]); -}); - test('get config no auth', function () { $response = $this->call('GET', '/api/v1/config', [ "service_body_id" => 0, diff --git a/src/tests/Feature/GroupApiTest.php b/src/tests/Feature/GroupApiTest.php new file mode 100644 index 000000000..fa8a67af4 --- /dev/null +++ b/src/tests/Feature/GroupApiTest.php @@ -0,0 +1,234 @@ +id = "200"; + $this->serviceBodyId = "44"; + $this->parentServiceBodyId = "43"; +}); + +test('save group', function () { + $_SESSION['auth_mechanism'] = AuthMechanism::V2; + $groupData = new Group(); + $groupData->group_name = "test"; + $groupData->group_shared_service_bodies = ["1060"]; + + $response = $this->call( + 'POST', + '/api/v1/groups', + ['serviceBodyId' => $this->serviceBodyId], + content: json_encode($groupData) + ); + + $response->assertJson([[ + "id"=>1, + "parent_id"=>null, + "service_body_id"=>intval($this->serviceBodyId), + "data"=>[$groupData->toArray()]]]) + ->assertHeader("Content-Type", "application/json") + ->assertStatus(200); +}); + +test('delete group', function () { + $groupData = new Group(); + $groupData->group_name = "Fake Group"; + $groupData->group_shared_service_bodies = [$this->serviceBodyId]; + + $groupId = ConfigData::createGroup( + $this->serviceBodyId, + $groupData, + ); + + $_SESSION['auth_mechanism'] = AuthMechanism::V2; + $response = $this->call('DELETE', sprintf('/api/v1/groups/%s', $groupId)); + $response->assertStatus(200) + ->assertHeader("Content-Type", "application/json") + ->assertJson(['message' => sprintf('Group %s deleted successfully', $groupId)]); +}); + +test('delete group that does not exist', function () { + $_SESSION['auth_mechanism'] = AuthMechanism::V2; + $response = $this->call('DELETE', sprintf('/api/v1/groups/%s', 1000)); + $response->assertStatus(404) + ->assertHeader("Content-Type", "application/json") + ->assertJson(['message' => 'Not found']); +}); + + +test('get groups for service body', function () { + $_SESSION['auth_mechanism'] = AuthMechanism::V2; + $groupData = new Group(); + $groupData->group_name = "Fake Group"; + $groupData->group_shared_service_bodies = [$this->serviceBodyId]; + + ConfigData::createGroup( + $this->serviceBodyId, + $groupData, + ); + + $id = ConfigData::select('id')->orderBy('id', 'desc')->first()->id; + + $this->call( + 'GET', + '/api/v1/groups', + ['serviceBodyId' => $this->serviceBodyId] + )->assertJson([[ + "id"=>$id, + "service_body_id"=>intval($this->serviceBodyId), + "data"=>[$groupData->toArray()]]]) + ->assertHeader("Content-Type", "application/json") + ->assertStatus(200); +}); + +test('get groups for service body no auth', function () { + $response = $this->call('GET', '/api/v1/groups', [ + "service_body_id" => 0, + ]); + $response + ->assertHeader("Location", "http://localhost/admin") + ->assertHeader("Content-Type", "text/html; charset=utf-8") + ->assertStatus(302); +}); + +test('update group', function () { + $_SESSION['auth_mechanism'] = AuthMechanism::V2; + $groupData = new Group(); + $groupData->group_name = "test"; + $groupData->group_shared_service_bodies = ["1060"]; + + $response = $this->call( + 'POST', + '/api/v1/groups', + ['serviceBodyId' => $this->serviceBodyId], + content: json_encode($groupData) + ); + + $response->assertJson([[ + "id"=>1, + "parent_id"=>null, + "service_body_id"=>intval($this->serviceBodyId), + "data"=>[$groupData->toArray()]]]) + ->assertHeader("Content-Type", "application/json") + ->assertStatus(200); + + $updatedGroupData = new Group(); + $updatedGroupData->group_name = "test2"; + $updatedGroupData->group_shared_service_bodies = ["1060", "1061"]; + + $response = $this->call( + 'PUT', + sprintf('/api/v1/groups/%s', 1), + content: json_encode($updatedGroupData) + ); + + $response->assertJson([[ + "id"=>1, + "parent_id"=>null, + "service_body_id"=>intval($this->serviceBodyId), + "data"=>[$updatedGroupData->toArray()]]]) + ->assertHeader("Content-Type", "application/json") + ->assertStatus(200); +}); + + +test('save group volunteers', function () { + $_SESSION['auth_mechanism'] = AuthMechanism::V2; + + $groupData = new Group(); + $groupData->group_name = "test"; + $groupData->group_shared_service_bodies = ["1060"]; + + $response = $this->call( + 'POST', + '/api/v1/groups', + ['serviceBodyId' => $this->serviceBodyId], + content: json_encode($groupData) + ); + + $groupId = json_decode($response->getContent())[0]->id; + + $volunteerData = new VolunteerData(); + $volunteerData->volunteer_phone_number = "19735559911"; + + $response = $this->call( + 'POST', + '/api/v1/groups/volunteers', + ['groupId' => $groupId, 'serviceBodyId' => $this->serviceBodyId], + content: json_encode([$volunteerData]) + ); + + $response->assertJson([ + "id"=>2, + "parent_id"=>$groupId, + "service_body_id"=>intval($this->serviceBodyId), + "data"=>[$volunteerData->toArray()]]) + ->assertHeader("Content-Type", "application/json") + ->assertStatus(200); +}); + +test('update group volunteers', function () { + $_SESSION['auth_mechanism'] = AuthMechanism::V2; + + $groupData = new Group(); + $groupData->group_name = "test"; + $groupData->group_shared_service_bodies = ["1060"]; + + $response = $this->call( + 'POST', + '/api/v1/groups', + ['serviceBodyId' => $this->serviceBodyId], + content: json_encode($groupData) + ); + + $groupId = json_decode($response->getContent())[0]->id; + + $volunteerData = new VolunteerData(); + $volunteerData->volunteer_phone_number = "19735559911"; + + $response = $this->call( + 'POST', + '/api/v1/groups/volunteers', + ['groupId' => $groupId, 'serviceBodyId' => $this->serviceBodyId], + content: json_encode([$volunteerData]) + ); + + $response->assertJson([ + "id"=>2, + "parent_id"=>$groupId, + "service_body_id"=>intval($this->serviceBodyId), + "data"=>[$volunteerData->toArray()]]) + ->assertHeader("Content-Type", "application/json") + ->assertStatus(200); + + $volunteerData = new VolunteerData(); + $volunteerData->volunteer_phone_number = "19735559912"; + + + $response = $this->call( + 'POST', + '/api/v1/groups/volunteers', + ['groupId' => $groupId], + content: json_encode([$volunteerData]) + ); + + $response->assertJson([ + "id"=>2, + "parent_id"=>$groupId, + "service_body_id"=>intval($this->serviceBodyId), + "data"=>[$volunteerData->toArray()]]) + ->assertHeader("Content-Type", "application/json") + ->assertStatus(200); +}); diff --git a/src/tests/Feature/HelplineDialerTest.php b/src/tests/Feature/HelplineDialerTest.php index 55674c939..4fcc43df8 100644 --- a/src/tests/Feature/HelplineDialerTest.php +++ b/src/tests/Feature/HelplineDialerTest.php @@ -97,9 +97,8 @@ $serviceBodyCallHandlingData->call_strategy = CycleAlgorithm::LINEAR_CYCLE_AND_VOICEMAIL; - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( $this->serviceBodyId, - $this->parentServiceBodyId, $serviceBodyCallHandlingData ); @@ -149,9 +148,8 @@ $serviceBodyCallHandlingData->volunteer_routing_enabled = true; $serviceBodyCallHandlingData->call_strategy = CycleAlgorithm::LINEAR_CYCLE_AND_VOICEMAIL; - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( $this->serviceBodyId, - $this->parentServiceBodyId, $serviceBodyCallHandlingData ); @@ -198,9 +196,8 @@ $serviceBodyCallHandlingData->call_strategy = CycleAlgorithm::LINEAR_CYCLE_AND_VOICEMAIL; - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( $this->serviceBodyId, - $this->parentServiceBodyId, $serviceBodyCallHandlingData ); $shifts = []; @@ -330,9 +327,8 @@ $serviceBodyCallHandlingData->volunteer_routing_enabled = true; $serviceBodyCallHandlingData->call_strategy = CycleAlgorithm::LINEAR_CYCLE_AND_VOICEMAIL; - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( $this->serviceBodyId, - $this->parentServiceBodyId, $serviceBodyCallHandlingData ); $shifts = []; @@ -459,9 +455,8 @@ $serviceBodyCallHandlingData->volunteer_routing_enabled = true; $serviceBodyCallHandlingData->call_strategy = CycleAlgorithm::LINEAR_CYCLE_AND_VOICEMAIL; - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( $this->serviceBodyId, - $this->parentServiceBodyId, $serviceBodyCallHandlingData ); $shifts = []; @@ -610,9 +605,8 @@ $serviceBodyCallHandlingData->volunteer_routing_enabled = true; $serviceBodyCallHandlingData->call_strategy = CycleAlgorithm::LINEAR_CYCLE_AND_VOICEMAIL; - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( $this->serviceBodyId, - $this->parentServiceBodyId, $serviceBodyCallHandlingData ); $shifts = []; @@ -647,7 +641,6 @@ ConfigData::createVolunteers( $this->serviceBodyId, - $this->parentServiceBodyId, [$volunteer1, $volunteer2], ); @@ -759,9 +752,8 @@ $serviceBodyCallHandlingData->volunteer_routing_enabled = true; $serviceBodyCallHandlingData->call_strategy = CycleAlgorithm::BLASTING; - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( $this->serviceBodyId, - $this->parentServiceBodyId, $serviceBodyCallHandlingData ); $shifts = []; @@ -888,9 +880,8 @@ $serviceBodyCallHandlingData->volunteer_routing_enabled = true; $serviceBodyCallHandlingData->call_strategy = CycleAlgorithm::BLASTING; - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( $this->serviceBodyId, - $this->parentServiceBodyId, $serviceBodyCallHandlingData ); $shifts = []; @@ -925,7 +916,6 @@ ConfigData::createVolunteers( $this->serviceBodyId, - $this->parentServiceBodyId, [$volunteer1, $volunteer2], ); diff --git a/src/tests/Feature/HelplineSMSTest.php b/src/tests/Feature/HelplineSMSTest.php index 24a1c30a8..581c6f74f 100644 --- a/src/tests/Feature/HelplineSMSTest.php +++ b/src/tests/Feature/HelplineSMSTest.php @@ -68,9 +68,8 @@ $serviceBodyCallHandlingData->volunteer_sms_notification_enabled = true; $serviceBodyCallHandlingData->call_strategy = CycleAlgorithm::LINEAR_CYCLE_AND_VOICEMAIL; - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( $this->serviceBodyId, - $this->parentServiceBodyId, $serviceBodyCallHandlingData ); @@ -156,9 +155,8 @@ $serviceBodyCallHandlingData->volunteer_sms_notification_enabled = true; $serviceBodyCallHandlingData->call_strategy = CycleAlgorithm::LINEAR_CYCLE_AND_VOICEMAIL; - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( $this->serviceBodyId, - $this->parentServiceBodyId, $serviceBodyCallHandlingData ); @@ -242,9 +240,8 @@ $serviceBodyCallHandlingData->volunteer_sms_notification_enabled = true; $serviceBodyCallHandlingData->call_strategy = CycleAlgorithm::LINEAR_CYCLE_AND_VOICEMAIL; - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( $this->serviceBodyId, - $this->parentServiceBodyId, $serviceBodyCallHandlingData ); diff --git a/src/tests/Feature/HelplineSearchTest.php b/src/tests/Feature/HelplineSearchTest.php index d6cf955af..b8da0d4de 100644 --- a/src/tests/Feature/HelplineSearchTest.php +++ b/src/tests/Feature/HelplineSearchTest.php @@ -149,7 +149,7 @@ $serviceBodyCallHandlingData->volunteer_routing_enabled = true; $serviceBodyCallHandlingData->call_strategy = CycleAlgorithm::LINEAR_CYCLE_AND_VOICEMAIL; - ConfigData::createCallHandling($serviceBodyId, $parentServiceBodyId, $serviceBodyCallHandlingData); + ConfigData::createServiceBodyCallHandling($serviceBodyId, $serviceBodyCallHandlingData); $response = $this->call($method, '/helpline-search.php', [ 'Digits' => "Buffalo, NY", @@ -186,9 +186,8 @@ $serviceBodyCallHandlingData->service_body_id = $this->serviceBodyId; $serviceBodyCallHandlingData->volunteer_routing_enabled = true; - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( $this->serviceBodyId, - $this->parentServiceBodyId, $serviceBodyCallHandlingData ); @@ -258,9 +257,8 @@ $serviceBodyCallHandlingData->service_body_id = $this->serviceBodyId; $serviceBodyCallHandlingData->volunteer_routing_enabled = true; - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( $this->serviceBodyId, - $this->parentServiceBodyId, $serviceBodyCallHandlingData ); @@ -294,9 +292,8 @@ $serviceBodyCallHandlingData->service_body_id = $this->serviceBodyId; $serviceBodyCallHandlingData->volunteer_routing_enabled = true; - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( $this->serviceBodyId, - $this->parentServiceBodyId, $serviceBodyCallHandlingData ); @@ -334,9 +331,8 @@ $serviceBodyCallHandlingData->volunteer_sms_notification_enabled = true; $serviceBodyCallHandlingData->gender_routing = true; - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( $this->serviceBodyId, - $this->parentServiceBodyId, $serviceBodyCallHandlingData ); @@ -397,9 +393,8 @@ $serviceBodyCallHandlingData->service_body_id = $this->serviceBodyId; $serviceBodyCallHandlingData->volunteer_routing_enabled = true; - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( $this->serviceBodyId, - $this->parentServiceBodyId, $serviceBodyCallHandlingData ); @@ -408,9 +403,8 @@ $redirectedServiceBody->service_body_id = 46; $redirectedServiceBody->volunteer_routing_enabled = true; - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( 46, - $this->parentServiceBodyId, $redirectedServiceBody ); diff --git a/src/tests/Feature/InitialIndexTest.php b/src/tests/Feature/InitialIndexTest.php index cb51f27f3..d372913e9 100644 --- a/src/tests/Feature/InitialIndexTest.php +++ b/src/tests/Feature/InitialIndexTest.php @@ -389,9 +389,8 @@ $handling->volunteer_routing = VolunteerRoutingType::VOLUNTEERS_AND_SMS; $handling->override_en_US_greeting = "https://fake.mp3"; - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( $this->serviceBodyId, - $this->parentServiceBodyId, $handling ); diff --git a/src/tests/Feature/ReportsApiTest.php b/src/tests/Feature/ReportsApiTest.php index 39b7c086a..bb25223a5 100644 --- a/src/tests/Feature/ReportsApiTest.php +++ b/src/tests/Feature/ReportsApiTest.php @@ -48,7 +48,7 @@ $_SESSION['auth_mechanism'] = AuthMechanism::V2; app()->instance(RootServerService::class, $this->rootServerMocks->getService()); - $id = 11; + $id = 1; $callSid = "abc123"; $service_body_id = 44; $date_range_start = "2023-01-01T00:00:00"; @@ -111,7 +111,7 @@ $_SESSION['auth_mechanism'] = AuthMechanism::V2; app()->instance(RootServerService::class, $this->rootServerMocks->getService()); $service_body_id = 44; - $id = 12; + $id = 1; $date_range_start = "2023-01-01 000:00:00"; $date_range_end = "2023-01-07 23:59:59"; $start_time = "2023-01-01 20:43:56"; @@ -173,7 +173,7 @@ $_SESSION['auth_mechanism'] = AuthMechanism::V2; app()->instance(RootServerService::class, $this->rootServerMocks->getService()); $service_body_id = 44; - $id = 13; + $id = 1; $date_range_start = "2023-01-01 000:00:00"; $date_range_end = "2023-01-07 23:59:59"; $start_time = "2023-01-01 20:43:56"; diff --git a/src/tests/Feature/ServiceBodyCallHandlingApiTest.php b/src/tests/Feature/ServiceBodyCallHandlingApiTest.php new file mode 100644 index 000000000..fa5f833de --- /dev/null +++ b/src/tests/Feature/ServiceBodyCallHandlingApiTest.php @@ -0,0 +1,121 @@ +id = "200"; + $this->serviceBodyId = "44"; + $this->parentServiceBodyId = "43"; +}); + +test('save call handling', function () { + $_SESSION['auth_mechanism'] = AuthMechanism::V2; + $callHandling = new ServiceBodyCallHandling(); + $callHandling->forced_caller_id_number = "123"; + + $response = $this->call( + 'POST', + '/api/v1/callHandling', + ['serviceBodyId' => $this->serviceBodyId], + content: json_encode($callHandling) + ); + + $response->assertJson([ + "id"=>1, + "parent_id"=>0, + "service_body_id"=>intval($this->serviceBodyId), + "data"=>[$callHandling->toArray()]]) + ->assertHeader("Content-Type", "application/json") + ->assertStatus(200); +}); + +test('get call handling for a service body', function () { + $_SESSION['auth_mechanism'] = AuthMechanism::V2; + $callHandling = new ServiceBodyCallHandling(); + $callHandling->forced_caller_id_number = "123"; + + ConfigData::createServiceBodyCallHandling( + $this->serviceBodyId, + $callHandling + ); + + $response = $this->call( + 'GET', + '/api/v1/callHandling', + ['serviceBodyId' => $this->serviceBodyId] + ); + + $response->assertJson([ + "id"=>1, + "parent_id"=>0, + "service_body_id"=>intval($this->serviceBodyId), + "data"=>[$callHandling->toArray()]]) + ->assertHeader("Content-Type", "application/json") + ->assertStatus(200); +}); + +test('get call handling for a service body that does not exist', function () { + $_SESSION['auth_mechanism'] = AuthMechanism::V2; + + $response = $this->call( + 'GET', + '/api/v1/callHandling', + ['serviceBodyId' => $this->serviceBodyId] + ); + + $response->assertJson([]) + ->assertHeader("Content-Type", "application/json") + ->assertStatus(200); +}); + +test('update call handling for a service body', function () { + $_SESSION['auth_mechanism'] = AuthMechanism::V2; + $callHandling = new ServiceBodyCallHandling(); + $callHandling->forced_caller_id_number = "123"; + + $response = $this->call( + 'POST', + '/api/v1/callHandling', + ['serviceBodyId' => $this->serviceBodyId], + content: json_encode($callHandling) + ); + + $response->assertJson([ + "id"=>1, + "parent_id"=>0, + "service_body_id"=>intval($this->serviceBodyId), + "data"=>[$callHandling->toArray()]]) + ->assertHeader("Content-Type", "application/json") + ->assertStatus(200); + + + $callHandling = new ServiceBodyCallHandling(); + $callHandling->forced_caller_id_number = "123abc"; + + $response = $this->call( + 'POST', + '/api/v1/callHandling', + ['serviceBodyId' => $this->serviceBodyId], + content: json_encode($callHandling) + ); + + $response->assertJson([ + "id"=>1, + "parent_id"=>0, + "service_body_id"=>intval($this->serviceBodyId), + "data"=>[$callHandling->toArray()]]) + ->assertHeader("Content-Type", "application/json") + ->assertStatus(200); +}); diff --git a/src/tests/Feature/VoicemailCompleteTest.php b/src/tests/Feature/VoicemailCompleteTest.php index 1aaf00200..58537968d 100644 --- a/src/tests/Feature/VoicemailCompleteTest.php +++ b/src/tests/Feature/VoicemailCompleteTest.php @@ -73,9 +73,8 @@ })); $this->utility->client->shouldReceive('calls')->with($this->callSid)->andReturn($callContextMock); - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( $this->serviceBodyId, - $this->parentServiceBodyId, $serviceBodyCallHandlingData ); @@ -165,9 +164,8 @@ $volunteer ); - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( $this->serviceBodyId, - $this->parentServiceBodyId, $serviceBodyCallHandlingData ); @@ -264,9 +262,8 @@ $volunteer ); - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( $this->serviceBodyId, - $this->parentServiceBodyId, $serviceBodyCallHandlingData ); @@ -331,9 +328,8 @@ ->once(); app()->instance(PHPMailer::class, $mailer); - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( $this->serviceBodyId, - $this->parentServiceBodyId, $serviceBodyCallHandlingData, ); diff --git a/src/tests/Feature/VoicemailTest.php b/src/tests/Feature/VoicemailTest.php index d1fcd807f..8f2b9d73d 100644 --- a/src/tests/Feature/VoicemailTest.php +++ b/src/tests/Feature/VoicemailTest.php @@ -32,9 +32,8 @@ $serviceBodyCallHandlingData->volunteer_sms_notification_enabled = true; $serviceBodyCallHandlingData->call_strategy = CycleAlgorithm::LINEAR_CYCLE_AND_VOICEMAIL; - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( $this->serviceBodyId, - $this->parentServiceBodyId, $serviceBodyCallHandlingData ); @@ -67,9 +66,8 @@ $serviceBodyCallHandlingData->volunteer_sms_notification_enabled = true; $serviceBodyCallHandlingData->call_strategy = CycleAlgorithm::LINEAR_CYCLE_AND_VOICEMAIL; - ConfigData::createCallHandling( + ConfigData::createServiceBodyCallHandling( $this->serviceBodyId, - $this->parentServiceBodyId, $serviceBodyCallHandlingData ); diff --git a/src/tests/Feature/VolunteersApiTest.php b/src/tests/Feature/VolunteersApiTest.php index a7a89936c..865891e24 100644 --- a/src/tests/Feature/VolunteersApiTest.php +++ b/src/tests/Feature/VolunteersApiTest.php @@ -6,6 +6,8 @@ use App\Constants\VolunteerType; use App\Models\ConfigData; use App\Services\RootServerService; +use App\Structures\Group; +use App\Structures\Volunteer; use App\Structures\VolunteerData; use App\Structures\VolunteerInfo; use App\Utilities\VolunteerScheduleHelpers; @@ -465,16 +467,19 @@ $volunteer ); + $groupData = new Group(); + $groupData->group_name = "test"; + $groupData->group_shared_service_bodies = [$firstServiceBodyId]; + $groupId = ConfigData::createGroup( $firstServiceBodyId, - $parentServiceBodyId, - (object)["group_name" => "test", "group_shared_service_bodies" => $firstServiceBodyId] + $groupData ); ConfigData::createGroupVolunteers( $firstServiceBodyId, $groupId, - $volunteer, + [$volunteer], ); ConfigData::addGroupToVolunteers( @@ -673,36 +678,106 @@ ->assertStatus(200); }); -test('get groups for service body', function () { +test('save volunteers', function () { $_SESSION['auth_mechanism'] = AuthMechanism::V2; - app()->instance(RootServerService::class, $this->rootServerMocks->getService()); - $groupData = ["group_name"=>"Fake Group", "group_shared_service_bodies"=>[$this->serviceBodyId]]; - ConfigData::createGroup( + $volunteerData = new VolunteerData(); + $volunteerData->volunteer_phone_number = "19735559911"; + + $response = $this->call( + 'POST', + '/api/v1/volunteers', + ['serviceBodyId' => $this->serviceBodyId], + content: json_encode([$volunteerData]) + ); + + $response->assertJson([ + "id"=>1, + "parent_id"=>0, + "service_body_id"=>intval($this->serviceBodyId), + "data"=>[$volunteerData->toArray()]]) + ->assertHeader("Content-Type", "application/json") + ->assertStatus(200); +}); + +test('get volunteers for a service body', function () { + $_SESSION['auth_mechanism'] = AuthMechanism::V2; + + $volunteerData = new VolunteerData(); + $volunteerData->volunteer_phone_number = "19735559911"; + + ConfigData::createVolunteers( $this->serviceBodyId, - $this->parentServiceBodyId, - (object)$groupData, + [$volunteerData] ); - $id = ConfigData::select('id')->orderBy('id', 'desc')->first()->id; + $response = $this->call( + 'POST', + '/api/v1/volunteers', + ['serviceBodyId' => $this->serviceBodyId], + content: json_encode([$volunteerData]) + ); - $this->call('GET', '/api/v1/volunteers/groups', [ - "service_body_id" => $this->serviceBodyId, - ])->assertJson([[ - "id"=>$id, - "service_body_id"=>intval($this->serviceBodyId), - "parent_id"=>intval($this->parentServiceBodyId), - "data"=>json_encode([$groupData])]]) + $response->assertJson([ + "id"=>1, + "parent_id"=>0, + "service_body_id"=>intval($this->serviceBodyId), + "data"=>[$volunteerData->toArray()]]) ->assertHeader("Content-Type", "application/json") ->assertStatus(200); }); -test('get groups for service body no auth', function () { - $response = $this->call('GET', '/api/v1/volunteers/groups', [ - "service_body_id" => 0, - ]); - $response - ->assertHeader("Location", "http://localhost/admin") - ->assertHeader("Content-Type", "text/html; charset=utf-8") - ->assertStatus(302); +test('get volunteers for a service body that does not exist', function () { + $_SESSION['auth_mechanism'] = AuthMechanism::V2; + + $response = $this->call( + 'GET', + '/api/v1/volunteers', + ['serviceBodyId' => $this->serviceBodyId] + ); + + $response->assertJson([]) + ->assertHeader("Content-Type", "application/json") + ->assertStatus(200); +}); + +test('update call volunteers for a service body', function () { + $_SESSION['auth_mechanism'] = AuthMechanism::V2; + + $volunteerData = new VolunteerData(); + $volunteerData->volunteer_phone_number = "19735559911"; + + $response = $this->call( + 'POST', + '/api/v1/volunteers', + ['serviceBodyId' => $this->serviceBodyId], + content: json_encode([$volunteerData]) + ); + + $response->assertJson([ + "id"=>1, + "parent_id"=>0, + "service_body_id"=>intval($this->serviceBodyId), + "data"=>[$volunteerData->toArray()]]) + ->assertHeader("Content-Type", "application/json") + ->assertStatus(200); + + + $volunteerData = new VolunteerData(); + $volunteerData->volunteer_phone_number = "19735559912"; + + $response = $this->call( + 'POST', + '/api/v1/volunteers', + ['serviceBodyId' => $this->serviceBodyId], + content: json_encode([$volunteerData]) + ); + + $response->assertJson([ + "id"=>1, + "parent_id"=>0, + "service_body_id"=>intval($this->serviceBodyId), + "data"=>[$volunteerData->toArray()]]) + ->assertHeader("Content-Type", "application/json") + ->assertStatus(200); }); diff --git a/src/tests/Pest.php b/src/tests/Pest.php index a503f4dd4..f431fc42c 100644 --- a/src/tests/Pest.php +++ b/src/tests/Pest.php @@ -1,17 +1,20 @@ beforeEach(function () { - env("ENVIRONMENT", "test"); - $_COOKIE["PHPSESSID"] = "fake"; -})->in('Feature'); +uses(TestCase::class, RefreshDatabase::class) + ->beforeEach(function () { + env("ENVIRONMENT", "test"); + $_COOKIE["PHPSESSID"] = "fake"; + $this->artisan('migrate:fresh'); + }) + ->in('Feature'); function setupTwilioService(): TwilioTestUtility {