Skip to content

Commit bba404f

Browse files
cybersoldattechmckenzieartsChri$
authored
Feature/lar 8 notification soumission darticle (#148)
* feat:LAR-8 create article process with action and add features test --------- Co-authored-by: Arthur Monney <[email protected]> Co-authored-by: cybersoldattech <[email protected]> Co-authored-by: Chri$ <[email protected]>
1 parent df41c36 commit bba404f

File tree

20 files changed

+785
-668
lines changed

20 files changed

+785
-668
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Actions\Article;
6+
7+
use App\Data\Article\CreateArticleData;
8+
use App\Gamify\Points\ArticleCreated;
9+
use App\Models\Article;
10+
use App\Notifications\PostArticleToTelegram;
11+
use Carbon\Carbon;
12+
use DateTimeInterface;
13+
use Illuminate\Support\Facades\Auth;
14+
15+
final class CreateArticleAction
16+
{
17+
public function execute(CreateArticleData $articleData): Article
18+
{
19+
if ($articleData->publishedAt && ! ($articleData->publishedAt instanceof DateTimeInterface)) {
20+
$articleData->publishedAt = new Carbon(
21+
time: $articleData->publishedAt,
22+
tz: config('app.timezone')
23+
);
24+
}
25+
26+
/** @var Article $article */
27+
$article = Article::query()->create([
28+
'title' => $articleData->title,
29+
'slug' => $articleData->title,
30+
'body' => $articleData->body,
31+
'published_at' => $articleData->publishedAt,
32+
'submitted_at' => $articleData->submittedAt,
33+
'approved_at' => $articleData->approvedAt,
34+
'show_toc' => $articleData->showToc,
35+
'canonical_url' => $articleData->canonicalUrl,
36+
'user_id' => Auth::id(),
37+
]);
38+
39+
if (collect($articleData->tags)->isNotEmpty()) {
40+
$article->syncTags(tags: $articleData->tags);
41+
}
42+
43+
if ($articleData->file) {
44+
$article->addMedia($articleData->file->getRealPath())->toMediaCollection('media');
45+
}
46+
47+
if ($article->isAwaitingApproval()) {
48+
// Envoi de la notification sur le channel Telegram pour la validation de l'article.
49+
Auth::user()?->notify(new PostArticleToTelegram($article));
50+
session()->flash('status', __('notifications.article.created'));
51+
}
52+
53+
if (Auth::user()?->hasAnyRole(['admin', 'moderator'])) {
54+
givePoint(new ArticleCreated($article));
55+
}
56+
57+
return $article;
58+
}
59+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Data\Article;
6+
7+
use Carbon\Carbon;
8+
use Illuminate\Http\UploadedFile;
9+
use Spatie\LaravelData\Data;
10+
11+
final class CreateArticleData extends Data
12+
{
13+
public function __construct(
14+
public string $title,
15+
public string $slug,
16+
public string $body,
17+
public string $showToc,
18+
public string $canonicalUrl,
19+
public ?Carbon $publishedAt,
20+
public ?Carbon $submittedAt,
21+
public ?Carbon $approvedAt,
22+
public ?UploadedFile $file,
23+
public array $tags = [],
24+
) {}
25+
}

app/Gamify/Points/ArticleCreated.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Gamify\Points;
6+
7+
use App\Models\Article;
8+
use App\Models\User;
9+
use QCod\Gamify\PointType;
10+
11+
final class ArticleCreated extends PointType
12+
{
13+
public int $points = 50;
14+
15+
public function __construct(Article $subject)
16+
{
17+
$this->subject = $subject;
18+
}
19+
20+
public function payee(): User
21+
{
22+
// @phpstan-ignore-next-line
23+
return $this->getSubject()->user;
24+
}
25+
}

app/Livewire/Articles/Create.php

+9-42
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44

55
namespace App\Livewire\Articles;
66

7-
use App\Events\ArticleWasSubmittedForApproval;
8-
use App\Gamify\Points\PostCreated;
9-
use App\Models\Article;
7+
use App\Actions\Article\CreateArticleAction;
8+
use App\Data\Article\CreateArticleData;
109
use App\Models\Tag;
1110
use App\Models\User;
1211
use App\Traits\WithArticleAttributes;
1312
use App\Traits\WithTagsAssociation;
14-
use Carbon\Carbon;
15-
use DateTimeInterface;
1613
use Illuminate\Contracts\View\View;
1714
use Illuminate\Support\Facades\Auth;
1815
use Livewire\Component;
@@ -52,46 +49,16 @@ public function store(): void
5249
/** @var User $user */
5350
$user = Auth::user();
5451

55-
if ($this->published_at && ! ($this->published_at instanceof DateTimeInterface)) {
56-
$this->published_at = new Carbon(
57-
time: $this->published_at,
58-
tz: config('app.timezone')
59-
);
60-
}
61-
62-
/** @var Article $article */
63-
$article = Article::create([
52+
$article = app(CreateArticleAction::class)->execute(CreateArticleData::from([
6453
'title' => $this->title,
6554
'slug' => $this->slug,
6655
'body' => $this->body,
67-
'published_at' => $this->published_at,
68-
'submitted_at' => $this->submitted_at,
69-
'approved_at' => $this->approved_at,
70-
'show_toc' => $this->show_toc,
71-
'canonical_url' => $this->canonical_url,
72-
'user_id' => $user->id,
73-
]);
74-
75-
if (collect($this->associateTags)->isNotEmpty()) {
76-
$article->syncTags(tags: $this->associateTags);
77-
}
78-
79-
if ($this->file) {
80-
$article->addMedia($this->file->getRealPath())->toMediaCollection('media');
81-
}
82-
83-
if ($article->isAwaitingApproval()) {
84-
if (app()->environment('production')) {
85-
// Envoi de la notification sur le channel Telegram pour la validation de l'article.
86-
event(new ArticleWasSubmittedForApproval($article));
87-
}
88-
89-
session()->flash('status', __('Merci d\'avoir soumis votre article. Vous aurez des nouvelles que lorsque nous accepterons votre article.'));
90-
}
91-
92-
if ($user->hasAnyRole(['admin', 'moderator'])) {
93-
givePoint(new PostCreated($article));
94-
}
56+
'publishedAt' => $this->published_at,
57+
'submittedAt' => $this->submitted_at,
58+
'approvedAt' => $this->approved_at,
59+
'showToc' => $this->show_toc,
60+
'canonicalUrl' => $this->canonical_url,
61+
]));
9562

9663
$user->hasRole('user') ?
9764
$this->redirectRoute('dashboard') :

app/Traits/WithArticleAttributes.php

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ trait WithArticleAttributes
3939
'tags_selected' => 'nullable|array',
4040
'canonical_url' => 'nullable|url',
4141
'file' => 'nullable|image|max:2048', // 1MB Max
42+
'show_toc' => 'boolean',
4243
];
4344

4445
public function removeImage(): void

0 commit comments

Comments
 (0)