Skip to content

Commit

Permalink
Feature: Create shops process
Browse files Browse the repository at this point in the history
  • Loading branch information
ok200lyndon committed Aug 19, 2024
1 parent 78f873c commit b9a69cc
Show file tree
Hide file tree
Showing 6 changed files with 604 additions and 10 deletions.
210 changes: 210 additions & 0 deletions app/Http/Controllers/Api/V1/ApiShopsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
<?php

/** @noinspection PhpUnusedParameterInspection */

namespace App\Http\Controllers\Api\V1;

use App\Enums\ApiResponse;
use App\Enums\PersonalAccessTokenAbility;
use App\Http\Controllers\Api\HandlesAPIRequests;
use App\Http\Controllers\Controller;
use App\Models\Team;
use App\Models\TeamUser;
use App\Models\User;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Validator;
use Knuckles\Scribe\Attributes\Authenticated;
use Knuckles\Scribe\Attributes\Endpoint;
use Knuckles\Scribe\Attributes\Group;
use Knuckles\Scribe\Attributes\Response;

#[Group('/shops', 'API for managing shops')]
class ApiShopsController extends Controller
{
use HandlesAPIRequests;

/**
* Set the related data the GET request is allowed to ask for
*/
public array $availableRelations = [];

public static array $searchableFields = [
];

#[Endpoint(
title: 'GET /',
authenticated: true
)]
#[Authenticated]
#[Response(
status: 403,
description: 'Method Not Allowed',
)]
public function index(): JsonResponse
{
$this->responseCode = 403;
$this->message = ApiResponse::RESPONSE_METHOD_NOT_ALLOWED->value;

return $this->respond();
}

#[Endpoint(
title: 'POST /',
description: 'Create a new shop.',
authenticated: true
)]
#[Authenticated]
#[Response(
content: '{"meta":{"responseCode":200,"limit":50,"offset":0,"message":"Saved. Here is the API Token for the user linked to this new team. It will only be displayed ONCE, so please store it in a secure manner.","cached":false,"availableRelations":[]},"data":"{TOKEN}"',
status: 200,
description: '',
)]
public function store(): JsonResponse
{
/**
* The validation array.
*/
$validationArray = [
'shop_name' => [
'required',
'string',
],
'user_email' => [
'required',
'email',
],
'user_name' => [
'required',
'string',
],
];

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

if ($validator->fails()) {

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

return $this->respond();
}

try {

/**
* Create a Team for the shop if one does not exist
*/
$shopName = $this->request->get('shop_name');

$shopTeam = Team::where('name', $shopName)->first();

if (is_null($shopTeam)) {
$shopTeam = new Team();
$shopTeam->name = $shopName;
$shopTeam->save();
}

/**
* Create a User for the shop if one does not exist
*/
$userEmail = $this->request->get('user_email');

$shopUser = User::where('email', $userEmail)->first();

if (is_null($shopUser)) {
$shopUser = new User();
$shopUser->email = $userEmail;
$shopUser->password = $userEmail;
$shopUser->name = $this->request->get('user_name');
$shopUser->current_team_id = $shopTeam->id;
$shopUser->save();
}

/**
* Create a TeamUser for the shop if one does not exist
*/
$shopTeamUser = TeamUser::where('team_id', $shopTeam->id)->where('user_id', $shopUser->id)->first();

if (is_null($shopTeamUser)) {
$shopTeamUser = new TeamUser();
$shopTeamUser->user_id = $shopUser->id;
$shopTeamUser->team_id = $shopTeam->id;
$shopTeamUser->save();
}

/**
* Create a PAT for the shop that has redemption capabilities
*/
$token = $shopUser->createToken(
name: $shopTeam->name,
abilities: PersonalAccessTokenAbility::redemptionAppTokenAbilities(),
);

$this->message = ApiResponse::RESPONSE_SAVED->value . '. Here is the API Token for the user linked to this new team. It will only be displayed ONCE, so please store it in a secure manner.';
$this->data = $token->plainTextToken;

} catch (Exception $e) {

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

}

return $this->respond();
}

#[Endpoint(
title: 'GET /{id}',
description: 'Get shop with ID {id}',
authenticated: true
)]
#[Authenticated]
#[Response(
status: 403,
description: 'Method Not Allowed',
)]
public function show(int $id)
{
$this->responseCode = 403;
$this->message = ApiResponse::RESPONSE_METHOD_NOT_ALLOWED->value;

return $this->respond();
}

#[Endpoint(
title: 'PUT /{id}',
description: 'Update shop with ID {id}.',
authenticated: true
)]
#[Authenticated]
#[Response(
status: 403,
description: 'Method Not Allowed',
)]
public function update(string $id)
{
$this->responseCode = 403;
$this->message = ApiResponse::RESPONSE_METHOD_NOT_ALLOWED->value;

return $this->respond();
}

