add: tests

This commit is contained in:
Michael 2024-08-12 15:55:03 +02:00
parent eebf859afa
commit ce7f620984
16 changed files with 681 additions and 152 deletions

View file

@ -0,0 +1,34 @@
<?php
namespace App\Http\Controllers\ThirdPartyServices;
use App\Http\Controllers\Controller;
use App\Mail\sendExportNotice;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use RuntimeException;
class EmailService extends Controller
{
public function __construct()
{
$this->email_address_recepient = env('EMAIL_ADDRESS_RECEPIENT', null);
$this->email_prefix_subject = env('EMAIL_PREFIX_SUBJECT', null);
$this->nextcloud_upload_folder_web_link = env('NEXTCLOUD_UPLOAD_FOLDER_WEB_LINK', false);
}
function send_export_via_email(string $from_date, string $to_date, string $file_name){
try{
$email_subject = $this->email_prefix_subject . ' '. $from_date.' - '.$to_date;
Mail::to($this->email_address_recepient)->send(new sendExportNotice(['export_date' => $email_subject, 'file_name' => $file_name, 'upload_folder_web_link' => $this->nextcloud_upload_folder_web_link], $email_subject));
return true;
}catch (\Exception $e) {
Log::error('send_export_via_email Email error - ' . $e->getMessage());
//throw new RuntimeException("E-EMAIL");
return false;
}
}
}

View file

@ -0,0 +1,48 @@
<?php
namespace App\Http\Controllers\ThirdPartyServices;
use App\Http\Controllers\Controller;
use App\Mail\sendExportNotice;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use RuntimeException;
class NextcloudService extends Controller
{
private $nextcloud_url;
private $nextcloud_user;
private $nextcloud_password;
public function __construct()
{
$this->nextcloud_url = env('NEXTCLOUD_URL', false);
$this->nextcloud_user = env('NEXTCLOUD_USER', false);
$this->nextcloud_password = env('NEXTCLOUD_PASSWORD', false);
$this->nextcloud_upload_folder_path = env('NEXTCLOUD_UPLOAD_FOLDER_PATH', false);
$this->nextcloud_upload_folder_web_link = env('NEXTCLOUD_UPLOAD_FOLDER_WEB_LINK', false);
}
function nextcloud_upload_csv(string $file_name, string $file_path)
{
try {
$nextcloud_url = $this->nextcloud_url . '/remote.php/dav/files/' . $this->nextcloud_user . '/' . $this->nextcloud_upload_folder_path . '/' . $file_name;
$file_contents = fopen($file_path, 'r');
try {
$res = Http::withBasicAuth($this->nextcloud_user, $this->nextcloud_password)
->attach($file_name, $file_contents)
->put($nextcloud_url);
$res->throw();
} catch (\Exception $e) {
Log::error('nextcloud_upload_csv Upload error - ' . $e->getMessage());
throw new RuntimeException("E-NEXTCLOUD-UPLOAD");
}
// Close the file handle
fclose($file_contents);
return $res;
} catch (\Exception $e) {
return false;
}
}
}

View file

