diff --git a/app/Helpers/Collaborator.php b/app/Helpers/Collaborator.php index 225ed706..870cdf88 100644 --- a/app/Helpers/Collaborator.php +++ b/app/Helpers/Collaborator.php @@ -3,13 +3,18 @@ namespace App\Helpers; use App\Mail\TaskAddedMail; -use Illuminate\Support\Facades\Log; -use App\Repositories\TodoRepository; +use App\Services\UserService; use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Mail; -class Collaborator extends TodoRepository +class Collaborator { + protected $userService; + public function __construct(UserService $userService) + { + $this->userService = $userService; + } public static function isAdmin(array $todo, $userId): bool { @@ -66,7 +71,7 @@ public function sendMails(array $user_ids, $subject, $message) for ($i = 0; $i < count($user_ids); $i++) { # code... $data['user_id'] = $user_ids[$i]; - $user = $this->findUser($data, $bearerToken); + $user = $this->userService->findUser($data, $bearerToken); // Log::info($user['email']) $this->sendMail($user, $subject, $message); } @@ -97,7 +102,4 @@ public static function sortAdminFirst(array $collaborators): array return $adminFirstList; } - public function findUser($data, $bearerToken){ - return $this->httpRepository->findUser($data,$bearerToken); - } } diff --git a/app/Http/Controllers/ArchiveController.php b/app/Http/Controllers/ArchiveController.php index cee7acb4..fadb462a 100644 --- a/app/Http/Controllers/ArchiveController.php +++ b/app/Http/Controllers/ArchiveController.php @@ -9,10 +9,6 @@ class ArchiveController extends Controller { protected $todoService; - /** - * Constructor that resolves todo service - * @param $todoService - */ public function __construct(TodoService $todoService) { @@ -33,6 +29,7 @@ public function archiveTodo(Request $request, $todoId) if (isset($todo['status'])) { $todo['archived_at'] = now(); } + $updatedTodo = array_merge($todo, ['archived_at' => now()]); $result = $this->todoService->update($updatedTodo, $todoId); @@ -40,15 +37,7 @@ public function archiveTodo(Request $request, $todoId) // Publish To Centrifugo Here $this->todoService->publishToRoomChannel($todo['channel'], $todo, "todo", "archive"); - return response()->json( - ["status" => "success", "type" => "Todo", "data" => array_merge( - [ - '_id' => $todoId], - $updatedTodo - ) - ], - 200 - ); + return response()->json(["status" => "success", "type" => "Todo", "data" => array_merge(['_id' => $todoId], $updatedTodo)], 200); } return response()->json(['status' => "error", 'message' => $result], 500); @@ -64,8 +53,8 @@ public function unArchiveTodo(Request $request, $todoId) return response()->json($todo, 404); } - $results = array_search($todo['archived_at'], $todo, true); - if ($results !== false) { + $results=array_search($todo['archived_at'],$todo,true); + if($results !== false) { $todo['archived_at'] = null; } @@ -73,16 +62,7 @@ public function unArchiveTodo(Request $request, $todoId) $result = $this->todoService->update($updatedTodo, $todoId); if (isset($result['modified_documents']) && $result['modified_documents'] > 0) { // Publish To Centrifugo Here - return response()->json( - ["status" => "success", "type" => "Todo", "data" => array_merge( - [ - '_id' => $todoId - ], - $updatedTodo - ) - ], - 200 - ); + return response()->json(["status" => "success", "type" => "Todo", "data" => array_merge(['_id' => $todoId], $updatedTodo)], 200); } return response()->json(['status' => "error", 'message' => $result], 500); } @@ -91,6 +71,8 @@ public function fetchArchived(Request $request) { // This function should normally be user centric // but for the ime being, leave for now + Sort::sortAsc($request); + $all = $this->todoService->all(); $archived = []; if (isset($all['status']) && $all['status'] == 404) { diff --git a/app/Http/Controllers/SideBar/TodoController.php b/app/Http/Controllers/SideBar/TodoController.php index f2878107..fd9badff 100644 --- a/app/Http/Controllers/SideBar/TodoController.php +++ b/app/Http/Controllers/SideBar/TodoController.php @@ -15,10 +15,9 @@ class TodoController extends Controller { use HTTPRepoResponseHandler; - protected $todoRepository; - protected $roomRepository; + protected $todoRepository, $roomRepository; - public function __construct(TodoRepository $todoRepository, RoomRepository $roomRepository) + function __construct(TodoRepository $todoRepository, RoomRepository $roomRepository) { $this->todoRepository = $todoRepository; $this->roomRepository = $roomRepository; @@ -33,10 +32,7 @@ public function index(Request $request) ]; // return response - return response()->json( - ['message' => "success", "data" => $this->respondWithData($this->todoRepository->findWhere($todo_attr))], - 200 - ); + return response()->json(['message' => "success", "data" => $this->respondWithData($this->todoRepository->findWhere($todo_attr))], 200); } public function store(Request $request) @@ -56,7 +52,7 @@ public function store(Request $request) $result = $this->todoRepository->create($todo_attr); // check response - if ($result['status'] == 200 && isset($result["data"])){ + if($result['status'] == 200 && isset($result["data"])){ // create room attr $room_attr = [ diff --git a/app/Http/Controllers/TaskController.php b/app/Http/Controllers/TaskController.php index 5ee50dbe..10d3e4f7 100644 --- a/app/Http/Controllers/TaskController.php +++ b/app/Http/Controllers/TaskController.php @@ -2,15 +2,19 @@ namespace App\Http\Controllers; +use Carbon\Carbon; use App\Helpers\Sort; +use Ramsey\Uuid\Uuid; use Illuminate\Support\Str; use Illuminate\Http\Request; use App\Helpers\Collaborator; use App\Services\TaskService; use App\Services\TodoService; use App\Services\UserService; +use App\Http\Requests\TaskRequest; +use App\Http\Resources\TodoResource; use App\Http\Requests\AddTaskRequest; -use App\Http\Requests\TaskCategoryRequest; +use App\Http\Requests\MarkTaskRequest; use Illuminate\Support\Facades\Validator; use App\Http\Resources\TodoResourceCollection; @@ -33,31 +37,32 @@ public function __construct(TaskService $taskService, TodoService $todoService, /** * Show the search results. - * @param Request $request - * @return mixed + * + * @return mixed */ public function index(Request $request) { - return response()->json(new TodoResourceCollection($this->taskService->all()), 200); + $task = $this->taskService->all(); + + if (isset($task['status']) && $task['status'] == '404') { + return response()->json(['message' => 'Tasks not found'], 404); + } + return response()->json(new TodoResourceCollection($task), 200); } - /** - * @return mixed - * This method orders the tasks in order of newly created - */ + public function getLatestTask() { return response()->json($this->taskService->getLatestTask()); } - /** - * @param string - * @return mixed - * returns a task by the TodoID - */ public function show($id) { - return response()->json($this->taskService->findBy('_id', $id), 200); + $tasks = $this->taskService->findBy('_id', $id); + if (empty($tasks)) { + return response()->json(['message' => 'Todo not found'], 404); + } + return response()->json($tasks, 200); } public function updateTaskDate(Request $request, $id) @@ -67,19 +72,46 @@ public function updateTaskDate(Request $request, $id) public function toggleArchiveStatus($id) { - return $this->taskService->toggleStatus($id); + $task = $this->taskService->find($id); // Get the Task + + $archived = array_key_exists('archived_at', $task) && $task['archived_at'] ? '' : Carbon::now(); // Set new date if it is null or empty, else set back to empty + + // prepare the payload + $data = array(); + $data['archived_at'] = $archived; + + //response from zccore + return response()->json($this->taskService->update($data, $id)); } - public function getTasksByCategory(TaskCategoryRequest $request) - { - return response()->json( - [ - 'message' => 'Request success', - 'data' => $this->taskService->taskCategory($request) - ], - 200 - ); + + public function getTasksByCategory(Request $request) + { + // Validation of input + $validator = Validator::make($request->all(), [ + 'category_id' => 'required|integer' + ]); + if ($validator->fails()) { + return response()->json([ + 'Error' => 'Request failed', + 'message' => $validator->errors() + ], 400); + } + // Search for the category + $allTasks = $this->taskService->all(); + Sort::sortAll($request); + + $newArr = []; + foreach ($allTasks as $value) { + if (isset($value['category_id']) && $value['category_id'] == $request->category_id) { + array_push($newArr, $value); + } + } + return response()->json([ + 'message' => 'Request success', + 'data' => $newArr + ], 200); } @@ -96,22 +128,108 @@ public function editTask(Request $request, $id) public function taskcollection(Request $request) { - return response()->json($this->taskService->taskCollection($request)); + $allTasks = $this->taskService->all(); + Sort::sortAll($request); + + $sort = $request->order; + if ($sort){ + $allTasks = collect($allTasks->sortBy('created_at'))->toArray; + } + + $time = time(); + $arr = array(); + foreach ($allTasks as $value) { + if (array_key_exists('end_date', [$value])) { + $end_date = $value['end_date']; + $convert_date = strtotime($end_date); + if ($convert_date >= $time) { + + $arr = $value; + } + } + } + return response()->json($arr); } public function sort(Request $request) { - return response()->json($this->taskService->sort($request)); + $parameter = $request->sort; + $tasks = $this->taskService->all(); + $collectionTasks = collect($tasks)->sortBy($parameter); + return $collectionTasks; } public function addTask(AddTaskRequest $request, $todoId) { - return response()->json(["status" => "success", "type" => "Todo", "data" => $this->taskService->add($request, $todoId)], 200); + $todo = $this->todoService->find($todoId); + + if (isset($todo['status']) && $todo['status'] == 404) { + return response()->json($todo, 404); + } + + + $newTasks = [ + "task_id" => Str::uuid(), "title" => $request->title, + "recurring" => $request->recurring, "status" => 0 + ]; + + array_push($todo['tasks'], $newTasks); + unset($todo['_id']); + + $result = $this->todoService->update($todo, $todoId); + + if (isset($result['modified_documents']) && $result['modified_documents'] > 0) { + + // Publish To Centrifugo + $todoWithId = array_merge(['_id' => $todoId], $todo); + $this->todoService->publishToRoomChannel($todo['channel'], $todoWithId, "Task", "create"); + // Send Mail + $user_ids = $this->collaboratorInstance->listAllUsersInTodo($todo); + $this->collaboratorInstance->sendMails($user_ids, 'Task Added', 'A task with the title'.$request->title.'has been added to the todo'); + + return response()->json(["status" => "success", "type" => "Todo", "data" => $todoWithId], 200); + } + + return response()->json(['status' => "error", 'message' => $result], 500); } public function markTask(Request $request, $todoId) { - return response()->json(["status" => "success", "data" => $this->taskService->MarkTask($request, $todoId)], 200); + $adminExist = false; + // inialize value for task + $todo = $this->todoService->findBy('_id', $todoId); + if (isset($todo['status']) && $todo['status'] == 404) { + return response()->json($todo, 404); + } + if ($todo['user_id'] != $request->user_id) { + foreach ($todo['collaborators'] as $key => $value) { + if ($value['user_id'] == $request->user_id && $value['admin_status'] == 1) { + $adminExist = true; + } + } + } else { + $adminExist = true; + } + for ($i = 0; $i < count($todo['tasks']); $i++) { + if ($todo['tasks'][$i]['task_id'] == $request->task_id) { + $todo['tasks'][$i]['status'] = $request->status; + } + } + + unset($todo['_id']); + + $result = $this->todoService->update($todo, $todoId); + if (isset($result['modified_documents']) && $result['modified_documents'] > 0) { + $todoWithId = array_merge(['_id' => $todoId], $todo); + $this->todoService->publishToRoomChannel($todo['channel'], $todoWithId, 'todo', 'update'); + + // Send Mail + $user_ids = $this->collaboratorInstance->listAllUsersInTodo($todo); + $this->collaboratorInstance->sendMails($user_ids, 'Task Added', 'A task with the title' . $request->title . 'has been marked in the todo'); + return response()->json(["status" => "success", "data" => $todoWithId], 200); + } else { + return response()->json(["status" => "error", "data" => $result], 500); + } } } diff --git a/app/Http/Controllers/TodoController.php b/app/Http/Controllers/TodoController.php index 181d0039..b7bbc3e8 100644 --- a/app/Http/Controllers/TodoController.php +++ b/app/Http/Controllers/TodoController.php @@ -11,7 +11,6 @@ use App\Helpers\Sort; use App\Providers\SidebarEvent; - class TodoController extends Controller { diff --git a/app/Http/Requests/TaskCategoryRequest.php b/app/Http/Requests/TaskCategoryRequest.php deleted file mode 100644 index fa8b4bd4..00000000 --- a/app/Http/Requests/TaskCategoryRequest.php +++ /dev/null @@ -1,30 +0,0 @@ - 'required|integer' - ]; - } -} diff --git a/app/Services/TaskService.php b/app/Services/TaskService.php index cf351327..cad73257 100644 --- a/app/Services/TaskService.php +++ b/app/Services/TaskService.php @@ -2,23 +2,20 @@ namespace App\Services; -use Carbon\Carbon; -use App\Helpers\Sort; use App\Helpers\Response; -use Illuminate\Support\Str; -use App\Helpers\Collaborator; use App\Repositories\TaskRepository; +//use App\Services\ServiceTrait; class TaskService extends TaskRepository { + //use ServiceTrait; /** * @return mixed */ public function all() { - $task = Response::checkAndServe($this->httpRepository->all()); - abort_if(isset($task['status']) && $task['status'] == '404', 200, 'Tasks not found'); - return $task; + + return Response::checkAndServe($this->httpRepository->all()); } /** @@ -27,6 +24,7 @@ public function all() */ public function create(array $data) { + return Response::checkAndServe($this->httpRepository->create($data)); } @@ -36,6 +34,7 @@ public function create(array $data) */ public function find($id) { + return Response::checkAndServe($this->httpRepository->find($id)); } @@ -45,9 +44,7 @@ public function find($id) */ public function findBy($attr, $value) { - $task = Response::checkAndServe($this->httpRepository->findBy($attr, $value)); - abort_if(empty($tasks), 200, "Todo not found"); - return $task; + return Response::checkAndServe($this->httpRepository->findBy($attr, $value)); } /** @@ -57,6 +54,7 @@ public function findBy($attr, $value) */ public function update($data, $id) { + return Response::checkAndServe($this->httpRepository->update($id, $data)); } @@ -66,6 +64,7 @@ public function update($data, $id) */ public function delete($id) { + return Response::checkAndServe($this->httpRepository->delete($id)); } @@ -97,139 +96,4 @@ public function getLatestTask() } } - /** - * @param string - * @return mixed - */ - public function toggleStatus($id) - { - $task = $this->find($id); // Get the Task - - $archived = array_key_exists('archived_at', $task) && $task['archived_at'] ? '' : Carbon::now(); // Set new date if it is null or empty, else set back to empty - - // prepare the payload - $data = array(); - $data['archived_at'] = $archived; - - //response from zccore - return response()->json($this->update($data, $id)); - } - - public function taskCategory($request) - { - // Search for the category - $allTasks = $this->all(); - Sort::sortAll($request); - - $newArr = []; - foreach ($allTasks as $value) { - if (isset($value['category_id']) && $value['category_id'] == $request->category_id) { - array_push($newArr, $value); - } - } - - return $newArr; - } - - public function taskCollection($request) - { - $allTasks = $this->all(); - Sort::sortAll($request); - - $sort = $request->order; - if ($sort){ - $allTasks = collect($allTasks->sortBy('created_at'))->toArray; - } - - $time = time(); - $arr = array(); - foreach ($allTasks as $value) { - if (array_key_exists('end_date', [$value])) { - $end_date = $value['end_date']; - $convert_date = strtotime($end_date); - if ($convert_date >= $time) { - - $arr = $value; - } - } - } - return $arr; - } - - public function sort($request) - { - $parameter = $request->sort; - $tasks = $this->all(); - $collectionTasks = collect($tasks)->sortBy($parameter); - return $collectionTasks; - } - - public function MarkTask($request, $todoId) - { - // inialize value for task - $todo = $this->findBy('_id', $todoId); - if (isset($todo['status']) && $todo['status'] == 404) { - return response()->json($todo, 404); - } - for ($i = 0; $i < count($todo['tasks']); $i++) { - if ($todo['tasks'][$i]['task_id'] == $request->task_id) { - $todo['tasks'][$i]['status'] = $request->status; - } - } - - unset($todo['_id']); - - $result = $this->todoService->update($todo, $todoId); - if (isset($result['modified_documents']) && $result['modified_documents'] > 0) { - $todoWithId = array_merge(['_id' => $todoId], $todo); - $this->publishToRoomChannel($todo['channel'], $todoWithId, 'todo', 'update'); - - // Send Mail - $user_ids = Collaborator::listAllUsersInTodo($todo); - $collab = new Collaborator; - $collab->sendMails($user_ids, 'Task Added', 'A task with the title' . $request->title . 'has been marked in the todo'); - return $todoWithId; - } else { - abort(500, $result); - } - } - - /** - * @param Request $request, $todoId string - * @return mixed - */ - public function add($request, $todoId) - { - $todo = $this->find($todoId); - - if (isset($todo['status']) && $todo['status'] == 404) { - return response()->json($todo, 404); - } - - - $newTasks = [ - "task_id" => Str::uuid(), "title" => $request->title, - "recurring" => $request->recurring, "status" => 0 - ]; - - array_push($todo['tasks'], $newTasks); - unset($todo['_id']); - - $result = $this->update($todo, $todoId); - - if (isset($result['modified_documents']) && $result['modified_documents'] > 0) { - - // Publish To Centrifugo - $todoWithId = array_merge(['_id' => $todoId], $todo); - $this->publishToRoomChannel($todo['channel'], $todoWithId, "Task", "create"); - // Send Mail - $user_ids = Collaborator::listAllUsersInTodo($todo); - $collab = new Collaborator; - $collab->sendMails($user_ids, 'Task Added', 'A task with the title'.$request->title.'has been added to the todo'); - - return $todoWithId; - } - - abort(500, $result); - } } diff --git a/composer.json b/composer.json index 90264a8b..4f6a9280 100644 --- a/composer.json +++ b/composer.json @@ -18,9 +18,7 @@ "laravel/sail": "^1.0.1", "mockery/mockery": "^1.4.2", "nunomaduro/collision": "^5.0", - "phpstan/phpstan": "^0.12.99", - "phpunit/phpunit": "^9.3.3", - "squizlabs/php_codesniffer": "^3.6" + "phpunit/phpunit": "^9.3.3" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 95ba1f41..c06d7e5e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "516abf23f5901c10fa6d2236db0182f0", + "content-hash": "6cadd819a6415b190ae2704ced6e6c8b", "packages": [ { "name": "asm89/stack-cors", @@ -6053,70 +6053,6 @@ }, "time": "2021-09-10T09:02:12+00:00" }, - { - "name": "phpstan/phpstan", - "version": "0.12.99", - "source": { - "type": "git", - "url": "https://github.com/phpstan/phpstan.git", - "reference": "b4d40f1d759942f523be267a1bab6884f46ca3f7" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/b4d40f1d759942f523be267a1bab6884f46ca3f7", - "reference": "b4d40f1d759942f523be267a1bab6884f46ca3f7", - "shasum": "" - }, - "require": { - "php": "^7.1|^8.0" - }, - "conflict": { - "phpstan/phpstan-shim": "*" - }, - "bin": [ - "phpstan", - "phpstan.phar" - ], - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "0.12-dev" - } - }, - "autoload": { - "files": [ - "bootstrap.php" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "description": "PHPStan - PHP Static Analysis Tool", - "support": { - "issues": "https://github.com/phpstan/phpstan/issues", - "source": "https://github.com/phpstan/phpstan/tree/0.12.99" - }, - "funding": [ - { - "url": "https://github.com/ondrejmirtes", - "type": "github" - }, - { - "url": "https://github.com/phpstan", - "type": "github" - }, - { - "url": "https://www.patreon.com/phpstan", - "type": "patreon" - }, - { - "url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan", - "type": "tidelift" - } - ], - "time": "2021-09-12T20:09:55+00:00" - }, { "name": "phpunit/php-code-coverage", "version": "9.2.7", @@ -7502,62 +7438,6 @@ ], "time": "2020-09-28T06:39:44+00:00" }, - { - "name": "squizlabs/php_codesniffer", - "version": "3.6.1", - "source": { - "type": "git", - "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", - "reference": "f268ca40d54617c6e06757f83f699775c9b3ff2e" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/f268ca40d54617c6e06757f83f699775c9b3ff2e", - "reference": "f268ca40d54617c6e06757f83f699775c9b3ff2e", - "shasum": "" - }, - "require": { - "ext-simplexml": "*", - "ext-tokenizer": "*", - "ext-xmlwriter": "*", - "php": ">=5.4.0" - }, - "require-dev": { - "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" - }, - "bin": [ - "bin/phpcs", - "bin/phpcbf" - ], - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.x-dev" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Greg Sherwood", - "role": "lead" - } - ], - "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", - "homepage": "https://github.com/squizlabs/PHP_CodeSniffer", - "keywords": [ - "phpcs", - "standards" - ], - "support": { - "issues": "https://github.com/squizlabs/PHP_CodeSniffer/issues", - "source": "https://github.com/squizlabs/PHP_CodeSniffer", - "wiki": "https://github.com/squizlabs/PHP_CodeSniffer/wiki" - }, - "time": "2021-10-11T04:00:11+00:00" - }, { "name": "theseer/tokenizer", "version": "1.2.1", @@ -7618,5 +7498,5 @@ "php": "^7.3|^8.0" }, "platform-dev": [], - "plugin-api-version": "2.0.0" + "plugin-api-version": "2.1.0" }