diff --git a/app/Http/Controllers/EmailHistoryController.php b/app/Http/Controllers/EmailHistoryController.php index 863ac93d6a3b..e8f2d4894fda 100644 --- a/app/Http/Controllers/EmailHistoryController.php +++ b/app/Http/Controllers/EmailHistoryController.php @@ -30,9 +30,11 @@ class EmailHistoryController extends BaseController $data = SystemLog::where('client_id', $client->id) ->where('category_id', SystemLog::CATEGORY_MAIL) ->orderBy('id', 'DESC') + ->cursor() ->map(function ($system_log) { if($system_log->log['history'] ?? false) { - return json_decode($system_log->log['history'], true); + return $system_log->log['history']; + // return json_decode($system_log->log['history'], true); } }); @@ -54,9 +56,11 @@ class EmailHistoryController extends BaseController ->where('category_id', SystemLog::CATEGORY_MAIL) ->whereJsonContains('log->history->entity_id', $this->encodePrimaryKey($request->entity_id)) ->orderBy('id', 'DESC') + ->cursor() ->map(function ($system_log) { if($system_log->log['history'] ?? false) { - return json_decode($system_log->log['history'], true); + return $system_log->log['history']; + // return json_decode($system_log->log['history'], true); } }); diff --git a/tests/Feature/InvoiceEmailTest.php b/tests/Feature/InvoiceEmailTest.php index 9609840e8ae2..2eb3e2da172f 100644 --- a/tests/Feature/InvoiceEmailTest.php +++ b/tests/Feature/InvoiceEmailTest.php @@ -11,14 +11,15 @@ namespace Tests\Feature; +use Tests\TestCase; +use App\Models\SystemLog; +use Tests\MockAccountData; use App\Jobs\Entity\EmailEntity; use App\Utils\Traits\GeneratesCounter; use Illuminate\Database\Eloquent\Model; -use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Session; -use Tests\MockAccountData; -use Tests\TestCase; use Illuminate\Validation\ValidationException; +use Illuminate\Foundation\Testing\DatabaseTransactions; /** * @test @@ -46,6 +47,96 @@ class InvoiceEmailTest extends TestCase } + public function testClientEmailHistory() + { + $system_log = new SystemLog(); + $system_log->company_id = $this->company->id; + $system_log->client_id = $this->client->id; + $system_log->category_id = SystemLog::CATEGORY_MAIL; + $system_log->event_id = SystemLog::EVENT_MAIL_SEND; + $system_log->type_id = SystemLog::TYPE_WEBHOOK_RESPONSE; + $system_log->log = [ + 'history' => [ + 'entity_id' => $this->invoice->hashed_id, + 'entity_type' => 'invoice', + 'subject' => 'Invoice #1', + 'events' => [ + [ + 'recipient' => 'bob@gmail.com', + 'status' => 'Delivered', + 'delivery_message' => 'A message that was deliveryed', + 'server' => 'email.mx.com', + 'server_ip' => '127.0.0.1', + 'date' => \Carbon\Carbon::parse('2023-10-10')->format('Y-m-d H:m:s') ?? '', + ], + ], + ] + ]; + + $system_log->save(); + + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/emails/clientHistory/'.$this->client->hashed_id); + + $response->assertStatus(200); + + $arr = $response->json(); + + $this->assertEquals('invoice', $arr[0]['entity_type']); + + } + + public function testEntityEmailHistory() + { + $system_log = new SystemLog(); + $system_log->company_id = $this->company->id; + $system_log->client_id = $this->client->id; + $system_log->category_id = SystemLog::CATEGORY_MAIL; + $system_log->event_id = SystemLog::EVENT_MAIL_SEND; + $system_log->type_id = SystemLog::TYPE_WEBHOOK_RESPONSE; + $system_log->log = [ + 'history' => [ + 'entity_id' => $this->invoice->hashed_id, + 'entity_type' => 'invoice', + 'subject' => 'Invoice #1', + 'events' => [ + [ + 'recipient' => 'bob@gmail.com', + 'status' => 'Delivered', + 'delivery_message' => 'A message that was deliveryed', + 'server' => 'email.mx.com', + 'server_ip' => '127.0.0.1', + 'date' => \Carbon\Carbon::parse('2023-10-10')->format('Y-m-d H:m:s') ?? '', + ], + ], + ] + ]; + + $system_log->save(); + + $data = [ + 'entity' => 'invoice', + 'entity_id' => $this->invoice->hashed_id, + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/emails/entityHistory/', $data); + + $response->assertStatus(200); + + $arr = $response->json(); + + $this->assertEquals('invoice', $arr[0]['entity_type']); + $this->assertEquals($this->invoice->hashed_id, $arr[0]['entity_id']); + + } + + public function testTemplateValidation() { $data = [