Skip to content

Commit

Permalink
Merge pull request #18 from anshour/main
Browse files Browse the repository at this point in the history
Perubahan state recap donor
  • Loading branch information
nuradiyana committed Feb 5, 2024
2 parents 9981415 + 9fbcb03 commit 6350dbe
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 34 deletions.
1 change: 1 addition & 0 deletions database/migrations/create_donation_donor_recap_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function up(): void
$table->string('donor_phone_number', 100)->nullable();
$table->string('donor_tax_number', 100)->nullable();
$table->text('donor_address')->nullable();
$table->string('state', 100)->nullable();
$table->string('disk', 20)->nullable();
$table->string('file_path')->nullable();
$table->string('result_disk')->nullable();
Expand Down
8 changes: 5 additions & 3 deletions src/Actions/AttachDonorDonationRecap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Facades\DB;
use Inisiatif\DonationRecap\Models\DonationRecap;
use Inisiatif\DonationRecap\Resolvers\DonorResolver;
use Inisiatif\DonationRecap\Enums\DonationRecapState;
use Inisiatif\DonationRecap\Models\DonationRecapDonor;
use Inisiatif\DonationRecap\Exceptions\CannotAttachDonor;
use Inisiatif\DonationRecap\Supports\RecapDonorAttachValidation;
Expand All @@ -29,9 +30,10 @@ public function handle(DonationRecap $recap, string $donorId): void
$donorData = $this->donorResolver->resolve($donorId);

/** @var DonationRecapDonor $donor */
$donor = $recap->donors()->create(
$donorData->toArray()
);
$donor = $recap->donors()->create([
...$donorData->toArray(),
'state' => DonationRecapState::new,
]);

$donor->recap()->increment('count_total');
});
Expand Down
2 changes: 1 addition & 1 deletion src/Actions/FetchDonorForRecapPagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function handle(DonationRecap $recap, Request $request): LengthAwarePagin
});
})->whereDoesntHave('recaps', function (Builder $builder) use ($recap): Builder {
return $builder->select('donation_recaps.id')
->whereNot('state', DonationRecapState::failure)
->whereNot('donation_recaps.state', DonationRecapState::failure)
->whereDate('end_at', $recap->getAttribute('end_at'))
->whereDate('start_at', $recap->getAttribute('start_at'))
->where('template_id', $recap->getAttribute('template_id'));
Expand Down
8 changes: 5 additions & 3 deletions src/Actions/NewOneDonorRecap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Support\Facades\DB;
use Inisiatif\DonationRecap\Models\DonationRecap;
use Inisiatif\DonationRecap\Enums\ProcessingState;
use Inisiatif\DonationRecap\Enums\DonationRecapState;
use Inisiatif\DonationRecap\Jobs\ProcessDonationRecap;
use Inisiatif\DonationRecap\DataTransfers\NewOneDonorRecapData;
Expand All @@ -23,9 +24,10 @@ public function handle(NewOneDonorRecapData $data): DonationRecap
'state' => DonationRecapState::new,
]);

$donationRecap->donors()->create(
$data->except('templateId', 'startAt', 'endAt')->toArray(),
);
$donationRecap->donors()->create([
...$data->except('templateId', 'startAt', 'endAt')->toArray(),
'state' => ProcessingState::new,
]);

return $donationRecap;
});
Expand Down
16 changes: 3 additions & 13 deletions src/Enums/DonationRecapState.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,8 @@
enum DonationRecapState: string
{
case new = 'new';

case processing = 'processing';
case processed = 'processed';
case done = 'done';
case failure = 'failure';

case collecting = 'collecting';

case collected = 'collected';

case generating = 'generating';

case generated = 'generated';

case combining = 'combining';

case combined = 'combined';
}
22 changes: 22 additions & 0 deletions src/Enums/ProcessingState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Inisiatif\DonationRecap\Enums;

