Skip to content

Commit c70bf09

Browse files
committed
wip
1 parent cf2b39d commit c70bf09

File tree

8 files changed

+33
-49
lines changed

8 files changed

+33
-49
lines changed

app/Http/Controllers/Articles/ArticlesController.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,13 @@ public function store(ArticleRequest $request)
115115

116116
$article = Article::findByUuidOrFail($uuid);
117117

118-
$this->maybeFlashSuccessMessage($article, $request);
118+
if ($article->isNotApproved()) {
119+
$this->success(
120+
$request->shouldBeSubmitted()
121+
? 'Thank you for submitting, unfortunately we can\'t accept every submission. You\'ll only hear back from us when we accept your article.'
122+
: 'Article successfully created!'
123+
);
124+
}
119125

120126
return $request->wantsJson()
121127
? ArticleResource::make($article)
@@ -172,15 +178,4 @@ public function delete(Request $request, Article $article)
172178
? response()->json([], Response::HTTP_NO_CONTENT)
173179
: redirect()->route('articles');
174180
}
175-
176-
private function maybeFlashSuccessMessage(Article $article, ArticleRequest $request): void
177-
{
178-
if ($article->isNotApproved()) {
179-
$this->success(
180-
$request->shouldBeSubmitted()
181-
? 'Thank you for submitting, unfortunately we can\'t accept every submission. You\'ll only hear back from us when we accept your article.'
182-
: 'Article successfully created!'
183-
);
184-
}
185-
}
186181
}

app/Jobs/UnVerifyAuthor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function __construct(private User $user)
2323
*/
2424
public function handle(): void
2525
{
26-
$this->user->unverifyAuthor();
26+
$this->user->author_verified_at = null;
27+
$this->user->save();
2728
}
2829
}

app/Jobs/VerifyAuthor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function __construct(private User $user)
2323
*/
2424
public function handle(): void
2525
{
26-
$this->user->verifyAuthor();
26+
$this->user->author_verified_at = now();
27+
$this->user->save();
2728
}
2829
}

app/Models/User.php

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -307,25 +307,7 @@ public function delete()
307307

308308
public function isVerifiedAuthor(): bool
309309
{
310-
return ! is_null($this->author_verified_at);
311-
}
312-
313-
public function isNotVerifiedAuthor(): bool
314-
{
315-
return !$this->isVerifiedAuthor();
316-
}
317-
318-
public function verifyAuthor(): void
319-
{
320-
$this->author_verified_at = now();
321-
$this->save();
322-
}
323-
324-
325-
public function unverifyAuthor(): void
326-
{
327-
$this->author_verified_at = null;
328-
$this->save();
310+
return ! is_null($this->author_verified_at) || $this->isAdmin();
329311
}
330312

331313
/**
@@ -338,14 +320,19 @@ public function unverifyAuthor(): void
338320
*/
339321
public function verifiedAuthorCanPublishMoreToday(): bool
340322
{
341-
$limit = 2; // Default limit for verified authors
342-
if ($this->isNotVerifiedAuthor()) {
323+
if ($this->isAdmin()) {
324+
return true;
325+
}
326+
327+
if (! $this->isVerifiedAuthor()) {
343328
return false;
344329
}
330+
345331
$publishedTodayCount = $this->articles()
346332
->whereDate('submitted_at', today())
347333
->where('submitted_at', '>', $this->author_verified_at)->count(); // to ensure we only count articles published after verify the author
348-
return $publishedTodayCount < $limit;
334+
335+
return $publishedTodayCount < 2;
349336
}
350337

351338
public function countSolutions(): int

database/factories/UserFactory.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,11 @@ public function moderator(): self
5151
return ['type' => User::MODERATOR];
5252
});
5353
}
54+
55+
public function verifiedAuthor(): self
56+
{
57+
return $this->state(function () {
58+
return ['author_verified_at' => now()];
59+
});
60+
}
5461
}

database/seeders/UserSeeder.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public function run(): void
2020
'github_username' => 'driesvints',
2121
'password' => bcrypt('password'),
2222
'type' => User::ADMIN,
23-
'author_verified_at' => now(),
2423
]);
2524

2625
User::factory()->createQuietly([

tests/CreatesUsers.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests;
44

55
use App\Models\User;
6+
use Database\Factories\UserFactory;
67

78
trait CreatesUsers
89
{
@@ -30,22 +31,14 @@ protected function loginAsAdmin(array $attributes = []): User
3031
return $this->login(array_merge($attributes, ['type' => User::ADMIN]));
3132
}
3233

33-
protected function createUser(array $attributes = []): User
34+
protected function createUser(array $attributes = [], ?UserFactory $userFactory = null): User
3435
{
35-
return User::factory()->create(array_merge([
36+
return ($userFactory ?? User::factory())->create(array_merge([
3637
'name' => 'John Doe',
3738
'username' => 'johndoe',
3839
'email' => '[email protected]',
3940
'password' => bcrypt('password'),
4041
'github_username' => 'johndoe',
4142
], $attributes));
4243
}
43-
44-
protected function createVerifiedAuthor(array $attributes = []): User
45-
{
46-
return $this->createUser(array_merge($attributes, [
47-
'author_verified_at' => now(),
48-
]));
49-
}
50-
5144
}

tests/Feature/ArticleTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use App\Jobs\SyncArticleImage;
55
use App\Models\Article;
66
use App\Models\Tag;
7+
use App\Models\User;
78
use App\Notifications\ArticleApprovedNotification;
89
use App\Notifications\ArticleSubmitted;
910
use Illuminate\Foundation\Testing\DatabaseMigrations;
@@ -586,7 +587,7 @@
586587
});
587588

588589
test('verified authors can publish two articles per day with no approval needed', function () {
589-
$author = $this->createVerifiedAuthor();
590+
$author = $this->createUser(userFactory: User::factory()->verifiedAuthor());
590591

591592
Article::factory()->count(2)->create([
592593
'author_id' => $author->id,
@@ -599,7 +600,7 @@
599600
test('verified authors skip the approval message when submitting new article', function () {
600601
Bus::fake(SyncArticleImage::class);
601602

602-
$author = $this->createVerifiedAuthor();
603+
$author = $this->createUser(userFactory: User::factory()->verifiedAuthor());
603604
$this->loginAs($author);
604605

605606
$response = $this->post('/articles', [

0 commit comments

Comments
 (0)