mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-08 08:12:17 +01:00
Merge branch 'feature/import' into feature/add_frontend_dashboard
# Conflicts: # app/Http/Controllers/Api/V1/TimeEntryController.php # app/Providers/JetstreamServiceProvider.php
This commit is contained in:
63
app/Http/Controllers/Api/V1/ImportController.php
Normal file
63
app/Http/Controllers/Api/V1/ImportController.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Requests\V1\Import\ImportRequest;
|
||||
use App\Models\Organization;
|
||||
use App\Service\Import\Importers\ImportException;
|
||||
use App\Service\Import\ImportService;
|
||||
use Illuminate\Auth\Access\AuthorizationException;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class ImportController extends Controller
|
||||
{
|
||||
/**
|
||||
* Import data into the organization
|
||||
*
|
||||
* @throws AuthorizationException
|
||||
*/
|
||||
public function import(Organization $organization, ImportRequest $request, ImportService $importService): JsonResponse
|
||||
{
|
||||
$this->checkPermission($organization, 'import');
|
||||
|
||||
try {
|
||||
$report = $importService->import(
|
||||
$organization,
|
||||
$request->input('type'),
|
||||
$request->input('data')
|
||||
);
|
||||
|
||||
return new JsonResponse([
|
||||
/** @var array{
|
||||
* clients: array{
|
||||
* created: int,
|
||||
* },
|
||||
* projects: array{
|
||||
* created: int,
|
||||
* },
|
||||
* tasks: array{
|
||||
* created: int,
|
||||
* },
|
||||
* time-entries: array{
|
||||
* created: int,
|
||||
* },
|
||||
* tags: array{
|
||||
* created: int,
|
||||
* },
|
||||
* users: array{
|
||||
* created: int,
|
||||
* }
|
||||
* } $report Import report */
|
||||
'report' => $report->toArray(),
|
||||
], 200);
|
||||
} catch (ImportException $exception) {
|
||||
report($exception);
|
||||
|
||||
return new JsonResponse([
|
||||
'message' => $exception->getMessage(),
|
||||
], 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Exceptions\TimeEntryStillRunning;
|
||||
use App\Exceptions\Api\TimeEntryStillRunningApiException;
|
||||
use App\Http\Requests\V1\TimeEntry\TimeEntryIndexRequest;
|
||||
use App\Http\Requests\V1\TimeEntry\TimeEntryStoreRequest;
|
||||
use App\Http\Requests\V1\TimeEntry\TimeEntryUpdateRequest;
|
||||
@@ -104,7 +104,7 @@ class TimeEntryController extends Controller
|
||||
/**
|
||||
* Create time entry
|
||||
*
|
||||
* @throws AuthorizationException|TimeEntryStillRunning
|
||||
* @throws AuthorizationException|TimeEntryStillRunningApiException
|
||||
*
|
||||
* @operationId createTimeEntry
|
||||
*/
|
||||
@@ -118,8 +118,7 @@ class TimeEntryController extends Controller
|
||||
|
||||
if ($request->get('end') === null && TimeEntry::query()->where('user_id', $request->get('user_id'))->where('end', null)->exists()) {
|
||||
// TODO: API documentation
|
||||
// TODO: Create concept for api exceptions
|
||||
throw new TimeEntryStillRunning('User already has an active time entry');
|
||||
throw new TimeEntryStillRunningApiException();
|
||||
}
|
||||
|
||||
$timeEntry = new TimeEntry();
|
||||
|
||||
56
app/Http/Controllers/Api/V1/UserController.php
Normal file
56
app/Http/Controllers/Api/V1/UserController.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Exceptions\Api\UserNotPlaceholderApiException;
|
||||
use App\Http\Requests\V1\User\UserIndexRequest;
|
||||
use App\Http\Resources\V1\User\UserCollection;
|
||||
use App\Models\Organization;
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Access\AuthorizationException;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Laravel\Jetstream\Contracts\InvitesTeamMembers;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
/**
|
||||
* List all users in an organization
|
||||
*
|
||||
* @throws AuthorizationException
|
||||
*/
|
||||
public function index(Organization $organization, UserIndexRequest $request): UserCollection
|
||||
{
|
||||
$this->checkPermission($organization, 'users:view');
|
||||
|
||||
$users = $organization->users()
|
||||
->paginate();
|
||||
|
||||
return UserCollection::make($users);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invite a placeholder user to become a real user in the organization
|
||||
*
|
||||
* @throws AuthorizationException|UserNotPlaceholderApiException
|
||||
*/
|
||||
public function invitePlaceholder(Organization $organization, User $user, Request $request): JsonResponse
|
||||
{
|
||||
$this->checkPermission($organization, 'users:invite-placeholder');
|
||||
|
||||
if (! $user->is_placeholder) {
|
||||
throw new UserNotPlaceholderApiException();
|
||||
}
|
||||
|
||||
app(InvitesTeamMembers::class)->invite(
|
||||
$request->user(),
|
||||
$organization,
|
||||
$user->email,
|
||||
'employee'
|
||||
);
|
||||
|
||||
return response()->json($user);
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ class ValidateSignature extends Middleware
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $except = [
|
||||
protected array $except = [
|
||||
// 'fbclid',
|
||||
// 'utm_campaign',
|
||||
// 'utm_content',
|
||||
|
||||
30
app/Http/Requests/V1/Import/ImportRequest.php
Normal file
30
app/Http/Requests/V1/Import/ImportRequest.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\V1\Import;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ImportRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, array<string|ValidationRule>>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'type' => [
|
||||
'required',
|
||||
'string',
|
||||
],
|
||||
'data' => [
|
||||
'required',
|
||||
'string',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ namespace App\Http\Requests\V1\Project;
|
||||
|
||||
use App\Models\Client;
|
||||
use App\Models\Organization;
|
||||
use App\Rules\ColorRule;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
@@ -25,6 +26,7 @@ class ProjectStoreRequest extends FormRequest
|
||||
{
|
||||
return [
|
||||
'name' => [
|
||||
// TODO: unique
|
||||
'required',
|
||||
'string',
|
||||
'min:1',
|
||||
@@ -34,6 +36,7 @@ class ProjectStoreRequest extends FormRequest
|
||||
'required',
|
||||
'string',
|
||||
'max:255',
|
||||
new ColorRule(),
|
||||
],
|
||||
'client_id' => [
|
||||
'nullable',
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace App\Http\Requests\V1\Project;
|
||||
|
||||
use App\Models\Client;
|
||||
use App\Models\Organization;
|
||||
use App\Rules\ColorRule;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
@@ -25,6 +26,7 @@ class ProjectUpdateRequest extends FormRequest
|
||||
{
|
||||
return [
|
||||
'name' => [
|
||||
// TODO: unique
|
||||
'required',
|
||||
'string',
|
||||
'max:255',
|
||||
@@ -33,6 +35,7 @@ class ProjectUpdateRequest extends FormRequest
|
||||
'required',
|
||||
'string',
|
||||
'max:255',
|
||||
new ColorRule(),
|
||||
],
|
||||
'client_id' => [
|
||||
'nullable',
|
||||
|
||||
@@ -18,6 +18,7 @@ class TagStoreRequest extends FormRequest
|
||||
{
|
||||
return [
|
||||
'name' => [
|
||||
// TODO: unique
|
||||
'required',
|
||||
'string',
|
||||
'min:1',
|
||||
|
||||
@@ -18,6 +18,7 @@ class TagUpdateRequest extends FormRequest
|
||||
{
|
||||
return [
|
||||
'name' => [
|
||||
// TODO: unique
|
||||
'required',
|
||||
'string',
|
||||
'min:1',
|
||||
|
||||
@@ -30,10 +30,7 @@ class TimeEntryIndexRequest extends FormRequest
|
||||
'uuid',
|
||||
new ExistsEloquent(User::class, null, function (Builder $builder): Builder {
|
||||
/** @var Builder<User> $builder */
|
||||
return $builder->whereHas('organizations', function (Builder $builder) {
|
||||
/** @var Builder<Organization> $builder */
|
||||
return $builder->whereKey($this->organization->getKey());
|
||||
});
|
||||
return $builder->belongsToOrganization($this->organization);
|
||||
}),
|
||||
],
|
||||
// Filter only time entries that have a start date before (not including) the given date (example: 2021-12-31)
|
||||
|
||||
@@ -33,10 +33,7 @@ class TimeEntryStoreRequest extends FormRequest
|
||||
'uuid',
|
||||
new ExistsEloquent(User::class, null, function (Builder $builder): Builder {
|
||||
/** @var Builder<User> $builder */
|
||||
return $builder->whereHas('organizations', function (Builder $builder) {
|
||||
/** @var Builder<Organization> $builder */
|
||||
return $builder->whereKey($this->organization->getKey());
|
||||
});
|
||||
return $builder->belongsToOrganization($this->organization);
|
||||
}),
|
||||
],
|
||||
// ID of the task that the time entry should belong to
|
||||
@@ -64,7 +61,7 @@ class TimeEntryStoreRequest extends FormRequest
|
||||
'description' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'max:255',
|
||||
'max:500',
|
||||
],
|
||||
// List of tag IDs
|
||||
'tags' => [
|
||||
|
||||
@@ -51,7 +51,7 @@ class TimeEntryUpdateRequest extends FormRequest
|
||||
'description' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'max:255',
|
||||
'max:500',
|
||||
],
|
||||
// List of tag IDs
|
||||
'tags' => [
|
||||
|
||||
26
app/Http/Requests/V1/User/UserIndexRequest.php
Normal file
26
app/Http/Requests/V1/User/UserIndexRequest.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\V1\User;
|
||||
|
||||
use App\Models\Organization;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
/**
|
||||
* @property Organization $organization
|
||||
*/
|
||||
class UserIndexRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, array<string|ValidationRule>>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
];
|
||||
}
|
||||
}
|
||||
17
app/Http/Resources/V1/User/UserCollection.php
Normal file
17
app/Http/Resources/V1/User/UserCollection.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources\V1\User;
|
||||
|
||||
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||
|
||||
class UserCollection extends ResourceCollection
|
||||
{
|
||||
/**
|
||||
* The resource that this resource collects.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $collects = UserResource::class;
|
||||
}
|
||||
40
app/Http/Resources/V1/User/UserResource.php
Normal file
40
app/Http/Resources/V1/User/UserResource.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources\V1\User;
|
||||
|
||||
use App\Http\Resources\V1\BaseResource;
|
||||
use App\Models\Membership;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* @property User $resource
|
||||
*/
|
||||
class UserResource extends BaseResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, string|bool|int|null|array<string>>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
/** @var Membership $membership */
|
||||
$membership = $this->resource->getRelationValue('membership');
|
||||
|
||||
return [
|
||||
/** @var string $id ID */
|
||||
'id' => $this->resource->id,
|
||||
/** @var string $name Name */
|
||||
'name' => $this->resource->name,
|
||||
/** @var string $email Email */
|
||||
'email' => $this->resource->email,
|
||||
/** @var string $role Role */
|
||||
'role' => $membership->role,
|
||||
/** @var bool $is_placeholder Placeholder user for imports, user might not really exist and does not know about this placeholder membership */
|
||||
'is_placeholder' => $this->resource->is_placeholder,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user