fix: env vars handled via conf file, add: functional tests

This commit is contained in:
Michael 2024-08-17 18:06:06 +02:00
parent 3d08b14f11
commit 21c4330f16
39 changed files with 416 additions and 172 deletions

View file

@ -2,6 +2,8 @@
namespace App\Console;
use App\Http\Controllers\GiteaApiController\GiteaExport;
use Dotenv\Dotenv;
use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;
@ -24,6 +26,24 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
//
$schedule->call(function () {
$firstDay = date('Y-m-01'); // First day of the current month
$lastDay = date('Y-m-t'); // Last day of the current month
$calculate_agent_cost = true;
$third_party_integrations_allow = true;
$third_party_integrations_nextcloud = true;
$third_party_integrations_open_project = false;
$email_send_allow = true;
$gitea_export = new GiteaExport(
$calculate_agent_cost,
$third_party_integrations_allow,
$third_party_integrations_nextcloud,
$third_party_integrations_open_project,
$email_send_allow,
);
$gitea_export->export_issues($firstDay, $lastDay, ['state' => 'closed']);
})->monthly();
}
}

View file

@ -9,12 +9,12 @@ class CheckSimpleAuthController extends Controller
{
public function check(Request $req) {
if (
getenv('GITEA_PARTNER_ORGANIZATION') === $req->input('organization')
config('app.gitea_partner_organization') === $req->input('organization')
&&
getenv('APP_PASSWORD') === $req->input('password')
config('app.app_password') === $req->input('password')
) {
return view('backend', [
'token' => getenv('GITEA_TOKEN')
'token' => config('app.gitea_token')
]);
} else {
return redirect('/');

View file

@ -17,11 +17,12 @@ class CsvController extends Controller
private $csv_data_handling;
public function __construct()
public function __construct($calculate_agent_cost = false)
{
$this->calculate_agent_cost = $calculate_agent_cost || config('app.gitea_calculate_agent_cost');
$this->csv_data_init = new CsvDataInit();
$this->csv_data_handling = new CsvDataHandling();
$this->third_party_integrations_allow = env('THIRD_PARTY_INTEGRATIONS_ALLOW', false);
$this->csv_data_handling = new CsvDataHandling($this->calculate_agent_cost);
$this->third_party_integrations_allow = config('app.third_party_integrations_allow', false);
}
function create_columns(array $issue, array $issue_time, array $company_agents = [])
@ -29,7 +30,7 @@ class CsvController extends Controller
try {
$array = $this->csv_data_init->initialize_csv_data($issue);
$array = $this->csv_data_init->handle_labels($array, $issue['labels']);
$array = $this->csv_data_handling->handle_csv_time($array, $issue, $issue_time, $company_agents);
$array = $this->csv_data_handling->handle_csv_time($array, $issue, $issue_time, $company_agents, $this->calculate_agent_cost);
return $array;
} catch (Error $e) {
Log::error('E-CSV-COLUMNS - ' . $e->getMessage());

View file

@ -12,8 +12,8 @@ class CsvAgentCost
public function __construct()
{
$this->internal_percentage_to_deduct = (float) getenv('PRICE_INTERNAL_PERCENTAGE_TO_DEDUCT', null);
$this->partner_organitation = strtolower(getenv('GITEA_PARTNER_ORGANIZATION'));
$this->internal_percentage_to_deduct = (float) config('app.price_internal_percentage_to_deduct', null);
$this->partner_organitation = strtolower(config('app.gitea_partner_organization'));
$this->CsvMinuteCostCalc = new CsvMinuteCostCalc();
}

View file

@ -8,13 +8,13 @@ class CsvCostCalc
* @var CsvMinuteCostCalc|(CsvMinuteCostCalc&object&\PHPUnit\Framework\MockObject\MockObject)|(CsvMinuteCostCalc&\PHPUnit\Framework\MockObject\MockObject)|(object&\PHPUnit\Framework\MockObject\MockObject)|\PHPUnit\Framework\MockObject\MockObject
*/
public function __construct()
public function __construct($calculate_agent_cost = false)
{
$this->internal_percentage_to_deduct = getenv('PRICE_INTERNAL_PERCENTAGE_TO_DEDUCT');
$this->partner_organitation = strtolower(getenv('GITEA_PARTNER_ORGANIZATION'));
$this->internal_percentage_to_deduct = config('app.price_internal_percentage_to_deduct');
$this->partner_organitation = strtolower(config('app.gitea_partner_organization'));
$this->CsvAgentCost = new CsvAgentCost();
$this->CsvTotalCostCalc = new CsvTotalCostCalc();
$this->CsvSumTotalsCalc = new CsvSumTotalsCalc();
$this->CsvSumTotalsCalc = new CsvSumTotalsCalc($calculate_agent_cost);
}

View file

@ -10,18 +10,18 @@ class CsvMinuteCostCalc
{
public function __construct()
{
$this->internal_percentage_to_deduct = getenv('PRICE_INTERNAL_PERCENTAGE_TO_DEDUCT');
$this->partner_organization = strtolower(getenv('GITEA_PARTNER_ORGANIZATION'));
$this->internal_percentage_to_deduct = config('app.price_internal_percentage_to_deduct');
$this->partner_organization = strtolower(config('app.gitea_partner_organization'));
$this->price_partner = [
'high' => $this->calculate_minute_cost((int)getenv('PRICE_PARTNER_HIGH')),
'normal' => $this->calculate_minute_cost((int)getenv('PRICE_PARTNER_NORMAL')),
'low' => $this->calculate_minute_cost((int)getenv('PRICE_PARTNER_LOW')),
'high' => $this->calculate_minute_cost((int)config('app.price_partner_high')),
'normal' => $this->calculate_minute_cost((int)config('app.price_partner_normal')),
'low' => $this->calculate_minute_cost((int)config('app.price_partner_low')),
'0'=>0,
];
$this->price_client = [
'high' => $this->calculate_minute_cost((int)getenv('PRICE_CLIENT_HIGH')),
'normal' => $this->calculate_minute_cost((int)getenv('PRICE_CLIENT_NORMAL')),
'low' => $this->calculate_minute_cost((int)getenv('PRICE_CLIENT_LOW')),
'high' => $this->calculate_minute_cost((int)config('app.price_client_high')),
'normal' => $this->calculate_minute_cost((int)config('app.price_client_normal')),
'low' => $this->calculate_minute_cost((int)config('app.price_client_low')),
'0'=>0,
];
}

View file

@ -7,9 +7,9 @@ class CsvSumTotalsCalc
private $calculate_agent_cost;
public function __construct()
public function __construct($calculate_agent_cost = false)
{
$this->calculate_agent_cost = env('GITEA_CALCULATE_AGENT_COST');
$this->calculate_agent_cost = $calculate_agent_cost || config('app.gitea_calculate_agent_cost');
}
private function sum_total_cost(array $array)

View file

@ -6,9 +6,9 @@ use App\Http\Controllers\CsvController\CsvCostCalc\CsvCostCalc;
class CsvDataHandling
{
public function __construct()
public function __construct($calculate_agent_cost = false)
{
$this->calculate_agent_cost = env('GITEA_CALCULATE_AGENT_COST');
$this->calculate_agent_cost = $calculate_agent_cost || config('app.gitea_calculate_agent_cost');
$this->csv_cost_calc = new CsvCostCalc();
}
@ -33,7 +33,7 @@ class CsvDataHandling
return [$total_issue_time, $agents_issue_time];
}
function handle_csv_time(array $array, array $issue, array $issue_time, array $company_agents = [])
function handle_csv_time(array $array, array $issue, array $issue_time, array $company_agents = [], $calculate_agent_cost = false)
{
[$total_time, $agents_time] = $this->get_issue_total_time($issue_time);
$array = $this->csv_cost_calc->total_time_cost($array, $total_time);

View file

@ -3,6 +3,7 @@
namespace App\Http\Controllers\GiteaApiController;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Config;
use Illuminate\Http\Request;
class GiteaApiController extends Controller
@ -16,7 +17,7 @@ class GiteaApiController extends Controller
public function export_closed_issues(Request $req)
{
if ($req->input('token') != getenv('GITEA_TOKEN')) {
if ($req->input('token') != config('app.gitea_token')) {
return redirect('/');
}
$from = $req->input('from_year') . '-' . $req->input('from_month') . '-01';
@ -24,7 +25,7 @@ class GiteaApiController extends Controller
$res = $this->gitea_export->export_issues($from, $to, ['state' => $req->input('issues_type')]);
return view('backend', [
'token' => getenv('GITEA_TOKEN'),
'token' => config('app.gitea_token'),
'download' => $res
]);
}

View file

@ -8,6 +8,8 @@ use App\Http\Controllers\CsvController\CsvCostCalc\CsvCostCalc;
use App\Http\Controllers\IssueValidationController;
use App\Http\Controllers\ThirdPartyServices\ThirdPartyServices;
use OwenVoke\Gitea\Client;
use Illuminate\Support\Facades\Config;
class GiteaExport extends Controller
{
@ -25,23 +27,33 @@ class GiteaExport extends Controller
private $third_party_services;
private $email_send_allow;
public function __construct()
public function __construct(
$calculate_agent_cost = false,
$third_party_integrations_allow = false,
$third_party_integrations_nextcloud = false,
$third_party_integrations_open_project = false,
$email_send_allow = false
)
{
$this->third_party_integrations_allow = env('THIRD_PARTY_INTEGRATIONS_ALLOW', false);
$this->calculate_agent_cost = env('GITEA_CALCULATE_AGENT_COST', false);
$this->email_send_allow = env('EMAIL_SEND_ALLOW', false);
$this->email_address = env('EMAIL_ADDRESS', false);
$this->email_prefix_subject = env('EMAIL_PREFIX_SUBJECT', false);
$this->giteaClient = new Client(null, null, getenv('GITEA_URL'));
$this->giteaClient->authenticate(getenv('GITEA_TOKEN'), null, Client::AUTH_ACCESS_TOKEN);
$this->third_party_integrations_allow = $third_party_integrations_allow || config('app.third_party_integrations_allow', false);
$this->calculate_agent_cost = $calculate_agent_cost || config('app.gitea_calculate_agent_cost', false);
$this->email_address = config('email.email_address', false);
$this->email_prefix_subject = config('email.email_prefix_subject', false);
$gitea_url = config('app.gitea_url');
$gitea_token = config('app.gitea_token');
$this->giteaClient = new Client(null, null, $gitea_url);
$this->giteaClient->authenticate($gitea_token, null, Client::AUTH_ACCESS_TOKEN);
$this->issue_validation_controller = new IssueValidationController();
$this->csv_controller = new CsvController();
$this->csv_controller = new CsvController($this->calculate_agent_cost);
$this->gitea_fetch = new GiteaFetch();
$this->csv_cost_calc = new CsvCostCalc();
$this->third_party_services = new ThirdPartyServices();
$this->csv_cost_calc = new CsvCostCalc($this->calculate_agent_cost);
$this->third_party_services = new ThirdPartyServices($third_party_integrations_nextcloud, $third_party_integrations_open_project, $email_send_allow);
}
private function handle_repos(string $from_date, string $to_date, array $repositories, array $issues_params){
private function handle_repos(string $from_date, string $to_date, array $repositories, array $issues_params)
{
$data = [];
foreach ($repositories as $repository) {
$issues = $this->gitea_fetch->get_issues($repository['name'], $issues_params);
@ -61,15 +73,15 @@ class GiteaExport extends Controller
function export_issues(string $from_date, string $to_date, array $issues_params)
{
$repositories = $this->gitea_fetch->get_repositories();
$data = $this->handle_repos($from_date, $to_date, $repositories, $issues_params);
$data = $this->handle_repos($from_date, $to_date, $repositories, $issues_params);
if (count($data) > 0) {
$company_agents = $this->calculate_agent_cost ? $this->gitea_fetch->get_company_agents() : [];
$data = $this->csv_cost_calc->sum_costs($data, $company_agents);
$csv_file = $this->csv_controller->create_csv($issues_params['state'], $from_date, $to_date, $data);
if($this->third_party_integrations_allow) {
if ($this->third_party_integrations_allow) {
$this->third_party_services->handle_third_party_services($csv_file['file_name'], $csv_file['file_path'], $data, $company_agents, $from_date, $to_date);
}
isset($csv_file['file_path']) ?? unlink($csv_file['file_path']);
isset($csv_file['file_path']) ?? unlink($csv_file['file_path']);
readfile($csv_file['file_path']);
unlink($csv_file['file_path']);
return true;

View file

@ -19,10 +19,10 @@ class GiteaFetch extends Controller
public function __construct()
{
$this->main_company_organization = getenv('GITEA_MAIN_COMPANY_ORGANIZATION');
$this->partner_organization = getenv('GITEA_PARTNER_ORGANIZATION');
$this->gitea_url = getenv('GITEA_URL');
$this->gitea_token = getenv('GITEA_TOKEN');
$this->main_company_organization = config('app.gitea_main_company_organization');
$this->partner_organization = config('app.gitea_partner_organization');
$this->gitea_url = config('app.gitea_url');
$this->gitea_token = config('app.gitea_token');
$this->giteaClient = new Client(null, null, $this->gitea_url);
$this->giteaClient->authenticate($this->gitea_token, null, Client::AUTH_ACCESS_TOKEN);
}

View file

@ -15,9 +15,9 @@ 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);
$this->email_address_recepient = config('mail.email_address_recepient', null);
$this->email_prefix_subject = config('mail.email_prefix_subject', null);
$this->nextcloud_upload_folder_web_link = config('mail.nextcloud_upload_folder_web_link', false);
}
function send_export_via_email(string $from_date, string $to_date, string $file_name){
try{

View file

@ -17,11 +17,11 @@ class NextcloudService extends Controller
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);
$this->nextcloud_url = config('app.nextcloud_url', false);
$this->nextcloud_user = config('app.nextcloud_user', false);
$this->nextcloud_password = config('app.nextcloud_password', false);
$this->nextcloud_upload_folder_path = config('app.nextcloud_upload_folder_path', false);
$this->nextcloud_upload_folder_web_link = config('app.nextcloud_upload_folder_web_link', false);
}
function nextcloud_upload_csv(string $file_name, string $file_path)

View file

@ -17,14 +17,14 @@ class OpenProjectService extends Controller
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);
$this->open_project_url = config('app.open_project_url', false);
$this->open_project_token = config('app.open_project_token', false);
$this->open_project_project_id = config('app.open_project_project_id', false);
$this->open_project_task_name = config('app.open_project_task_name', false);
$this->open_project_group_id = config('app.open_project_group_id', false);
$this->open_project_special_nicks_list = config('app.open_project_special_nicks_list', false);
$this->open_project_work_package_type_id = config('app.open_project_work_package_type_id', false);
$this->open_project_work_package_user_owner_id = config('app.open_project_work_package_user_owner_id', false);
}
private function open_project_project_member_task_id(stdClass $open_project_member)

View file

@ -12,15 +12,15 @@ class ThirdPartyServices extends Controller
private $third_party_integrations_nextcloud;
private $third_party_integrations_open_project;
public function __construct()
public function __construct($third_party_integrations_nextcloud, $third_party_integrations_open_project, $email_send_allow)
{
$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->third_party_integrations_nextcloud = $third_party_integrations_nextcloud || config('app.third_party_integrations_nextcloud', false);
$this->third_party_integrations_open_project = $third_party_integrations_open_project || config('app.third_party_integrations_open_project', false);
$this->email_send_allow = $email_send_allow || config('email.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);
$this->calculate_agent_cost = config('app.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)