mirror of
https://bitbucket.org/studiotem/jandl-taskmanager-app.git
synced 2026-10-21 14:29:34 +00:00
215 lines
7.9 KiB
PHP
215 lines
7.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Office365;
|
|
|
|
use App\Models\Blocker;
|
|
use App\Models\MasterTask;
|
|
use App\Models\Project;
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class Office365Service
|
|
{
|
|
public static function getCalendarEvents()
|
|
{
|
|
$token = self::getAccessToken();
|
|
$groupId = env('OFFICE365_GROUP_ID');
|
|
|
|
$response = Http::withToken($token)->get("https://graph.microsoft.com/v1.0/groups/$groupId/events");
|
|
|
|
return $response->json()['value'];
|
|
}
|
|
|
|
public static function getAccessToken()
|
|
{
|
|
$tenantId = env('MICROSOFT_TENANT_ID');
|
|
$clientId = env('MICROSOFT_CLIENT_ID');
|
|
$clientSecret = env('MICROSOFT_CLIENT_SECRET');
|
|
$username = env('MICROSOFT_USERNAME');
|
|
$password = env('MICROSOFT_PASSWORD');
|
|
|
|
$response = Http::asForm()->post("https://login.microsoftonline.com/{$tenantId}/oauth2/v2.0/token", [
|
|
'grant_type' => 'password',
|
|
'client_id' => $clientId,
|
|
'client_secret' => $clientSecret,
|
|
'username' => $username,
|
|
'password' => $password,
|
|
'scope' => 'https://graph.microsoft.com/.default',
|
|
]);
|
|
|
|
if ($response->failed()) {
|
|
dd($response->body());
|
|
throw new \Exception('Failed to obtain access token: ' . $response->body());
|
|
}
|
|
|
|
return $response->json()['access_token'];
|
|
}
|
|
|
|
public static function addBlockers(mixed $blockers)
|
|
{
|
|
foreach ($blockers as $blocker) {
|
|
self::addBlocker($blocker);
|
|
}
|
|
}
|
|
|
|
private static function addBlocker(mixed $blocker)
|
|
{
|
|
$attendees = $blocker['attendees'];
|
|
|
|
// pre kazdeho cloveka z eventu, ktory je zaroven userom v TM vytvorime blocker
|
|
foreach ($attendees as $attendee) {
|
|
$user = User::where('email', $attendee['emailAddress']['address'])->first();
|
|
|
|
if ( ! $user) {
|
|
continue;
|
|
}
|
|
|
|
$blockerToInsert = [];
|
|
|
|
$blockerToInsert['office365_id'] = $blocker['id'];
|
|
$blockerToInsert['office365_change_key'] = $blocker['changeKey'];
|
|
$blockerToInsert['user_id'] = $user->id;
|
|
$blockerToInsert['name'] = $blocker['subject'];
|
|
$blockerToInsert['type'] = "office365_blocker";
|
|
$blockerToInsert['start'] = $blocker['start']['dateTime'];
|
|
$blockerToInsert['end'] = $blocker['end']['dateTime'];
|
|
$blockerToInsert['approved'] = 1;
|
|
$blockerToInsert['created_at'] = Carbon::now()->toDateTimeString();
|
|
$blockerToInsert['updated_at'] = Carbon::now()->toDateTimeString();
|
|
|
|
Blocker::insert($blockerToInsert);
|
|
}
|
|
}
|
|
|
|
public static function editBlockers(mixed $editedBlockers)
|
|
{
|
|
foreach ($editedBlockers as $editedBlocker) {
|
|
self::editBlocker($editedBlocker);
|
|
}
|
|
}
|
|
|
|
private static function editBlocker(mixed $editedBlocker)
|
|
{
|
|
$attendees = $editedBlocker['attendees'];
|
|
|
|
// pre kazdeho cloveka z eventu, ktory je zaroven userom v TM updatneme blocker
|
|
foreach ($attendees as $attendee) {
|
|
$user = User::where('email', $attendee['emailAddress']['address'])->first();
|
|
|
|
if ( ! $user) {
|
|
continue;
|
|
}
|
|
|
|
$blocker = Blocker::where('office365_id', $editedBlocker['id'])->where('user_id', $user->id)->first();
|
|
|
|
$blocker->update([
|
|
'office365_change_key' => $editedBlocker['changeKey'],
|
|
'name' => $editedBlocker['subject'],
|
|
'start' => $editedBlocker['start']['dateTime'],
|
|
'end' => $editedBlocker['end']['dateTime'],
|
|
'updated_at' => Carbon::now()->toDateTimeString(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
public static function addSubtasks(mixed $newSubtasks)
|
|
{
|
|
foreach ($newSubtasks as $newSubtask) {
|
|
$project = Project::where('code', explode(' ', $newSubtask['subject'])[0])->first();
|
|
|
|
// ak existuje viac master taskov, pouzijeme posledny
|
|
$mastertask = MasterTask::where('project_id', $project->id)->latest()->first();
|
|
// ak neexistuje master task k danemu projektu, tak sa subtask neprida
|
|
if ( ! $mastertask) {
|
|
continue;
|
|
}
|
|
|
|
self::addSubtask($newSubtask, $mastertask);
|
|
}
|
|
}
|
|
|
|
private static function addSubtask(mixed $newSubtask, mixed $mastertask)
|
|
{
|
|
$attendees = $newSubtask['attendees'];
|
|
|
|
$subtasksToInsert = [];
|
|
$i = 0;
|
|
|
|
|
|
// pre kazdeho cloveka z eventu, ktory je zaroven userom v TM vytvorime subtask
|
|
foreach ($attendees as $attendee) {
|
|
$user = User::where('email', $attendee['emailAddress']['address'])->first();
|
|
$creator = User::where('email', $newSubtask['organizer']['emailAddress']['address'])->first();
|
|
|
|
if ( ! $user || ! $creator) {
|
|
continue;
|
|
}
|
|
|
|
$subtasksToInsert[$i]['user_id'] = $user->id;
|
|
// vymazem prve slovo z title, ktore je project code
|
|
$subtasksToInsert[$i]['title'] = substr($newSubtask['subject'],
|
|
strpos($newSubtask['subject'], ' ') + 1);
|
|
|
|
|
|
// estimated_time je rozdiel medzi end.dateTime a start.dateTime zakhruhleny na najblizsiu 0.5 hodinu
|
|
$subtasksToInsert[$i]['estimated_time'] = round((strtotime($newSubtask['end']['dateTime']) - strtotime($newSubtask['start']['dateTime'])) / 3600,
|
|
1);
|
|
$subtasksToInsert[$i]['tracked_time'] = 0;
|
|
$subtasksToInsert[$i]['created_by'] = $creator->id;
|
|
$subtasksToInsert[$i]['office365_id'] = $newSubtask['id'];
|
|
$subtasksToInsert[$i]['office365_change_key'] = $newSubtask['changeKey'];
|
|
$subtasksToInsert[$i]['type'] = 'office365';
|
|
$subtasksToInsert[$i]['description'] = $newSubtask['body']['content'];
|
|
$subtasksToInsert[$i]['deadline'] = $newSubtask['end']['dateTime'];
|
|
$subtasksToInsert[$i]['office365_location'] = $newSubtask['location']['displayName'];
|
|
$subtasksToInsert[$i]['is_finished'] = 0;
|
|
}
|
|
|
|
$subtasks = $mastertask->subtasks()->createMany($subtasksToInsert);
|
|
|
|
// vytvorenie timeblocks
|
|
foreach ($subtasks as $subtask) {
|
|
$subtask->syncTimeblocks([
|
|
[
|
|
'sub_task_id' => $subtask->id,
|
|
'start' => $newSubtask['start']['dateTime'],
|
|
'end' => $newSubtask['end']['dateTime'],
|
|
'is_finished' => 0,
|
|
]
|
|
]);
|
|
}
|
|
|
|
// manualne musim trigernut tuto operaciu, lebo cez Observer sa to nespusti - ten je nastaveny len na update subtasku
|
|
$mastertask->recalculateEstimatedTime();
|
|
}
|
|
|
|
public static function finishSubtasks(mixed $subtasks)
|
|
{
|
|
foreach ($subtasks as $subtask) {
|
|
// ukoncenie timeblocks
|
|
foreach ($subtask->timeblocks as $timeblock) {
|
|
$timeblock->update([
|
|
'is_finished' => 1,
|
|
]);
|
|
}
|
|
|
|
// pridanie odtrackovaneho casu
|
|
$subtask->syncTimesheets([
|
|
[
|
|
'sub_task_id' => $subtask->id,
|
|
'employee_id' => $subtask->user_id,
|
|
'time' => $subtask->estimated_time,
|
|
],
|
|
]);
|
|
|
|
$subtask->update([
|
|
'is_finished' => 1,
|
|
]);
|
|
}
|
|
|
|
// manualne musim trignernut tuto operaciu, lebo cez Observer sa to nespusti - ten je nastaveny len na update subtasku
|
|
$subtasks->first()->mastertask->recalculateTrackedTime();
|
|
}
|
|
}
|