Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hellsythe committed Mar 27, 2024
1 parent d811c4a commit af34881
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 43 deletions.
22 changes: 2 additions & 20 deletions src/Http/Controllers/WabaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Sdkconsultoria\WhatsappCloudApi\Http\Controllers;

use Sdkconsultoria\WhatsappCloudApi\Models\Template;
use Sdkconsultoria\WhatsappCloudApi\Lib\Template\Adapter\MetaToApp;
use Sdkconsultoria\WhatsappCloudApi\Models\Waba;
use Sdkconsultoria\WhatsappCloudApi\Services\WabaManagerService;

Expand Down Expand Up @@ -31,28 +31,10 @@ public function loadTemplatesFromWaba(string $wabaId)
private function saveTemplates($templates, $wabaId)
{
foreach ($templates['data'] as $template) {
$this->saveTemplate($template, $wabaId);
resolve(MetaToApp::class)->process($template, $wabaId);
}
}

private function saveTemplate($template, $wabaId)
{
$templateModel = Template::where('template_id', $template['id'])->first();

if (! $templateModel) {
$templateModel = new Template();
}

$templateModel->waba_id = $wabaId;
$templateModel->name = $template['name'];
$templateModel->status = $template['status'];
$templateModel->category = $template['category'];
$templateModel->language = $template['language'];
$templateModel->template_id = $template['id'];
$templateModel->content = json_encode($template);
$templateModel->save();
}

public function getWabaInfoFromMeta(string $wabaId)
{
return resolve(WabaManagerService::class)->getWabaInfo($wabaId);
Expand Down
14 changes: 1 addition & 13 deletions src/Lib/Template/SendTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function Send(WabaPhone $wabaPhone, Template $template, string $to, array

$messageModel = new Message();
$messageModel->direction = 'toClient';
$messageModel->body = $this->fixComponentsWithVars($template);
$messageModel->body = json_encode($template->getComponentsWithVars());
$messageModel->timestamp = time();
$messageModel->message_id = $message['messages'][0]['id'];
$messageModel->type = 'template';
Expand All @@ -27,18 +27,6 @@ public function Send(WabaPhone $wabaPhone, Template $template, string $to, array
$messageModel->save();
}

private function fixComponentsWithVars(Template $template)
{
$components = $template->getComponentsWithVars();
$fixedComponents = [];

foreach ($components['components'] as $component) {
$fixedComponents[$component['type']] = $component;
}

return json_encode($fixedComponents);
}

private function getChatId($wabaPhone, $to)
{
$chat = Chat::firstOrCreate([
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Template extends Model

public function getComponents()
{
return json_decode($this->content, true);
return json_decode($this->content, true)['components'];
}

public function getComponentsWithVars(): array
Expand Down
22 changes: 13 additions & 9 deletions tests/Feature/Template/SendTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ public function test_send_text_header_template()
$wabaPhone = WabaPhone::factory()->create();
$template = Template::factory()->create([
'content' => json_encode([
'body' => [
'text' => 'Hello this is a test',
'components' => [
'body' => [
'text' => 'Hello this is a test',
],
'header' => ['format' => 'text', 'text' => 'Header text'],
],
'header' => ['format' => 'text', 'text' => 'Header text'],
]),
]);
$messageId = 'wamid.'.$this->faker()->numberBetween(111, 450);
Expand All @@ -76,12 +78,12 @@ public function test_send_text_header_template_missing_vars()
{
$wabaPhone = WabaPhone::factory()->create();
$template = Template::factory()->create([
'content' => json_encode([
'content' => json_encode(['components' => [
'body' => [
'text' => 'Hello this is a test',
],
'header' => ['format' => 'text', 'text' => 'Header {{1}} text'],
]),
]]),
]);

$this->post(route('message.template.send'), [
Expand All @@ -96,7 +98,7 @@ public function test_send_text_template_with_missing_vars()
{
$wabaPhone = WabaPhone::factory()->create();
$template = Template::factory()->create([
'content' => json_encode(['body' => ['text' => 'Hello {{1}}']]),
'content' => json_encode(['components' => ['body' => ['text' => 'Hello {{1}}']]]),
]);

$this->post(route('message.template.send'), [
Expand All @@ -112,7 +114,7 @@ public function test_send_text_template_with_vars()
{
$wabaPhone = WabaPhone::factory()->create();
$template = Template::factory()->create([
'content' => json_encode(['body' => ['text' => 'Hello {{1}}']]),
'content' => json_encode(['components' => ['body' => ['text' => 'Hello {{1}}']]]),
]);
$messageId = 'wamid.'.$this->faker()->numberBetween(111, 450);

Expand Down Expand Up @@ -142,7 +144,9 @@ public function test_send_template_header_image()
{
$wabaPhone = WabaPhone::factory()->create();
$template = Template::factory()->create([
'content' => json_encode(['body' => ['text' => 'Hello this is a test'], 'header' => ['format' => 'image']]),
'content' => json_encode([
'components' => ['components' => ['body' => ['text' => 'Hello this is a test'], 'header' => ['format' => 'image']]],
]),
]);
$messageId = 'wamid.'.$this->faker()->numberBetween(111, 450);

Expand Down Expand Up @@ -174,7 +178,7 @@ public function test_send_template_header_image_missing_var()
{
$wabaPhone = WabaPhone::factory()->create();
$template = Template::factory()->create([
'content' => json_encode(['body' => ['text' => 'Hello this is a test'], 'header' => ['format' => 'image']]),
'content' => json_encode(['components' => ['body' => ['text' => 'Hello this is a test'], 'header' => ['format' => 'image']]]),
]);

$this->post(route('message.template.send'), [
Expand Down

0 comments on commit af34881

Please sign in to comment.