Skip to content

Commit

Permalink
updated config & migration stub
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanwilliammd committed Sep 26, 2023
1 parent 7f7a84a commit c72247d
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 0 deletions.
18 changes: 18 additions & 0 deletions config/satusehatintegration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

return [

/*
* This is the name of the table that will be created by the migration and
* used by the Activity model shipped with this package.
*/
'token_table_name' => 'satusehat_tokens',

/*
* This is the database connection that will be used by the migration and
* the Activity model shipped with this package. In case it's not set
* Laravel database.default will be used instead.
*/
'database_connection' => env('ACTIVITY_LOGGER_DB_CONNECTION'),

];
32 changes: 32 additions & 0 deletions database/migrations/create_satusehat_tokens_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateSatusehatTokensTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create(config('satusehatintegration.token_table_name'), function (Blueprint $table) {
$table->string('environment');
$table->longText('token');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists(config('satusehatintegration.token_table_name'));
}
}
Empty file added src/Models/SatusehatToken.php
Empty file.
57 changes: 57 additions & 0 deletions src/SatusehatIntegrationClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

namespace Ivanwilliammd\SatusehatIntegration;

// Guzzle HTTP Package
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Stream\Stream;
use GuzzleHttp\Exception\ClientException;



class SatusehatIntegrationClass
{
public $patient_dev = ['P02478375538', 'P02428473601', 'P03647103112', 'P01058967035', 'P01836748436', 'P01654557057', 'P00805884304', 'P00883356749', 'P00912894463'];
Expand Down Expand Up @@ -30,4 +38,53 @@ public function __construct()
$this->organization_id = env('ORGID_DEV');
}
}

public static function oauth2()
{
$token = SatusehatToken::where('environment', env("SATUSEHAT_ENV"))->orderBy('created_at', 'desc')
->where('created_at', '>', now()->subMinutes(50))->first();

if($token){
return $token->token;
}

$client = new Client();

$headers = [
'Content-Type' => 'application/x-www-form-urlencoded'
];
$options = [
'form_params' => [
'client_id' => $this->client_id,
'client_secret' => $this->client_secret
]];

// Create session
$url = $this->auth_url . '/accesstoken?grant_type=client_credentials';
$request = new Request('POST', $url, $headers);

try {
$res = $client->sendAsync($request, $options)->wait();
$contents = json_decode($res->getBody()->getContents());

if (isset($contents->access_token)){
SatusehatToken::create([
'environment' => env("SATUSEHAT_ENV"),
'token' => $contents->access_token
]);
return $contents->access_token;
}
else{
// return $this->respondError($oauth2);
return null;
}
}
catch (ClientException $e) {
// error.
$res = json_decode($e->getResponse()->getBody()->getContents());
$issue_information = $res->issue[0]->details->text;
return $issue_information;
}
}

}
58 changes: 58 additions & 0 deletions src/SatusehatIntegrationServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Ivanwilliammd\SatusehatIntegration;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\ServiceProvider;

class ActivitylogServiceProvider extends ServiceProvider
{
public function boot()
{
$this->publishes([
__DIR__.'/../config/satusehatintegration.php' => config_path('satusehatintegration.php'),
], 'config');

$this->mergeConfigFrom(__DIR__.'/../config/satusehatintegration.php', 'satusehatintegration');

if (! class_exists('CreateActivityLogTable')) {
$timestamp = date('Y_m_d_His', time());

$this->publishes([
__DIR__.'/../migrations/create_activity_log_table.php.stub' => database_path("/migrations/{$timestamp}_create_activity_log_table.php"),
], 'migrations');
}
}

public function register()
{
$this->app->bind('command.activitylog:clean', CleanActivitylogCommand::class);

$this->commands([
'command.activitylog:clean',
]);

$this->app->bind(ActivityLogger::class);

$this->app->singleton(ActivityLogStatus::class);
}

public static function determineActivityModel(): string
{
$activityModel = config('activitylog.activity_model') ?? ActivityModel::class;

if (! is_a($activityModel, Activity::class, true)
|| ! is_a($activityModel, Model::class, true)) {
throw InvalidConfiguration::modelIsNotValid($activityModel);
}

return $activityModel;
}

public static function getActivityModelInstance(): ActivityContract
{
$activityModelClassName = self::determineActivityModel();

return new $activityModelClassName();
}
}

0 comments on commit c72247d

Please sign in to comment.