Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/stable v2 #116

Merged
merged 4 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions app/Http/Controllers/BillingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Resources\Item as ItemResource;
use App\Models\Board;
use Carbon\Carbon;
use App\Models\Item;
use App\Models\Link;
use Inertia\Inertia;
use Illuminate\Support\Facades\URL;
use App\Models\Board;
use App\Models\Standup;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Insane\Treasurer\Models\Plan;
use Insane\Treasurer\Models\Subscription;
use Illuminate\Support\Facades\URL;
use Insane\Treasurer\PaypalServiceV2;
use Insane\Treasurer\Models\Subscription;
use App\Http\Resources\Item as ItemResource;

class BillingController extends Controller
{
Expand All @@ -33,7 +33,7 @@ public function index(Request $request)
"user_id" => $user->id
])->get(),
"transactions" => function () use ($request) {
return $request->user()->subscriptionTransactions();
return $request->user()->currentTeam->subscriptionTransactions();
}
]);
}
Expand Down
53 changes: 53 additions & 0 deletions app/Http/Controllers/NeatlancerController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Http\Controllers;

use App\Models\User;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;

class NeatlancerController {

public function connect() {
// request()->session()->put('state', $state = Str::random(40));

// $query = http_build_query([
// 'client_id' => '98e6222a-6bd7-4f85-a4a6-01931fd0e3fe',
// 'redirect_uri' => config('app.url') .'/oauth/accept',
// 'response_type' => 'code',
// 'scope' => '',
// 'state' => $state,
// // 'prompt' => "consent"
// // 'prompt' => '', // "none", "consent", or "login"
// ]);

// return redirect(config('app.sso_url') . '/oauth/authorize?'.$query);
return Socialite::driver('laravelpassport')->redirect();
}

public function accept() {
$accessInfo = Socialite::driver('laravelpassport')->user();

$guzzle = new Client(['base_uri' => config('app.sso_url')]);

$raw_response = $guzzle->get('/api/current-user', [
'headers' => [ 'Authorization' => 'Bearer ' . $accessInfo->token ],
]);

$userInfo = json_decode($raw_response->getBody()->getContents());

$user = User::updateOrCreate([
'neatlancer_id' => $userInfo->id,
], [
'name' => $userInfo->name,
'email' => $userInfo->email,
// 'avatar' => $userInfo?->avatar,
'neatlancer_token' => $accessInfo->token,
'neatlancer_refresh_token' => $accessInfo->refreshToken,
]);

Auth::login($user);
return redirect('/dashboard');
}
}
1 change: 1 addition & 0 deletions app/Http/Resources/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function toArray($request)
'title' => $this->title,
'resource_type' => $this->resource_type,
'done' => $this->done,
'points' => $this->points,
'commit_date' => $this->commit_date,
'color' => $this->color,
'stage' => $this->stage ? $this->stage->name : "",
Expand Down
11 changes: 0 additions & 11 deletions app/Libraries/GoogleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,6 @@ public static function getSheetsService($integrationId) {
return $service;
}

public static function storeIntegration($data, $user) {
Integration::updateOrCreate([
"team_id" => $user->current_team_id,
"user_id" => $user->id,
"name" => $data->service_name,
"automation_service_id" => $data->service_id
], [
"hash" => $user->email
]);
}

