diff --git a/rest/app/Http/Controllers/ConsumePollingServiceController.php b/rest/app/Http/Controllers/ConsumePollingServiceController.php index 295cd182..d3bbbbf6 100644 --- a/rest/app/Http/Controllers/ConsumePollingServiceController.php +++ b/rest/app/Http/Controllers/ConsumePollingServiceController.php @@ -57,4 +57,21 @@ public function reward(Request $request) // return consume status return ['status' => $success]; } + + /** + * consume polling service for OathKeeper + * + * @return \Illuminate\Http\JsonResponse + */ + public function status(Request $request) + { + // get body of array and convert it to object + $transaction = array_to_object($request->all()); + + // process transaction + $success = PollingHelper::processStatusEvent($transaction); + + // return consume status + return ['status' => $success]; + } } diff --git a/rest/app/Libraries/PollingHelper.php b/rest/app/Libraries/PollingHelper.php index 8aba6eab..abec5eb5 100644 --- a/rest/app/Libraries/PollingHelper.php +++ b/rest/app/Libraries/PollingHelper.php @@ -8,6 +8,7 @@ use \App\Models\RewardActivity; use \App\Models\RoleContract; use \App\Models\Slot; +use \App\Models\Status; class PollingHelper { @@ -109,4 +110,29 @@ public static function processRewardEvent($transaction) return $success; } + public static function processStatusEvent($transaction) + { + // intial status for success + $success = -1; + + switch ($transaction->event_name) { + // StatusAdded event + case 'StatusAdded': + $success = Status::statusAdded($transaction); + break; + + // StateChanged event + case 'StateChanged': + $success = Status::stateChanged($transaction); + break; + + // StatusTypeChanged event + case 'StatusTypeChanged': + $success = Status::statusTypeChanged($transaction); + break; + } + + return $success; + } + } diff --git a/rest/app/Models/Status.php b/rest/app/Models/Status.php index 4fef048b..27254772 100644 --- a/rest/app/Models/Status.php +++ b/rest/app/Models/Status.php @@ -23,46 +23,16 @@ public function scopeFilters($query, $filters) } /** - * Consume AMQP + * create Reward when `StatusAdded` event triggered * - * @param Object $payload: payload data send by AMQP server + * @param Object $payload: payload data send by Polling-Server * @return Boolean the success or failure message */ - public static function consumeAMQP($payload) + public static function statusAdded($payload) { - $saved = false; + // get data object + $data = $payload->data; - switch ($payload->eventIdentifier) { - - // StatusAdded event - case 'StatusAdded': - $saved = Status::store($payload->data); - break; - - // StateChanged event - case 'StateChanged': - $saved = Status::updateState($payload->data); - break; - - // StatusTypeChanged event - case 'StatusTypeChanged': - $saved = Status::updateStatusType($payload->data); - break; - - } - - // Return process status - return $saved; - } - - /** - * Store status - * - * @param Object $data: payload data send by AMQP server - * @return Boolean the success or failure message - */ - public static function store($data) - { $exists = Status::where(['wallet' => $data->statusHolder])->first(); info($exists); @@ -84,13 +54,16 @@ public static function store($data) } /** - * Update state of a Status + * create Reward when `StateChanged` event triggered * - * @param Object $data: payload data send by AMQP server + * @param Object $payload: payload data send by Polling-Server * @return Boolean the success or failure message */ - public static function updateState($data) + public static function stateChanged($payload) { + // get data object + $data = $payload->data; + $status = Status::where(['wallet' => $data->statusHolder])->first(); if (!isset($status)) { @@ -104,13 +77,16 @@ public static function updateState($data) } /** - * Update type of a Status + * create Reward when `StatusTypeChanged` event triggered * - * @param Object $data: payload data send by AMQP server + * @param Object $payload: payload data send by Polling-Server * @return Boolean the success or failure message */ - public static function updateStatusType($data) + public static function statusTypeChanged($payload) { + // get data object + $data = $payload->data; + $status = Status::where(['wallet' => $data->statusHolder])->first(); if (!isset($status)) { diff --git a/rest/routes/web.php b/rest/routes/web.php index 1e0c2de8..0c03a672 100644 --- a/rest/routes/web.php +++ b/rest/routes/web.php @@ -8,6 +8,7 @@ $api->post('oath-keeper', 'App\Http\Controllers\ConsumePollingServiceController@oathKeeper'); $api->post('advocate', 'App\Http\Controllers\ConsumePollingServiceController@advocate'); $api->post('reward', 'App\Http\Controllers\ConsumePollingServiceController@reward'); + $api->post('status', 'App\Http\Controllers\ConsumePollingServiceController@status'); }); $api->group(['middleware' => 'wallet.auth'], function ($api) {