diff --git a/app/Events/Quote/QuoteWasArchived.php b/app/Events/Quote/QuoteWasArchived.php index 03fd3a82d985..873fecb31ef3 100644 --- a/app/Events/Quote/QuoteWasArchived.php +++ b/app/Events/Quote/QuoteWasArchived.php @@ -12,6 +12,7 @@ namespace App\Events\Quote; use App\Models\Company; +use App\Models\Quote; use Illuminate\Queue\SerializesModels; class QuoteWasArchived diff --git a/app/Events/Quote/QuoteWasCreated.php b/app/Events/Quote/QuoteWasCreated.php index 49420e087749..0fc4155da2bb 100644 --- a/app/Events/Quote/QuoteWasCreated.php +++ b/app/Events/Quote/QuoteWasCreated.php @@ -12,6 +12,7 @@ namespace App\Events\Quote; use App\Models\Company; +use App\Models\Quote; use Illuminate\Queue\SerializesModels; /** diff --git a/app/Events/Quote/QuoteWasDeleted.php b/app/Events/Quote/QuoteWasDeleted.php index fdf3273b2bd7..9bfac894e092 100644 --- a/app/Events/Quote/QuoteWasDeleted.php +++ b/app/Events/Quote/QuoteWasDeleted.php @@ -12,6 +12,7 @@ namespace App\Events\Quote; use App\Models\Company; +use App\Models\Quote; use Illuminate\Queue\SerializesModels; /** diff --git a/app/Events/Quote/QuoteWasRestored.php b/app/Events/Quote/QuoteWasRestored.php index 53abe1bc8ade..6f2e4dd97974 100644 --- a/app/Events/Quote/QuoteWasRestored.php +++ b/app/Events/Quote/QuoteWasRestored.php @@ -12,6 +12,7 @@ namespace App\Events\Quote; use App\Models\Company; +use App\Models\Quote; use Illuminate\Queue\SerializesModels; /** diff --git a/app/Http/Controllers/QuoteController.php b/app/Http/Controllers/QuoteController.php index 6cf4cdd25744..a8d93a3e713c 100644 --- a/app/Http/Controllers/QuoteController.php +++ b/app/Http/Controllers/QuoteController.php @@ -11,6 +11,8 @@ namespace App\Http\Controllers; +use App\Events\Quote\QuoteWasCreated; +use App\Events\Quote\QuoteWasUpdated; use App\Factory\CloneInvoiceFactory; use App\Factory\CloneInvoiceToQuoteFactory; use App\Factory\CloneQuoteFactory; @@ -31,6 +33,7 @@ use App\Models\Quote; use App\Repositories\QuoteRepository; use App\Transformers\InvoiceTransformer; use App\Transformers\QuoteTransformer; +use App\Utils\Ninja; use App\Utils\TempFile; use App\Utils\Traits\MakesHash; use Illuminate\Http\Request; @@ -204,6 +207,8 @@ class QuoteController extends BaseController $quote = $this->quote_repo->save($request->all(), QuoteFactory::create(auth()->user()->company()->id, auth()->user()->id)); + event(new QuoteWasCreated($quote, $quote->company, Ninja::eventVars())); + return $this->itemResponse($quote); } @@ -378,6 +383,8 @@ class QuoteController extends BaseController $quote = $this->quote_repo->save($request->all(), $quote); + event(new QuoteWasUpdated($quote, $quote->company, Ninja::eventVars())); + return $this->itemResponse($quote); } @@ -658,15 +665,26 @@ class QuoteController extends BaseController }, basename($quote->pdf_file_path())); //return response()->download(TempFile::path($quote->pdf_file_path()), basename($quote->pdf_file_path())); break; + case 'restore': + $this->quote_repo->restore($quote); + + if (!$bulk) + return $this->listResponse($quote); + + break; case 'archive': $this->quote_repo->archive($quote); - return $this->listResponse($quote); + if (!$bulk) + return $this->listResponse($quote); + break; case 'delete': $this->quote_repo->delete($quote); - return $this->listResponse($quote); + if (!$bulk) + return $this->listResponse($quote); + break; case 'email': $quote->service()->sendEmail(); @@ -679,6 +697,7 @@ class QuoteController extends BaseController if (! $bulk) { return $this->itemResponse($quote); } + break; // no break default: return response()->json(['message' => "The requested action `{$action}` is not available."], 400); diff --git a/tests/Integration/EventTest.php b/tests/Integration/EventTest.php index 2df79eec7736..73bec63fce06 100644 --- a/tests/Integration/EventTest.php +++ b/tests/Integration/EventTest.php @@ -27,6 +27,11 @@ use App\Events\Payment\PaymentWasCreated; use App\Events\Payment\PaymentWasDeleted; use App\Events\Payment\PaymentWasRestored; use App\Events\Payment\PaymentWasUpdated; +use App\Events\Quote\QuoteWasArchived; +use App\Events\Quote\QuoteWasCreated; +use App\Events\Quote\QuoteWasDeleted; +use App\Events\Quote\QuoteWasRestored; +use App\Events\Quote\QuoteWasUpdated; use App\Models\Credit; use App\Models\Design; use App\Models\Invoice; @@ -59,6 +64,69 @@ class EventTest extends TestCase $this->makeTestData(); } + public function testQuoteEvents() + { + + /* Test fire new invoice */ + $data = [ + 'client_id' => $this->client->hashed_id, + 'number' => 'dude', + ]; + + $this->expectsEvents([ + QuoteWasCreated::class, + QuoteWasUpdated::class, + QuoteWasArchived::class, + QuoteWasRestored::class, + QuoteWasDeleted::class, + ]); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/quotes/', $data) + ->assertStatus(200); + + + $arr = $response->json(); + + $data = [ + 'client_id' => $this->client->hashed_id, + 'number' => 'dude2', + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->put('/api/v1/quotes/' . $arr['data']['id'], $data) + ->assertStatus(200); + + + $data = [ + 'ids' => [$arr['data']['id']], + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/quotes/bulk?action=archive', $data) + ->assertStatus(200); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/quotes/bulk?action=restore', $data) + ->assertStatus(200); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/quotes/bulk?action=delete', $data) + ->assertStatus(200); + + } + + //@TODO paymentwasvoided //@TODO paymentwasrefunded