feat: ship Wave 5-6 multi-surface hygiene, optimization, and bug fixes

This commit is contained in:
Yun Chan 2026-11-14 11:51:41 +09:00
parent 01ef3293cf
commit 66b6b45ba9
399 changed files with 17828 additions and 11330 deletions

View file

@ -1,8 +1,14 @@
import { isRecord, isUuid } from '@d3ro/core/guards'
import type { RealtimeChannel } from '@supabase/supabase-js'
import type { Meeting, Team, TeamRole } from '@d3ro/api-client'
import { PUBLIC_SITE_ORIGIN, SITE_URLS } from '@d3ro/core/web-urls'
import { supabase } from '../../lib/supabase'
import { invokeEdgeFunction, type EdgeFunctionFailure } from '../../lib/edge-functions'
import {
createLabeledUuidAssert,
isSupabaseAuthFailure,
isSupabaseNetworkFailure,
} from '../shared/service-guards'
export interface TeamSummary extends Team {
role: TeamRole
@ -96,7 +102,6 @@ export interface TeamSubscription {
unsubscribe: () => Promise<void>
}
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
const EMAIL_PATTERN = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
const INVITE_TOKEN_PATTERN = /^[A-Za-z0-9_-]{32}$/
const TEAM_NAME_MAX_LENGTH = 80
@ -105,9 +110,6 @@ const TRUSTED_INVITE_ORIGIN = PUBLIC_SITE_ORIGIN
// '/accept-invite' — 사이트 정본 URL에서 경로만 떼어 끝 슬래시 유무를 모두 허용한다.
const INVITE_PATH = SITE_URLS.acceptInvite.slice(PUBLIC_SITE_ORIGIN.length).replace(/\/$/, '')
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value)
}
function isIsoDate(value: unknown): value is string {
return typeof value === 'string' && Number.isFinite(Date.parse(value))
@ -122,11 +124,9 @@ function firstRow(value: unknown): unknown {
return value
}
function requireUuid(value: string, label: string): void {
if (!UUID_PATTERN.test(value)) {
throw new TeamServiceError('validation', `${label} is invalid`)
}
}
const requireUuid = createLabeledUuidAssert(
(label) => new TeamServiceError('validation', `${label} is invalid`),
)
export function normalizeTeamName(value: string): string {
const name = value.trim().replace(/\s+/g, ' ')
@ -204,11 +204,11 @@ function normalizeTeam(value: unknown): Team {
const avatarUrl = value.avatar_url ?? null
if (
typeof value.id !== 'string'
|| !UUID_PATTERN.test(value.id)
|| !isUuid(value.id)
|| typeof value.name !== 'string'
|| value.name.trim().length === 0
|| typeof value.owner_id !== 'string'
|| !UUID_PATTERN.test(value.owner_id)
|| !isUuid(value.owner_id)
|| (avatarUrl !== null && typeof avatarUrl !== 'string')
|| !isIsoDate(value.created_at)
|| !isIsoDate(value.updated_at)
@ -236,7 +236,7 @@ export function normalizeTeamMember(value: unknown): TeamMemberDirectoryEntry {
const avatarUrl = value.avatar_url ?? value.avatarUrl ?? null
if (
typeof userId !== 'string'
|| !UUID_PATTERN.test(userId)
|| !isUuid(userId)
|| !isRole(value.role)
|| !isIsoDate(joinedAt)
|| (displayName !== null && typeof displayName !== 'string')
@ -262,7 +262,7 @@ export function normalizeTeamInvite(value: unknown): TeamInviteListEntry {
const invitedBy = value.invited_by ?? value.invitedBy ?? null
if (
typeof value.id !== 'string'
|| !UUID_PATTERN.test(value.id)
|| !isUuid(value.id)
|| typeof value.email !== 'string'
|| (value.role !== 'admin' && value.role !== 'member')
|| !isIsoDate(expiresAt)
@ -305,7 +305,7 @@ export function normalizeTeamActivity(value: unknown): TeamActivityEntry {
const body = value.body ?? null
if (
typeof value.id !== 'string'
|| !UUID_PATTERN.test(value.id)
|| !isUuid(value.id)
|| typeof value.team_id !== 'string'
|| !isActivityKind(value.kind)
|| (actorId !== null && typeof actorId !== 'string')
@ -343,13 +343,12 @@ function toTeamServiceError(error: unknown): TeamServiceError {
if (error instanceof TeamServiceError) return error
const details = errorDetails(error)
const lower = details.message.toLowerCase()
const info = { code: details.code, message: details.message, lower }
const hasCode = (code: string): boolean => details.code === code || lower.includes(code)
if (
error instanceof TypeError
|| lower.includes('network request failed')
|| lower.includes('failed to fetch')
) return new TeamServiceError('network', details.message || 'Team request failed')
if (details.code === 'PGRST301' || lower.includes('jwt') || lower.includes('not authenticated')) {
if (isSupabaseNetworkFailure(error, info)) {
return new TeamServiceError('network', details.message || 'Team request failed')
}
if (isSupabaseAuthFailure(info) || lower.includes('not authenticated')) {
return new TeamServiceError('auth', details.message || 'Authentication is required')
}
if (details.code === 'PGRST202' || lower.includes('could not find the function')) {
@ -545,7 +544,7 @@ export async function createTeamActivity(
p_body: trimmed === '' ? null : trimmed,
p_metadata: {},
})
if (!isRecord(result) || typeof result.id !== 'string' || !UUID_PATTERN.test(result.id)) {
if (!isRecord(result) || !isUuid(result.id)) {
throw new TeamServiceError('invalid-response', 'Team activity response is malformed')
}
return {
@ -720,7 +719,7 @@ export async function createTeamInvite(
const emailError = data.email_error ?? null
if (
typeof data.id !== 'string'
|| !UUID_PATTERN.test(data.id)
|| !isUuid(data.id)
|| typeof data.url !== 'string'
|| !isIsoDate(expiresAt)
|| typeof emailSent !== 'boolean'
@ -760,7 +759,7 @@ export async function acceptTeamInvite(tokenValue: string): Promise<TeamAcceptRe
const alreadyMember = data.already_member ?? false
if (
typeof teamId !== 'string'
|| !UUID_PATTERN.test(teamId)
|| !isUuid(teamId)
|| !isRole(role)
|| typeof alreadyMember !== 'boolean'
) {