From 6a6781831b8ce3bebc1c8e5e67f85159b9f900e5 Mon Sep 17 00:00:00 2001 From: Alexis Gousseau Date: Mon, 28 Sep 2026 15:52:43 +0200 Subject: [PATCH] feat: :sparkles: set winning proofs optionnal --- .../Requests/Fo/SubmitWinningProofRequest.php | 72 ++++++++++--------- app/Mail/Won/WinningProofAdminMail.php | 26 +++---- app/Models/Participation.php | 6 +- app/Policies/ParticipationPolicy.php | 27 ++++--- config/proofs.php | 7 ++ 5 files changed, 81 insertions(+), 57 deletions(-) create mode 100644 config/proofs.php diff --git a/app/Http/Requests/Fo/SubmitWinningProofRequest.php b/app/Http/Requests/Fo/SubmitWinningProofRequest.php index d46b221..adf7d7a 100644 --- a/app/Http/Requests/Fo/SubmitWinningProofRequest.php +++ b/app/Http/Requests/Fo/SubmitWinningProofRequest.php @@ -33,42 +33,50 @@ class SubmitWinningProofRequest extends CreateAccountRequest $this->integer('country') : ($authUser ? (new CrmRepository())->fetchCrmCustomer($authUser)->country->value : 0); $countryRules = match ($countryCode) { - Country::belgium->value => [ - 'phone' => 'phone:BE', - ], - Country::luxembourg->value => [ - 'phone' => 'phone:LU', - ], - Country::germany->value => [ - 'phone' => 'phone:DE', - ], - default => [ - 'phone' => 'phone:FR', - ] + Country::belgium->value => ['phone' => 'phone:BE'], + Country::luxembourg->value => ['phone' => 'phone:LU'], + Country::germany->value => ['phone' => 'phone:DE'], + default => ['phone' => 'phone:FR'], }; - return \array_merge(Arr::only( - parent::rules(), + return \array_merge( + Arr::only( + parent::rules(), + [ + 'lastname', + 'firstname', + 'address', + 'address2', + 'postal_code', + 'city', + 'country', + ] + ), [ - 'lastname', - 'firstname', - 'address', - 'address2', - 'postal_code', - 'city', - 'country' - ] - ), [ - 'phone' => [ - 'required', - 'max:255', - $countryRules['phone'], + 'phone' => [ + 'required', + 'max:255', + $countryRules['phone'], + ], ], - // * Min 10 KB, Max 2MB - 'sticker' => 'required|mimes:jpeg,jpg,png,pdf|min:10|max:2048', - 'ticket' => 'required|mimes:jpeg,jpg,png,pdf|min:10|max:2048', - 'barcode' => 'required|mimes:jpeg,jpg,png,pdf|min:10|max:2048', - ]); + $this->proofRules() + ); + } + + /** + * Rules of winning proof. + * + * @return array + */ + private function proofRules(): array + { + // * Min 10 KB, Max 2MB + $fileRules = 'required|mimes:jpeg,jpg,png,pdf|min:10|max:2048'; + + return collect(['sticker', 'ticket', 'barcode']) + ->filter(fn (string $proof) => config("proofs.$proof")) + ->mapWithKeys(fn (string $proof) => [$proof => $fileRules]) + ->all(); } /** diff --git a/app/Mail/Won/WinningProofAdminMail.php b/app/Mail/Won/WinningProofAdminMail.php index 548ffb6..af83193 100644 --- a/app/Mail/Won/WinningProofAdminMail.php +++ b/app/Mail/Won/WinningProofAdminMail.php @@ -28,10 +28,10 @@ class WinningProofAdminMail extends Mailable implements ShouldQueue * @param \LDC\LDCCRM\Models\Customer $customer The customer who won. * @param string|null $unique_code The unique code used to participate if any. * @param string|null $ean_code The product code used to participate if any. - * @param string $sticker Fichier image du sticker utilisé. - * @param string $ticket Fichier image du ticket de caisse prouvant + * @param string|null $sticker Fichier image du sticker utilisé. + * @param string|null $ticket Fichier image du ticket de caisse prouvant * l'achat du produit stické. - * @param string $barcode Fichier image du code-barres du produit acheté. + * @param string|null $barcode Fichier image du code-barres du produit acheté. * * @return void */ @@ -42,9 +42,9 @@ class WinningProofAdminMail extends Mailable implements ShouldQueue protected Customer $customer, protected ?string $unique_code, protected ?string $ean_code, - protected string $sticker, - protected string $ticket, - protected string $barcode, + protected ?string $sticker, + protected ?string $ticket, + protected ?string $barcode, ) { } @@ -71,7 +71,7 @@ class WinningProofAdminMail extends Mailable implements ShouldQueue public function content(): \Illuminate\Mail\Mailables\Content { $participation = []; - if (!is_null($this->ean_code)) { + if (!is_null($this->unique_code)) { $participation = [ trans('Code Unique : :code', ['code' => $this->unique_code]), ]; @@ -106,10 +106,12 @@ class WinningProofAdminMail extends Mailable implements ShouldQueue */ public function attachments(): array { - return [ - Attachment::fromPath(FileStorageHelper::getFileFullPath($this->sticker)), - Attachment::fromPath(FileStorageHelper::getFileFullPath($this->ticket)), - Attachment::fromPath(FileStorageHelper::getFileFullPath($this->barcode)), - ]; + return collect([$this->sticker, $this->ticket, $this->barcode]) + ->filter() + ->map(fn (string $file) => Attachment::fromPath( + FileStorageHelper::getFileFullPath($file) + )) + ->values() + ->all(); } } diff --git a/app/Models/Participation.php b/app/Models/Participation.php index 43bc9fb..82aba4c 100644 --- a/app/Models/Participation.php +++ b/app/Models/Participation.php @@ -17,9 +17,9 @@ use IPLib\Address\AddressInterface; * Participation. * * @property \IPLib\Address\AddressInterface $ip IP - * @property string $sticker Path of the sticker file - * @property string $ticket Path of the ticket file - * @property string $barcode Path of the barcode file + * @property string|null $sticker Path of the sticker file + * @property string|null $ticket Path of the ticket file + * @property string|null $barcode Path of the barcode file * @property string|null $discount_code Treatment code of the BR * @property string $comment Administrators comments * @property boolean $prize_sent Has the won prize been sent ? diff --git a/app/Policies/ParticipationPolicy.php b/app/Policies/ParticipationPolicy.php index 7c23104..f7f95db 100644 --- a/app/Policies/ParticipationPolicy.php +++ b/app/Policies/ParticipationPolicy.php @@ -90,19 +90,26 @@ class ParticipationPolicy { /** @var \App\Models\Settings */ $settings = app(Settings::class); - return $settings->winning_proof and Participation::query() + + // Columns of the winning proofs enabled in the config. + $enabledProofs = array_keys(array_filter(config('proofs'))); + + // Winning proofs are disabled globally or no proof is enabled in the config. + if (! $settings->winning_proof || empty($enabledProofs)) { + return false; + } + + return Participation::query() ->where('prize_sent', false) ->whereHas( 'customer', - fn (Builder $q) => - $q->where('id', $authUser->id) - )->where('id', $model->id)->whereHas('instantWin') - ->where(function (Builder $q) { - $q->whereNull( - ['sticker', 'ticket', 'barcode'], - 'or' - ); - })->exists(); + fn (Builder $q) => $q->where('id', $authUser->id) + ) + ->where('id', $model->id) + ->whereHas('instantWin') + // At least one enabled proof has not been submitted yet. + ->where(fn (Builder $q) => $q->whereNull($enabledProofs, 'or')) + ->exists(); } /** diff --git a/config/proofs.php b/config/proofs.php new file mode 100644 index 0000000..b8a49d5 --- /dev/null +++ b/config/proofs.php @@ -0,0 +1,7 @@ + filter_var(env('PROOF_STICKER', true), FILTER_VALIDATE_BOOLEAN), + 'ticket' => filter_var(env('PROOF_TICKET', true), FILTER_VALIDATE_BOOLEAN), + 'barcode' => filter_var(env('PROOF_BARCODE', true), FILTER_VALIDATE_BOOLEAN), +];