levelled up logging, albeit with a bit of verbosity...

This commit is contained in:
user
2026-03-01 04:36:17 +02:00
parent 596dcc78fc
commit 5bf1148a4f
6 changed files with 731 additions and 277 deletions

View File

@@ -10,7 +10,7 @@ import type {
} from "./data";
import { type Err } from "@pkg/result";
import { notificationErrors } from "./errors";
import { logger } from "@pkg/logger";
import { logDomainEvent } from "@pkg/logger";
export class NotificationRepository {
constructor(private db: Database) {}
@@ -20,7 +20,20 @@ export class NotificationRepository {
filters: NotificationFilters,
pagination: PaginationOptions,
): ResultAsync<PaginatedNotifications, Err> {
logger.info("Getting notifications with filters", { ...fctx, filters });
const startedAt = Date.now();
logDomainEvent({
event: "notifications.list.started",
fctx,
meta: {
hasSearch: Boolean(filters.search),
isRead: filters.isRead,
isArchived: filters.isArchived,
page: pagination.page,
pageSize: pagination.pageSize,
sortBy: pagination.sortBy,
sortOrder: pagination.sortOrder,
},
});
const { userId, isRead, isArchived, type, category, priority, search } =
filters;
@@ -68,8 +81,11 @@ export class NotificationRepository {
return ResultAsync.fromPromise(
this.db.select({ count: count() }).from(notifications).where(whereClause),
(error) => {
logger.error("Failed to get notifications count", {
...fctx,
logDomainEvent({
level: "error",
event: "notifications.list.failed",
fctx,
durationMs: Date.now() - startedAt,
error,
});
return notificationErrors.getNotificationsFailed(
@@ -109,8 +125,11 @@ export class NotificationRepository {
.limit(pageSize)
.offset(offset),
(error) => {
logger.error("Failed to get notifications data", {
...fctx,
logDomainEvent({
level: "error",
event: "notifications.list.failed",
fctx,
durationMs: Date.now() - startedAt,
error,
});
return notificationErrors.getNotificationsFailed(
@@ -120,11 +139,15 @@ export class NotificationRepository {
},
).map((data) => {
const totalPages = Math.ceil(total / pageSize);
logger.info("Retrieved notifications", {
...fctx,
count: data.length,
page,
totalPages,
logDomainEvent({
event: "notifications.list.succeeded",
fctx,
durationMs: Date.now() - startedAt,
meta: {
count: data.length,
page,
totalPages,
},
});
return {
@@ -143,10 +166,11 @@ export class NotificationRepository {
notificationIds: number[],
userId: string,
): ResultAsync<boolean, Err> {
logger.info("Marking notifications as read", {
...fctx,
notificationIds,
userId,
const startedAt = Date.now();
logDomainEvent({
event: "notifications.mark_read.started",
fctx,
meta: { userId, notificationCount: notificationIds.length },
});
return ResultAsync.fromPromise(
@@ -164,8 +188,11 @@ export class NotificationRepository {
),
),
(error) => {
logger.error("Failed to mark notifications as read", {
...fctx,
logDomainEvent({
level: "error",
event: "notifications.mark_read.failed",
fctx,
durationMs: Date.now() - startedAt,
error,
});
return notificationErrors.markAsReadFailed(
@@ -174,9 +201,11 @@ export class NotificationRepository {
);
},
).map(() => {
logger.info("Notifications marked as read successfully", {
...fctx,
notificationIds,
logDomainEvent({
event: "notifications.mark_read.succeeded",
fctx,
durationMs: Date.now() - startedAt,
meta: { notificationCount: notificationIds.length },
});
return true;
});
@@ -187,10 +216,11 @@ export class NotificationRepository {
notificationIds: number[],
userId: string,
): ResultAsync<boolean, Err> {
logger.info("Marking notifications as unread", {
...fctx,
notificationIds,
userId,
const startedAt = Date.now();
logDomainEvent({
event: "notifications.mark_unread.started",
fctx,
meta: { userId, notificationCount: notificationIds.length },
});
return ResultAsync.fromPromise(
@@ -208,8 +238,11 @@ export class NotificationRepository {
),
),
(error) => {
logger.error("Failed to mark notifications as unread", {
...fctx,
logDomainEvent({
level: "error",
event: "notifications.mark_unread.failed",
fctx,
durationMs: Date.now() - startedAt,
error,
});
return notificationErrors.markAsUnreadFailed(
@@ -218,9 +251,11 @@ export class NotificationRepository {
);
},
).map(() => {
logger.info("Notifications marked as unread successfully", {
...fctx,
notificationIds,
logDomainEvent({
event: "notifications.mark_unread.succeeded",
fctx,
durationMs: Date.now() - startedAt,
meta: { notificationCount: notificationIds.length },
});
return true;
});
@@ -231,10 +266,11 @@ export class NotificationRepository {
notificationIds: number[],
userId: string,
): ResultAsync<boolean, Err> {
logger.info("Archiving notifications", {
...fctx,
notificationIds,
userId,
const startedAt = Date.now();
logDomainEvent({
event: "notifications.archive.started",
fctx,
meta: { userId, notificationCount: notificationIds.length },
});
return ResultAsync.fromPromise(
@@ -251,8 +287,11 @@ export class NotificationRepository {
),
),
(error) => {
logger.error("Failed to archive notifications", {
...fctx,
logDomainEvent({
level: "error",
event: "notifications.archive.failed",
fctx,
durationMs: Date.now() - startedAt,
error,
});
return notificationErrors.archiveFailed(
@@ -261,9 +300,11 @@ export class NotificationRepository {
);
},
).map(() => {
logger.info("Notifications archived successfully", {
...fctx,
notificationIds,
logDomainEvent({
event: "notifications.archive.succeeded",
fctx,
durationMs: Date.now() - startedAt,
meta: { notificationCount: notificationIds.length },
});
return true;
});
@@ -274,10 +315,11 @@ export class NotificationRepository {
notificationIds: number[],
userId: string,
): ResultAsync<boolean, Err> {
logger.info("Unarchiving notifications", {
...fctx,
notificationIds,
userId,
const startedAt = Date.now();
logDomainEvent({
event: "notifications.unarchive.started",
fctx,
meta: { userId, notificationCount: notificationIds.length },
});
return ResultAsync.fromPromise(
@@ -294,8 +336,11 @@ export class NotificationRepository {
),
),
(error) => {
logger.error("Failed to unarchive notifications", {
...fctx,
logDomainEvent({
level: "error",
event: "notifications.unarchive.failed",
fctx,
durationMs: Date.now() - startedAt,
error,
});
return notificationErrors.unarchiveFailed(
@@ -304,9 +349,11 @@ export class NotificationRepository {
);
},
).map(() => {
logger.info("Notifications unarchived successfully", {
...fctx,
notificationIds,
logDomainEvent({
event: "notifications.unarchive.succeeded",
fctx,
durationMs: Date.now() - startedAt,
meta: { notificationCount: notificationIds.length },
});
return true;
});
@@ -317,10 +364,11 @@ export class NotificationRepository {
notificationIds: number[],
userId: string,
): ResultAsync<boolean, Err> {
logger.info("Deleting notifications", {
...fctx,
notificationIds,
userId,
const startedAt = Date.now();
logDomainEvent({
event: "notifications.delete.started",
fctx,
meta: { userId, notificationCount: notificationIds.length },
});
return ResultAsync.fromPromise(
@@ -333,8 +381,11 @@ export class NotificationRepository {
),
),
(error) => {
logger.error("Failed to delete notifications", {
...fctx,
logDomainEvent({
level: "error",
event: "notifications.delete.failed",
fctx,
durationMs: Date.now() - startedAt,
error,
});
return notificationErrors.deleteNotificationsFailed(
@@ -343,9 +394,11 @@ export class NotificationRepository {
);
},
).map(() => {
logger.info("Notifications deleted successfully", {
...fctx,
notificationIds,
logDomainEvent({
event: "notifications.delete.succeeded",
fctx,
durationMs: Date.now() - startedAt,
meta: { notificationCount: notificationIds.length },
});
return true;
});
@@ -355,7 +408,12 @@ export class NotificationRepository {
fctx: FlowExecCtx,
userId: string,
): ResultAsync<number, Err> {
logger.info("Getting unread count", { ...fctx, userId });
const startedAt = Date.now();
logDomainEvent({
event: "notifications.unread_count.started",
fctx,
meta: { userId },
});
return ResultAsync.fromPromise(
this.db
@@ -369,7 +427,13 @@ export class NotificationRepository {
),
),
(error) => {
logger.error("Failed to get unread count", { ...fctx, error });
logDomainEvent({
level: "error",
event: "notifications.unread_count.failed",
fctx,
durationMs: Date.now() - startedAt,
error,
});
return notificationErrors.getUnreadCountFailed(
fctx,
error instanceof Error ? error.message : String(error),
@@ -377,7 +441,12 @@ export class NotificationRepository {
},
).map((result) => {
const count = result[0]?.count || 0;
logger.info("Retrieved unread count", { ...fctx, count });
logDomainEvent({
event: "notifications.unread_count.succeeded",
fctx,
durationMs: Date.now() - startedAt,
meta: { count },
});
return count;
});
}