Skip to content

Commit

Permalink
Return vars from api response (#32)
Browse files Browse the repository at this point in the history
* Return vars from api response

* hotfix

* dates fix

---------

Co-authored-by: Tomasz Smolarek <tomasz.smolarek@escolasoft.com>
  • Loading branch information
dyfero and dyfero committed Jul 7, 2023
1 parent 7cbefbd commit 86c5aa3
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 10 deletions.
25 changes: 19 additions & 6 deletions src/Courses/CommonUserAndCourseVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,39 @@
use EscolaLms\Courses\Models\Course;
use EscolaLms\Templates\Events\EventWrapper;
use EscolaLms\TemplatesPdf\Core\PdfVariables;
use Illuminate\Support\Carbon;

abstract class CommonUserAndCourseVariables extends PdfVariables
{
const VAR_USER_NAME = '@VarUserName';
const VAR_COURSE_TITLE = '@VarCourseTitle';
const VAR_USER_NAME = '@VarUserName';
const VAR_COURSE_TITLE = '@VarCourseTitle';
const VAR_COURSE_SUBTITLE = '@VarCourseSubtitle';
const VAR_COURSE_ACTIVE_FROM = '@VarCourseActiveFrom';
const VAR_COURSE_ACTIVE_TO = '@VarCourseActiveTo';
const VAR_COURSE_CATEGORIES = '@VarCourseCategories';

public static function mockedVariables(?User $user = null): array
{
$faker = \Faker\Factory::create();
return array_merge(parent::mockedVariables(), [
self::VAR_USER_NAME => $faker->name(),
self::VAR_COURSE_TITLE => $faker->word(),
self::VAR_USER_NAME => $faker->name(),
self::VAR_COURSE_TITLE => $faker->word(),
self::VAR_COURSE_SUBTITLE => $faker->word(),
self::VAR_COURSE_ACTIVE_FROM => $faker->word(),
self::VAR_COURSE_ACTIVE_TO => $faker->dateTime()->format('Y-m-d'),
self::VAR_COURSE_CATEGORIES => $faker->dateTime()->format('Y-m-d'),
]);
}

public static function variablesFromEvent(EventWrapper $event): array
{
return array_merge(parent::variablesFromEvent($event), [
self::VAR_USER_NAME => $event->user()->name,
self::VAR_COURSE_TITLE => $event->getCourse()->title,
self::VAR_USER_NAME => $event->user()->name,
self::VAR_COURSE_TITLE => $event->getCourse()->title,
self::VAR_COURSE_SUBTITLE => $event->getCourse()->subtitle,
self::VAR_COURSE_ACTIVE_FROM => $event->getCourse()->active_from ? Carbon::make($event->getCourse()->active_from)->format('Y-m-d') : '',
self::VAR_COURSE_ACTIVE_TO => $event->getCourse()->active_to ? Carbon::make($event->getCourse()->active_to)->format('Y-m-d') : '',
self::VAR_COURSE_CATEGORIES => $event->getCourse()->categories->pluck('name')->implode(', '),
]);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Http/Resources/PdfListResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EscolaLms\TemplatesPdf\Http\Resources;

use EscolaLms\TemplatesPdf\Models\FabricPDF;
use EscolaLms\TemplatesPdf\Parsers\VarsParser;
use Illuminate\Http\Resources\Json\JsonResource;

class PdfListResource extends JsonResource
Expand All @@ -19,6 +20,7 @@ public function toArray($request)
'title' => $this->title,
'template' => $this->template,
'user_id' => $this->user_id,
'vars' => VarsParser::parseVars($this->vars),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
Expand Down
6 changes: 4 additions & 2 deletions src/Http/Resources/PdfResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EscolaLms\TemplatesPdf\Http\Resources;

use EscolaLms\TemplatesPdf\Models\FabricPDF;
use EscolaLms\TemplatesPdf\Parsers\VarsParser;
use Illuminate\Http\Resources\Json\JsonResource;
use EscolaLms\Auth\Models\User;

Expand All @@ -20,10 +21,11 @@ public function toArray($request)
'id' => $this->id,
'template' => $this->template,
'path' => $this->path,
'user_id' => $this->user_id,
'user' => User::find($this->user_id),
'user_id' => $this->user_id,
'user' => User::find($this->user_id),
'title' => $this->title,
'content' => $this->content ? json_decode($this->content) : null,
'vars' => VarsParser::parseVars($this->vars),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
Expand Down
16 changes: 16 additions & 0 deletions src/Parsers/VarsParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace EscolaLms\TemplatesPdf\Parsers;

use Illuminate\Support\Str;

class VarsParser
{
public static function parseVars(?array $vars = []): array
{
return collect($vars)
->filter(fn($var, $key) => !Str::contains($key, 'Global'))
->mapWithKeys(fn($var, $key) => [Str::snake(Str::replace("@", '', $key)) => $var])
->toArray();
}
}
7 changes: 6 additions & 1 deletion tests/Api/CoursesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace EscolaLms\TemplatesPdf\Tests\Api;

use EscolaLms\Categories\Models\Category;
use EscolaLms\Core\Models\User as CoreUser;
use EscolaLms\Core\Tests\ApiTestTrait;
use EscolaLms\Core\Tests\CreatesUsers;
Expand All @@ -24,6 +25,7 @@
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Log\Events\MessageLogged;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Notification;
Expand Down Expand Up @@ -56,7 +58,10 @@ public function testUserFinishedCourseNotification(): void
PdfCreated::class,
]);

$course = Course::factory()->create(['status' => CourseStatusEnum::PUBLISHED]);
$course = Course::factory()->create(['status' => CourseStatusEnum::PUBLISHED, 'active_from' => Carbon::now()]);
$course->categories()->attach(Category::factory()->create());
$course->categories()->attach(Category::factory()->create());

$lesson = Lesson::factory([
'course_id' => $course->getKey()
])->create();
Expand Down
36 changes: 35 additions & 1 deletion tests/Api/PdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testCanReadExisting(): void
{
$pdf = FabricPDF::factory()->createOne(
[
'user_id' => $this->user->id
'user_id' => $this->user->id,
]
);

Expand All @@ -41,6 +41,7 @@ public function testCanReadExisting(): void
'id',
'path',
'template',
'vars'
],
'message'
]);
Expand All @@ -51,6 +52,39 @@ public function testCanReadExisting(): void
$response->assertStatus(403);
}

