From d11cc1784d7c34fdfe00c9a4117ef835a64faf22 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 13 Aug 2020 11:55:58 +1000 Subject: [PATCH] Fixes for tests --- app/Http/Controllers/CompanyController.php | 75 +++++++++++++++++++- app/Http/Controllers/MigrationController.php | 11 --- 2 files changed, 73 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/CompanyController.php b/app/Http/Controllers/CompanyController.php index f7f91bc472b0..8e78de614a4d 100644 --- a/app/Http/Controllers/CompanyController.php +++ b/app/Http/Controllers/CompanyController.php @@ -495,8 +495,79 @@ class CompanyController extends BaseController } } -// ob_flush(); - return response()->json(['message' => 'success'], 200); } + + /** + * + * Purge Company + * + * @OA\Post( + * path="/api/v1/companies/purge/{company}", + * operationId="postCompanyPurge", + * tags={"companies"}, + * summary="Attempts to purge a company record and all its child records", + * description="Attempts to purge a company record and all its child records", + * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), + * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), + * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), + * @OA\Parameter( + * name="company", + * in="path", + * description="The Company Hashed ID", + * example="D2J234DFA", + * required=true, + * @OA\Schema( + * type="string", + * format="string", + * ), + * ), + * @OA\Response( + * response=200, + * description="Returns a refresh object", + * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), + * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), + * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), + * @OA\JsonContent(ref="#/components/schemas/CompanyUser"), + * ), + * @OA\Response( + * response=422, + * description="Validation error", + * @OA\JsonContent(ref="#/components/schemas/ValidationError"), + * ), + * @OA\Response( + * response="default", + * description="Unexpected Error", + * @OA\JsonContent(ref="#/components/schemas/Error"), + * ), + * ) + */ + public function purgeCompany(Company $company) + { + $account = $company->account; + $company_id = $company->id; + + $company->delete(); + + if($account->companies->count() >= 1) + auth()->user()->setCompany($account->companies->first()); + + if($company_id == $account->default_company_id){ + + $new_default_company = $account->companies->first(); + + if($new_default_company){ + $account->default_company_id = $new_default_company->id; + $account->save(); + } + + } + + $this->entity_type = CompanyUser::class; + $this->entity_transformer = CompanyUserTransformer::class; + + $company_user = $account->company_users->where('user_id', auth()->user()->id); + + return $this->refreshResponse($company_user); + } } diff --git a/app/Http/Controllers/MigrationController.php b/app/Http/Controllers/MigrationController.php index 86eb1e343ef3..5c90d6de6105 100644 --- a/app/Http/Controllers/MigrationController.php +++ b/app/Http/Controllers/MigrationController.php @@ -77,18 +77,7 @@ class MigrationController extends BaseController */ public function purgeCompany(Company $company) { - // $account = $company->account; - // if($account->default_company_id == $company->id) - // { - // $companies = $account->companies; - - // if($companies->count() > 1) - // { - - - // } - // } $company->delete(); return response()->json(['message' => 'Company purged'], 200);