Pricing - Bill per workspace ($12/mo or $120/yr each); Stripe quantity tracks the workspace count and syncs on workspace create/delete. - 2,500 AI credits per workspace, pooled at the account level; monthly reset on the billing anniversary, annual granted upfront (no rollover). - One social account per network per workspace; remove all count-based limits (workspace/social/member) and the legacy plan tiers (single Workspace plan). Onboarding (cloud only: SELF_HOSTED=false + PostHog) - Replace the /subscribe plan picker with /onboarding persona selection (Creator/Freelancer/Startup/Agency/Small business/Other), saved on the user (users.persona) and mirrored to PostHog, then Stripe Checkout on the monthly price. 8-day trial so Stripe displays 7. Billing screen - Remove the Change Plan dialog (dead with a single plan); add an annual-upgrade banner for monthly subscribers (swapToYearly). - Current-plan card shows the workspace count instead of the plan name. System AI - Brand analyzer / workspace autofill is always allowed and never debits credits (system feature, not the user's usage). Self-hosted (SELF_HOSTED=true) bypasses all billing, credit, limit, network, and onboarding logic.
232 lines
7.2 KiB
PHP
232 lines
7.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\App;
|
|
|
|
use App\Actions\Ai\AutofillBrand;
|
|
use App\Actions\Workspace\CreateWorkspace;
|
|
use App\Actions\Workspace\DeleteWorkspace;
|
|
use App\Enums\Workspace\BrandFont;
|
|
use App\Enums\Workspace\BrandVoiceTrait;
|
|
use App\Enums\Workspace\ImageStyle;
|
|
use App\Http\Requests\App\Workspace\StoreWorkspaceRequest;
|
|
use App\Http\Requests\App\Workspace\UpdateWorkspaceRequest;
|
|
use App\Http\Resources\App\WorkspaceMemberResource;
|
|
use App\Models\Workspace;
|
|
use App\Services\Brand\LogoAttacher;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
use RuntimeException;
|
|
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
|
|
use Throwable;
|
|
|
|
class WorkspaceController extends Controller
|
|
{
|
|
public function searchMembers(Request $request): AnonymousResourceCollection
|
|
{
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
abort_if(! $workspace, SymfonyResponse::HTTP_FORBIDDEN);
|
|
|
|
$this->authorize('view', $workspace);
|
|
|
|
$term = trim((string) $request->input('q', ''));
|
|
|
|
$members = $workspace->members()
|
|
->where('users.id', '!=', $request->user()->id)
|
|
->when($term !== '', fn ($query) => $query->where('users.name', 'ilike', '%'.$term.'%'))
|
|
->orderBy('users.name')
|
|
->limit(50)
|
|
->get(['users.id', 'users.name', 'users.email']);
|
|
|
|
return WorkspaceMemberResource::collection($members);
|
|
}
|
|
|
|
public function index(Request $request): Response
|
|
{
|
|
$user = $request->user();
|
|
|
|
$workspaces = $user->workspaces()
|
|
->with('media')
|
|
->withCount(['socialAccounts', 'posts'])
|
|
->latest()
|
|
->get();
|
|
|
|
return Inertia::render('workspaces/Index', [
|
|
'workspaces' => $workspaces,
|
|
'currentWorkspaceId' => $user->current_workspace_id,
|
|
]);
|
|
}
|
|
|
|
public function create(Request $request): Response|RedirectResponse
|
|
{
|
|
$user = $request->user();
|
|
|
|
if (! config('trypost.self_hosted')
|
|
&& $user->ownedWorkspacesCount() > 0
|
|
&& ! $user->account?->hasActiveSubscription()) {
|
|
return redirect()->route('app.billing.index')
|
|
->with('message', 'Subscribe to create more workspaces.');
|
|
}
|
|
|
|
return Inertia::render('workspaces/Create', [
|
|
'availableFonts' => BrandFont::values(),
|
|
'availableImageStyles' => ImageStyle::values(),
|
|
'availableVoiceTraits' => BrandVoiceTrait::grouped(),
|
|
]);
|
|
}
|
|
|
|
public function autofillBrand(Request $request, AutofillBrand $autofill): JsonResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'url' => ['required', 'string', 'max:255'],
|
|
]);
|
|
|
|
try {
|
|
$metadata = $autofill(data_get($validated, 'url'));
|
|
} catch (RuntimeException $e) {
|
|
return response()->json(['message' => $e->getMessage()], SymfonyResponse::HTTP_UNPROCESSABLE_ENTITY);
|
|
}
|
|
|
|
return response()->json($metadata->toArray());
|
|
}
|
|
|
|
public function store(StoreWorkspaceRequest $request, LogoAttacher $logoAttacher): RedirectResponse
|
|
{
|
|
$user = $request->user();
|
|
|
|
$validated = $request->validated();
|
|
|
|
$workspace = CreateWorkspace::execute($user, $validated);
|
|
|
|
if ($logoUrl = data_get($validated, 'logo_url')) {
|
|
try {
|
|
$logoAttacher->attach($workspace, $logoUrl);
|
|
} catch (Throwable $e) {
|
|
Log::warning('Logo attach failed during workspace creation', [
|
|
'workspace_id' => $workspace->id,
|
|
'logo_url' => $logoUrl,
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
return redirect()->route('app.accounts', ['openDialog' => 'true'])
|
|
->with('success', __('workspaces.create.success'));
|
|
}
|
|
|
|
public function switch(Request $request, Workspace $workspace): RedirectResponse
|
|
{
|
|
$user = $request->user();
|
|
|
|
if (! $user->belongsToWorkspace($workspace)) {
|
|
abort(403);
|
|
}
|
|
|
|
$user->switchWorkspace($workspace);
|
|
|
|
return redirect()->route('app.calendar');
|
|
}
|
|
|
|
public function settings(Request $request): Response|RedirectResponse
|
|
{
|
|
$user = $request->user();
|
|
$workspace = $user->currentWorkspace;
|
|
|
|
if (! $workspace) {
|
|
return redirect()->route('app.workspaces.create');
|
|
}
|
|
|
|
$this->authorize('update', $workspace);
|
|
|
|
return Inertia::render('settings/workspace/Workspace', [
|
|
'workspace' => $workspace,
|
|
]);
|
|
}
|
|
|
|
public function brandSettings(Request $request): Response|RedirectResponse
|
|
{
|
|
$user = $request->user();
|
|
$workspace = $user->currentWorkspace;
|
|
|
|
if (! $workspace) {
|
|
return redirect()->route('app.workspaces.create');
|
|
}
|
|
|
|
$this->authorize('update', $workspace);
|
|
|
|
return Inertia::render('settings/workspace/Brand', [
|
|
'workspace' => $workspace,
|
|
'availableFonts' => BrandFont::values(),
|
|
'availableImageStyles' => ImageStyle::values(),
|
|
'availableVoiceTraits' => BrandVoiceTrait::grouped(),
|
|
]);
|
|
}
|
|
|
|
public function uploadLogo(Request $request): RedirectResponse
|
|
{
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
$this->authorize('update', $workspace);
|
|
|
|
$request->validate([
|
|
'photo' => ['required', 'image', 'max:2048'],
|
|
]);
|
|
|
|
$workspace->clearMediaCollection('logo');
|
|
$workspace->addMedia($request->file('photo'), 'logo');
|
|
$workspace->unsetRelation('media');
|
|
|
|
return back()->with('flash.success', __('settings.flash.logo_updated'));
|
|
}
|
|
|
|
public function deleteLogo(Request $request): RedirectResponse
|
|
{
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
$this->authorize('update', $workspace);
|
|
|
|
$workspace->clearMediaCollection('logo');
|
|
$workspace->unsetRelation('media');
|
|
|
|
return back()->with('flash.success', __('settings.flash.logo_deleted'));
|
|
}
|
|
|
|
public function updateSettings(UpdateWorkspaceRequest $request): RedirectResponse
|
|
{
|
|
$user = $request->user();
|
|
$workspace = $user->currentWorkspace;
|
|
|
|
if (! $workspace) {
|
|
return redirect()->route('app.workspaces.create');
|
|
}
|
|
|
|
$this->authorize('update', $workspace);
|
|
|
|
$workspace->update($request->validated());
|
|
|
|
return back()->with('flash.success', __('settings.flash.workspace_updated'));
|
|
}
|
|
|
|
public function destroy(Request $request, Workspace $workspace): RedirectResponse
|
|
{
|
|
$this->authorize('delete', $workspace);
|
|
|
|
$user = $request->user();
|
|
|
|
if (! config('trypost.self_hosted') && $workspace->account->workspaces()->count() <= 1) {
|
|
return back()->with('flash.error', __('workspaces.cannot_delete_last'));
|
|
}
|
|
|
|
DeleteWorkspace::execute($user, $workspace);
|
|
|
|
return redirect()->route('app.workspaces.index')
|
|
->with('flash.success', __('workspaces.flash.deleted'));
|
|
}
|
|
}
|