mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Working on importing json
This commit is contained in:
parent
ec66a4fdc5
commit
f8bba8b7c0
10
app/Exceptions/ImportCompanyFailed.php
Normal file
10
app/Exceptions/ImportCompanyFailed.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class ImportCompanyFailed extends Exception
|
||||
{
|
||||
// ..
|
||||
}
|
@ -101,7 +101,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $activity;
|
||||
|
||||
})->makeHidden(['id'])->toArray();
|
||||
})->makeHidden(['id'])->all();
|
||||
|
||||
$this->export_data['backups'] = $this->company->all_activities()->with('backup')->cursor()->map(function ($activity){
|
||||
|
||||
@ -114,16 +114,16 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $backup;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
$this->export_data['users'] = $this->company->users()->withTrashed()->cursor()->map(function ($user){
|
||||
|
||||
$user->account_id = $this->encodePrimaryKey($user->account_id);
|
||||
$user->id = $this->encodePrimaryKey($user->id);
|
||||
// $user->id = $this->encodePrimaryKey($user->id);
|
||||
|
||||
return $user;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
|
||||
$this->export_data['client_contacts'] = $this->company->client_contacts->map(function ($client_contact){
|
||||
@ -132,7 +132,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $client_contact;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
|
||||
$this->export_data['client_gateway_tokens'] = $this->company->client_gateway_tokens->map(function ($client_gateway_token){
|
||||
@ -141,7 +141,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $client_gateway_token;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
|
||||
$this->export_data['clients'] = $this->company->clients->map(function ($client){
|
||||
@ -150,7 +150,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $client;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
$this->export_data['company'] = $this->company->toArray();
|
||||
|
||||
@ -161,7 +161,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $company_gateway;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
$this->export_data['company_tokens'] = $this->company->tokens->map(function ($token){
|
||||
|
||||
@ -169,7 +169,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $token;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
$this->export_data['company_ledger'] = $this->company->ledger->map(function ($ledger){
|
||||
|
||||
@ -177,7 +177,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $ledger;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
$this->export_data['company_users'] = $this->company->company_users->map(function ($company_user){
|
||||
|
||||
@ -185,7 +185,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $company_user;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
$this->export_data['credits'] = $this->company->credits->map(function ($credit){
|
||||
|
||||
@ -194,7 +194,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $credit;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
|
||||
$this->export_data['credit_invitations'] = CreditInvitation::where('company_id', $this->company->id)->withTrashed()->cursor()->map(function ($credit){
|
||||
@ -203,9 +203,9 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $credit;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
$this->export_data['designs'] = $this->company->user_designs->makeHidden(['id'])->toArray();
|
||||
$this->export_data['designs'] = $this->company->user_designs->makeHidden(['id'])->all();
|
||||
|
||||
$this->export_data['documents'] = $this->company->documents->map(function ($document){
|
||||
|
||||
@ -213,7 +213,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $document;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
$this->export_data['expense_categories'] = $this->company->expenses->map(function ($expense_category){
|
||||
|
||||
@ -221,7 +221,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $expense_category;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
|
||||
$this->export_data['expenses'] = $this->company->expenses->map(function ($expense){
|
||||
@ -231,7 +231,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $expense;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
$this->export_data['group_settings'] = $this->company->group_settings->map(function ($gs){
|
||||
|
||||
@ -239,7 +239,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $gs;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
|
||||
$this->export_data['invoices'] = $this->company->invoices->map(function ($invoice){
|
||||
@ -249,7 +249,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $invoice;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
|
||||
$this->export_data['invoice_invitations'] = InvoiceInvitation::where('company_id', $this->company->id)->withTrashed()->cursor()->map(function ($invoice){
|
||||
@ -258,7 +258,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $invoice;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
$this->export_data['payment_terms'] = $this->company->user_payment_terms->map(function ($term){
|
||||
|
||||
@ -266,7 +266,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $term;
|
||||
|
||||
})->makeHidden(['id'])->toArray();
|
||||
})->makeHidden(['id'])->all();
|
||||
|
||||
$this->export_data['paymentables'] = $this->company->payments()->with('paymentables')->cursor()->map(function ($paymentable){
|
||||
|
||||
@ -274,7 +274,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $paymentable;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
$this->export_data['payments'] = $this->company->payments->map(function ($payment){
|
||||
|
||||
@ -283,7 +283,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $payment;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
|
||||
$this->export_data['projects'] = $this->company->projects->map(function ($project){
|
||||
@ -293,7 +293,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $project;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
$this->export_data['quotes'] = $this->company->quotes->map(function ($quote){
|
||||
|
||||
@ -302,7 +302,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $quote;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
|
||||
$this->export_data['quote_invitations'] = QuoteInvitation::where('company_id', $this->company->id)->withTrashed()->cursor()->map(function ($quote){
|
||||
@ -311,7 +311,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $quote;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
|
||||
$this->export_data['recurring_invoices'] = $this->company->recurring_invoices->map(function ($ri){
|
||||
@ -320,7 +320,7 @@ class CompanyExport implements ShouldQueue
|
||||
$ri = $this->transformArrayOfKeys($ri, ['client_id', 'vendor_id', 'project_id', 'design_id', 'subscription_id']);
|
||||
return $ri;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
|
||||
$this->export_data['recurring_invoice_invitations'] = RecurringInvoiceInvitation::where('company_id', $this->company->id)->withTrashed()->cursor()->map(function ($ri){
|
||||
@ -329,7 +329,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $ri;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
$this->export_data['subscriptions'] = $this->company->subscriptions->map(function ($subscription){
|
||||
|
||||
@ -338,7 +338,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $subscription;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
|
||||
$this->export_data['system_logs'] = $this->company->system_logs->map(function ($log){
|
||||
@ -348,7 +348,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $log;
|
||||
|
||||
})->makeHidden(['id'])->toArray();
|
||||
})->makeHidden(['id'])->all();
|
||||
|
||||
$this->export_data['tasks'] = $this->company->tasks->map(function ($task){
|
||||
|
||||
@ -357,7 +357,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $task;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
$this->export_data['task_statuses'] = $this->company->task_statuses->map(function ($status){
|
||||
|
||||
@ -367,7 +367,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $status;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
$this->export_data['tax_rates'] = $this->company->tax_rates->map(function ($rate){
|
||||
|
||||
@ -376,13 +376,13 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $rate;
|
||||
|
||||
})->makeHidden(['id'])->toArray();
|
||||
})->makeHidden(['id'])->all();
|
||||
|
||||
$this->export_data['vendors'] = $this->company->vendors->map(function ($vendor){
|
||||
|
||||
return $this->transformBasicEntities($vendor);
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
|
||||
$this->export_data['vendor_contacts'] = VendorContact::where('company_id', $this->company->id)->withTrashed()->cursor()->map(function ($vendor){
|
||||
@ -392,7 +392,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $vendor;
|
||||
|
||||
})->toArray();
|
||||
})->all();
|
||||
|
||||
$this->export_data['webhooks'] = $this->company->webhooks->map(function ($hook){
|
||||
|
||||
@ -401,7 +401,7 @@ class CompanyExport implements ShouldQueue
|
||||
|
||||
return $hook;
|
||||
|
||||
})->makeHidden(['id'])->toArray();
|
||||
})->makeHidden(['id'])->all();
|
||||
|
||||
//write to tmp and email to owner();
|
||||
|
||||
|
@ -11,6 +11,7 @@
|
||||
|
||||
namespace App\Jobs\Company;
|
||||
|
||||
use App\Exceptions\ImportCompanyFailed;
|
||||
use App\Exceptions\NonExistingMigrationFile;
|
||||
use App\Jobs\Mail\NinjaMailerJob;
|
||||
use App\Jobs\Mail\NinjaMailerObject;
|
||||
@ -19,6 +20,7 @@ use App\Libraries\MultiDB;
|
||||
use App\Mail\DownloadBackup;
|
||||
use App\Mail\DownloadInvoices;
|
||||
use App\Models\Company;
|
||||
use App\Models\CompanyUser;
|
||||
use App\Models\CreditInvitation;
|
||||
use App\Models\InvoiceInvitation;
|
||||
use App\Models\QuoteInvitation;
|
||||
@ -100,7 +102,7 @@ class CompanyImport implements ShouldQueue
|
||||
{
|
||||
MultiDB::setDb($this->company->db);
|
||||
|
||||
$this->company =Company::where('company_key', $this->company->company_key)->firstOrFail();
|
||||
$this->company = Company::where('company_key', $this->company->company_key)->firstOrFail();
|
||||
$this->account = $this->company->account;
|
||||
|
||||
$this->unzipFile()
|
||||
@ -168,6 +170,9 @@ class CompanyImport implements ShouldQueue
|
||||
foreach ($this->backup_file->users as $user)
|
||||
{
|
||||
|
||||
if(User::where('email', $user->email)->where('account_id', '!=', $this->account->id)->exists())
|
||||
throw new ImportCompanyFailed("{$user->email} is already in the system attached to a different account");
|
||||
|
||||
$new_user = User::firstOrNew(
|
||||
['email' => $user->email],
|
||||
(array)$user,
|
||||
@ -176,15 +181,38 @@ class CompanyImport implements ShouldQueue
|
||||
$new_user->account_id = $this->account->id;
|
||||
$new_user->save(['timestamps' => false]);
|
||||
|
||||
$this->ids['users']["{$user->id}"] = $new_user->id;
|
||||
$this->ids['users']["{$user->hashed_id}"] = $new_user->id;
|
||||
|
||||
}
|
||||
|
||||
Expense::reguard();
|
||||
User::reguard();
|
||||
|
||||
}
|
||||
|
||||
private function importCompanyUsers()
|
||||
{
|
||||
CompanyUser::unguard();
|
||||
|
||||
foreach($this->backup_file->company_users as $cu)
|
||||
{
|
||||
$user_id = $this->transformId($cu->user_id);
|
||||
|
||||
$new_cu = CompanyUser::firstOrNew(
|
||||
['user_id' => $user_id, 'company_id', $this->company->id],
|
||||
(array)$cu,
|
||||
);
|
||||
|
||||
$new_cu->account_id = $this->account->id;
|
||||
$new_cu->save(['timestamps' => false]);
|
||||
|
||||
}
|
||||
|
||||
CompanyUser::reguard();
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function transformId(string$resource, string $old): int
|
||||
public function transformId(string $resource, string $old): int
|
||||
{
|
||||
if (! array_key_exists($resource, $this->ids)) {
|
||||
throw new \Exception("Resource {$resource} not available.");
|
||||
|
@ -13,6 +13,8 @@ namespace Tests\Feature\Import;
|
||||
use App\Jobs\Import\CSVImport;
|
||||
use App\Models\Account;
|
||||
use App\Models\Client;
|
||||
use App\Models\Company;
|
||||
use App\Models\CompanyUser;
|
||||
use App\Models\Expense;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Payment;
|
||||
@ -37,6 +39,7 @@ class ImportCompanyTest extends TestCase
|
||||
use MakesHash;
|
||||
|
||||
public $account;
|
||||
public $company;
|
||||
|
||||
public function setUp() :void
|
||||
{
|
||||
@ -49,6 +52,7 @@ class ImportCompanyTest extends TestCase
|
||||
$this->withoutExceptionHandling();
|
||||
|
||||
$this->account = Account::factory()->create();
|
||||
$this->company = Company::factory()->create(['account_id' => $this->account->id]);
|
||||
}
|
||||
|
||||
public function testBackupJsonRead()
|
||||
@ -122,6 +126,8 @@ class ImportCompanyTest extends TestCase
|
||||
|
||||
User::unguard();
|
||||
|
||||
$this->assertEquals(2, count($backup_json_object->users));
|
||||
|
||||
foreach ($backup_json_object->users as $user)
|
||||
{
|
||||
$user_array = (array)$user;
|
||||
@ -142,7 +148,51 @@ class ImportCompanyTest extends TestCase
|
||||
User::reguard();
|
||||
|
||||
$this->assertEquals(2, User::count());
|
||||
|
||||
$this->assertEquals(2, count($backup_json_object->company_users));
|
||||
|
||||
CompanyUser::unguard();
|
||||
|
||||
foreach($backup_json_object->company_users as $cu)
|
||||
{
|
||||
$user_id = $this->transformId('users', $cu->user_id);
|
||||
|
||||
$cu_array = (array)$cu;
|
||||
unset($cu_array['user_id']);
|
||||
unset($cu_array['company_id']);
|
||||
unset($cu_array['account_id']);
|
||||
unset($cu_array['hashed_id']);
|
||||
unset($cu_array['id']);
|
||||
|
||||
$new_cu = CompanyUser::firstOrNew(
|
||||
['user_id' => $user_id, 'company_id' => $this->company->id],
|
||||
(array)$cu_array,
|
||||
);
|
||||
|
||||
$new_cu->account_id = $this->account->id;
|
||||
$new_cu->save(['timestamps' => false]);
|
||||
|
||||
}
|
||||
|
||||
CompanyUser::reguard();
|
||||
|
||||
$this->assertEquals(2, CompanyUser::count());
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
private function transformId(string $resource, string $old): int
|
||||
{
|
||||
if (! array_key_exists($resource, $this->ids)) {
|
||||
throw new \Exception("Resource {$resource} not available.");
|
||||
}
|
||||
|
||||
if (! array_key_exists("{$old}", $this->ids[$resource])) {
|
||||
throw new \Exception("Missing resource key: {$old}");
|
||||
}
|
||||
|
||||
return $this->ids[$resource]["{$old}"];
|
||||
}
|
||||
|
||||
}
|
||||
|
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user