@ -0,0 +1,208 @@
<?php
namespace App\Http\Controllers\ThirdPartyServices;
use App\Http\Controllers\Controller;
use App\Mail\sendExportNotice;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use RuntimeException;
use stdClass;
class OpenProjectService extends Controller
{
private $open_project_url;
public function __construct()
{
$this->open_project_url = env('OPEN_PROJECT_URL', false);
$this->open_project_token = env('OPEN_PROJECT_TOKEN', false);
$this->open_project_project_id = env('OPEN_PROJECT_PROJECT_ID', false);
$this->open_project_task_name = env('OPEN_PROJECT_TASK_NAME', false);
$this->open_project_group_id = env('OPEN_PROJECT_GROUP_ID', false);
$this->open_project_special_nicks_list = env('OPEN_PROJECT_SPECIAL_NICKS_LIST', false);
$this->open_project_work_package_type_id = env('OPEN_PROJECT_WORK_PACKAGE_TYPE_ID', false);
$this->open_project_work_package_user_owner_id = env('OPEN_PROJECT_WORK_PACKAGE_USER_OWNER_ID', false);
}
private function open_project_project_member_task_id(stdClass $open_project_member)
{
try {
$filter = [
[
"subject" => [
"operator" => "~",
"values" => [$this->open_project_task_name . ' ' . $open_project_member->name]
]
]
];
$filter = json_encode($filter);
$open_project_project_work_packages_url = $this->open_project_url . '/api/v3/projects/' . $this->open_project_project_id . '/work_packages';
$open_project_project_member_task_id = Http::withBasicAuth('apikey', $this->open_project_token)->withHeaders(['Accept' => 'application/hal+json'])->get($open_project_project_work_packages_url, [
'filters' => $filter
]);
$open_project_project_member_task_id->throw();
} catch (\Exception $e) {
Log::error('open_project_add_task_to_agent E-OPEN_PROJECT-URL - ' . $e->getMessage());
throw new RuntimeException("E-OPEN_PROJECT-URL");
}
$open_project_project_member_task_id = json_decode($open_project_project_member_task_id->body());
try {
$open_project_project_member_task_id = $open_project_project_member_task_id->_embedded->elements;
return $open_project_project_member_task_id['0']->id;
} catch (\Exception $e) {
Log::error('open_project_project_member_task_id - No Task ' . $this->open_project_task_name . ' ' . $open_project_member->name . 'found ' . $e->getMessage());
return null;
}
}
private function open_project_get_main_group_members()
{
try {
$open_project_main_group_members_url = $this->open_project_url . '/api/v3/groups/' . $this->open_project_group_id;
$open_project_main_group_members = Http::withBasicAuth('apikey', $this->open_project_token)->withHeaders(['Accept' => 'application/hal+json'])->get($open_project_main_group_members_url);
$open_project_main_group_members->throw();
} catch (\Exception $e) {
Log::error('open_project_add_task_to_agent E-OPEN_PROJECT-URL - ' . $e->getMessage());
throw new RuntimeException("E-OPEN_PROJECT-URL");
}
$open_project_main_group_members = json_decode($open_project_main_group_members->body());
$open_project_main_group_members = $open_project_main_group_members->_embedded->members;
foreach ($open_project_main_group_members as $open_project_member) {
if (!isset($open_project_member->name)) throw new RuntimeException("E-OPEN_PROJECT-MEMBERS-NO-NAME");
$open_project_project_member_task_id = $this->open_project_project_member_task_id($open_project_member);
if (!$open_project_project_member_task_id) break;
$open_project_member_list[] = ['name' => strtolower($open_project_member->name), 'id' => $open_project_member->id, 'work_package_id' => $open_project_project_member_task_id, 'total' => 0];
}
return $open_project_member_list;
}
private
function git_extract_agents_total_sum(array $issues, array $company_agents)
{
$issues_totals = end($issues);
$agents_total_cost = [];
foreach ($company_agents as $agent_name) {
$agents_total_cost[$agent_name] = $issues_totals[$agent_name . ' costo'];
}
return $agents_total_cost;
}
private function git_open_project_handle_nick_discrepancies(array $open_project_member_list)
{
try {
$open_project_special_nicks_list = json_decode($this->open_project_special_nicks_list);
foreach ($open_project_member_list as &$open_project_member) {
foreach ($open_project_special_nicks_list as $nick_on_open_project => $nick_on_git) {
if ($open_project_member['name'] == $nick_on_open_project) {
$open_project_member['name'] = $nick_on_git;
}
}
}
return $open_project_member_list;
} catch (\Exception $e) {
Log::error('git_open_project_handle_nick_discrepancies E-OPEN_PROJECT-NICK - ' . $e->getMessage() . ' ' . $e->getFile() . ' ' . $e->getLine());
throw new RuntimeException("E-OPEN_PROJECT-NICK");
}
}
private
function open_project_add_to_members_total(array $open_project_member_list, array $git_extract_agents_total_sum)
{
try {
if (isset($this->open_project_special_nicks_list)) {
$open_project_member_list = $this->git_open_project_handle_nick_discrepancies($open_project_member_list);
}
foreach ($open_project_member_list as &$open_project_member) {
$open_project_member['total'] = $git_extract_agents_total_sum[$open_project_member['name']];
}
return $open_project_member_list;
} catch (\Exception $e) {
Log::error('open_project_add_to_members_total E-OPEN_PROJECT-ADD-TOTAL - ' . $e->getMessage());
throw new RuntimeException("E-OPEN_PROJECT-ADD-TOTAL");
}
}
private function set_parent_of_child_task($child_id, $parent_id)
{
try {
$open_project_work_package_set_parent = $this->open_project_url . '/api/v3/work_packages/' . $child_id;
$request_body = [
'parent' => $parent_id,
];
$open_project_work_package_parent = Http::withBasicAuth('apikey', $this->open_project_token)->withHeaders(['Accept' => 'application/hal+json'])->post($open_project_work_package_set_parent, json_encode($request_body));
$open_project_work_package_parent->throw();
return true;
} catch (\Exception $e) {
Log::error('open_project_add_task_to_agent E-OPEN_PROJECT-URL - ' . $e->getMessage());
throw new RuntimeException("E-OPEN_PROJECT-URL");
}
}
function open_project_create_tasks(string $package_name, array $open_project_member)
{
try {
$open_project_work_package_creation_url = $this->open_project_url . '/api/v3/projects/' . $this->open_project_project_id . '/work_packages';
$request_body = [
'subject' => $package_name,
'description' => [
"format" => 'plain',
"raw" => $open_project_member['total'],
],
"_links" => [
"type" => [
"href" => "/api/v3/types/1".$this->open_project_work_package_type_id
],
"assignee" => [
"href" => "/api/v3/users/".$this->open_project_work_package_user_owner_id
],
"responsible" => [
"href" => "/api/v3/users/".$this->open_project_work_package_user_owner_id
],
'project' => [
'href' => '/api/v3/projects/' . $this->open_project_project_id
]]
];
$open_project_work_package_creation = Http::withBasicAuth('apikey', $this->open_project_token)->withHeaders(['Accept' => 'application/hal+json'])->post($open_project_work_package_creation_url, json_encode($request_body));
$open_project_work_package_creation->throw();
return $open_project_work_package_creation->json()['id'];
} catch (\Exception $e) {
Log::error('Error creating work package', [
'status' => $open_project_work_package_creation->status(),
'body' => $open_project_work_package_creation->body(),
]);
Log::error('open_project_create_tasks E-OPEN_PROJECT-CREATE-SINGLE-TASK - ' . $e->getMessage() . ' ' . $e->getFile() . ' ' . $e->getLine());
throw new RuntimeException("E-OPEN_PROJECT-URL");
}
}
private function open_project_handle_tasks_creation(array $open_project_members_with_total, string $from_date, string $to_date)
{
try {
$package_name = $from_date . ' - ' . $to_date;
foreach ($open_project_members_with_total as $open_project_member) {
$child_work_package = $this->open_project_create_tasks($package_name, $open_project_member);
return $this->set_parent_of_child_task($child_work_package, $open_project_member['work_package_id']);
}
} catch (\Exception $e) {
Log::error('open_project_add_task_to_agent E-OPEN_PROJECT-URL - ' . $e->getMessage() . ' ' . $e->getFile() . ' ' . $e->getLine());
throw new RuntimeException("E-OPEN_PROJECT-URL");
}
}
function open_project_add_task_to_agent(array $issues, array $company_agents, string $from_date, string $to_date)
{
try {
$open_project_member_list = $this->open_project_get_main_group_members();
$git_extract_agents_total_sum = $this->git_extract_agents_total_sum($issues, $company_agents);
$open_project_members_with_total = $this->open_project_add_to_members_total($open_project_member_list, $git_extract_agents_total_sum);
return $this->open_project_handle_tasks_creation($open_project_members_with_total, $from_date, $to_date);
} catch (\Exception $e) {
Log::error('open_project - ' . $e->getMessage());
return false;
}
}
}

