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:
93
repositories/ActivityLogRepository.ts
Normal file
93
repositories/ActivityLogRepository.ts
Normal file
@ -0,0 +1,93 @@
|
||||
import type { SqliteDatabase } from '@/lib/db/sqlite';
|
||||
import type { ActivityLog } from '@/lib/types';
|
||||
import type { Paginated } from '@/repositories/NotificationRepository';
|
||||
|
||||
const DEFAULT_PAGE_SIZE = 20;
|
||||
|
||||
export interface CreateActivityLogInput {
|
||||
projectId: number;
|
||||
actorId: number;
|
||||
action: string;
|
||||
targetType: string;
|
||||
targetId: number | null;
|
||||
metadataJson: string | null;
|
||||
}
|
||||
|
||||
interface ActivityLogRow {
|
||||
id: number;
|
||||
project_id: number;
|
||||
actor_id: number;
|
||||
action: string;
|
||||
target_type: string;
|
||||
target_id: number | null;
|
||||
metadata_json: string | null;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
function mapActivityLog(row: ActivityLogRow): ActivityLog {
|
||||
return {
|
||||
id: row.id,
|
||||
projectId: row.project_id,
|
||||
actorId: row.actor_id,
|
||||
action: row.action,
|
||||
targetType: row.target_type,
|
||||
targetId: row.target_id,
|
||||
metadataJson: row.metadata_json,
|
||||
createdAt: row.created_at,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* activity_logsテーブルへのデータアクセスを担うRepository。
|
||||
*/
|
||||
export class ActivityLogRepository {
|
||||
constructor(private readonly db: SqliteDatabase) {}
|
||||
|
||||
create(input: CreateActivityLogInput): ActivityLog {
|
||||
const now = new Date().toISOString();
|
||||
const result = this.db.execute(
|
||||
`INSERT INTO activity_logs (project_id, actor_id, action, target_type, target_id, metadata_json, created_at)
|
||||
VALUES (@projectId, @actorId, @action, @targetType, @targetId, @metadataJson, @createdAt)`,
|
||||
{
|
||||
projectId: input.projectId,
|
||||
actorId: input.actorId,
|
||||
action: input.action,
|
||||
targetType: input.targetType,
|
||||
targetId: input.targetId,
|
||||
metadataJson: input.metadataJson,
|
||||
createdAt: now,
|
||||
}
|
||||
);
|
||||
return {
|
||||
id: Number(result.lastInsertRowid),
|
||||
projectId: input.projectId,
|
||||
actorId: input.actorId,
|
||||
action: input.action,
|
||||
targetType: input.targetType,
|
||||
targetId: input.targetId,
|
||||
metadataJson: input.metadataJson,
|
||||
createdAt: now,
|
||||
};
|
||||
}
|
||||
|
||||
/** プロジェクト別のアクティビティログ一覧(作成日時降順・ページネーション) */
|
||||
findByProject(
|
||||
projectId: number,
|
||||
page: number = 1,
|
||||
pageSize: number = DEFAULT_PAGE_SIZE
|
||||
): Paginated<ActivityLog> {
|
||||
const offset = (page - 1) * pageSize;
|
||||
const items = this.db.query<ActivityLogRow>(
|
||||
`SELECT * FROM activity_logs
|
||||
WHERE project_id = @projectId
|
||||
ORDER BY created_at DESC, id DESC
|
||||
LIMIT @pageSize OFFSET @offset`,
|
||||
{ projectId, pageSize, offset }
|
||||
);
|
||||
const row = this.db.get<{ count: number }>(
|
||||
'SELECT COUNT(*) AS count FROM activity_logs WHERE project_id = @projectId',
|
||||
{ projectId }
|
||||
);
|
||||
return { items: items.map(mapActivityLog), total: row?.count ?? 0 };
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user