public function testCanReadWithVars(): void
{
$pdf = FabricPDF::factory()->createOne(
[
'user_id' => $this->user->id,
'vars' => [
'@GlobalSettingsValue' => 'value_1',
'@VarExampleValue' => 'value_2'
]
]
);

$this->actingAs($this->user)
->getJson('/api/pdfs/' . $pdf->id)
->assertOk()
->assertJsonStructure([
'data' => [
'content',
'created_at',
'id',
'path',
'template',
'vars'
],
'message'
])
->assertJsonFragment([
'vars' => [
'var_example_value' => 'value_2'
]
]);
}

public function testAdminCanReadAny(): void
{
$pdf = FabricPDF::factory()->createOne(
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace EscolaLms\TemplatesPdf\Tests;

use EscolaLms\Categories\EscolaLmsCategoriesServiceProvider;
use EscolaLms\Core\Models\User;
use EscolaLms\TemplatesPdf\EscolaLmsTemplatesPdfServiceProvider;
use EscolaLms\Templates\Database\Seeders\PermissionTableSeeder as TemplatesPermissionTableSeeder;
Expand Down Expand Up @@ -32,6 +33,7 @@ protected function getPackageProviders($app): array
PermissionServiceProvider::class,
EscolaLmsCourseServiceProvider::class,
EscolaLmsTemplatesServiceProvider::class,
EscolaLmsCategoriesServiceProvider::class,
EscolaLmsTemplatesPdfServiceProvider::class,
];
}
Expand Down

0 comments on commit 86c5aa3

Please sign in to comment.