added db transaction to jobs

This commit is contained in:
Denis Duliçi
2020-06-26 13:40:19 +03:00
parent f20f5c9def
commit acdc9da2c8
57 changed files with 606 additions and 469 deletions

View File

@@ -8,10 +8,10 @@ use Artisan;
class CreateCompany extends Job
{
protected $request;
protected $company;
protected $request;
/**
* Create a new job instance.
*
@@ -29,15 +29,17 @@ class CreateCompany extends Job
*/
public function handle()
{
$this->company = Company::create($this->request->all());
\DB::transaction(function () {
$this->company = Company::create($this->request->all());
// Clear settings
setting()->setExtraColumns(['company_id' => $this->company->id]);
setting()->forgetAll();
// Clear settings
setting()->setExtraColumns(['company_id' => $this->company->id]);
setting()->forgetAll();
$this->callSeeds();
$this->callSeeds();
$this->updateSettings();
$this->updateSettings();
});
return $this->company;
}

View File

@@ -9,6 +9,8 @@ use App\Models\Common\Contact;
class CreateContact extends Job
{
protected $contact;
protected $request;
/**
@@ -28,13 +30,15 @@ class CreateContact extends Job
*/
public function handle()
{
if ($this->request->get('create_user', 'false') === 'true') {
$this->createUser();
}
\DB::transaction(function () {
if ($this->request->get('create_user', 'false') === 'true') {
$this->createUser();
}
$contact = Contact::create($this->request->all());
$this->contact = Contact::create($this->request->all());
});
return $contact;
return $this->contact;
}
public function createUser()

View File

@@ -9,6 +9,8 @@ use App\Utilities\Widgets;
class CreateDashboard extends Job
{
protected $dashboard;
protected $request;
/**
@@ -30,13 +32,15 @@ class CreateDashboard extends Job
{
$this->request['enabled'] = $this->request['enabled'] ?? 1;
$this->dashboard = Dashboard::create($this->request->only(['company_id', 'name', 'enabled']));
\DB::transaction(function () {
$this->dashboard = Dashboard::create($this->request->only(['company_id', 'name', 'enabled']));
$this->attachToUser();
$this->attachToUser();
if ($this->request->has('with_widgets')) {
$this->createWidgets();
}
if ($this->request->has('with_widgets')) {
$this->createWidgets();
}
});
return $this->dashboard;
}

View File

@@ -7,6 +7,8 @@ use App\Models\Common\Item;
class CreateItem extends Job
{
protected $item;
protected $request;
/**
@@ -26,15 +28,17 @@ class CreateItem extends Job
*/
public function handle()
{
$item = Item::create($this->request->all());
\DB::transaction(function () {
$this->item = Item::create($this->request->all());
// Upload picture
if ($this->request->file('picture')) {
$media = $this->getMedia($this->request->file('picture'), 'items');
// Upload picture
if ($this->request->file('picture')) {
$media = $this->getMedia($this->request->file('picture'), 'items');
$item->attachMedia($media, 'picture');
}
$this->item->attachMedia($media, 'picture');
}
});
return $item;
return $this->item;
}
}

View File

@@ -7,6 +7,8 @@ use App\Models\Common\Report;
class CreateReport extends Job
{
protected $report;
protected $request;
/**
@@ -26,8 +28,10 @@ class CreateReport extends Job
*/
public function handle()
{
$report = Report::create($this->request->all());
\DB::transaction(function () {
$this->report = Report::create($this->request->all());
});
return $report;
return $this->report;
}
}

View File

@@ -31,14 +31,16 @@ class DeleteCompany extends Job
{
$this->authorize();
$this->deleteRelationships($this->company, [
'accounts', 'bills', 'bill_histories', 'bill_items', 'bill_item_taxes', 'bill_totals', 'categories',
'contacts', 'currencies', 'dashboards', 'email_templates', 'invoices', 'invoice_histories', 'invoice_items',
'invoice_item_taxes', 'invoice_totals', 'items', 'modules', 'module_histories', 'reconciliations',
'recurring', 'reports', 'settings', 'taxes', 'transactions', 'transfers', 'widgets',
]);
\DB::transaction(function () {
$this->deleteRelationships($this->company, [
'accounts', 'bills', 'bill_histories', 'bill_items', 'bill_item_taxes', 'bill_totals', 'categories',
'contacts', 'currencies', 'dashboards', 'email_templates', 'invoices', 'invoice_histories', 'invoice_items',
'invoice_item_taxes', 'invoice_totals', 'items', 'modules', 'module_histories', 'reconciliations',
'recurring', 'reports', 'settings', 'taxes', 'transactions', 'transfers', 'widgets',
]);
$this->company->delete();
$this->company->delete();
});
return true;
}

View File

@@ -31,11 +31,13 @@ class DeleteContact extends Job
{
$this->authorize();
if ($user = $this->contact->user) {
$this->dispatch(new DeleteUser($user));
}
\DB::transaction(function () {
if ($user = $this->contact->user) {
$this->dispatch(new DeleteUser($user));
}
$this->contact->delete();
$this->contact->delete();
});
return true;
}

View File

@@ -30,9 +30,11 @@ class DeleteDashboard extends Job
{
$this->authorize();
$this->deleteRelationships($this->dashboard, ['widgets']);
\DB::transaction(function () {
$this->deleteRelationships($this->dashboard, ['widgets']);
$this->dashboard->delete();
$this->dashboard->delete();
});
return true;
}

View File

@@ -27,7 +27,9 @@ class DeleteItem extends Job
{
$this->authorize();
$this->item->delete();
\DB::transaction(function () {
$this->item->delete();
});
return true;
}

View File

@@ -25,7 +25,9 @@ class DeleteReport extends Job
*/
public function handle()
{
$this->report->delete();
\DB::transaction(function () {
$this->report->delete();
});
return true;
}

View File

@@ -35,45 +35,47 @@ class UpdateCompany extends Job
{
$this->authorize();
$this->company->update($this->request->all());
\DB::transaction(function () {
$this->company->update($this->request->all());
// Clear current and load given company settings
setting()->setExtraColumns(['company_id' => $this->company->id]);
setting()->forgetAll();
setting()->load(true);
// Clear current and load given company settings
setting()->setExtraColumns(['company_id' => $this->company->id]);
setting()->forgetAll();
setting()->load(true);
if ($this->request->has('name')) {
setting()->set('company.name', $this->request->get('name'));
}
if ($this->request->has('email')) {
setting()->set('company.email', $this->request->get('email'));
}
if ($this->request->has('address')) {
setting()->set('company.address', $this->request->get('address'));
}
if ($this->request->has('currency')) {
setting()->set('default.currency', $this->request->get('currency'));
}
if ($this->request->has('locale')) {
setting()->set('default.locale', $this->request->get('locale'));
}
if ($this->request->file('logo')) {
$company_logo = $this->getMedia($this->request->file('logo'), 'settings', $this->company->id);
if ($company_logo) {
$this->company->attachMedia($company_logo, 'company_logo');
setting()->set('company.logo', $company_logo->id);
if ($this->request->has('name')) {
setting()->set('company.name', $this->request->get('name'));
}
}
setting()->save();
setting()->forgetAll();
if ($this->request->has('email')) {
setting()->set('company.email', $this->request->get('email'));
}
if ($this->request->has('address')) {
setting()->set('company.address', $this->request->get('address'));
}
if ($this->request->has('currency')) {
setting()->set('default.currency', $this->request->get('currency'));
}
if ($this->request->has('locale')) {
setting()->set('default.locale', $this->request->get('locale'));
}
if ($this->request->file('logo')) {
$company_logo = $this->getMedia($this->request->file('logo'), 'settings', $this->company->id);
if ($company_logo) {
$this->company->attachMedia($company_logo, 'company_logo');
setting()->set('company.logo', $company_logo->id);
}
}
setting()->save();
setting()->forgetAll();
});
return $this->company;
}

View File

@@ -33,11 +33,13 @@ class UpdateContact extends Job
{
$this->authorize();
if ($this->request->get('create_user', 'false') === 'true') {
$this->createUser();
}
\DB::transaction(function () {
if ($this->request->get('create_user', 'false') === 'true') {
$this->createUser();
}
$this->contact->update($this->request->all());
$this->contact->update($this->request->all());
});
return $this->contact;
}

View File

@@ -35,11 +35,13 @@ class UpdateDashboard extends Job
{
$this->authorize();
$this->dashboard->update($this->request->all());
\DB::transaction(function () {
$this->dashboard->update($this->request->all());
if ($this->request->has('users')) {
$this->dashboard->users()->sync($this->request->get('users'));
}
if ($this->request->has('users')) {
$this->dashboard->users()->sync($this->request->get('users'));
}
});
return $this->dashboard;
}

View File

@@ -30,14 +30,16 @@ class UpdateItem extends Job
*/
public function handle()
{
$this->item->update($this->request->all());
\DB::transaction(function () {
$this->item->update($this->request->all());
// Upload picture
if ($this->request->file('picture')) {
$media = $this->getMedia($this->request->file('picture'), 'items');
// Upload picture
if ($this->request->file('picture')) {
$media = $this->getMedia($this->request->file('picture'), 'items');
$this->item->attachMedia($media, 'picture');
}
$this->item->attachMedia($media, 'picture');
}
});
return $this->item;
}

View File

@@ -30,7 +30,9 @@ class UpdateReport extends Job
*/
public function handle()
{
$this->report->update($this->request->all());
\DB::transaction(function () {
$this->report->update($this->request->all());
});
return $this->report;
}