#[Endpoint(
title: 'DELETE /',
description: 'DELETE shop with ID {id}.',
authenticated: true
)]
#[Authenticated]
#[Response(
status: 403,
description: 'Method Not Allowed',
)]
public function destroy(string $id)
{
$this->responseCode = 403;
$this->message = ApiResponse::RESPONSE_METHOD_NOT_ALLOWED->value;

return $this->respond();
}
}
109 changes: 99 additions & 10 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use App\Http\Controllers\Api\V1\ApiMyTeamController;
use App\Http\Controllers\Api\V1\ApiMyTeamsController;
use App\Http\Controllers\Api\V1\ApiMyTeamVouchersController;
use App\Http\Controllers\Api\V1\ApiShopsController;
use App\Http\Controllers\Api\V1\ApiSystemStatisticsController;
use App\Http\Middleware\CheckAdminStatus;
use Illuminate\Support\Facades\Route;
Expand Down Expand Up @@ -186,6 +187,59 @@
]
);

/**
* Shops
*/
Route::post('/shops', [ApiShopsController::class, 'store'])
->name('api.v1.shops.post')
->middleware(
[
'abilities:' .
PersonalAccessTokenAbility::SUPER_ADMIN->value . ',' .
PersonalAccessTokenAbility::SHOPS_CREATE->value,
]
);

Route::get('/shops', [ApiShopsController::class, 'index'])
->name('api.v1.shops.getMany')
->middleware(
[
'abilities:' .
PersonalAccessTokenAbility::SUPER_ADMIN->value . ',' .
PersonalAccessTokenAbility::SHOPS_READ->value,
]
);

Route::get('/shops/{id}', [ApiShopsController::class, 'show'])
->name('api.v1.shops.get')
->middleware(
[
'abilities:' .
PersonalAccessTokenAbility::SUPER_ADMIN->value . ',' .
PersonalAccessTokenAbility::SHOPS_READ->value,
]
);

Route::put('/shops/{id}', [ApiShopsController::class, 'update'])
->name('api.v1.shops.put')
->middleware(
[
'abilities:' .
PersonalAccessTokenAbility::SUPER_ADMIN->value . ',' .
PersonalAccessTokenAbility::SHOPS_UPDATE->value,
]
);

Route::delete('/shops/{id}', [ApiShopsController::class, 'destroy'])
->name('api.v1.shops.delete')
->middleware(
[
'abilities:' .
PersonalAccessTokenAbility::SUPER_ADMIN->value . ',' .
PersonalAccessTokenAbility::SHOPS_DELETE->value,
]
);

/**
* System Statistics
*/
Expand Down Expand Up @@ -247,16 +301,51 @@
Route::prefix('admin')
->middleware(['auth:sanctum', CheckAdminStatus::class])
->group(function () {
Route::resource('/audit-items', ApiAdminAuditItemsController::class)->names('api.v1.admin.audit-items');
Route::resource('/search', ApiAdminSearchController::class)->names('api.v1.admin.search');
Route::resource('/system-statistics', ApiAdminSystemStatisticsController::class)->names('api.v1.admin.system-statistics');
Route::resource('/team-merchant-teams', ApiAdminTeamMerchantTeamsController::class)->names('api.v1.admin.team-merchant-teams');
Route::resource('/team-service-teams', ApiAdminTeamServiceTeamsController::class)->names('api.v1.admin.team-service-teams');
Route::resource('/team-users', ApiAdminTeamUsersController::class)->names('api.v1.admin.team-users');
Route::resource('/teams', ApiAdminTeamsController::class)->names('api.v1.admin.teams');
Route::resource('/user-personal-access-tokens', ApiAdminUserPersonalAccessTokensController::class)->names('api.v1.admin.tokens');
Route::resource('/users', ApiAdminUsersController::class)->names('api.v1.admin.users');

});
Route::resource(
'/audit-items',
ApiAdminAuditItemsController::class
)->names('api.v1.admin.audit-items');

Route::resource(
'/search',
ApiAdminSearchController::class
)->names('api.v1.admin.search');

Route::resource(
'/system-statistics',
ApiAdminSystemStatisticsController::class
)->names('api.v1.admin.system-statistics');

Route::resource(
'/team-merchant-teams',
ApiAdminTeamMerchantTeamsController::class
)->names('api.v1.admin.team-merchant-teams');

Route::resource(
'/team-service-teams',
ApiAdminTeamServiceTeamsController::class
)->names('api.v1.admin.team-service-teams');

Route::resource(
'/team-users',
ApiAdminTeamUsersController::class
)->names('api.v1.admin.team-users');

Route::resource(
'/teams',
ApiAdminTeamsController::class
)->names('api.v1.admin.teams');

Route::resource(
'/user-personal-access-tokens',
ApiAdminUserPersonalAccessTokensController::class
)->names('api.v1.admin.tokens');

Route::resource(
'/users',
ApiAdminUsersController::class
)->names('api.v1.admin.users');

});
});
Loading

0 comments on commit b9a69cc

Please sign in to comment.