removed session from jobs

This commit is contained in:
Denis Duliçi 2020-12-25 12:08:15 +03:00
parent 5fa882b4e3
commit 7c126169c8
17 changed files with 51 additions and 39 deletions

View File

@ -35,7 +35,7 @@ class Companies extends BulkAction
foreach ($companies as $company) { foreach ($companies as $company) {
try { try {
$this->dispatch(new UpdateCompany($company, $request->merge(['enabled' => 1]))); $this->dispatch(new UpdateCompany($company, $request->merge(['enabled' => 1]), session('company_id')));
} catch (\Exception $e) { } catch (\Exception $e) {
flash($e->getMessage())->error(); flash($e->getMessage())->error();
} }
@ -48,7 +48,7 @@ class Companies extends BulkAction
foreach ($companies as $company) { foreach ($companies as $company) {
try { try {
$this->dispatch(new UpdateCompany($company, $request->merge(['enabled' => 0]))); $this->dispatch(new UpdateCompany($company, $request->merge(['enabled' => 0]), session('company_id')));
} catch (\Exception $e) { } catch (\Exception $e) {
flash($e->getMessage())->error(); flash($e->getMessage())->error();
} }
@ -61,7 +61,7 @@ class Companies extends BulkAction
foreach ($companies as $company) { foreach ($companies as $company) {
try { try {
$this->dispatch(new DeleteCompany($company)); $this->dispatch(new DeleteCompany($company, session('company_id')));
} catch (\Exception $e) { } catch (\Exception $e) {
flash($e->getMessage())->error(); flash($e->getMessage())->error();
} }

View File

@ -23,6 +23,8 @@ class Update extends Command
public $alias; public $alias;
public $company;
public $new; public $new;
public $old; public $old;
@ -51,6 +53,7 @@ class Update extends Command
set_time_limit(3600); // 1 hour set_time_limit(3600); // 1 hour
$this->alias = $this->argument('alias'); $this->alias = $this->argument('alias');
$this->company = $this->argument('company');
if (false === $this->new = $this->getNewVersion()) { if (false === $this->new = $this->getNewVersion()) {
$this->error('Not able to get the latest version of ' . $this->alias . '!'); $this->error('Not able to get the latest version of ' . $this->alias . '!');
@ -60,8 +63,8 @@ class Update extends Command
$this->old = $this->getOldVersion(); $this->old = $this->getOldVersion();
session(['company_id' => $this->argument('company')]); session(['company_id' => $this->company]);
setting()->setExtraColumns(['company_id' => $this->argument('company')]); setting()->setExtraColumns(['company_id' => $this->company]);
if (!$path = $this->download()) { if (!$path = $this->download()) {
return self::CMD_ERROR; return self::CMD_ERROR;
@ -156,7 +159,7 @@ class Update extends Command
$this->info('Finishing update...'); $this->info('Finishing update...');
try { try {
$this->dispatch(new FinishUpdate($this->alias, $this->new, $this->old)); $this->dispatch(new FinishUpdate($this->alias, $this->new, $this->old, $this->company));
} catch (\Exception $e) { } catch (\Exception $e) {
$this->error($e->getMessage()); $this->error($e->getMessage());

View File

@ -69,7 +69,7 @@ class Companies extends ApiController
public function update(Company $company, Request $request) public function update(Company $company, Request $request)
{ {
try { try {
$company = $this->dispatch(new UpdateCompany($company, $request)); $company = $this->dispatch(new UpdateCompany($company, $request, session('company_id')));
return $this->item($company->fresh(), new Transformer()); return $this->item($company->fresh(), new Transformer());
} catch (\Exception $e) { } catch (\Exception $e) {
@ -86,7 +86,7 @@ class Companies extends ApiController
public function enable(Company $company) public function enable(Company $company)
{ {
try { try {
$company = $this->dispatch(new UpdateCompany($company, request()->merge(['enabled' => 1]))); $company = $this->dispatch(new UpdateCompany($company, request()->merge(['enabled' => 1]), session('company_id')));
return $this->item($company->fresh(), new Transformer()); return $this->item($company->fresh(), new Transformer());
} catch (\Exception $e) { } catch (\Exception $e) {
@ -103,7 +103,7 @@ class Companies extends ApiController
public function disable(Company $company) public function disable(Company $company)
{ {
try { try {
$company = $this->dispatch(new UpdateCompany($company, request()->merge(['enabled' => 0]))); $company = $this->dispatch(new UpdateCompany($company, request()->merge(['enabled' => 0]), session('company_id')));
return $this->item($company->fresh(), new Transformer()); return $this->item($company->fresh(), new Transformer());
} catch (\Exception $e) { } catch (\Exception $e) {
@ -120,7 +120,7 @@ class Companies extends ApiController
public function destroy(Company $company) public function destroy(Company $company)
{ {
try { try {
$this->dispatch(new DeleteCompany($company)); $this->dispatch(new DeleteCompany($company, session('company_id')));
return $this->response->noContent(); return $this->response->noContent();
} catch (\Exception $e) { } catch (\Exception $e) {

View File

@ -115,7 +115,7 @@ class Companies extends Controller
{ {
$company_id = session('company_id'); $company_id = session('company_id');
$response = $this->ajaxDispatch(new UpdateCompany($company, $request)); $response = $this->ajaxDispatch(new UpdateCompany($company, $request, session('company_id')));
if ($response['success']) { if ($response['success']) {
$response['redirect'] = route('companies.index'); $response['redirect'] = route('companies.index');
@ -147,7 +147,7 @@ class Companies extends Controller
*/ */
public function enable(Company $company) public function enable(Company $company)
{ {
$response = $this->ajaxDispatch(new UpdateCompany($company, request()->merge(['enabled' => 1]))); $response = $this->ajaxDispatch(new UpdateCompany($company, request()->merge(['enabled' => 1]), session('company_id')));
if ($response['success']) { if ($response['success']) {
$response['message'] = trans('messages.success.enabled', ['type' => trans_choice('general.companies', 1)]); $response['message'] = trans('messages.success.enabled', ['type' => trans_choice('general.companies', 1)]);
@ -165,7 +165,7 @@ class Companies extends Controller
*/ */
public function disable(Company $company) public function disable(Company $company)
{ {
$response = $this->ajaxDispatch(new UpdateCompany($company, request()->merge(['enabled' => 0]))); $response = $this->ajaxDispatch(new UpdateCompany($company, request()->merge(['enabled' => 0]), session('company_id')));
if ($response['success']) { if ($response['success']) {
$response['message'] = trans('messages.success.disabled', ['type' => trans_choice('general.companies', 1)]); $response['message'] = trans('messages.success.disabled', ['type' => trans_choice('general.companies', 1)]);
@ -183,7 +183,7 @@ class Companies extends Controller
*/ */
public function destroy(Company $company) public function destroy(Company $company)
{ {
$response = $this->ajaxDispatch(new DeleteCompany($company)); $response = $this->ajaxDispatch(new DeleteCompany($company, session('company_id')));
$response['redirect'] = route('companies.index'); $response['redirect'] = route('companies.index');

View File

@ -274,7 +274,7 @@ class Updates extends Controller
set_time_limit(900); // 15 minutes set_time_limit(900); // 15 minutes
try { try {
$this->dispatch(new FinishUpdate($request['alias'], $request['version'], $request['installed'])); $this->dispatch(new FinishUpdate($request['alias'], $request['version'], $request['installed'], session('company_id')));
$json = [ $json = [
'success' => true, 'success' => true,

View File

@ -236,7 +236,7 @@ class Item extends Controller
public function install(Request $request) public function install(Request $request)
{ {
try { try {
$this->dispatch(new InstallModule($request['alias'])); $this->dispatch(new InstallModule($request['alias'], session('company_id')));
$name = module($request['alias'])->getName(); $name = module($request['alias'])->getName();
@ -275,7 +275,7 @@ class Item extends Controller
try { try {
$name = module($alias)->getName(); $name = module($alias)->getName();
$this->dispatch(new UninstallModule($alias)); $this->dispatch(new UninstallModule($alias, session('company_id')));
$message = trans('modules.uninstalled', ['module' => $name]); $message = trans('modules.uninstalled', ['module' => $name]);
@ -294,7 +294,7 @@ class Item extends Controller
try { try {
$name = module($alias)->getName(); $name = module($alias)->getName();
$this->dispatch(new EnableModule($alias)); $this->dispatch(new EnableModule($alias, session('company_id')));
$message = trans('modules.enabled', ['module' => $name]); $message = trans('modules.enabled', ['module' => $name]);
@ -313,7 +313,7 @@ class Item extends Controller
try { try {
$name = module($alias)->getName(); $name = module($alias)->getName();
$this->dispatch(new DisableModule($alias)); $this->dispatch(new DisableModule($alias, session('company_id')));
$message = trans('modules.disabled', ['module' => $name]); $message = trans('modules.disabled', ['module' => $name]);

View File

@ -71,7 +71,7 @@ class CreateBankingDocumentTransaction extends Job
$this->request['amount'] = $this->model->amount - $this->model->paid_amount; $this->request['amount'] = $this->model->amount - $this->model->paid_amount;
} }
$this->request['company_id'] = session('company_id'); $this->request['company_id'] = $this->model->company_id;
$this->request['currency_code'] = isset($this->request['currency_code']) ? $this->request['currency_code'] : $this->model->currency_code; $this->request['currency_code'] = isset($this->request['currency_code']) ? $this->request['currency_code'] : $this->model->currency_code;
$this->request['type'] = ($this->model->type === Document::INVOICE_TYPE) ? 'income' : 'expense'; $this->request['type'] = ($this->model->type === Document::INVOICE_TYPE) ? 'income' : 'expense';
$this->request['paid_at'] = isset($this->request['paid_at']) ? $this->request['paid_at'] : Date::now()->format('Y-m-d'); $this->request['paid_at'] = isset($this->request['paid_at']) ? $this->request['paid_at'] : Date::now()->format('Y-m-d');

View File

@ -59,7 +59,7 @@ class CreateContact extends Job
$user = User::create($data); $user = User::create($data);
$user->roles()->attach($customer_role); $user->roles()->attach($customer_role);
$user->companies()->attach(session('company_id')); $user->companies()->attach($data['company_id']);
$this->request['user_id'] = $user->id; $this->request['user_id'] = $user->id;
} }

View File

@ -3,7 +3,6 @@
namespace App\Jobs\Common; namespace App\Jobs\Common;
use App\Abstracts\Job; use App\Abstracts\Job;
use App\Models\Common\Company;
use App\Traits\Users; use App\Traits\Users;
class DeleteCompany extends Job class DeleteCompany extends Job
@ -12,14 +11,17 @@ class DeleteCompany extends Job
protected $company; protected $company;
protected $active_company_id;
/** /**
* Create a new job instance. * Create a new job instance.
* *
* @param $request * @param $request
*/ */
public function __construct($company) public function __construct($company, $active_company_id)
{ {
$this->company = $company; $this->company = $company;
$this->active_company_id = $active_company_id;
} }
/** /**
@ -53,7 +55,7 @@ class DeleteCompany extends Job
public function authorize() public function authorize()
{ {
// Can't delete active company // Can't delete active company
if ($this->company->id == session('company_id')) { if ($this->company->id == $this->active_company_id) {
$message = trans('companies.error.delete_active'); $message = trans('companies.error.delete_active');
throw new \Exception($message); throw new \Exception($message);

View File

@ -16,16 +16,19 @@ class UpdateCompany extends Job
protected $request; protected $request;
protected $active_company_id;
/** /**
* Create a new job instance. * Create a new job instance.
* *
* @param $company * @param $company
* @param $request * @param $request
*/ */
public function __construct($company, $request) public function __construct($company, $request, $active_company_id)
{ {
$this->company = $company; $this->company = $company;
$this->request = $this->getRequestInstance($request); $this->request = $this->getRequestInstance($request);
$this->active_company_id = $active_company_id;
} }
/** /**
@ -94,7 +97,7 @@ class UpdateCompany extends Job
public function authorize() public function authorize()
{ {
// Can't disable active company // Can't disable active company
if (($this->request->get('enabled', 1) == 0) && ($this->company->id == session('company_id'))) { if (($this->request->get('enabled', 1) == 0) && ($this->company->id == $this->active_company_id)) {
$message = trans('companies.error.disable_active'); $message = trans('companies.error.disable_active');
throw new \Exception($message); throw new \Exception($message);

View File

@ -77,7 +77,7 @@ class UpdateContact extends Job
$user = User::create($data); $user = User::create($data);
$user->roles()->attach($customer_role); $user->roles()->attach($customer_role);
$user->companies()->attach(session('company_id')); $user->companies()->attach($data['company_id']);
$this->request['user_id'] = $user->id; $this->request['user_id'] = $user->id;
} }

View File

@ -20,10 +20,10 @@ class DisableModule extends Job
* @param $company_id * @param $company_id
* @param $locale * @param $locale
*/ */
public function __construct($alias, $company_id = null, $locale = null) public function __construct($alias, $company_id, $locale = null)
{ {
$this->alias = $alias; $this->alias = $alias;
$this->company_id = $company_id ?: session('company_id'); $this->company_id = $company_id;
$this->locale = $locale ?: app()->getLocale(); $this->locale = $locale ?: app()->getLocale();
} }

View File

@ -17,10 +17,10 @@ class DownloadModule extends Job
* @param $alias * @param $alias
* @param $company_id * @param $company_id
*/ */
public function __construct($alias, $company_id = null) public function __construct($alias, $company_id)
{ {
$this->alias = $alias; $this->alias = $alias;
$this->company_id = $company_id ?: session('company_id'); $this->company_id = $company_id;
} }
/** /**

View File

@ -20,10 +20,10 @@ class EnableModule extends Job
* @param $company_id * @param $company_id
* @param $locale * @param $locale
*/ */
public function __construct($alias, $company_id = null, $locale = null) public function __construct($alias, $company_id, $locale = null)
{ {
$this->alias = $alias; $this->alias = $alias;
$this->company_id = $company_id ?: session('company_id'); $this->company_id = $company_id;
$this->locale = $locale ?: app()->getLocale(); $this->locale = $locale ?: app()->getLocale();
} }

View File

@ -14,18 +14,22 @@ class FinishUpdate extends Job
protected $old; protected $old;
protected $company_id;
/** /**
* Create a new job instance. * Create a new job instance.
* *
* @param $alias * @param $alias
* @param $new * @param $new
* @param $old * @param $old
* @param $company_id
*/ */
public function __construct($alias, $new, $old) public function __construct($alias, $new, $old, $company_id)
{ {
$this->alias = $alias; $this->alias = $alias;
$this->new = $new; $this->new = $new;
$this->old = $old; $this->old = $old;
$this->company_id = $company_id;
} }
/** /**
@ -36,7 +40,7 @@ class FinishUpdate extends Job
public function handle() public function handle()
{ {
if ($this->alias == 'core') { if ($this->alias == 'core') {
$companies = [session('company_id')]; $companies = [$this->company_id];
} else { } else {
$companies = Module::alias($this->alias)->allCompanies()->cursor(); $companies = Module::alias($this->alias)->allCompanies()->cursor();
} }

View File

@ -20,10 +20,10 @@ class InstallModule extends Job
* @param $company_id * @param $company_id
* @param $locale * @param $locale
*/ */
public function __construct($alias, $company_id = null, $locale = null) public function __construct($alias, $company_id, $locale = null)
{ {
$this->alias = $alias; $this->alias = $alias;
$this->company_id = $company_id ?: session('company_id'); $this->company_id = $company_id;
$this->locale = $locale ?: app()->getLocale(); $this->locale = $locale ?: app()->getLocale();
} }

View File

@ -20,10 +20,10 @@ class UninstallModule extends Job
* @param $company_id * @param $company_id
* @param $locale * @param $locale
*/ */
public function __construct($alias, $company_id = null, $locale = null) public function __construct($alias, $company_id, $locale = null)
{ {
$this->alias = $alias; $this->alias = $alias;
$this->company_id = $company_id ?: session('company_id'); $this->company_id = $company_id;
$this->locale = $locale ?: app()->getLocale(); $this->locale = $locale ?: app()->getLocale();
} }