258 lines
		
	
	
		
			9.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
			
		
		
	
	
			258 lines
		
	
	
		
			9.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
<?php
 | 
						|
 | 
						|
namespace Modules\Admin\app\Http\Controllers;
 | 
						|
 | 
						|
use App\Http\Controllers\Controller;
 | 
						|
use App\Models\User;
 | 
						|
use App\Services\JiraService;
 | 
						|
use App\Traits\AnalyzeData;
 | 
						|
use App\Traits\HasFilterRequest;
 | 
						|
use App\Traits\HasOrderByRequest;
 | 
						|
use App\Traits\HasSearchRequest;
 | 
						|
use Carbon\Carbon;
 | 
						|
use Carbon\CarbonPeriod;
 | 
						|
use Illuminate\Http\Request;
 | 
						|
use Modules\Admin\app\Models\Criteria;
 | 
						|
use Modules\Admin\app\Models\Sprint;
 | 
						|
use Modules\Admin\app\Models\SprintCriteria;
 | 
						|
use Modules\Admin\app\Models\UserCriteria;
 | 
						|
 | 
						|
class CriteriasController extends Controller
 | 
						|
{
 | 
						|
    use HasOrderByRequest;
 | 
						|
    use HasFilterRequest;
 | 
						|
    use HasSearchRequest;
 | 
						|
    use AnalyzeData;
 | 
						|
 | 
						|
    protected $jiraService;
 | 
						|
 | 
						|
    public function __construct(JiraService $jiraService)
 | 
						|
    {
 | 
						|
        $this->jiraService = $jiraService;
 | 
						|
    }
 | 
						|
 | 
						|
    public function getAllUserJira()
 | 
						|
    {
 | 
						|
        $all_users = User::all();
 | 
						|
        $users = $this->jiraService->getUsersByEmails($all_users);
 | 
						|
        return $users;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Get all criterias of a sprint
 | 
						|
     *
 | 
						|
     * @param int $sprintId The id of the sprint
 | 
						|
     * @return \Illuminate\Database\Eloquent\Collection|null A collection of Criteria models or null if the sprint is not found
 | 
						|
     */
 | 
						|
    public function getCriteriasForSprint($sprintId)
 | 
						|
    {
 | 
						|
        $sprint = Sprint::with('criterias')->find($sprintId);
 | 
						|
        if (!$sprint) {
 | 
						|
            $allUser = self::getAllUserJira();
 | 
						|
 | 
						|
            $issues = $this->jiraService->getAllIssueByIdSprint($sprintId);
 | 
						|
            $uniqueUsers = [];
 | 
						|
            // echo json_encode($issues);
 | 
						|
            // die;
 | 
						|
            if ($issues) {
 | 
						|
                $issues = $issues["issues"];
 | 
						|
                foreach ($issues as $key => $issue) {
 | 
						|
                    $assignee = $issue["fields"]["assignee"];
 | 
						|
                    if ($assignee) {
 | 
						|
                        if (!in_array($assignee["accountId"], $uniqueUsers)) {
 | 
						|
                            array_push($uniqueUsers, $assignee["accountId"]);
 | 
						|
                        }
 | 
						|
                    }
 | 
						|
                }
 | 
						|
            }
 | 
						|
            // echo json_encode(allUser)
 | 
						|
            // dd();
 | 
						|
 | 
						|
            $filteredObjects = array_filter($allUser, function ($user) use ($uniqueUsers) {
 | 
						|
                $user = $user[0];
 | 
						|
                return in_array($user['accountId'], $uniqueUsers);
 | 
						|
            });
 | 
						|
 | 
						|
            $result = array_map(function ($item) {
 | 
						|
                $item = $item[0];
 | 
						|
                return [
 | 
						|
                    'accountId' => $item['accountId'],
 | 
						|
                    'emailAddress' => $item['emailAddress'],
 | 
						|
                    'displayName' => $item['displayName'],
 | 
						|
                ];
 | 
						|
            }, $filteredObjects);
 | 
						|
 | 
						|
            //Get all user trong db => find info theo id
 | 
						|
 | 
						|
            //Get criteria for sprint
 | 
						|
            $criterias = Criteria::where('type', "SPRINT")->get();
 | 
						|
            //Get criteria for user
 | 
						|
            $users = Criteria::where('type', "MEMBER")->get();
 | 
						|
            echo json_encode($result);
 | 
						|
            echo json_encode($users);
 | 
						|
            dd();
 | 
						|
 | 
						|
 | 
						|
 | 
						|
            $data = [
 | 
						|
                "criterias" => $criterias,
 | 
						|
                "users" => $users
 | 
						|
            ];
 | 
						|
        } else {
 | 
						|
            $users = $sprint->users()->get();
 | 
						|
            $data = $sprint;
 | 
						|
            $data->users = $users;
 | 
						|
        }
 | 
						|
        return AbstractController::ResultSuccess($data);
 | 
						|
    }
 | 
						|
 | 
						|
    public function convertDataReponse($criterias)
 | 
						|
    {
 | 
						|
        $data = [];
 | 
						|
        foreach ($criterias as $key => $criteria) {
 | 
						|
            $data[$key]["criteria_name"] = $criteria->name;
 | 
						|
            $data[$key]["description"] = $criteria->description;
 | 
						|
            $data[$key]["type"] = $criteria->type;
 | 
						|
            foreach ($criteria->users as $keyChild => $user) {
 | 
						|
                $dataChild[$keyChild]["user_id"] = $user->id;
 | 
						|
                $dataChild[$keyChild]["user_name"] = $user->name;
 | 
						|
                $dataChild[$keyChild]["email"] = $user->email;
 | 
						|
                $dataChild[$keyChild]["point"] = $user->pivot->point;
 | 
						|
                $dataChild[$keyChild]["note"] = $user->pivot->note;
 | 
						|
                $dataChild[$keyChild]["sprint_id"] = $user->pivot->sprint_id;
 | 
						|
            }
 | 
						|
            $data[$key]['users'] = $dataChild;
 | 
						|
        }
 | 
						|
 | 
						|
        return $data;
 | 
						|
    }
 | 
						|
    /**
 | 
						|
     * Get all criterias of a user
 | 
						|
     *
 | 
						|
     * @param int $userId The id of the user
 | 
						|
     * @return \Illuminate\Database\Eloquent\Collection|null A collection of Criteria models or null if the user is not found
 | 
						|
     */
 | 
						|
    public function getCriteriasForUser($userId)
 | 
						|
    {
 | 
						|
        $criterias = Criteria::whereHas('users', function ($query) use ($userId) {
 | 
						|
            $query->where('user_id', $userId);
 | 
						|
        })->with(['users' => function ($query) use ($userId) {
 | 
						|
            $query->where('user_id', $userId);
 | 
						|
        }])->get();
 | 
						|
 | 
						|
        $data = self::convertDataReponse($criterias);
 | 
						|
        return AbstractController::ResultSuccess($data);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Get all criterias of a user for a sprint
 | 
						|
     *
 | 
						|
     * @param int $userId The id of the user
 | 
						|
     * @param int $sprintId The id of the sprint
 | 
						|
     * @return \Illuminate\Database\Eloquent\Collection|null A collection of Criteria models or null if the user or sprint is not found
 | 
						|
     */
 | 
						|
    public function getCriteriasForUserBySprint($userId, $sprintId)
 | 
						|
    {
 | 
						|
        $criterias = Criteria::whereHas('users', function ($query) use ($userId, $sprintId) {
 | 
						|
            $query->where('user_id', $userId)->where('sprint_id', $sprintId);
 | 
						|
        })->with(['users' => function ($query) use ($userId, $sprintId) {
 | 
						|
            $query->where('user_id', $userId)->where('sprint_id', $sprintId);
 | 
						|
        }])->get();
 | 
						|
 | 
						|
        $data = self::convertDataReponse($criterias);
 | 
						|
        return AbstractController::ResultSuccess($data);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Get all criterias
 | 
						|
     *
 | 
						|
     * @return \Illuminate\Database\Eloquent\Collection A collection of Criteria models
 | 
						|
     */
 | 
						|
    public function getAllCriterias(Request $request)
 | 
						|
    {
 | 
						|
        if ($request->type) {
 | 
						|
            $responseData = Criteria::where('type', $request->type)->get();
 | 
						|
        } else {
 | 
						|
            $responseData = Criteria::all();
 | 
						|
        }
 | 
						|
        return AbstractController::ResultSuccess($responseData);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Update or create multiple SprintCriteria and UserCriteria records
 | 
						|
     *
 | 
						|
     * @param int $sprintId The id of the sprint
 | 
						|
     * @param array $criteriaData An array of criteria data. Each element of the array should be an associative array with the following keys:
 | 
						|
     *     - criteria_id: The id of the criteria
 | 
						|
     *     - point: The point of the criteria
 | 
						|
     *     - expect_result: The expected result of the criteria
 | 
						|
     *     - actual_result: The actual result of the criteria
 | 
						|
     *     - note: The note of the criteria
 | 
						|
     *     - user_id (optional): The id of the user
 | 
						|
     *     - user_point (optional): The point of the user (default is the same as point)
 | 
						|
     *     - user_note (optional): The note of the user (default is the same as note)
 | 
						|
     * @return bool True if successful, false otherwise
 | 
						|
     */
 | 
						|
    public function updateCriteriasForSprint($sprintId, Request $request)
 | 
						|
    {
 | 
						|
        $user = auth('admins')->user();
 | 
						|
 | 
						|
        // Tạo hoặc lấy Sprint
 | 
						|
        $sprint = Sprint::firstOrCreate(['id' => $sprintId], [
 | 
						|
            'name' => 'Default Sprint Name',
 | 
						|
            'start_date' => now(),
 | 
						|
            'end_date' => now()->addDays(7),
 | 
						|
            'project_id' => 1,
 | 
						|
        ]);
 | 
						|
 | 
						|
        $criteriaData = $request->criterias;
 | 
						|
 | 
						|
        foreach ($criteriaData as $data) {
 | 
						|
            // print_r($data);
 | 
						|
            $criteriaId = (int)$data['criteria_id'];
 | 
						|
 | 
						|
            // Kiểm tra xem criteria có tồn tại không
 | 
						|
            $criteria = Criteria::find($criteriaId);
 | 
						|
            if (!$criteria) {
 | 
						|
                // Nếu không tồn tại, có thể ném ra ngoại lệ hoặc trả về lỗi
 | 
						|
                return response()->json(['error' => 'Criteria ID ' . $criteriaId . ' does not exist'], 400);
 | 
						|
            }
 | 
						|
 | 
						|
            $point = $data['point'];
 | 
						|
            $expectResult = $data['expect_result'];
 | 
						|
            $actualResult = $data['actual_result'];
 | 
						|
            $note = $data['note'];
 | 
						|
 | 
						|
            // Cập nhật hoặc tạo mới SprintCriteria
 | 
						|
            SprintCriteria::updateOrCreate(
 | 
						|
                ['sprint_id' => $sprint->id, 'criteria_id' => $criteriaId],
 | 
						|
                ['point' => $point, 'expect_result' => $expectResult, 'actual_result' => $actualResult, 'note' => $note]
 | 
						|
            );
 | 
						|
 | 
						|
            if (isset($data['users'])) {
 | 
						|
                // Xóa hết các bản ghi UserCriteria theo sprint_id và criteria_id
 | 
						|
                UserCriteria::where('criteria_id', $criteriaId)
 | 
						|
                    ->where('sprint_id', $sprint->id)
 | 
						|
                    ->delete();
 | 
						|
                foreach ($data['users'] as $userData) {
 | 
						|
                    $userId = $userData['user_id'];
 | 
						|
                    $userPoint = $userData['point'] ?? $point;
 | 
						|
                    $userNote = $userData['note'] ?? $note;
 | 
						|
 | 
						|
                    // Chèn lại các bản ghi mới vào bảng UserCriteria
 | 
						|
                    UserCriteria::create([
 | 
						|
                        'user_id' => $userId,
 | 
						|
                        'criteria_id' => (int)$criteriaId,
 | 
						|
                        'sprint_id' => $sprint->id,
 | 
						|
                        'point' => $userPoint,
 | 
						|
                        'note' => $userNote,
 | 
						|
                        'created_by' => $user->name
 | 
						|
                    ]);
 | 
						|
                }
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        return AbstractController::ResultSuccess("");
 | 
						|
    }
 | 
						|
}
 |