View file

@ -0,0 +1,40 @@
<?php
namespace App\Http\Controllers\ThirdPartyServices;
use App\Http\Controllers\Controller;
use App\Http\Controllers\ThirdPartyServices\NextcloudService;
use App\Http\Controllers\ThirdPartyServices\OpenProjectService;
use App\Http\Controllers\ThirdPartyServices\EmailService;
class ThirdPartyServices extends Controller
{
private $third_party_integrations_nextcloud;
private $third_party_integrations_open_project;
public function __construct()
{
$this->third_party_integrations_nextcloud = env('THIRD_PARTY_INTEGRATIONS_NEXTCLOUD', false);
$this->third_party_integrations_open_project = env('THIRD_PARTY_INTEGRATIONS_OPEN_PROJECT', false);
$this->email_send_allow = env('EMAIL_SEND_ALLOW', false);
$this->nextcloud_service = new \App\Http\Controllers\ThirdPartyServices\NextcloudService();
$this->open_project_service = new \App\Http\Controllers\ThirdPartyServices\OpenProjectService();
$this->email_service = new \App\Http\Controllers\ThirdPartyServices\EmailService();
$this->calculate_agent_cost = env('GITEA_CALCULATE_AGENT_COST', false);
}
function handle_third_party_services(string $file_name, string $file_path, array $issues, array $company_agents, string $from_date, string $to_date)
{
$nextcloud_res = $open_project_res = $email_res = false;
if ($this->third_party_integrations_nextcloud) {
$nextcloud_res = $this->nextcloud_service->nextcloud_upload_csv($file_name, $file_path);
}
if ($this->third_party_integrations_open_project && $this->calculate_agent_cost) {
$open_project_res = $this->open_project_service->open_project_add_task_to_agent($issues, $company_agents, $from_date, $to_date);
}
if($this->email_send_allow) {
$email_res = $this->email_service->send_export_via_email($from_date, $to_date, $file_name);
}
return [$nextcloud_res, $open_project_res, $email_res];
}
}