From 39a3e62d07a12e1536fcc712531145722b29ec00 Mon Sep 17 00:00:00 2001 From: Kristaps Berzinch Date: Sun, 28 Jul 2024 16:32:11 -0400 Subject: [PATCH] Provide merch size in distribution API --- app/Http/Controllers/MerchandiseController.php | 3 ++- .../Resources/DuesTransactionMerchandise.php | 4 ++++ app/Models/DuesTransactionMerchandise.php | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/MerchandiseController.php b/app/Http/Controllers/MerchandiseController.php index bf0b318b6..f0e6043d0 100644 --- a/app/Http/Controllers/MerchandiseController.php +++ b/app/Http/Controllers/MerchandiseController.php @@ -146,7 +146,8 @@ private static function getDuesTransactionMerchandise( 'dues_transaction_merchandise.provided_at', 'dues_transaction_merchandise.provided_by', 'dues_transaction_merchandise.provided_via', - 'dues_transaction_merchandise.merchandise_id' + 'dues_transaction_merchandise.merchandise_id', + 'dues_transaction_merchandise.dues_transaction_id' ) ->whereHas('transaction', static function (Builder $query) use ($user) { $query->where('user_id', $user->id) diff --git a/app/Http/Resources/DuesTransactionMerchandise.php b/app/Http/Resources/DuesTransactionMerchandise.php index 0d2b03b31..74cbff730 100644 --- a/app/Http/Resources/DuesTransactionMerchandise.php +++ b/app/Http/Resources/DuesTransactionMerchandise.php @@ -29,6 +29,10 @@ public function toArray(Request $request): array 'provided_at' => $this->provided_at, 'provided_by' => Manager::make($this->providedBy), 'provided_via' => $this->provided_via, + 'size' => [ + 'short' => $this->size, + 'display_name' => $this->size === null ? null : \App\Models\User::$shirt_sizes[$this->size], + ], 'merchandise' => $this->when($this->withMerchandise, $this->merchandise), ]; } diff --git a/app/Models/DuesTransactionMerchandise.php b/app/Models/DuesTransactionMerchandise.php index 96dd4bb58..3fd9cba53 100644 --- a/app/Models/DuesTransactionMerchandise.php +++ b/app/Models/DuesTransactionMerchandise.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\Pivot; +use Illuminate\Support\Str; /** * App\Models\DuesTransactionMerchandise. @@ -19,6 +20,7 @@ * @property \Illuminate\Support\Carbon|null $created_at * @property \Illuminate\Support\Carbon|null $updated_at * @property-read \App\Models\User|null $providedBy + * @property-read ?string $size * * @method static \Illuminate\Database\Eloquent\Builder|DuesTransactionMerchandise newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|DuesTransactionMerchandise newQuery() @@ -80,4 +82,18 @@ public function merchandise(): BelongsTo { return $this->belongsTo(Merchandise::class); } + + /** + * Get the size for this DuesTransactionMerchandise based on data about the merchandise item and user. + */ + public function getSizeAttribute(): ?string + { + if (Str::contains(Str::lower($this->merchandise->name), 'shirt')) { + return $this->transaction->user->shirt_size; + } elseif (Str::contains(Str::lower($this->merchandise->name), 'polo')) { + return $this->transaction->user->polo_size; + } else { + return null; + } + } }