Add stop insurance endpoint
This commit is contained in:
parent
36e8fa3a00
commit
c44249ba38
|
@ -403,13 +403,6 @@ class InsuranceController extends Controller
|
|||
]);
|
||||
|
||||
$insurance = NhInsurance::findOrFail($id);
|
||||
$latestSubscription = NhInsurancesSubscription::where('network_id', $insurance->network_id)->where('user_id', $insurance->user_id)
|
||||
->whereIn('state', [InsuranceSubscriptionState::UNDER_VALIDATION, InsuranceSubscriptionState::AWAITING_FURTHER_INFORMATION])->orderBy('created_at', 'DESC')->first();
|
||||
|
||||
if (isset($latestSubscription)) {
|
||||
return $this->errorResponse(trans('errors.subscription_cannot_be_submitted', ['state' => mb_strtolower(trans('states.' . $latestSubscription->state), 'UTF-8')]));
|
||||
}
|
||||
|
||||
$user = $insurance->user;
|
||||
$identification = $insurance->user->identification;
|
||||
|
||||
|
@ -456,4 +449,100 @@ class InsuranceController extends Controller
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @OA\Put(
|
||||
* path="/insurances/{id}/stop",
|
||||
* summary="Arreter son assurance",
|
||||
* tags={"Assurances"},
|
||||
* security={{"api_key":{}}},
|
||||
* @OA\Parameter(
|
||||
* parameter="id",
|
||||
* name="id",
|
||||
* description="ID de l'assurance",
|
||||
* in="path",
|
||||
* required=true,
|
||||
* @OA\Schema(
|
||||
* type="integer",
|
||||
* default=12
|
||||
* )
|
||||
* ),
|
||||
* @OA\RequestBody(
|
||||
* description="Corps de la requete",
|
||||
* required=true,
|
||||
* @OA\MediaType(
|
||||
* mediaType="application/json",
|
||||
* @OA\Schema(
|
||||
* @OA\Property(
|
||||
* property="password",
|
||||
* description = "Mot de passe",
|
||||
* type="string",
|
||||
* example= "12345"
|
||||
* ),
|
||||
* ),
|
||||
* ),
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=200,
|
||||
* description="OK",
|
||||
* @OA\JsonContent(
|
||||
* ref="#/components/schemas/ApiResponse",
|
||||
* example = {"status":200,"response":"Suprresion réussie","error":null}
|
||||
* )
|
||||
* )
|
||||
* )
|
||||
*/
|
||||
public function stopInsurance(Request $request, $id)
|
||||
{
|
||||
$this->validate($request, [
|
||||
'password' => 'required|string',
|
||||
]);
|
||||
|
||||
$insurance = NhInsurance::findOrFail($id);
|
||||
$latestSubscription = NhInsurancesSubscription::where('network_id', $insurance->network_id)->where('user_id', $insurance->user_id)
|
||||
->whereIn('state', [InsuranceSubscriptionState::UNDER_VALIDATION, InsuranceSubscriptionState::AWAITING_FURTHER_INFORMATION])->orderBy('created_at', 'DESC')->first();
|
||||
|
||||
if (isset($latestSubscription)) {
|
||||
return $this->errorResponse(trans('errors.subscription_cannot_be_submitted', ['state' => mb_strtolower(trans('states.' . $latestSubscription->state), 'UTF-8')]));
|
||||
}
|
||||
|
||||
$user = $insurance->user;
|
||||
$identification = $insurance->user->identification;
|
||||
|
||||
if (!isset($identification) || $identification->status == 0)
|
||||
return $this->errorResponse(trans('errors.user_identification_required'));
|
||||
|
||||
if (!checkPassword($request->password, $user->encrypted_password, $user->salt))
|
||||
return $this->errorResponse(trans('messages.incorrect_user_password'));
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$datetime = $this->getCurrentTimeByCountryCode($user->network->country->code_country);
|
||||
|
||||
$newSubscription = NhInsurancesSubscription::create([
|
||||
'network_id' => $insurance->network_id,
|
||||
'user_id' => $insurance->user_id,
|
||||
'insurance_subscription_id' => $this->generateSubscriptionID(),
|
||||
'number_of_beneficiaries' => $insurance->number_of_beneficiaries,
|
||||
'number_of_months' => $insurance->number_of_months,
|
||||
'bonus_amount' => $insurance->bonus_amount,
|
||||
'total_bonus_amount' => $insurance->total_bonus_amount,
|
||||
'insurance_action' => InsuranceAction::STOP_INSURANCE,
|
||||
'state' => InsuranceSubscriptionState::UNDER_VALIDATION,
|
||||
'created_at' => $datetime, 'updated_at' => $datetime
|
||||
]);
|
||||
|
||||
|
||||
Event::dispatch(new InsuranceEvent($newSubscription, trans('messages.insurance_stop'), trans('messages.insurance_stop_mail', ['name' => $newSubscription->user->lastname, 'subscription_id' => $newSubscription->insurance_subscription_id,
|
||||
'bonus_amount' => $this->toMoneyWithNetwork($newSubscription->total_bonus_amount, $newSubscription->network_id), 'number_of_beneficiaries' => $newSubscription->number_of_beneficiaries,
|
||||
'gender' => trans('states.' . $identification->gender), 'insurance_name' => $insurance->network->name])));
|
||||
|
||||
DB::commit();
|
||||
return $this->successResponse(trans('messages.insurance_stop_successful'));
|
||||
} catch (Throwable $e) {
|
||||
Log::error($e->getMessage() . '\n' . $e->getTraceAsString());
|
||||
DB::rollBack();
|
||||
return $this->errorResponse(trans('errors.unexpected_error'), 500);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -415,6 +415,24 @@ class InsuranceSubscriptionController extends Controller
|
|||
$subscription->updated_at = $datetime;
|
||||
$subscription->save();
|
||||
|
||||
$mail_data = ['name' => $subscription->user->lastname, 'subscription_id' => $subscription->insurance_subscription_id,
|
||||
'bonus_amount' => $this->toMoneyWithNetwork($subscription->total_bonus_amount, $subscription->network_id), 'number_of_beneficiaries' => $subscription->number_of_beneficiaries,
|
||||
'gender' => trans('states.' . $subscription->user->identification->gender), 'insurance_name' => $subscription->network->name];
|
||||
if ($subscription->insurance_action == InsuranceAction::STOP_INSURANCE) {
|
||||
$insurance = NhInsurance::where('user_id', $subscription->user_id)->where('network_id', $subscription->network_id)->firstOrFail();
|
||||
$insurance->state = InsuranceState::STOPPED;
|
||||
$insurance->updated_at = $datetime;
|
||||
$insurance->save();
|
||||
|
||||
$message = trans('messages.insurance_stop_accepted');
|
||||
$notification = trans('messages.insurance_stop_accepted_notification', ['subscription_id' => $subscription->insurance_subscription_id]);
|
||||
$mail = trans('messages.insurance_stop_accepted_mail', $mail_data);
|
||||
} else {
|
||||
$message = trans('messages.insurance_subscription_accepted');
|
||||
$notification = trans('messages.insurance_subscription_accepted_notification', ['subscription_id' => $subscription->insurance_subscription_id]);
|
||||
$mail = trans('messages.insurance_subscription_accepted_mail', $mail_data);
|
||||
}
|
||||
|
||||
NhInsurancesSubscriptionsHistory::create([
|
||||
'action' => 'EDIT',
|
||||
'insurance_subscription_id' => $subscription->insurance_subscription_id,
|
||||
|
@ -425,9 +443,8 @@ class InsuranceSubscriptionController extends Controller
|
|||
'created_at' => $datetime, 'updated_at' => $datetime,
|
||||
]);
|
||||
|
||||
Event::dispatch(new InsuranceEvent($subscription, trans('messages.insurance_subscription_accepted'), trans('messages.insurance_subscription_accepted_mail', ['name' => $subscription->user->lastname, 'subscription_id' => $subscription->insurance_subscription_id,
|
||||
'bonus_amount' => $this->toMoneyWithNetwork($subscription->total_bonus_amount, $subscription->network_id), 'number_of_beneficiaries' => $subscription->number_of_beneficiaries,
|
||||
'gender' => trans('states.' . $subscription->user->identification->gender), 'insurance_name' => $subscription->network->name]), trans('messages.insurance_subscription_accepted_notification', ['subscription_id' => $subscription->insurance_subscription_id])));
|
||||
Event::dispatch(new InsuranceEvent($subscription, $message, $mail, $notification));
|
||||
|
||||
DB::commit();
|
||||
return $this->successResponse(trans('messages.successful_transaction'));
|
||||
} catch (Throwable $e) {
|
||||
|
@ -752,6 +769,9 @@ class InsuranceSubscriptionController extends Controller
|
|||
$type = $request->input('type');
|
||||
if ($type != 'ALL') {
|
||||
// Les souscriptions payables
|
||||
if ($type == 'ACCEPTED') {
|
||||
$query = $query->whereNotIn('insurance_action', [InsuranceAction::STOP_INSURANCE]);
|
||||
}
|
||||
$query = $query->where('state', $type)->whereDoesntHave('payment');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,5 +8,6 @@ abstract class InsuranceAction
|
|||
const RENEWAL = 'RENEWAL';
|
||||
const ADDITION_OF_BENEFICIARY = 'ADDITION_OF_BENEFICIARY';
|
||||
const DELETION_OF_BENEFICIARY = 'DELETION_OF_BENEFICIARY';
|
||||
const STOP_INSURANCE = 'STOP_INSURANCE';
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class UpdateInsuranceActionInNhInsurancesSubscriptionsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('nh_insurances_subscriptions', function (Blueprint $table) {
|
||||
DB::statement("alter table nh_insurances_subscriptions
|
||||
modify insurance_action enum ('ACTIVATION', 'ADDITION_OF_BENEFICIARY', 'DELETION_OF_BENEFICIARY', 'STOP_INSURANCE') default 'ACTIVATION' not null comment 'Action que l''utilisateur souhaite effectuer sur son assurance';");
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('nh_insurances_subscriptions', function (Blueprint $table) {
|
||||
//
|
||||
});
|
||||
}
|
||||
}
|
|
@ -138,4 +138,26 @@ A new consultation or prescription has been made with your insurance.
|
|||
'care_request_accepted_notification' => "Your care request :request_id has been accepted",
|
||||
'generated_invoice_mail' => "Invoice issued by :agent for the period :period",
|
||||
'insurance_deletion_beneficiary_successful' => "Deletion of your insurance beneficiary successful",
|
||||
'insurance_stop_successful' => "Request to stop your insurance successful",
|
||||
'insurance_stop' => "Request to stop your insurance",
|
||||
'insurance_stop_mail' => ":gender :name ,
|
||||
|
||||
Your insurance stop request is being validated.
|
||||
Request information :
|
||||
- ID: :subscription_id
|
||||
- Insurance name: :insurance_name
|
||||
- Premium amount: :bonus_amount
|
||||
- Number of beneficiaries : :number_of_beneficiaries
|
||||
",
|
||||
'insurance_stop_accepted' => "Request to stop your insurance accepted",
|
||||
'insurance_stop_accepted_notification' => "Your request to stop your insurance :subscription_id has been accepted",
|
||||
'insurance_stop_accepted_mail' => ":gender :name ,
|
||||
|
||||
Your request to stop your insurance has been accepted.
|
||||
Application information:
|
||||
- ID: :subscription_id
|
||||
- Name of the insurance: :insurance_name
|
||||
- Amount to be paid: :bonus_amount
|
||||
- Number of beneficiaries : :number_of_beneficiaries
|
||||
",
|
||||
];
|
||||
|
|
|
@ -25,5 +25,6 @@ return [
|
|||
'EXONERATION' => "EXONERATION",
|
||||
'HAVING_RIGHT' => "HAVING RIGHT",
|
||||
'INSURED' => 'INSURED',
|
||||
'INVOICE_ISSUED' => 'INVOICE ISSUED'
|
||||
'INVOICE_ISSUED' => 'INVOICE ISSUED',
|
||||
'STOP_INSURANCE' => "STOP INSURANCE"
|
||||
];
|
||||
|
|
|
@ -154,4 +154,26 @@ Une nouvelle execution de prescription vient d'etre effectuée sur votre assuran
|
|||
'care_request_accepted_notification' => "Votre demande d'autorisation de soins :request_id a été acceptée",
|
||||
'generated_invoice_mail' => "Facture émise par :agent pour la période :period",
|
||||
'insurance_deletion_beneficiary_successful' => "Suppression d'ayant droit à votre assurance réussie",
|
||||
'insurance_stop_successful' => "Demande d'arrêt de votre assurance réussie",
|
||||
'insurance_stop' => "Demande d'arrêt de votre assurance",
|
||||
'insurance_stop_mail' => ":gender :name ,
|
||||
|
||||
Votre demande d'arrêt d'assurance est en cours de validation.
|
||||
Informations de la demande :
|
||||
- ID : :subscription_id
|
||||
- Nom de l'assurance : :insurance_name
|
||||
- Montant de la prime : :bonus_amount
|
||||
- Nombre d'ayants droit : :number_of_beneficiaries
|
||||
",
|
||||
'insurance_stop_accepted' => "Demande d'arrêt de votre assurance acceptée",
|
||||
'insurance_stop_accepted_notification' => "Votre demande d'arrêt de votre assurance :subscription_id a été acceptée.",
|
||||
'insurance_stop_accepted_mail' => ":gender :name ,
|
||||
|
||||
Votre demande d'arrêt de votre assurance a été acceptée.
|
||||
Informations de la demande :
|
||||
- ID : :subscription_id
|
||||
- Nom de l'assurance : :insurance_name
|
||||
- Montant à payer : :bonus_amount
|
||||
- Nombre d'ayants droit : :number_of_beneficiaries
|
||||
",
|
||||
];
|
||||
|
|
|
@ -25,5 +25,6 @@ return [
|
|||
'EXONERATION' => "EXONERATION",
|
||||
'HAVING_RIGHT' => "AYANT DROIT",
|
||||
'INSURED' => 'ASSURÉ',
|
||||
'INVOICE_ISSUED' => 'FACTURE ÉMISE'
|
||||
'INVOICE_ISSUED' => 'FACTURE ÉMISE',
|
||||
'STOP_INSURANCE' => "ARRÊT DE L'ASSURANCE"
|
||||
];
|
||||
|
|
|
@ -19,6 +19,7 @@ $router->group(['prefix' => '', 'middleware' => 'auth'], function () use ($route
|
|||
$router->get('networks', 'InsuranceController@getInsurancesNetworks');
|
||||
$router->put('{id}/add-beneficiaries', 'InsuranceController@addBeneficiaries');
|
||||
$router->put('{id}/delete-beneficiaries', 'InsuranceController@deleteBeneficiaries');
|
||||
$router->put('{id}/stop', 'InsuranceController@stopInsurance');
|
||||
|
||||
// Subscriptions
|
||||
$router->group(['prefix' => '/subscriptions'], function () use ($router) {
|
||||
|
|
Loading…
Reference in New Issue