40
common/Workspaces/Actions/CrupdateWorkspace.php
Executable file
40
common/Workspaces/Actions/CrupdateWorkspace.php
Executable file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Workspaces\Actions;
|
||||
|
||||
use Auth;
|
||||
use Common\Workspaces\Workspace;
|
||||
|
||||
class CrupdateWorkspace
|
||||
{
|
||||
public function __construct(protected Workspace $workspace)
|
||||
{
|
||||
}
|
||||
|
||||
public function execute(
|
||||
array $data,
|
||||
Workspace|null $initialWorkspace = null
|
||||
): Workspace {
|
||||
if ($initialWorkspace) {
|
||||
$workspace = $initialWorkspace;
|
||||
} else {
|
||||
$workspace = $this->workspace->newInstance([
|
||||
'owner_id' => Auth::id(),
|
||||
]);
|
||||
}
|
||||
|
||||
$attributes = [
|
||||
'name' => $data['name'],
|
||||
];
|
||||
|
||||
$workspace->fill($attributes)->save();
|
||||
|
||||
if (!$initialWorkspace) {
|
||||
$workspace
|
||||
->members()
|
||||
->create(['user_id' => Auth::id(), 'is_owner' => true]);
|
||||
}
|
||||
|
||||
return $workspace;
|
||||
}
|
||||
}
|
||||
30
common/Workspaces/Actions/DeleteInviteNotification.php
Executable file
30
common/Workspaces/Actions/DeleteInviteNotification.php
Executable file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Workspaces\Actions;
|
||||
|
||||
use App\Models\User;
|
||||
use Common\Workspaces\Notifications\WorkspaceInvitation;
|
||||
use Common\Workspaces\WorkspaceInvite;
|
||||
use Illuminate\Notifications\DatabaseNotification;
|
||||
|
||||
class DeleteInviteNotification
|
||||
{
|
||||
public function execute(WorkspaceInvite $invite, User $user): void
|
||||
{
|
||||
$notifications = $user
|
||||
->notifications()
|
||||
->where('type', WorkspaceInvitation::class)
|
||||
->limit(20)
|
||||
->get();
|
||||
|
||||
$notification = $notifications->first(function (
|
||||
DatabaseNotification $notification,
|
||||
) use ($invite) {
|
||||
return $notification->data['inviteId'] === $invite->id;
|
||||
});
|
||||
|
||||
if ($notification) {
|
||||
$notification->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
36
common/Workspaces/Actions/DeleteWorkspaces.php
Executable file
36
common/Workspaces/Actions/DeleteWorkspaces.php
Executable file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Workspaces\Actions;
|
||||
|
||||
use Common\Workspaces\Events\WorkspaceDeleted;
|
||||
use Common\Workspaces\Workspace;
|
||||
use Common\Workspaces\WorkspaceMember;
|
||||
|
||||
class DeleteWorkspaces
|
||||
{
|
||||
/**
|
||||
* @var Workspace
|
||||
*/
|
||||
private $workspace;
|
||||
|
||||
public function __construct(Workspace $workspace)
|
||||
{
|
||||
$this->workspace = $workspace;
|
||||
}
|
||||
|
||||
public function execute($ids)
|
||||
{
|
||||
$workspaces = $this->workspace->whereIn('id', $ids)->get();
|
||||
|
||||
$workspaces->each(function(Workspace $workspace) {
|
||||
$workspace->invites()->delete();
|
||||
$workspace->members->each(function (WorkspaceMember $member) use($workspace) {
|
||||
app(RemoveMemberFromWorkspace::class)->execute($workspace, $member->id);
|
||||
});
|
||||
event(new WorkspaceDeleted($workspace->id, $workspace->owner_id));
|
||||
});
|
||||
|
||||
$this->workspace->whereIn('id', $ids)->delete();
|
||||
}
|
||||
|
||||
}
|
||||
27
common/Workspaces/Actions/JoinWorkspace.php
Executable file
27
common/Workspaces/Actions/JoinWorkspace.php
Executable file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Workspaces\Actions;
|
||||
|
||||
use App\Models\User;
|
||||
use Common\Workspaces\WorkspaceInvite;
|
||||
use Session;
|
||||
|
||||
class JoinWorkspace
|
||||
{
|
||||
public function execute(WorkspaceInvite $invite, User $user)
|
||||
{
|
||||
if ($invite->email === $user->email) {
|
||||
$invite->workspace
|
||||
->members()
|
||||
->firstOrCreate(
|
||||
['user_id' => $user->id],
|
||||
['role_id' => $invite->role_id],
|
||||
);
|
||||
|
||||
app(DeleteInviteNotification::class)->execute($invite, $user);
|
||||
|
||||
$invite->delete();
|
||||
}
|
||||
Session::remove('activeWorkspace');
|
||||
}
|
||||
}
|
||||
38
common/Workspaces/Actions/RemoveMemberFromWorkspace.php
Executable file
38
common/Workspaces/Actions/RemoveMemberFromWorkspace.php
Executable file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Workspaces\Actions;
|
||||
|
||||
use Common\Workspaces\Workspace;
|
||||
use Common\Workspaces\WorkspaceMember;
|
||||
use const App\Providers\WORKSPACED_RESOURCES;
|
||||
|
||||
class RemoveMemberFromWorkspace
|
||||
{
|
||||
public function execute(Workspace $workspace, int $userToBeRemoved)
|
||||
{
|
||||
// transfer workspace resources to owner
|
||||
if ($workspace->owner_id !== $userToBeRemoved) {
|
||||
foreach (WORKSPACED_RESOURCES as $model) {
|
||||
$baseName = class_basename($model);
|
||||
$namespace = "App\Workspaces\Transfer{$baseName}";
|
||||
if (class_exists($namespace)) {
|
||||
app($namespace)->execute(
|
||||
$workspace->id,
|
||||
$workspace->owner_id,
|
||||
$userToBeRemoved,
|
||||
);
|
||||
} else {
|
||||
app($model)
|
||||
->where('workspace_id', $workspace->id)
|
||||
->where('user_id', $userToBeRemoved)
|
||||
->update(['user_id' => $workspace->owner_id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
app(WorkspaceMember::class)
|
||||
->where('workspace_id', $workspace->id)
|
||||
->where('user_id', $userToBeRemoved)
|
||||
->delete();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user