feat(m5): notifications & activity log foundation
- Expand NotificationRepository (unread list paginated, count, markRead ownership-scoped); new ActivityLogRepository (create, findByProject paginated) with deterministic created_at+id ordering - NotificationService (notifyOnEvent, resolveTargets for 8 event types, injectable SseBroadcaster for M8) + ActivityLogService (logActivity, listByProject) - NotificationEvent/SseEvent/SseBroadcaster types in lib/types - APIs: GET /api/notifications, POST /api/notifications/:id/read, GET /api/projects/:projectId/activity - Screens: notifications list + MarkReadButton, NotificationBadge in Header, project activity page + ProjectNav link - Unit tests for both repositories and services (28 new)
This commit is contained in:
25
app/api/notifications/[id]/read/route.ts
Normal file
25
app/api/notifications/[id]/read/route.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getCurrentUser } from '@/lib/auth/getCurrentUser';
|
||||
import { createNotificationService } from '@/lib/api/services';
|
||||
import { UnauthorizedError, NotFoundError } from '@/lib/errors';
|
||||
import { handleApiError } from '@/lib/api/handleError';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
export async function POST(
|
||||
_request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return handleApiError(new UnauthorizedError());
|
||||
}
|
||||
const { id } = await params;
|
||||
|
||||
const service = createNotificationService();
|
||||
const updated = service.markRead(Number(id), user.id);
|
||||
if (!updated) {
|
||||
return handleApiError(new NotFoundError('Notification', id));
|
||||
}
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
19
app/api/notifications/route.ts
Normal file
19
app/api/notifications/route.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getCurrentUser } from '@/lib/auth/getCurrentUser';
|
||||
import { createNotificationService } from '@/lib/api/services';
|
||||
import { UnauthorizedError } from '@/lib/errors';
|
||||
import { handleApiError } from '@/lib/api/handleError';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return handleApiError(new UnauthorizedError());
|
||||
}
|
||||
|
||||
const page = Number(request.nextUrl.searchParams.get('page') ?? '1') || 1;
|
||||
const service = createNotificationService();
|
||||
const result = service.listUnread(user.id, page);
|
||||
return NextResponse.json(result);
|
||||
}
|
||||
34
app/api/projects/[projectId]/activity/route.ts
Normal file
34
app/api/projects/[projectId]/activity/route.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getCurrentUser } from '@/lib/auth/getCurrentUser';
|
||||
import {
|
||||
createProjectService,
|
||||
createActivityLogService,
|
||||
} from '@/lib/api/services';
|
||||
import { UnauthorizedError } from '@/lib/errors';
|
||||
import { handleApiError } from '@/lib/api/handleError';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ projectId: string }> }
|
||||
) {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return handleApiError(new UnauthorizedError());
|
||||
}
|
||||
const { projectId } = await params;
|
||||
|
||||
// メンバーシップ確認(非参加者は403)
|
||||
const projectService = createProjectService();
|
||||
try {
|
||||
projectService.getProject(user.id, Number(projectId));
|
||||
} catch (error) {
|
||||
return handleApiError(error);
|
||||
}
|
||||
|
||||
const page = Number(request.nextUrl.searchParams.get('page') ?? '1') || 1;
|
||||
const activityService = createActivityLogService();
|
||||
const result = activityService.listByProject(Number(projectId), page);
|
||||
return NextResponse.json(result);
|
||||
}
|
||||
27
app/notifications/page.tsx
Normal file
27
app/notifications/page.tsx
Normal file
@ -0,0 +1,27 @@
|
||||
import { redirect } from 'next/navigation';
|
||||
import { getCurrentUser, toPublicUser } from '@/lib/auth/getCurrentUser';
|
||||
import { createNotificationService } from '@/lib/api/services';
|
||||
import { Header } from '@/components/layout/Header';
|
||||
import { NotificationList } from '@/components/notifications/NotificationList';
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
export default async function NotificationsPage() {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
redirect('/login');
|
||||
}
|
||||
|
||||
const service = createNotificationService();
|
||||
const { items } = service.listUnread(user.id, 1);
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-50">
|
||||
<Header user={toPublicUser(user)} />
|
||||
<main className="mx-auto max-w-2xl space-y-6 p-6">
|
||||
<h1 className="text-2xl font-bold">通知</h1>
|
||||
<NotificationList notifications={items} />
|
||||
</main>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
81
app/projects/[projectId]/activity/page.tsx
Normal file
81
app/projects/[projectId]/activity/page.tsx
Normal file
@ -0,0 +1,81 @@
|
||||
import { redirect } from 'next/navigation';
|
||||
import { getCurrentUser, toPublicUser } from '@/lib/auth/getCurrentUser';
|
||||
import {
|
||||
createProjectService,
|
||||
createActivityLogService,
|
||||
} from '@/lib/api/services';
|
||||
import { Header } from '@/components/layout/Header';
|
||||
import { ProjectNav } from '@/components/layout/ProjectNav';
|
||||
import { ForbiddenError, NotFoundError } from '@/lib/errors';
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
const ACTION_LABELS: Record<string, string> = {
|
||||
todo_created: 'ToDo作成',
|
||||
todo_updated: 'ToDo更新',
|
||||
todo_completed: 'ToDo完了',
|
||||
file_uploaded: 'ファイルアップロード',
|
||||
board_posted: '掲示板投稿',
|
||||
comment_added: 'コメント追加',
|
||||
note_created: 'メモ作成',
|
||||
note_updated: 'メモ更新',
|
||||
meeting_created: 'ミーティング作成',
|
||||
member_added: 'メンバー追加',
|
||||
milestone_updated: 'マイルストーン更新',
|
||||
};
|
||||
|
||||
export default async function ProjectActivityPage({
|
||||
params,
|
||||
}: {
|
||||
params: Promise<{ projectId: string }>;
|
||||
}) {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
redirect('/login');
|
||||
}
|
||||
const { projectId } = await params;
|
||||
|
||||
const projectService = createProjectService();
|
||||
let project;
|
||||
try {
|
||||
project = projectService.getProject(user.id, Number(projectId));
|
||||
} catch (error) {
|
||||
if (error instanceof ForbiddenError || error instanceof NotFoundError) {
|
||||
redirect('/dashboard');
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
const activityService = createActivityLogService();
|
||||
const { items } = activityService.listByProject(project.id, 1);
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-50">
|
||||
<Header user={toPublicUser(user)} />
|
||||
<ProjectNav projectId={project.id} active="activity" />
|
||||
<main className="mx-auto max-w-3xl space-y-6 p-6">
|
||||
<h1 className="text-2xl font-bold">アクティビティログ</h1>
|
||||
{items.length === 0 ? (
|
||||
<p className="text-sm text-gray-400">アクティビティはありません。</p>
|
||||
) : (
|
||||
<ul className="divide-y rounded-lg border bg-white shadow-sm">
|
||||
{items.map((log) => (
|
||||
<li key={log.id} className="p-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="rounded bg-gray-100 px-2 py-0.5 text-xs">
|
||||
{ACTION_LABELS[log.action] ?? log.action}
|
||||
</span>
|
||||
<span className="text-xs text-gray-400">{log.createdAt}</span>
|
||||
</div>
|
||||
<p className="mt-2 text-sm text-gray-700">
|
||||
{log.targetType}
|
||||
{log.targetId !== null ? ` #${log.targetId}` : ''}
|
||||
</p>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</main>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user