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:
Ken Yasue
2026-06-25 01:20:24 +02:00
parent 95722e99f1
commit 3e02feb221
20 changed files with 1197 additions and 3 deletions

View File

@ -9,10 +9,39 @@ export interface CreateNotificationInput {
body: string | null;
}
interface NotificationRow {
id: number;
user_id: number;
project_id: number | null;
type: string;
title: string;
body: string | null;
read_at: string | null;
created_at: string;
}
function mapNotification(row: NotificationRow): Notification {
return {
id: row.id,
userId: row.user_id,
projectId: row.project_id,
type: row.type as NotificationType,
title: row.title,
body: row.body,
readAt: row.read_at,
createdAt: row.created_at,
};
}
export interface Paginated<T> {
items: T[];
total: number;
}
export const DEFAULT_PAGE_SIZE = 20;
/**
* notificationsテーブルへのデータアクセスを担うRepository。
* M4ではメンバー追加通知のために create のみ実装する。
* 一覧取得・既読化は M5 で拡張する。
*/
export class NotificationRepository {
constructor(private readonly db: SqliteDatabase) {}
@ -42,4 +71,41 @@ export class NotificationRepository {
createdAt: now,
};
}
/** ユーザーの未読通知一覧(作成日時降順・ページネーション) */
findUnreadByUser(
userId: number,
page: number = 1,
pageSize: number = DEFAULT_PAGE_SIZE
): Paginated<Notification> {
const offset = (page - 1) * pageSize;
const items = this.db.query<NotificationRow>(
`SELECT * FROM notifications
WHERE user_id = @userId AND read_at IS NULL
ORDER BY created_at DESC, id DESC
LIMIT @pageSize OFFSET @offset`,
{ userId, pageSize, offset }
);
const total = this.countUnreadByUser(userId);
return { items: items.map(mapNotification), total };
}
countUnreadByUser(userId: number): number {
const row = this.db.get<{ count: number }>(
'SELECT COUNT(*) AS count FROM notifications WHERE user_id = @userId AND read_at IS NULL',
{ userId }
);
return row?.count ?? 0;
}
/** 通知を既読にする(本人所有の未読通知のみ) */
markRead(id: number, userId: number): boolean {
const now = new Date().toISOString();
const result = this.db.execute(
`UPDATE notifications SET read_at = @now
WHERE id = @id AND user_id = @userId AND read_at IS NULL`,
{ now, id, userId }
);
return result.changes > 0;
}
}