jiraService = $jiraService; } public function fetchAllIssues($startAt = 0, $maxResults = 50) { try { $allIssues = []; $projects = $this->jiraService->getAllProjects(); $promises = []; foreach ($projects as $project) { if ($project['key'] !== 'GCT') { $promises[] = $this->jiraService->getIssuesAsync($project['key'], $startAt, $maxResults) ->then(function ($response) use ($project) { $issues = []; $data = json_decode($response->getBody()->getContents(), true); foreach ($data['issues'] as $issue) { $issues[] = [ 'summary' => $issue['fields']['summary'] ?? null, 'desc' => $issue['fields']['description']['content'][0] ?? null, 'assignee' => $issue['fields']['assignee']['displayName'] ?? null, 'status' => $issue['fields']['status']['name'] ?? null, 'worklogs' => json_encode(array_map(function ($log) { return [ 'author' => $log['author']['displayName'] ?? null, 'timeSpent' => $log['timeSpent'] ?? null, 'started' => $log['started'] ?? null, 'updated' => $log['updated'] ?? null, ]; }, $issue['fields']['worklog']['worklogs'] ?? []), JSON_PRETTY_PRINT), 'originalEstimate' => isset($issue['fields']['timeoriginalestimate']) ? $issue['fields']['timeoriginalestimate'] / 60 / 60 : null, 'timeSpent' => isset($issue['fields']['timespent']) ? $issue['fields']['timespent'] / 60 / 60 : null ]; } return ['project' => $project['name'], 'issues' => $issues]; }); } } $results = Utils::settle($promises)->wait(); foreach ($results as $result) { if ($result['state'] === 'fulfilled') { $allIssues[] = $result['value']; } else { // Handle the errors if necessary \Log::error("Error fetching issues: " . $result['reason']->getMessage()); } } return response()->json($allIssues); } catch (\Exception $e) { return response()->json(['error' => $e->getMessage()], 500); } } public function getAllProject() { $projects = $this->jiraService->getAllProjects(); return response()->json([ 'data' => $projects, 'status' => true ], 200); } public function fetchIssuesByProject(Request $request) { $project = ['key' => $request->key, 'name' => $request->name]; $allIssues = []; if ($project['key'] !== 'GCT') { $startAt = 0; $issues = []; $total = 0; $issueLength = 0; $checked = true; // while ($checked) { $response = $this->jiraService->getIssues($project['key'], $startAt); $total = $response['total']; $issueLength = count($response['issues']); foreach ($response['issues'] as $issue) { $issues[] = [ 'summary' => $issue['fields']['summary'] ?? null, 'desc' => $issue['fields']['description']['content'][0] ?? null, 'assignee' => $issue['fields']['assignee']['displayName'] ?? null, 'status' => $issue['fields']['status']['name'] ?? null, 'worklogs' => json_encode(array_map(function ($log) { return [ 'author' => $log['author']['displayName'] ?? null, 'timeSpent' => $log['timeSpent'] ?? null, 'started' => $log['started'] ?? null, 'updated' => $log['updated'] ?? null, ]; }, $issue['fields']['worklog']['worklogs'] ?? []), JSON_PRETTY_PRINT), 'originalEstimate' => isset($issue['fields']['timeoriginalestimate']) ? $issue['fields']['timeoriginalestimate'] / 60 / 60 : null, 'timeSpent' => isset($issue['fields']['timespent']) ? $issue['fields']['timespent'] / 60 / 60 : null ]; // } // if (($startAt + $issueLength >= $total && $total !== 0) || $total === 0) { // $checked = false; // } // $startAt += $issueLength; } $allIssues[] = [ 'project' => $project['name'], 'issues' => $issues ]; return response()->json([ 'data' => $allIssues, 'status' => true ], 200); } return response()->json([ 'data' => $allIssues, 'status' => false ], 500); } public function exportToExcel() { $allIssues = $this->fetchAllIssues()->original; $fileName = 'allIssues.xlsx'; Excel::store(new \App\Exports\IssuesExport($allIssues), $fileName); return response()->download(storage_path("app/{$fileName}")); } public function getAllUserWorkLogs(Request $request) { try { $workLogs = $this->jiraService->getAllUserWorkLogs($request->startDate, $request->endDate); return response()->json([ 'data' => $workLogs, 'status' => true ], 200); } catch (\Exception $e) { return response()->json(['error' => $e->getMessage()], 500); } } public function sendReport() { $now = new DateTime('now', new DateTimeZone('Asia/Bangkok')); $dateFormatted = $now->format('Y-m-d'); $workLogs = $this->jiraService->getAllUserWorkLogs('2024-06-14', '2024-06-14'); // $tasks = [ // [ // 'status' => 'Done', // 'summary' => '[LT-37] TEST TỔNG THỂ & BUILD PRODUCTION GD 1', // 'estimate' => '8H', // 'total_time_spent' => '8.5H', // 'time_spent' => '2H', // 'start_date' => '00:30 2024/05/28', // 'comment' => 'Check on stage' // ], // [ // 'status' => 'In Progress', // 'summary' => '[IPSPro-826] TEST TASK ERP', // 'estimate' => '0H', // 'total_time_spent' => '13H', // 'time_spent' => '1H', // 'start_date' => '03:30 2024/05/28', // 'comment' => 'check ERP update Incoming & PO add SN' // ] // ]; $tasksByUser = $this->formatWorkLogsByUser($workLogs); Mail::to('luanlt632000@gmail.com')->send(new WorklogReport($tasksByUser)); // return "Email sent successfully!"; return response()->json([ 'data' => $tasksByUser, 'status' => true ], 200); } private function formatWorkLogsByUser($workLogs) { // Assuming each workLog has a 'user' field $tasksByUser = []; foreach ($workLogs as $log) { $user = $log['username']; if (!isset($tasksByUser[$user])) { $tasksByUser[$user] = []; } foreach ($log['information']['issues'] as $issue) { // $today = Carbon::today('Asia/Ho_Chi_Minh'); $today = Carbon::create('2024','6','14'); $filteredWorklogs = []; foreach ($issue['fields']['worklog']['worklogs'] as $worklog) { $started = Carbon::parse($worklog['started']); if ($started->isSameDay($today) && $worklog['updateAuthor']['displayName'] == $user) { $filteredWorklogs[] = $worklog; } } $tasksByUser[$user][] = [ 'status' => $issue['fields']['status']['name'], 'summary' => $issue['fields']['summary'], 'estimate' => $issue['fields']['timeoriginalestimate'], 'time_spent' => $issue['fields']['timespent'], 'worklogs' => $filteredWorklogs, ]; } } return $tasksByUser; } }