0dd1f5ebc1
* feat: multi-tenant company refactor (GNU-19) Introduce companies table, company_members, and user_preferences to support multiple companies per user. All data scoping changes from user_id to company_id across the entire codebase. Key changes: - Database migration: new tables, company_id on 40+ tables, backfill, RLS rewrite from user_id to company-member-based, updated RPCs - Types: Company, CompanyMember, CompanyRole, UserPreferences types; company_id added to all entity interfaces; companyId on all events - Engine: all 7 core functions take companyId; storno, period, year-end services updated; 16 report generators updated - Middleware: company context resolution (cookie → prefs → first company) - API routes: ~120 routes updated with requireCompanyId() - Frontend: CompanyProvider context, layout/dashboard/onboarding updated - Extensions: context factory, 9 extensions, all lib files updated - Tests: 1880 tests passing, all helpers updated with company_id defaults Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add database migrations for multi-tenant company and team system (GNU-19) Adds company_invitations, company creation RPC, team_members, account deletion RPC, and teams table refactor migrations. Updates base multi-tenant migration with cascading FKs and onboarding_step column. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add team types and update core infrastructure for multi-tenancy (GNU-19) Adds TeamRole, MemberSource, and Team types. Refactors Supabase service client to be stateless, updates middleware for team-aware routing, extends CompanyContext with team/role fields, and updates extension service types to accept companyId. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * refactor: thread company_id through business logic functions (GNU-19) Replaces user_id scoping with company_id across all lib modules: bookkeeping, documents, transactions, invoices, reconciliation, tax, deadlines, and import. Updates corresponding tests. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * refactor: thread company_id through API routes and extensions (GNU-19) Updates all existing API routes to extract and pass companyId. Updates enable-banking and arcim-migration extensions for company-scoped transaction ingestion and sync. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add company and team management API routes (GNU-19) Adds CRUD endpoints for company members, company invitations, team members, and team invitations. Includes invite token utilities, email templates, and company switch server action. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add team/company UI components, pages, and dashboard updates (GNU-19) Adds CompanySwitcher, ConsultantEmptyState, Step0RoleChoice, company members and team management panels. Updates dashboard layout for team-aware routing, onboarding for multi-step role choice, and auth callback for team invite acceptance. Ignores supabase/.branches/. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: add null guards for company in import page (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: move appUrl declaration to outer scope in invite route (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: add optional chaining for company.name in members section (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: add optional chaining for second company.name in members section (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: add null guards for company in extension components (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: pass companyId to executeSIEImport in arcim-migration extension (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: update tests to use companyId instead of userId and improve type handling --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
309 lines
11 KiB
TypeScript
309 lines
11 KiB
TypeScript
'use client'
|
|
|
|
import { useState, useEffect, useCallback } from 'react'
|
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Input } from '@/components/ui/input'
|
|
import { Label } from '@/components/ui/label'
|
|
import { Badge } from '@/components/ui/badge'
|
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
|
|
import { useToast } from '@/components/ui/use-toast'
|
|
import { useCompany } from '@/contexts/CompanyContext'
|
|
import { Loader2, Plus, Trash2, Mail, Clock, Users } from 'lucide-react'
|
|
|
|
interface CompanyMemberItem {
|
|
id: string
|
|
user_id: string
|
|
email: string
|
|
role: string
|
|
source: 'direct' | 'team'
|
|
joined_at: string
|
|
is_current_user: boolean
|
|
}
|
|
|
|
interface CompanyInvitation {
|
|
id: string
|
|
email: string
|
|
role: string
|
|
status: string
|
|
expires_at: string
|
|
created_at: string
|
|
}
|
|
|
|
const ROLE_LABELS: Record<string, string> = {
|
|
owner: 'Ägare',
|
|
admin: 'Admin',
|
|
member: 'Medlem',
|
|
viewer: 'Läsbehörighet',
|
|
}
|
|
|
|
export function CompanyMembersSection() {
|
|
const { toast } = useToast()
|
|
const { company } = useCompany()
|
|
const [isLoading, setIsLoading] = useState(true)
|
|
const [members, setMembers] = useState<CompanyMemberItem[]>([])
|
|
const [invitations, setInvitations] = useState<CompanyInvitation[]>([])
|
|
const [inviteEmail, setInviteEmail] = useState('')
|
|
const [inviteRole, setInviteRole] = useState<string>('viewer')
|
|
const [isSending, setIsSending] = useState(false)
|
|
const [removingId, setRemovingId] = useState<string | null>(null)
|
|
const [revokingId, setRevokingId] = useState<string | null>(null)
|
|
const [canInvite, setCanInvite] = useState(false)
|
|
|
|
const fetchMembers = useCallback(async () => {
|
|
try {
|
|
const res = await fetch('/api/company/members')
|
|
const data = await res.json()
|
|
if (res.ok) {
|
|
setMembers(data.data.members)
|
|
setInvitations(data.data.invitations)
|
|
setCanInvite(data.data.canInvite)
|
|
}
|
|
} catch {
|
|
// Silently fail
|
|
} finally {
|
|
setIsLoading(false)
|
|
}
|
|
}, [])
|
|
|
|
useEffect(() => {
|
|
fetchMembers()
|
|
}, [fetchMembers])
|
|
|
|
const handleInvite = async (e: React.FormEvent) => {
|
|
e.preventDefault()
|
|
const email = inviteEmail.trim().toLowerCase()
|
|
if (!email) return
|
|
|
|
setIsSending(true)
|
|
try {
|
|
const res = await fetch('/api/company/members/invite', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ email, role: inviteRole }),
|
|
})
|
|
const data = await res.json()
|
|
|
|
if (!res.ok) {
|
|
toast({ title: 'Fel', description: data.error, variant: 'destructive' })
|
|
return
|
|
}
|
|
|
|
if (data.data.inviteUrl) {
|
|
console.log('[DEV] Company invite URL:', data.data.inviteUrl)
|
|
}
|
|
toast({
|
|
title: 'Inbjudan skickad',
|
|
description: data.data.inviteUrl
|
|
? 'Länk loggad i konsolen (F12)'
|
|
: `E-post skickad till ${email}.`,
|
|
})
|
|
setInviteEmail('')
|
|
setInviteRole('viewer')
|
|
fetchMembers()
|
|
} catch {
|
|
toast({ title: 'Fel', description: 'Kunde inte skicka inbjudan.', variant: 'destructive' })
|
|
} finally {
|
|
setIsSending(false)
|
|
}
|
|
}
|
|
|
|
const handleRemoveMember = async (memberId: string) => {
|
|
setRemovingId(memberId)
|
|
try {
|
|
const res = await fetch(`/api/company/members/${memberId}`, { method: 'DELETE' })
|
|
const data = await res.json()
|
|
|
|
if (!res.ok) {
|
|
toast({ title: 'Fel', description: data.error, variant: 'destructive' })
|
|
return
|
|
}
|
|
|
|
toast({ title: 'Medlem borttagen' })
|
|
fetchMembers()
|
|
} catch {
|
|
toast({ title: 'Fel', description: 'Kunde inte ta bort medlem.', variant: 'destructive' })
|
|
} finally {
|
|
setRemovingId(null)
|
|
}
|
|
}
|
|
|
|
const handleRevokeInvite = async (inviteId: string) => {
|
|
setRevokingId(inviteId)
|
|
try {
|
|
const res = await fetch(`/api/company/members/invite/${inviteId}`, { method: 'DELETE' })
|
|
const data = await res.json()
|
|
|
|
if (!res.ok) {
|
|
toast({ title: 'Fel', description: data.error, variant: 'destructive' })
|
|
return
|
|
}
|
|
|
|
toast({ title: 'Inbjudan återkallad' })
|
|
fetchMembers()
|
|
} catch {
|
|
toast({ title: 'Fel', description: 'Kunde inte återkalla inbjudan.', variant: 'destructive' })
|
|
} finally {
|
|
setRevokingId(null)
|
|
}
|
|
}
|
|
|
|
if (isLoading) {
|
|
return (
|
|
<div className="flex items-center justify-center py-8">
|
|
<Loader2 className="h-5 w-5 animate-spin text-muted-foreground" />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-6">
|
|
{/* Invite form — disabled, coming soon */}
|
|
{canInvite && (
|
|
<Card className="relative overflow-hidden">
|
|
<div className="absolute inset-0 bg-background/60 backdrop-blur-[1px] z-10 flex items-center justify-center">
|
|
<Badge variant="secondary" className="text-xs font-medium">
|
|
Kommer snart
|
|
</Badge>
|
|
</div>
|
|
<CardHeader>
|
|
<CardTitle className="text-base">Bjud in till {company?.name}</CardTitle>
|
|
<CardDescription>
|
|
Personen får tillgång till enbart detta företag.
|
|
</CardDescription>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<div className="flex gap-3 pointer-events-none">
|
|
<div className="flex-1">
|
|
<Label htmlFor="company-invite-email" className="sr-only">E-postadress</Label>
|
|
<Input
|
|
id="company-invite-email"
|
|
type="email"
|
|
placeholder="namn@example.com"
|
|
disabled
|
|
/>
|
|
</div>
|
|
<Button disabled>
|
|
<Plus className="h-4 w-4 mr-1.5" />
|
|
Bjud in
|
|
</Button>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
)}
|
|
|
|
{/* Members list */}
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle className="text-base flex items-center gap-2">
|
|
<Users className="h-4 w-4" />
|
|
Medlemmar
|
|
</CardTitle>
|
|
<CardDescription>
|
|
{members.length} {members.length === 1 ? 'medlem' : 'medlemmar'} i {company?.name}
|
|
</CardDescription>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<div className="divide-y divide-border/40">
|
|
{members.map((member) => (
|
|
<div key={member.id} className="flex items-center justify-between py-3 first:pt-0 last:pb-0">
|
|
<div className="flex items-center gap-3 min-w-0">
|
|
<div className="h-8 w-8 rounded-full bg-muted/60 flex items-center justify-center flex-shrink-0">
|
|
<span className="text-xs font-medium text-muted-foreground">
|
|
{member.email.charAt(0).toUpperCase()}
|
|
</span>
|
|
</div>
|
|
<div className="min-w-0">
|
|
<p className="text-sm font-medium truncate">
|
|
{member.email}
|
|
{member.is_current_user && (
|
|
<span className="text-muted-foreground font-normal ml-1">(du)</span>
|
|
)}
|
|
</p>
|
|
<div className="flex items-center gap-1.5">
|
|
<span className="text-xs text-muted-foreground">
|
|
{ROLE_LABELS[member.role] || member.role}
|
|
</span>
|
|
{member.source === 'team' && (
|
|
<Badge variant="outline" className="text-[10px] px-1.5 py-0">
|
|
Team
|
|
</Badge>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
{canInvite && !member.is_current_user && member.role !== 'owner' && member.source !== 'team' && (
|
|
<Button
|
|
variant="ghost"
|
|
size="icon"
|
|
className="h-8 w-8 text-muted-foreground hover:text-destructive"
|
|
onClick={() => handleRemoveMember(member.id)}
|
|
disabled={removingId === member.id}
|
|
>
|
|
{removingId === member.id ? (
|
|
<Loader2 className="h-3.5 w-3.5 animate-spin" />
|
|
) : (
|
|
<Trash2 className="h-3.5 w-3.5" />
|
|
)}
|
|
</Button>
|
|
)}
|
|
</div>
|
|
))}
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
|
|
{/* Pending invitations — hidden while invites are disabled */}
|
|
{false && invitations.length > 0 && (
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle className="text-base">Väntande inbjudningar</CardTitle>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<div className="divide-y divide-border/40">
|
|
{invitations.map((inv) => (
|
|
<div key={inv.id} className="flex items-center justify-between py-3 first:pt-0 last:pb-0">
|
|
<div className="flex items-center gap-3 min-w-0">
|
|
<div className="h-8 w-8 rounded-full bg-muted/60 flex items-center justify-center flex-shrink-0">
|
|
<Mail className="h-3.5 w-3.5 text-muted-foreground" />
|
|
</div>
|
|
<div className="min-w-0">
|
|
<p className="text-sm font-medium truncate">{inv.email}</p>
|
|
<div className="flex items-center gap-1.5 text-xs text-muted-foreground">
|
|
<Clock className="h-3 w-3" />
|
|
<span>
|
|
Går ut {new Date(inv.expires_at).toLocaleDateString('sv-SE')}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div className="flex items-center gap-2">
|
|
<Badge variant="secondary" className="text-xs">
|
|
{ROLE_LABELS[inv.role] || inv.role}
|
|
</Badge>
|
|
{canInvite && (
|
|
<Button
|
|
variant="ghost"
|
|
size="icon"
|
|
className="h-8 w-8 text-muted-foreground hover:text-destructive"
|
|
onClick={() => handleRevokeInvite(inv.id)}
|
|
disabled={revokingId === inv.id}
|
|
>
|
|
{revokingId === inv.id ? (
|
|
<Loader2 className="h-3.5 w-3.5 animate-spin" />
|
|
) : (
|
|
<Trash2 className="h-3.5 w-3.5" />
|
|
)}
|
|
</Button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|