Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ok200lyndon committed Sep 17, 2024
1 parent 2969a1e commit a497da6
Show file tree
Hide file tree
Showing 8 changed files with 464 additions and 34 deletions.
12 changes: 11 additions & 1 deletion app/Console/Commands/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Console\Commands;

use App\Models\Team;
use App\Models\TeamServiceTeam;
use App\Models\User;
use Illuminate\Console\Command;

Expand All @@ -26,8 +28,16 @@ class TestCommand extends Command
*/
public function handle()
{
$teams = Team::factory(8)->createQuietly();

$me = User::find(3);
foreach ($teams as $team) {

TeamServiceTeam::factory()->createQuietly(
[
'service_team_id' => $team->id,
'team_id' => 1
]
);
}
}
}
14 changes: 14 additions & 0 deletions app/Enums/VoucherSetType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Enums;

enum VoucherSetType: string
{
case FOOD_EQUITY = 'food equity';
case PROMOTION = 'promotion';

public static function values(): array
{
return array_column(self::cases(), 'value');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use App\Models\VoucherSet;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Knuckles\Scribe\Attributes\Authenticated;
use Knuckles\Scribe\Attributes\Endpoint;
use Knuckles\Scribe\Attributes\Group;
Expand Down Expand Up @@ -124,8 +126,82 @@ public function index(): JsonResponse
*/
public function store(): JsonResponse
{
$this->responseCode = 403;
$this->message = ApiResponse::RESPONSE_METHOD_NOT_ALLOWED->value;
$validationArray = [
'is_test' => [
'required',
'boolean',
],
'allocated_to_service_team_id' => [
'required',
Rule::exists('teams', 'id'),
],
'merchant_team_ids' => [
'required',
'array',
],
'merchant_team_ids.*' => [
'integer',
Rule::exists('teams', 'id'),
],
'funded_by_team_id' => [
'sometimes',
],
'total_set_value' => [
'required',
],
'denominations' => [
'required',
'array',
],
'expires_at' => [
'required',
'string',
],
'voucher_set_type' => [
'required',
'string',
],
];

$validator = Validator::make($this->request->all(), $validationArray);

if ($validator->fails()) {

$this->responseCode = 400;
$this->message = $validator->errors()
->first();

return $this->respond();

}

try {

$model = new VoucherSet();

foreach ($validationArray as $key => $validationRule) {
$value = $this->request->get($key);
if (isset($value)) {
if ($key == 'denominations') {
$value = json_encode($value);
}
$model->$key = $value;
}
}

$model->save();

$this->message = ApiResponse::RESPONSE_SAVED->value;

$this->data = $model;

}
catch (Exception $e) {

$this->responseCode = 500;
$this->message = ApiResponse::RESPONSE_ERROR->value . ': "' . $e->getMessage() . '".';

}

return $this->respond();
}
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Middleware/HandleInertiaRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Middleware;

use App\Enums\PersonalAccessTokenAbility;
use App\Enums\VoucherSetType;
use App\Models\Country;
use App\Models\Team;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -43,11 +44,12 @@ public function share(Request $request): array
'auth' => [
'user' => $request->user(),
'currentTeam' => $team,
'teamCountry' => Country::find($team->country_id),
'teamCountry' => Country::find($team?->country_id),
],
'personalAccessTokenAbilities' => PersonalAccessTokenAbility::groupsAbilityCasesWithDefinitions(),
'platformAppTokenAbilities' => PersonalAccessTokenAbility::platformAppTokenAbilities(),
'isImpersonating' => session('vine:impersonator'),
'voucherSetTypes' => VoucherSetType::values(),
];
}
}
2 changes: 2 additions & 0 deletions database/factories/VoucherSetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Database\Factories;

use App\Enums\VoucherSetType;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
Expand All @@ -26,6 +27,7 @@ public function definition(): array
'num_vouchers' => $this->faker->numberBetween(1, 100),
'num_voucher_redemptions' => $this->faker->numberBetween(1, 10),
'expires_at' => now()->addDays(30),
'voucher_set_type' => fake()->randomElement(VoucherSetType::values()),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public function up(): void
$table->uuid('id')->primary();
$table->unsignedBigInteger('created_by_team_id')->index('vs_cbti');
$table->unsignedBigInteger('allocated_to_service_team_id')->index('vs_atsti');
$table->unsignedBigInteger('funded_by_team_id')->nullable()->index('vs_fbti');
$table->unsignedBigInteger('created_by_user_id')->index('vs_cbui');
$table->boolean('is_test')->default(0)->index('vs_it');
$table->unsignedBigInteger('total_set_value')->default(0)->index('vs_tsv');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('voucher_sets', function (Blueprint $table) {

$table->unsignedBigInteger('funded_by_team_id')
->nullable()
->index('vs_fbti')
->after('allocated_to_service_team_id');

$table->string('voucher_set_type')
->index('vs_vst')
->after('is_test');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('voucher_sets', function (Blueprint $table) {
$table->dropColumn(['funded_by_team_id', 'voucher_set_type']);
});
}
};
Loading

0 comments on commit a497da6

Please sign in to comment.