ManagementSystem/BACKEND/Modules/Admin/app/Http/Controllers/JiraController.php

173 lines
6.7 KiB
PHP

<?php
namespace Modules\Admin\app\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Traits\HasFilterRequest;
use App\Traits\HasOrderByRequest;
use App\Traits\HasSearchRequest;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Services\JiraService;
use GuzzleHttp\Promise\Utils;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Http\JsonResponse;
class JiraController extends Controller
{
use HasOrderByRequest;
use HasFilterRequest;
use HasSearchRequest;
protected $jiraService;
public function __construct(JiraService $jiraService)
{
$this->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);
}
}
}