enum ProcessingState: string
{
case new = 'new';
case collecting = 'collecting';

case collected = 'collected';

case generating = 'generating';

case generated = 'generated';

case combining = 'combining';

case combined = 'combined';

}
2 changes: 1 addition & 1 deletion src/Http/Controllers/SendDonationRecapController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class SendDonationRecapController
{
public function store(DonationRecap $recap): JsonResponse
{
\abort_unless($recap->inState(DonationRecapState::combined), 400, 'Donation recap is not in combined state.');
\abort_unless($recap->inState(DonationRecapState::done), 404);

\dispatch(new SendingDonationRecapJob($recap));

Expand Down
3 changes: 3 additions & 0 deletions src/Http/Controllers/SendRecapPerDonorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Http\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Inisiatif\DonationRecap\Actions\SendRecapPerDonor;
use Inisiatif\DonationRecap\Enums\ProcessingState;
use Inisiatif\DonationRecap\Models\DonationRecapDonor;
use Inisiatif\DonationRecap\Exceptions\CannotSendRecap;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Expand All @@ -16,6 +17,8 @@ final class SendRecapPerDonorController
public function store(DonationRecapDonor $donor, SendRecapPerDonor $sendRecapPerDonor): JsonResponse
{
try {
\abort_unless($donor->inState(ProcessingState::combined), 404);

$sendRecapPerDonor->handle($donor);

return new JsonResponse(null, Response::HTTP_NO_CONTENT);
Expand Down
7 changes: 4 additions & 3 deletions src/Jobs/BuildDonationRecapDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Inisiatif\DonationRecap\Enums\DonationRecapState;
use Inisiatif\DonationRecap\Models\DonationRecapDonor;
use Inisiatif\DonationRecap\Builders\DonationRecapDetailBuilder;
use Inisiatif\DonationRecap\Enums\ProcessingState;

final class BuildDonationRecapDetail implements ShouldBeUnique, ShouldQueue
{
Expand All @@ -30,11 +31,11 @@ public function __construct(
public function handle(DonationRecapDetailBuilder $builder): void
{
if ($this->donationRecap->inState(DonationRecapState::new)) {
$this->donationRecap->state(DonationRecapState::collecting);
$this->donor->state(ProcessingState::collecting);

$builder->buildFor($this->donationRecap, $this->donor);

$this->donationRecap->state(DonationRecapState::collected);
$this->donor->state(ProcessingState::collected);

$this->donationRecap->recordHistory(
\sprintf('Mengambil detail donasi untuk %s', $this->donor->getAttribute('donor_name')),
Expand All @@ -45,7 +46,7 @@ public function handle(DonationRecapDetailBuilder $builder): void

public function uniqueId(): string
{
return $this->donationRecap->getKey().'|'.$this->donor->getKey();
return $this->donationRecap->getKey() . '|' . $this->donor->getKey();
}

public function failed(Throwable $exception): void
Expand Down
12 changes: 7 additions & 5 deletions src/Jobs/CombineDonorRecapFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Inisiatif\DonationRecap\Models\DonationRecap;
use Inisiatif\DonationRecap\DonationRecap as Recap;
use Inisiatif\DonationRecap\Enums\DonationRecapState;
use Inisiatif\DonationRecap\Enums\ProcessingState;
use Inisiatif\DonationRecap\Models\DonationRecapDonor;
use Inisiatif\DonationRecap\Models\DonationRecapTemplate;

Expand All @@ -33,12 +34,13 @@ public function __construct(

public function handle(): void
{
if ($this->donationRecap->inState(DonationRecapState::generated)) {
if ($this->donationRecap->inState(DonationRecapState::new)) {
$this->donationRecap->recordHistory(
\sprintf('Menggabungkan file rekap dengan template untuk %s', $this->donor->getAttribute('donor_name')),
$this->donor->getAttribute('donor_id'));
$this->donor->getAttribute('donor_id')
);

$this->donationRecap->state(DonationRecapState::combining);
$this->donor->state(ProcessingState::combining);

/** @var DonationRecapTemplate $template */
$template = $this->donationRecap->template()->first();
Expand All @@ -58,13 +60,13 @@ public function handle(): void
'result_file_path' => $path,
]);

$this->donationRecap->state(DonationRecapState::combined);
$this->donor->state(ProcessingState::combined);
}
}

public function uniqueId(): string
{
return $this->donationRecap->getKey().'|'.$this->donor->getKey();
return $this->donationRecap->getKey() . '|' . $this->donor->getKey();
}

public function failed(Throwable $exception): void
Expand Down
9 changes: 5 additions & 4 deletions src/Jobs/GenerateDonorRecapFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Inisiatif\DonationRecap\Models\DonationRecap;
use Inisiatif\DonationRecap\DonationRecap as Recap;
use Inisiatif\DonationRecap\Enums\DonationRecapState;
use Inisiatif\DonationRecap\Enums\ProcessingState;
use Inisiatif\DonationRecap\Models\DonationRecapDonor;

final class GenerateDonorRecapFile implements ShouldBeUnique, ShouldQueue
Expand All @@ -33,13 +34,13 @@ public function __construct(

public function handle(): void
{
if ($this->donationRecap->inState(DonationRecapState::collected)) {
if ($this->donationRecap->inState(DonationRecapState::new)) {
$this->donationRecap->recordHistory(
\sprintf('Membuat file rekap donasi untuk %s', $this->donor->getAttribute('donor_name')),
$this->donor->getAttribute('donor_id')
);

$this->donationRecap->state(DonationRecapState::generating);
$this->donor->state(ProcessingState::generating);

$path = \sprintf('%s/%s/%s.pdf', Recap::getFileGeneratedBasePath(), now()->year, Str::random(64));

Expand All @@ -61,13 +62,13 @@ public function handle(): void
'file_path' => $path,
]);

$this->donationRecap->state(DonationRecapState::generated);
$this->donor->state(ProcessingState::generated);
}
}

public function uniqueId(): string
{
return $this->donationRecap->getKey().'|'.$this->donor->getKey();
return $this->donationRecap->getKey() . '|' . $this->donor->getKey();
}

public function failed(Throwable $exception): void
Expand Down
4 changes: 4 additions & 0 deletions src/Jobs/IncreaseProgressDonationRecap.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public function __construct(

public function handle(): void
{
if ($this->donationRecap->isLastRecordProcessed()) {
$this->donationRecap->state(DonationRecapState::done);
}

$this->donationRecap->recordHistory('Update progress pembuatan rekap donasi');

DonationRecap::query()
Expand Down
5 changes: 5 additions & 0 deletions src/Models/DonationRecap.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public function inState(DonationRecapState $new): bool
return $state->value === $new->value;
}

public function isLastRecordProcessed(): bool
{
return $this->getAttribute('count_total') === ($this->getAttribute('count_progress') + 1);
}

public function getItemCollection(): Collection
{
return $this->items()->oldest('donation_transaction_date')->get();
Expand Down
21 changes: 20 additions & 1 deletion src/Models/DonationRecapDonor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use FromHome\Kutt\Input\CreateShortLinkInput;
use Inisiatif\DonationRecap\Enums\ProcessingState;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Inisiatif\DonationRecap\DonationRecap as Recap;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand All @@ -18,6 +19,10 @@ final class DonationRecapDonor extends Model

protected $guarded = [];

protected $casts = [
'state' => ProcessingState::class,
];

protected $appends = [
'file_url',
'result_file_url',
Expand All @@ -33,6 +38,20 @@ public function donor(): BelongsTo
return $this->belongsTo(Recap::getDonorClassModel());
}

public function inState(ProcessingState $new): bool
{
$state = $this->getAttribute('state');

return $state->value === $new->value;
}

public function state(ProcessingState $state): self
{
$this->update(['state' => $state]);

return $this;
}

/**
* @return resource|null
*/
Expand Down Expand Up @@ -92,7 +111,7 @@ protected function generateFileUrl(string $path): string
$baseUrl = Recap::getDefaultFileUrl();

if ($baseUrl) {
return $baseUrl.'/'.$path;
return $baseUrl . '/' . $path;
}

$disk = Storage::disk($this->getAttribute('disk'));
Expand Down

0 comments on commit 6350dbe

Please sign in to comment.