Skip to content

Commit

Permalink
New database first staging test
Browse files Browse the repository at this point in the history
  • Loading branch information
jefponte committed Nov 9, 2023
1 parent dd4be2a commit bc28be4
Show file tree
Hide file tree
Showing 239 changed files with 14,514 additions and 6,517 deletions.
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ ENV COMPOSER_ALLOW_SUPERUSER=1
ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update && apt-get install -y --no-install-recommends libpq-dev locales curl nano unzip \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN docker-php-ext-install pdo pdo_pgsql
RUN docker-php-ext-configure opcache --enable-opcache
RUN curl -fsSLk https://deb.nodesource.com/setup_18.x | bash - && apt-get install -y nodejs
RUN npm install
RUN npm run build

RUN curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" && \
curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256" && \
Expand Down
34 changes: 34 additions & 0 deletions app/Enums/OrderStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Enums;

use Spatie\Enum\Laravel\Enum;

/**
* @method static self reopened()
* @method static self opened()
* @method static self closed()
* @method static self inProgress()
* @method static self reserved()
* @method static self pendingResource()
* @method static self pendingCustomerResponse()
* @method static self committed()
* @method static self canceled()
*/
class OrderStatus extends Enum
{
protected static function values(): array
{
return [
'opened' => 'opened',
'reopened' => 'reopened',
'closed' => 'closed',
'inProgress' => 'in progress',
'reserved' => 'reserved',
'pendingResource' => 'pending resource',
'pendingCustomerResponse' => 'pending customer response',
'committed' => 'committed',
'canceled' => 'canceled',
];
}
}
24 changes: 24 additions & 0 deletions app/Enums/UserRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Enums;

use Spatie\Enum\Laravel\Enum;

/**
* @method static self customer()
* @method static self provider()
* @method static self administrator()
* @method static self disabled()
*/
class UserRole extends Enum
{
protected static function values(): array
{
return [
'customer' => 'customer',
'provider' => 'provider',
'administrator' => 'administrator',
'disabled' => 'disabled',
];
}
}
79 changes: 79 additions & 0 deletions app/Http/Controllers/Api/Auth/AuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace App\Http\Controllers\Api\Auth;

use App\Http\Controllers\Controller;
use App\Http\Requests\Api\AuthRequest;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Validation\ValidationException;

class AuthController extends Controller
{
public function auth(AuthRequest $request)
{

$apiOrigin = env('UNILAB_API_ORIGIN') === null ? 'https://api.unilab.edu.br/api' : env('UNILAB_API_ORIGIN');

$dataAPi = ['login' => $request->login, 'senha' => $request->password];
$response = Http::post($apiOrigin.'/authenticate', $dataAPi);
$responseJ = json_decode($response->body());
$userId = 0;
if (isset($responseJ->id)) {
$userId = intval($responseJ->id);
}
if ($userId === 0) {
throw ValidationException::withMessages([
'login' => trans('auth.failed'),
]);
}
$headers = [
'Authorization' => 'Bearer '.$responseJ->access_token,
];
$response = Http::withHeaders($headers)->get($apiOrigin.'/user', $headers);
$responseJ2 = json_decode($response->body());

$response = Http::withHeaders($headers)->get($apiOrigin.'/bond', $headers);
$responseJ3 = json_decode($response->body());

$user = User::firstOrNew(['id' => $userId]);
$user->id = $userId;
$user->name = $responseJ2->nome;
$user->email = $responseJ2->email;
$user->login = $responseJ2->login;
$user->division_sig = $responseJ3[0]->sigla_unidade;
$user->division_sig_id = $responseJ3[0]->id_unidade;
if ($user->role == null) {
$user->role = $responseJ2->id_status_servidor != 1 ? 'disabled' : 'customer';
}
$user->password = $request->password;
$user->save();
$token = $user->createToken($request->device_name)->plainTextToken;

return response()->json(
['token' => $token, 'user' => $user]
);
}

public function logout(Request $request)
{
auth()->user()->tokens->each(function ($token, $key) {
$token->delete();
});

return response()->json([
'message' => 'Logged out successfully!',
'status_code' => 200,
], 200);
}

public function me(Request $request)
{
$user = $request->user();

return response()->json(
['me' => $user]
);
}
}
112 changes: 112 additions & 0 deletions app/Http/Controllers/Api/BasicCrudController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Facades\Validator;

abstract class BasicCrudController extends Controller
{
protected $defaultPerPage = 15;

abstract protected function model();

abstract protected function rulesStore();

abstract protected function rulesUpdate();

abstract protected function resource();

abstract protected function resourceCollection();

public function index(Request $request)
{
$perPage = (int) $request->get('per_page', $this->defaultPerPage);
$hasFilter = in_array(Filterable::class, class_uses($this->model()));

$query = $this->queryBuilder();

if ($hasFilter) {
$query = $query->filter($request->all());
}

$data = $request->has('all') || ! $this->defaultPerPage
? $query->get()
: $query->paginate($perPage);

$resourceCollectionClass = $this->resourceCollection();
$refClass = new \ReflectionClass($this->resourceCollection());

return $refClass->isSubclassOf(ResourceCollection::class)
? new $resourceCollectionClass($data)
: $resourceCollectionClass::collection($data);
}

public function store(Request $request)
{
$validatedData = $this->validate($request, $this->rulesStore());
$obj = $this->queryBuilder()->create($validatedData);
$obj->refresh();
$resource = $this->resource();

return new $resource($obj);
}

protected function findOrFail($id)
{
$model = $this->model();
$keyName = (new $model)->getRouteKeyName();

return $this->queryBuilder()->where($keyName, $id)->firstOrFail();

}

protected function rulesPatch()
{
return array_map(function ($rules) {
if (is_array($rules)) {
$exists = in_array('required', $rules);
if ($exists) {
array_unshift($rules, 'sometimes');
}
} else {
return str_replace('required', 'sometimes|required', $rules);
}

return $rules;
}, $this->rulesUpdate());
}

public function destroyCollection(Request $request)
{
$data = $this->validateIds($request);
$this->model()::whereIn('id', $data['ids'])->delete();

return response()->noContent();
}

protected function validateIds(Request $request)
{
$model = $this->model();
$ids = explode(',', $request->get('ids'));
$validator = Validator::make(
[
'ids' => $ids,
],
[
'ids' => 'required|exists:'.(new $model)->getTable().',id',
]
);

return $validator->validate();
}

protected function queryBuilder(): Builder
{
return $this->model()::query();
}
}
81 changes: 81 additions & 0 deletions app/Http/Controllers/Api/DivisionsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace App\Http\Controllers\Api;

use App\Http\Resources\DivisionResource;
use App\Models\Division;
use Illuminate\Http\Request;

class DivisionsController extends BasicCrudController
{
public function __construct()
{
$this->authorizeResource(Division::class, 'division');
}

public function show(Division $division)
{
return new DivisionResource($division);
}

/**
* Update the specified resource in storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Division $division)
{
$this->validate($request, [
'name' => 'required|max:255',
'description' => 'required|max:255',
'email' => 'required|max:255',
]);

$division->update($request->all());

return response()->json($division, 200);
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(Division $division)
{
$division->delete();

return response()->json(null, 204);
}

private $rules = [
'name' => 'required|max:255',
];

protected function model()
{
return Division::class;
}

protected function rulesStore()
{
return $this->rules;
}

protected function rulesUpdate()
{
return $this->rules;
}

protected function resourceCollection()
{
return $this->resource();
}

protected function resource()
{
return DivisionResource::class;
}
}
Loading

0 comments on commit bc28be4

Please sign in to comment.