public static function requestAccessToken($data, $user) {
$client = new GoogleClient([
"client_id" => config('integrations.google.client_id')
Expand Down
152 changes: 152 additions & 0 deletions app/Libraries/SunService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php
namespace App\Libraries;

use App\Jobs\ProcessCalendar;
use App\Jobs\ProcessGmail;
use App\Models\User;
use App\Models\Automation;
use App\Models\Integration;
use Exception;
use Google\Client as GoogleClient;
use Google\Service\Calendar;
use Google\Service\Gmail;

class GoogleService
{

public static function getTokens($userId) {
return User::find($userId);
}
public static function getConfigPath() {
return base_path(config("integrations.google.credentials_path"));
}

public static function setTokens($data, $user, $integrationId = null) {
if (!$integrationId && $_GET['code']) {

$client = new GoogleClient([ 'client_id' => config('integrations.google.client_id')]);
$client->setAuthConfig(self::getConfigPath());
$client->setAccessType('offline');
$userIdToken = $_GET['code'];
$tokenResponse = $client->fetchAccessTokenWithAuthCode($userIdToken);
$integration = Integration::where([
'user_id' => $user->id,
'team_id' => $user->current_team_id,
'name' => 'Google'
])->first();
$googleUser = $client->verifyIdToken($tokenResponse["id_token"]);
if ($googleUser['email'] == $user->email) {
$integration->token = encrypt($tokenResponse['access_token']);
$integration->save();
session(['g_token', json_encode($tokenResponse)]);
return;
}
throw new Exception("Error obtaining the token" . $googleUser['email']);
} else if ($integrationId) {
$integration = Integration::find($integrationId);
$integration->token = encrypt($data->refresh_token);
session(['g_token', json_encode($data)]);

return;
};
}

public static function getClient($integrationId) {
$integration = Integration::find($integrationId);
$client = new GoogleClient();
$client->setAuthConfig(self::getConfigPath());
if (!$accessToken = session('g_token')) {
$accessToken = $client->fetchAccessTokenWithRefreshToken(decrypt($integration->token));
}

$client->setAccessToken($accessToken);

if ($client->isAccessTokenExpired()) {
dd($accessToken);
if ($refreshToken = $client->getRefreshToken()) {
$tokenResponse = $client->fetchAccessTokenWithRefreshToken($refreshToken);
self::setTokens((object) [
'access_token' => $accessToken,
'refresh_token' => $refreshToken
],
$integration->user,
$integrationId);
$client->setAccessToken($accessToken);

}
}
return $client;
}

public static function storeIntegration($data, $user) {
Integration::updateOrCreate([
"team_id" => $user->current_team_id,
"user_id" => $user->id,
"name" => $data->service_name,
"automation_service_id" => $data->service_id
], [
"hash" => $user->email
]);
}

// services
public static function createItemFromCalendar($automationId, $afterResponse = null) {
$automation = Automation::find($automationId);
echo "$automation->name $automation->id \n";
$method = $afterResponse ? "dispatchAfterResponse" : "dispatch";
ProcessCalendar::$method($automation);
return true;
}

public static function listCalendars(int $integrationId) {
$client = self::getClient($integrationId);
$service = new Calendar($client);
return $service->calendarList->listCalendarList();
}

public static function createItemFromGmail($automationId, $afterResponse = null) {
$automation = Automation::find($automationId);
echo "$automation->name $automation->id \n";
$method = $afterResponse ? "dispatchAfterResponse" : "dispatch";
ProcessGmail::$method($automation);
return true;
}

public static function getSheetsService($integrationId) {
$client = GoogleService::getClient($integrationId);
$service = new Google_Service_Sheets($client);
return $service;
}

public static function storeIntegration($data, $user) {
Integration::updateOrCreate([
"team_id" => $user->current_team_id,
"user_id" => $user->id,
"name" => $data->service_name,
"automation_service_id" => $data->service_id
], [
"hash" => $user->email
]);
}

public static function requestAccessToken($data, $user) {
$client = new GoogleClient([
"client_id" => config('integrations.google.client_id')
]);
$client->addScope([
Gmail::GMAIL_READONLY,
Calendar::CALENDAR_READONLY
]);
$client->setRedirectUri(config('app.url') . "/services/accept-oauth");
$client->setAccessType('offline');
$client->setLoginHint($user->email);
$client->setApprovalPrompt('auto');
$client->setIncludeGrantedScopes(true);

$authUrl = $client->createAuthUrl();
if ($authUrl) {
self::storeIntegration($data, $user);
}
return $authUrl;
}
}
16 changes: 15 additions & 1 deletion app/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,21 @@

class Item extends Model
{
protected $fillable = ['board_id', 'team_id', 'stage_id','resource_id', 'rrule' ,'resource_type', 'resource_origin', 'title', 'order', 'user_id', 'done','commit_date', 'points'];
protected $fillable = [
'board_id',
'team_id',
'stage_id',
'resource_id',
'rrule' ,
'resource_type',
'resource_origin',
'title',
'order',
'user_id',
'done',
'commit_date',
'points'
];
protected $with = ['fields', 'checklist'];
use HasFactory;
use ItemScopeTrait;
Expand Down
6 changes: 5 additions & 1 deletion app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ class EventServiceProvider extends ServiceProvider
TeamCreated::class => [
CreateTeamWorkspace::class,
CreateTeamSettings::class
]
],
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
// ... other providers
\SocialiteProviders\LaravelPassport\LaravelPassportExtendSocialite::class.'@handle',
],
];

/**
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
"doctrine/dbal": "^2.12",
"freesgen/querify": "dev-master",
"google/apiclient": "2.12.6",
"inertiajs/inertia-laravel": "^0.5.4",
"inertiajs/inertia-laravel": "^0.6.9",
"insane/treasurer": "dev-master",
"laravel/framework": "^9.19",
"laravel/jetstream": "^2.11",
"laravel/sanctum": "^3.0",
"laravel/socialite": "^5.6",
"laravel/tinker": "^2.7",
"php-mime-mail-parser/php-mime-mail-parser": "^7.0",
"pusher/pusher-php-server": "^7.0",
"rlanvin/php-rrule": "^2.2",
"socialiteproviders/laravelpassport": "^4.3",
"tightenco/ziggy": "^1.4.3"
},
"require-dev": {
Expand Down
Loading
Loading