levelled up logging, albeit with a bit of verbosity...
This commit is contained in:
@@ -88,6 +88,126 @@ const logger = winston.createLogger({
|
|||||||
|
|
||||||
const stream = { write: (message: string) => logger.http(message.trim()) };
|
const stream = { write: (message: string) => logger.http(message.trim()) };
|
||||||
|
|
||||||
|
type LogLevel = keyof typeof levels;
|
||||||
|
type ErrorKind = "validation" | "auth" | "db" | "external" | "unknown";
|
||||||
|
type FlowCtxLike = {
|
||||||
|
flowId: string;
|
||||||
|
userId?: string;
|
||||||
|
sessionId?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
const REDACTED_KEYS = new Set([
|
||||||
|
"password",
|
||||||
|
"code",
|
||||||
|
"secret",
|
||||||
|
"token",
|
||||||
|
"verificationtoken",
|
||||||
|
"backupcodes",
|
||||||
|
"authorization",
|
||||||
|
"headers",
|
||||||
|
"hash",
|
||||||
|
]);
|
||||||
|
|
||||||
|
function sanitizeMeta(input: Record<string, unknown>) {
|
||||||
|
const sanitized = Object.fromEntries(
|
||||||
|
Object.entries(input).map(([key, value]) => {
|
||||||
|
if (value === undefined) {
|
||||||
|
return [key, undefined];
|
||||||
|
}
|
||||||
|
const lowered = key.toLowerCase();
|
||||||
|
if (REDACTED_KEYS.has(lowered)) {
|
||||||
|
return [key, "[REDACTED]"];
|
||||||
|
}
|
||||||
|
return [key, value];
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
return Object.fromEntries(
|
||||||
|
Object.entries(sanitized).filter(([, value]) => value !== undefined),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function classifyError(error: unknown): ErrorKind {
|
||||||
|
if (!error) return "unknown";
|
||||||
|
if (typeof error === "object" && error && "code" in error) {
|
||||||
|
const code = String((error as { code?: unknown }).code ?? "").toUpperCase();
|
||||||
|
if (code.includes("AUTH") || code.includes("UNAUTHORIZED")) return "auth";
|
||||||
|
if (code.includes("VALIDATION") || code.includes("INVALID")) return "validation";
|
||||||
|
if (code.includes("DB") || code.includes("DATABASE")) return "db";
|
||||||
|
}
|
||||||
|
return "unknown";
|
||||||
|
}
|
||||||
|
|
||||||
|
function errorMessage(error: unknown) {
|
||||||
|
if (error instanceof Error) return error.message;
|
||||||
|
if (typeof error === "string") return error;
|
||||||
|
if (error && typeof error === "object" && "message" in error) {
|
||||||
|
return String((error as { message?: unknown }).message ?? "Unknown error");
|
||||||
|
}
|
||||||
|
return "Unknown error";
|
||||||
|
}
|
||||||
|
|
||||||
|
function writeLog(level: LogLevel, message: string, payload: Record<string, unknown>) {
|
||||||
|
switch (level) {
|
||||||
|
case "error":
|
||||||
|
logger.error(message, payload);
|
||||||
|
return;
|
||||||
|
case "warn":
|
||||||
|
logger.warn(message, payload);
|
||||||
|
return;
|
||||||
|
case "debug":
|
||||||
|
logger.debug(message, payload);
|
||||||
|
return;
|
||||||
|
case "http":
|
||||||
|
logger.http(message, payload);
|
||||||
|
return;
|
||||||
|
default:
|
||||||
|
logger.info(message, payload);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function logDomainEvent({
|
||||||
|
level = "info",
|
||||||
|
event,
|
||||||
|
fctx,
|
||||||
|
durationMs,
|
||||||
|
error,
|
||||||
|
retryable,
|
||||||
|
meta,
|
||||||
|
}: {
|
||||||
|
level?: LogLevel;
|
||||||
|
event: string;
|
||||||
|
fctx: FlowCtxLike;
|
||||||
|
durationMs?: number;
|
||||||
|
error?: unknown;
|
||||||
|
retryable?: boolean;
|
||||||
|
meta?: Record<string, unknown>;
|
||||||
|
}) {
|
||||||
|
const payload: Record<string, unknown> = {
|
||||||
|
event,
|
||||||
|
flowId: fctx.flowId,
|
||||||
|
userId: fctx.userId,
|
||||||
|
sessionId: fctx.sessionId,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (durationMs !== undefined) payload.duration_ms = durationMs;
|
||||||
|
if (retryable !== undefined) payload.retryable = retryable;
|
||||||
|
if (error !== undefined) {
|
||||||
|
payload.error_kind = classifyError(error);
|
||||||
|
payload.error_message = errorMessage(error);
|
||||||
|
if (error && typeof error === "object" && "code" in error) {
|
||||||
|
payload.error_code = String(
|
||||||
|
(error as { code?: unknown }).code ?? "UNKNOWN",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (meta) {
|
||||||
|
Object.assign(payload, sanitizeMeta(meta));
|
||||||
|
}
|
||||||
|
|
||||||
|
writeLog(level, event, payload);
|
||||||
|
}
|
||||||
|
|
||||||
function getError(payload: Err, error?: any) {
|
function getError(payload: Err, error?: any) {
|
||||||
logger.error(JSON.stringify({ payload, error }, null, 2));
|
logger.error(JSON.stringify({ payload, error }, null, 2));
|
||||||
return {
|
return {
|
||||||
@@ -100,4 +220,4 @@ function getError(payload: Err, error?: any) {
|
|||||||
} as Err;
|
} as Err;
|
||||||
}
|
}
|
||||||
|
|
||||||
export { getError, logger, stream };
|
export { getError, logDomainEvent, logger, stream };
|
||||||
|
|||||||
@@ -3,13 +3,13 @@ import { FlowExecCtx } from "@core/flow.execution.context";
|
|||||||
import { UserRepository } from "@domains/user/repository";
|
import { UserRepository } from "@domains/user/repository";
|
||||||
import { getRedisInstance, Redis } from "@pkg/keystore";
|
import { getRedisInstance, Redis } from "@pkg/keystore";
|
||||||
import { TwofaRepository } from "./repository";
|
import { TwofaRepository } from "./repository";
|
||||||
|
import { logDomainEvent } from "@pkg/logger";
|
||||||
import { auth } from "../auth/config.base";
|
import { auth } from "../auth/config.base";
|
||||||
import type { TwoFaSession } from "./data";
|
import type { TwoFaSession } from "./data";
|
||||||
import { User } from "@domains/user/data";
|
import { User } from "@domains/user/data";
|
||||||
import { settings } from "@core/settings";
|
import { settings } from "@core/settings";
|
||||||
import { type Err } from "@pkg/result";
|
import { type Err } from "@pkg/result";
|
||||||
import { twofaErrors } from "./errors";
|
import { twofaErrors } from "./errors";
|
||||||
import { logger } from "@pkg/logger";
|
|
||||||
import { db } from "@pkg/db";
|
import { db } from "@pkg/db";
|
||||||
|
|
||||||
export class TwofaController {
|
export class TwofaController {
|
||||||
@@ -33,7 +33,13 @@ export class TwofaController {
|
|||||||
|
|
||||||
isUserBanned(fctx: FlowExecCtx, userId: string) {
|
isUserBanned(fctx: FlowExecCtx, userId: string) {
|
||||||
return this.userRepo.isUserBanned(fctx, userId).orElse((error) => {
|
return this.userRepo.isUserBanned(fctx, userId).orElse((error) => {
|
||||||
logger.error("Error checking user ban status:", error);
|
logDomainEvent({
|
||||||
|
level: "error",
|
||||||
|
event: "security.twofa.user_ban_check.failed",
|
||||||
|
fctx,
|
||||||
|
error,
|
||||||
|
meta: { userId },
|
||||||
|
});
|
||||||
return okAsync(false);
|
return okAsync(false);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -58,19 +64,34 @@ export class TwofaController {
|
|||||||
code: string,
|
code: string,
|
||||||
headers: Headers,
|
headers: Headers,
|
||||||
) {
|
) {
|
||||||
|
const startedAt = Date.now();
|
||||||
|
logDomainEvent({
|
||||||
|
event: "security.twofa.verify_and_enable.started",
|
||||||
|
fctx,
|
||||||
|
meta: { userId: user.id },
|
||||||
|
});
|
||||||
|
|
||||||
return this.is2faEnabled(fctx, user.id)
|
return this.is2faEnabled(fctx, user.id)
|
||||||
.andThen((enabled) => {
|
.andThen((enabled) => {
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
|
logDomainEvent({
|
||||||
|
level: "warn",
|
||||||
|
event: "security.twofa.verify_and_enable.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
error: {
|
||||||
|
code: "ALREADY_ENABLED",
|
||||||
|
message: "2FA already enabled",
|
||||||
|
},
|
||||||
|
meta: { userId: user.id },
|
||||||
|
});
|
||||||
return errAsync(twofaErrors.alreadyEnabled(fctx));
|
return errAsync(twofaErrors.alreadyEnabled(fctx));
|
||||||
}
|
}
|
||||||
return okAsync(undefined);
|
return okAsync(undefined);
|
||||||
})
|
})
|
||||||
.andThen(() => {
|
.andThen(() =>
|
||||||
logger.info(`Verifying 2fa for ${user.id} : ${code}`, {
|
this.twofaRepo.verifyAndEnable2FA(fctx, user.id, code),
|
||||||
flowId: fctx.flowId,
|
)
|
||||||
});
|
|
||||||
return this.twofaRepo.verifyAndEnable2FA(fctx, user.id, code);
|
|
||||||
})
|
|
||||||
.andThen((verified) => {
|
.andThen((verified) => {
|
||||||
if (verified) {
|
if (verified) {
|
||||||
return ResultAsync.combine([
|
return ResultAsync.combine([
|
||||||
@@ -82,8 +103,27 @@ export class TwofaController {
|
|||||||
fctx,
|
fctx,
|
||||||
user.id,
|
user.id,
|
||||||
),
|
),
|
||||||
]).map(() => true);
|
]).map(() => {
|
||||||
|
logDomainEvent({
|
||||||
|
event: "security.twofa.verify_and_enable.succeeded",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
meta: { userId: user.id },
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
logDomainEvent({
|
||||||
|
level: "warn",
|
||||||
|
event: "security.twofa.verify_and_enable.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
error: {
|
||||||
|
code: "INVALID_CODE",
|
||||||
|
message: "2FA code verification failed",
|
||||||
|
},
|
||||||
|
meta: { userId: user.id },
|
||||||
|
});
|
||||||
return okAsync(verified);
|
return okAsync(verified);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -169,7 +209,12 @@ export class TwofaController {
|
|||||||
)
|
)
|
||||||
.map(() => undefined)
|
.map(() => undefined)
|
||||||
.orElse((error) => {
|
.orElse((error) => {
|
||||||
logger.error("Error marking initial 2FA as complete:", error);
|
logDomainEvent({
|
||||||
|
level: "error",
|
||||||
|
event: "security.twofa.mark_initial_verification.failed",
|
||||||
|
fctx: { flowId: crypto.randomUUID() },
|
||||||
|
error,
|
||||||
|
});
|
||||||
return okAsync(undefined);
|
return okAsync(undefined);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import { settings } from "@core/settings";
|
|||||||
import type { Err } from "@pkg/result";
|
import type { Err } from "@pkg/result";
|
||||||
import { twofaErrors } from "./errors";
|
import { twofaErrors } from "./errors";
|
||||||
import { Redis } from "@pkg/keystore";
|
import { Redis } from "@pkg/keystore";
|
||||||
import { logger } from "@pkg/logger";
|
import { logDomainEvent, logger } from "@pkg/logger";
|
||||||
import { nanoid } from "nanoid";
|
import { nanoid } from "nanoid";
|
||||||
|
|
||||||
type TwoFaSetup = {
|
type TwoFaSetup = {
|
||||||
@@ -53,25 +53,49 @@ export class TwofaRepository {
|
|||||||
userId: string,
|
userId: string,
|
||||||
returnUndefined?: boolean,
|
returnUndefined?: boolean,
|
||||||
): ResultAsync<TwoFactor | undefined, Err> {
|
): ResultAsync<TwoFactor | undefined, Err> {
|
||||||
logger.info("Getting user 2FA info", { ...fctx, userId });
|
const startedAt = Date.now();
|
||||||
|
logDomainEvent({
|
||||||
|
event: "security.twofa.get_info.started",
|
||||||
|
fctx,
|
||||||
|
meta: { userId },
|
||||||
|
});
|
||||||
|
|
||||||
return ResultAsync.fromPromise(
|
return ResultAsync.fromPromise(
|
||||||
this.db.query.twoFactor.findFirst({
|
this.db.query.twoFactor.findFirst({
|
||||||
where: eq(twoFactor.userId, userId),
|
where: eq(twoFactor.userId, userId),
|
||||||
}),
|
}),
|
||||||
() => twofaErrors.dbError(fctx, "Failed to query 2FA info"),
|
(error) => {
|
||||||
|
logDomainEvent({
|
||||||
|
level: "error",
|
||||||
|
event: "security.twofa.get_info.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
error,
|
||||||
|
meta: { userId },
|
||||||
|
});
|
||||||
|
return twofaErrors.dbError(fctx, "Failed to query 2FA info");
|
||||||
|
},
|
||||||
).andThen((found) => {
|
).andThen((found) => {
|
||||||
if (!found) {
|
if (!found) {
|
||||||
logger.debug("2FA info not found for user", {
|
logDomainEvent({
|
||||||
...fctx,
|
level: "warn",
|
||||||
userId,
|
event: "security.twofa.get_info.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
error: { code: "NOT_FOUND", message: "2FA info not found" },
|
||||||
|
meta: { userId },
|
||||||
});
|
});
|
||||||
if (returnUndefined) {
|
if (returnUndefined) {
|
||||||
return okAsync(undefined);
|
return okAsync(undefined);
|
||||||
}
|
}
|
||||||
return errAsync(twofaErrors.notFound(fctx));
|
return errAsync(twofaErrors.notFound(fctx));
|
||||||
}
|
}
|
||||||
logger.info("2FA info retrieved successfully", { ...fctx, userId });
|
logDomainEvent({
|
||||||
|
event: "security.twofa.get_info.succeeded",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
meta: { userId },
|
||||||
|
});
|
||||||
return okAsync(found as TwoFactor);
|
return okAsync(found as TwoFactor);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -105,7 +129,12 @@ export class TwofaRepository {
|
|||||||
userId: string,
|
userId: string,
|
||||||
secret: string,
|
secret: string,
|
||||||
): ResultAsync<string, Err> {
|
): ResultAsync<string, Err> {
|
||||||
logger.info("Starting 2FA setup", { ...fctx, userId });
|
const startedAt = Date.now();
|
||||||
|
logDomainEvent({
|
||||||
|
event: "security.twofa.setup.started",
|
||||||
|
fctx,
|
||||||
|
meta: { userId },
|
||||||
|
});
|
||||||
|
|
||||||
return ResultAsync.fromSafePromise(
|
return ResultAsync.fromSafePromise(
|
||||||
(async () => {
|
(async () => {
|
||||||
@@ -124,16 +153,25 @@ export class TwofaRepository {
|
|||||||
this.EXPIRY_TIME,
|
this.EXPIRY_TIME,
|
||||||
JSON.stringify(payload),
|
JSON.stringify(payload),
|
||||||
);
|
);
|
||||||
logger.info("Created temp 2FA session", {
|
logDomainEvent({
|
||||||
...fctx,
|
event: "security.twofa.setup.succeeded",
|
||||||
userId,
|
fctx,
|
||||||
expiresIn: this.EXPIRY_TIME,
|
durationMs: Date.now() - startedAt,
|
||||||
|
meta: { userId, expiresInSec: this.EXPIRY_TIME },
|
||||||
});
|
});
|
||||||
return secret;
|
return secret;
|
||||||
})(),
|
})(),
|
||||||
).mapErr(() =>
|
).mapErr((error) => {
|
||||||
twofaErrors.dbError(fctx, "Setting to data store failed"),
|
logDomainEvent({
|
||||||
);
|
level: "error",
|
||||||
|
event: "security.twofa.setup.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
error,
|
||||||
|
meta: { userId },
|
||||||
|
});
|
||||||
|
return twofaErrors.dbError(fctx, "Setting to data store failed");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
verifyAndEnable2FA(
|
verifyAndEnable2FA(
|
||||||
@@ -141,7 +179,12 @@ export class TwofaRepository {
|
|||||||
userId: string,
|
userId: string,
|
||||||
code: string,
|
code: string,
|
||||||
): ResultAsync<boolean, Err> {
|
): ResultAsync<boolean, Err> {
|
||||||
logger.info("Verifying and enabling 2FA", { ...fctx, userId });
|
const startedAt = Date.now();
|
||||||
|
logDomainEvent({
|
||||||
|
event: "security.twofa.verify_enable.started",
|
||||||
|
fctx,
|
||||||
|
meta: { userId },
|
||||||
|
});
|
||||||
|
|
||||||
return ResultAsync.fromPromise(
|
return ResultAsync.fromPromise(
|
||||||
this.store.get(this.getKey(userId)),
|
this.store.get(this.getKey(userId)),
|
||||||
@@ -149,9 +192,16 @@ export class TwofaRepository {
|
|||||||
)
|
)
|
||||||
.andThen((payload) => {
|
.andThen((payload) => {
|
||||||
if (!payload) {
|
if (!payload) {
|
||||||
logger.error("Setup session not found", {
|
logDomainEvent({
|
||||||
...fctx,
|
level: "warn",
|
||||||
userId,
|
event: "security.twofa.verify_enable.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
error: {
|
||||||
|
code: "SETUP_NOT_FOUND",
|
||||||
|
message: "2FA setup session not found",
|
||||||
|
},
|
||||||
|
meta: { userId },
|
||||||
});
|
});
|
||||||
return errAsync(twofaErrors.setupNotFound(fctx));
|
return errAsync(twofaErrors.setupNotFound(fctx));
|
||||||
}
|
}
|
||||||
@@ -161,10 +211,16 @@ export class TwofaRepository {
|
|||||||
const key = this.getKey(userId);
|
const key = this.getKey(userId);
|
||||||
|
|
||||||
if (payloadObj.tries >= this.MAX_SETUP_ATTEMPTS) {
|
if (payloadObj.tries >= this.MAX_SETUP_ATTEMPTS) {
|
||||||
logger.warn("Max setup attempts reached", {
|
logDomainEvent({
|
||||||
...fctx,
|
level: "warn",
|
||||||
userId,
|
event: "security.twofa.verify_enable.failed",
|
||||||
tries: payloadObj.tries,
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
error: {
|
||||||
|
code: "MAX_ATTEMPTS_REACHED",
|
||||||
|
message: "Max setup attempts reached",
|
||||||
|
},
|
||||||
|
meta: { userId, attempts: payloadObj.tries },
|
||||||
});
|
});
|
||||||
return ResultAsync.fromPromise(this.store.del(key), () =>
|
return ResultAsync.fromPromise(this.store.del(key), () =>
|
||||||
twofaErrors.dbError(
|
twofaErrors.dbError(
|
||||||
@@ -180,11 +236,20 @@ export class TwofaRepository {
|
|||||||
!this.checkTotp(payloadObj.secret, code) ||
|
!this.checkTotp(payloadObj.secret, code) ||
|
||||||
code === payloadObj.lastUsedCode
|
code === payloadObj.lastUsedCode
|
||||||
) {
|
) {
|
||||||
logger.warn("Invalid 2FA code during setup", {
|
logDomainEvent({
|
||||||
...fctx,
|
level: "warn",
|
||||||
userId,
|
event: "security.twofa.verify_enable.failed",
|
||||||
tries: payloadObj.tries + 1,
|
fctx,
|
||||||
codeReused: code === payloadObj.lastUsedCode,
|
durationMs: Date.now() - startedAt,
|
||||||
|
error: {
|
||||||
|
code: "INVALID_CODE",
|
||||||
|
message: "Invalid or replayed setup code",
|
||||||
|
},
|
||||||
|
meta: {
|
||||||
|
userId,
|
||||||
|
attempts: payloadObj.tries + 1,
|
||||||
|
codeReused: code === payloadObj.lastUsedCode,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
return ResultAsync.fromPromise(
|
return ResultAsync.fromPromise(
|
||||||
this.store.setex(
|
this.store.setex(
|
||||||
@@ -232,9 +297,11 @@ export class TwofaRepository {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
.map(() => {
|
.map(() => {
|
||||||
logger.info("2FA enabled successfully", {
|
logDomainEvent({
|
||||||
...fctx,
|
event: "security.twofa.verify_enable.succeeded",
|
||||||
userId,
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
meta: { userId },
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
@@ -368,10 +435,11 @@ export class TwofaRepository {
|
|||||||
userAgent?: string;
|
userAgent?: string;
|
||||||
},
|
},
|
||||||
): ResultAsync<TwoFaSession, Err> {
|
): ResultAsync<TwoFaSession, Err> {
|
||||||
logger.info("Creating 2FA verification session", {
|
const startedAt = Date.now();
|
||||||
...fctx,
|
logDomainEvent({
|
||||||
userId: params.userId,
|
event: "security.twofa.create_session.started",
|
||||||
sessionId: params.sessionId,
|
fctx,
|
||||||
|
meta: { userId: params.userId, sessionId: params.sessionId },
|
||||||
});
|
});
|
||||||
|
|
||||||
return ResultAsync.fromSafePromise(
|
return ResultAsync.fromSafePromise(
|
||||||
@@ -402,12 +470,29 @@ export class TwofaRepository {
|
|||||||
userAgent: params.userAgent,
|
userAgent: params.userAgent,
|
||||||
})
|
})
|
||||||
.returning(),
|
.returning(),
|
||||||
() => twofaErrors.dbError(fctx, "Failed to create 2FA session"),
|
(error) => {
|
||||||
|
logDomainEvent({
|
||||||
|
level: "error",
|
||||||
|
event: "security.twofa.create_session.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
error,
|
||||||
|
meta: { userId: params.userId },
|
||||||
|
});
|
||||||
|
return twofaErrors.dbError(
|
||||||
|
fctx,
|
||||||
|
"Failed to create 2FA session",
|
||||||
|
);
|
||||||
|
},
|
||||||
).map(([session]) => {
|
).map(([session]) => {
|
||||||
logger.info("2FA verification session created", {
|
logDomainEvent({
|
||||||
...fctx,
|
event: "security.twofa.create_session.succeeded",
|
||||||
sessionId: session.id,
|
fctx,
|
||||||
userId: params.userId,
|
durationMs: Date.now() - startedAt,
|
||||||
|
meta: {
|
||||||
|
twofaSessionId: session.id,
|
||||||
|
userId: params.userId,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
return session as TwoFaSession;
|
return session as TwoFaSession;
|
||||||
}),
|
}),
|
||||||
@@ -418,7 +503,12 @@ export class TwofaRepository {
|
|||||||
fctx: FlowExecCtx,
|
fctx: FlowExecCtx,
|
||||||
token: string,
|
token: string,
|
||||||
): ResultAsync<TwoFaSession | null, Err> {
|
): ResultAsync<TwoFaSession | null, Err> {
|
||||||
logger.debug("Getting 2FA session by token", { ...fctx });
|
const startedAt = Date.now();
|
||||||
|
logDomainEvent({
|
||||||
|
level: "debug",
|
||||||
|
event: "security.twofa.get_session.started",
|
||||||
|
fctx,
|
||||||
|
});
|
||||||
|
|
||||||
return ResultAsync.fromPromise(
|
return ResultAsync.fromPromise(
|
||||||
this.db
|
this.db
|
||||||
@@ -431,15 +521,36 @@ export class TwofaRepository {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
.limit(1),
|
.limit(1),
|
||||||
() => twofaErrors.dbError(fctx, "Failed to query 2FA session"),
|
(error) => {
|
||||||
|
logDomainEvent({
|
||||||
|
level: "error",
|
||||||
|
event: "security.twofa.get_session.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
error,
|
||||||
|
});
|
||||||
|
return twofaErrors.dbError(fctx, "Failed to query 2FA session");
|
||||||
|
},
|
||||||
).map((result) => {
|
).map((result) => {
|
||||||
if (!result.length) {
|
if (!result.length) {
|
||||||
logger.warn("2FA session not found or expired", { ...fctx });
|
logDomainEvent({
|
||||||
|
level: "warn",
|
||||||
|
event: "security.twofa.get_session.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
error: {
|
||||||
|
code: "SESSION_NOT_FOUND",
|
||||||
|
message: "2FA session not found or expired",
|
||||||
|
},
|
||||||
|
});
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
logger.debug("2FA session found", {
|
logDomainEvent({
|
||||||
...fctx,
|
level: "debug",
|
||||||
sessionId: result[0].id,
|
event: "security.twofa.get_session.succeeded",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
meta: { twofaSessionId: result[0].id },
|
||||||
});
|
});
|
||||||
return result[0] as TwoFaSession;
|
return result[0] as TwoFaSession;
|
||||||
});
|
});
|
||||||
@@ -547,17 +658,37 @@ export class TwofaRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cleanupExpiredSessions(fctx: FlowExecCtx): ResultAsync<number, Err> {
|
cleanupExpiredSessions(fctx: FlowExecCtx): ResultAsync<number, Err> {
|
||||||
logger.info("Cleaning up expired 2FA sessions", { ...fctx });
|
const startedAt = Date.now();
|
||||||
|
logDomainEvent({
|
||||||
|
event: "security.twofa.cleanup_expired.started",
|
||||||
|
fctx,
|
||||||
|
});
|
||||||
|
|
||||||
return ResultAsync.fromPromise(
|
return ResultAsync.fromPromise(
|
||||||
this.db
|
this.db
|
||||||
.delete(twofaSessions)
|
.delete(twofaSessions)
|
||||||
.where(lt(twofaSessions.expiresAt, new Date())),
|
.where(lt(twofaSessions.expiresAt, new Date())),
|
||||||
() =>
|
(error) => {
|
||||||
twofaErrors.dbError(fctx, "Failed to cleanup expired sessions"),
|
logDomainEvent({
|
||||||
|
level: "error",
|
||||||
|
event: "security.twofa.cleanup_expired.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
error,
|
||||||
|
});
|
||||||
|
return twofaErrors.dbError(
|
||||||
|
fctx,
|
||||||
|
"Failed to cleanup expired sessions",
|
||||||
|
);
|
||||||
|
},
|
||||||
).map((result) => {
|
).map((result) => {
|
||||||
const count = result.length || 0;
|
const count = result.length || 0;
|
||||||
logger.info("Expired sessions cleaned up", { ...fctx, count });
|
logDomainEvent({
|
||||||
|
event: "security.twofa.cleanup_expired.succeeded",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
meta: { count },
|
||||||
|
});
|
||||||
return count;
|
return count;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import type {
|
|||||||
} from "./data";
|
} from "./data";
|
||||||
import { type Err } from "@pkg/result";
|
import { type Err } from "@pkg/result";
|
||||||
import { notificationErrors } from "./errors";
|
import { notificationErrors } from "./errors";
|
||||||
import { logger } from "@pkg/logger";
|
import { logDomainEvent } from "@pkg/logger";
|
||||||
|
|
||||||
export class NotificationRepository {
|
export class NotificationRepository {
|
||||||
constructor(private db: Database) {}
|
constructor(private db: Database) {}
|
||||||
@@ -20,7 +20,20 @@ export class NotificationRepository {
|
|||||||
filters: NotificationFilters,
|
filters: NotificationFilters,
|
||||||
pagination: PaginationOptions,
|
pagination: PaginationOptions,
|
||||||
): ResultAsync<PaginatedNotifications, Err> {
|
): 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 } =
|
const { userId, isRead, isArchived, type, category, priority, search } =
|
||||||
filters;
|
filters;
|
||||||
@@ -68,8 +81,11 @@ export class NotificationRepository {
|
|||||||
return ResultAsync.fromPromise(
|
return ResultAsync.fromPromise(
|
||||||
this.db.select({ count: count() }).from(notifications).where(whereClause),
|
this.db.select({ count: count() }).from(notifications).where(whereClause),
|
||||||
(error) => {
|
(error) => {
|
||||||
logger.error("Failed to get notifications count", {
|
logDomainEvent({
|
||||||
...fctx,
|
level: "error",
|
||||||
|
event: "notifications.list.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
error,
|
error,
|
||||||
});
|
});
|
||||||
return notificationErrors.getNotificationsFailed(
|
return notificationErrors.getNotificationsFailed(
|
||||||
@@ -109,8 +125,11 @@ export class NotificationRepository {
|
|||||||
.limit(pageSize)
|
.limit(pageSize)
|
||||||
.offset(offset),
|
.offset(offset),
|
||||||
(error) => {
|
(error) => {
|
||||||
logger.error("Failed to get notifications data", {
|
logDomainEvent({
|
||||||
...fctx,
|
level: "error",
|
||||||
|
event: "notifications.list.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
error,
|
error,
|
||||||
});
|
});
|
||||||
return notificationErrors.getNotificationsFailed(
|
return notificationErrors.getNotificationsFailed(
|
||||||
@@ -120,11 +139,15 @@ export class NotificationRepository {
|
|||||||
},
|
},
|
||||||
).map((data) => {
|
).map((data) => {
|
||||||
const totalPages = Math.ceil(total / pageSize);
|
const totalPages = Math.ceil(total / pageSize);
|
||||||
logger.info("Retrieved notifications", {
|
logDomainEvent({
|
||||||
...fctx,
|
event: "notifications.list.succeeded",
|
||||||
count: data.length,
|
fctx,
|
||||||
page,
|
durationMs: Date.now() - startedAt,
|
||||||
totalPages,
|
meta: {
|
||||||
|
count: data.length,
|
||||||
|
page,
|
||||||
|
totalPages,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -143,10 +166,11 @@ export class NotificationRepository {
|
|||||||
notificationIds: number[],
|
notificationIds: number[],
|
||||||
userId: string,
|
userId: string,
|
||||||
): ResultAsync<boolean, Err> {
|
): ResultAsync<boolean, Err> {
|
||||||
logger.info("Marking notifications as read", {
|
const startedAt = Date.now();
|
||||||
...fctx,
|
logDomainEvent({
|
||||||
notificationIds,
|
event: "notifications.mark_read.started",
|
||||||
userId,
|
fctx,
|
||||||
|
meta: { userId, notificationCount: notificationIds.length },
|
||||||
});
|
});
|
||||||
|
|
||||||
return ResultAsync.fromPromise(
|
return ResultAsync.fromPromise(
|
||||||
@@ -164,8 +188,11 @@ export class NotificationRepository {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
(error) => {
|
(error) => {
|
||||||
logger.error("Failed to mark notifications as read", {
|
logDomainEvent({
|
||||||
...fctx,
|
level: "error",
|
||||||
|
event: "notifications.mark_read.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
error,
|
error,
|
||||||
});
|
});
|
||||||
return notificationErrors.markAsReadFailed(
|
return notificationErrors.markAsReadFailed(
|
||||||
@@ -174,9 +201,11 @@ export class NotificationRepository {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
).map(() => {
|
).map(() => {
|
||||||
logger.info("Notifications marked as read successfully", {
|
logDomainEvent({
|
||||||
...fctx,
|
event: "notifications.mark_read.succeeded",
|
||||||
notificationIds,
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
meta: { notificationCount: notificationIds.length },
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
@@ -187,10 +216,11 @@ export class NotificationRepository {
|
|||||||
notificationIds: number[],
|
notificationIds: number[],
|
||||||
userId: string,
|
userId: string,
|
||||||
): ResultAsync<boolean, Err> {
|
): ResultAsync<boolean, Err> {
|
||||||
logger.info("Marking notifications as unread", {
|
const startedAt = Date.now();
|
||||||
...fctx,
|
logDomainEvent({
|
||||||
notificationIds,
|
event: "notifications.mark_unread.started",
|
||||||
userId,
|
fctx,
|
||||||
|
meta: { userId, notificationCount: notificationIds.length },
|
||||||
});
|
});
|
||||||
|
|
||||||
return ResultAsync.fromPromise(
|
return ResultAsync.fromPromise(
|
||||||
@@ -208,8 +238,11 @@ export class NotificationRepository {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
(error) => {
|
(error) => {
|
||||||
logger.error("Failed to mark notifications as unread", {
|
logDomainEvent({
|
||||||
...fctx,
|
level: "error",
|
||||||
|
event: "notifications.mark_unread.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
error,
|
error,
|
||||||
});
|
});
|
||||||
return notificationErrors.markAsUnreadFailed(
|
return notificationErrors.markAsUnreadFailed(
|
||||||
@@ -218,9 +251,11 @@ export class NotificationRepository {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
).map(() => {
|
).map(() => {
|
||||||
logger.info("Notifications marked as unread successfully", {
|
logDomainEvent({
|
||||||
...fctx,
|
event: "notifications.mark_unread.succeeded",
|
||||||
notificationIds,
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
meta: { notificationCount: notificationIds.length },
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
@@ -231,10 +266,11 @@ export class NotificationRepository {
|
|||||||
notificationIds: number[],
|
notificationIds: number[],
|
||||||
userId: string,
|
userId: string,
|
||||||
): ResultAsync<boolean, Err> {
|
): ResultAsync<boolean, Err> {
|
||||||
logger.info("Archiving notifications", {
|
const startedAt = Date.now();
|
||||||
...fctx,
|
logDomainEvent({
|
||||||
notificationIds,
|
event: "notifications.archive.started",
|
||||||
userId,
|
fctx,
|
||||||
|
meta: { userId, notificationCount: notificationIds.length },
|
||||||
});
|
});
|
||||||
|
|
||||||
return ResultAsync.fromPromise(
|
return ResultAsync.fromPromise(
|
||||||
@@ -251,8 +287,11 @@ export class NotificationRepository {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
(error) => {
|
(error) => {
|
||||||
logger.error("Failed to archive notifications", {
|
logDomainEvent({
|
||||||
...fctx,
|
level: "error",
|
||||||
|
event: "notifications.archive.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
error,
|
error,
|
||||||
});
|
});
|
||||||
return notificationErrors.archiveFailed(
|
return notificationErrors.archiveFailed(
|
||||||
@@ -261,9 +300,11 @@ export class NotificationRepository {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
).map(() => {
|
).map(() => {
|
||||||
logger.info("Notifications archived successfully", {
|
logDomainEvent({
|
||||||
...fctx,
|
event: "notifications.archive.succeeded",
|
||||||
notificationIds,
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
meta: { notificationCount: notificationIds.length },
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
@@ -274,10 +315,11 @@ export class NotificationRepository {
|
|||||||
notificationIds: number[],
|
notificationIds: number[],
|
||||||
userId: string,
|
userId: string,
|
||||||
): ResultAsync<boolean, Err> {
|
): ResultAsync<boolean, Err> {
|
||||||
logger.info("Unarchiving notifications", {
|
const startedAt = Date.now();
|
||||||
...fctx,
|
logDomainEvent({
|
||||||
notificationIds,
|
event: "notifications.unarchive.started",
|
||||||
userId,
|
fctx,
|
||||||
|
meta: { userId, notificationCount: notificationIds.length },
|
||||||
});
|
});
|
||||||
|
|
||||||
return ResultAsync.fromPromise(
|
return ResultAsync.fromPromise(
|
||||||
@@ -294,8 +336,11 @@ export class NotificationRepository {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
(error) => {
|
(error) => {
|
||||||
logger.error("Failed to unarchive notifications", {
|
logDomainEvent({
|
||||||
...fctx,
|
level: "error",
|
||||||
|
event: "notifications.unarchive.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
error,
|
error,
|
||||||
});
|
});
|
||||||
return notificationErrors.unarchiveFailed(
|
return notificationErrors.unarchiveFailed(
|
||||||
@@ -304,9 +349,11 @@ export class NotificationRepository {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
).map(() => {
|
).map(() => {
|
||||||
logger.info("Notifications unarchived successfully", {
|
logDomainEvent({
|
||||||
...fctx,
|
event: "notifications.unarchive.succeeded",
|
||||||
notificationIds,
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
meta: { notificationCount: notificationIds.length },
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
@@ -317,10 +364,11 @@ export class NotificationRepository {
|
|||||||
notificationIds: number[],
|
notificationIds: number[],
|
||||||
userId: string,
|
userId: string,
|
||||||
): ResultAsync<boolean, Err> {
|
): ResultAsync<boolean, Err> {
|
||||||
logger.info("Deleting notifications", {
|
const startedAt = Date.now();
|
||||||
...fctx,
|
logDomainEvent({
|
||||||
notificationIds,
|
event: "notifications.delete.started",
|
||||||
userId,
|
fctx,
|
||||||
|
meta: { userId, notificationCount: notificationIds.length },
|
||||||
});
|
});
|
||||||
|
|
||||||
return ResultAsync.fromPromise(
|
return ResultAsync.fromPromise(
|
||||||
@@ -333,8 +381,11 @@ export class NotificationRepository {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
(error) => {
|
(error) => {
|
||||||
logger.error("Failed to delete notifications", {
|
logDomainEvent({
|
||||||
...fctx,
|
level: "error",
|
||||||
|
event: "notifications.delete.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
error,
|
error,
|
||||||
});
|
});
|
||||||
return notificationErrors.deleteNotificationsFailed(
|
return notificationErrors.deleteNotificationsFailed(
|
||||||
@@ -343,9 +394,11 @@ export class NotificationRepository {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
).map(() => {
|
).map(() => {
|
||||||
logger.info("Notifications deleted successfully", {
|
logDomainEvent({
|
||||||
...fctx,
|
event: "notifications.delete.succeeded",
|
||||||
notificationIds,
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
meta: { notificationCount: notificationIds.length },
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
@@ -355,7 +408,12 @@ export class NotificationRepository {
|
|||||||
fctx: FlowExecCtx,
|
fctx: FlowExecCtx,
|
||||||
userId: string,
|
userId: string,
|
||||||
): ResultAsync<number, Err> {
|
): 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(
|
return ResultAsync.fromPromise(
|
||||||
this.db
|
this.db
|
||||||
@@ -369,7 +427,13 @@ export class NotificationRepository {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
(error) => {
|
(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(
|
return notificationErrors.getUnreadCountFailed(
|
||||||
fctx,
|
fctx,
|
||||||
error instanceof Error ? error.message : String(error),
|
error instanceof Error ? error.message : String(error),
|
||||||
@@ -377,7 +441,12 @@ export class NotificationRepository {
|
|||||||
},
|
},
|
||||||
).map((result) => {
|
).map((result) => {
|
||||||
const count = result[0]?.count || 0;
|
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;
|
return count;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import { FlowExecCtx } from "@/core/flow.execution.context";
|
import { FlowExecCtx } from "@/core/flow.execution.context";
|
||||||
import { traceResultAsync } from "@core/observability";
|
import { traceResultAsync } from "@core/observability";
|
||||||
import { ERROR_CODES, type Err } from "@pkg/result";
|
import { ERROR_CODES, type Err } from "@pkg/result";
|
||||||
import { getError, logger } from "@pkg/logger";
|
import { getError, logDomainEvent } from "@pkg/logger";
|
||||||
import { auth } from "../auth/config.base";
|
import { auth } from "../auth/config.base";
|
||||||
import { account } from "@pkg/db/schema";
|
import { account } from "@pkg/db/schema";
|
||||||
import { ResultAsync } from "neverthrow";
|
import { ResultAsync, errAsync, okAsync } from "neverthrow";
|
||||||
import { Database, eq } from "@pkg/db";
|
import { Database, eq } from "@pkg/db";
|
||||||
import { nanoid } from "nanoid";
|
import { nanoid } from "nanoid";
|
||||||
|
|
||||||
@@ -40,9 +40,11 @@ export class AccountRepository {
|
|||||||
fctx,
|
fctx,
|
||||||
attributes: { "app.user.id": userId },
|
attributes: { "app.user.id": userId },
|
||||||
fn: () => {
|
fn: () => {
|
||||||
logger.info("Checking if account exists for user", {
|
const startedAt = Date.now();
|
||||||
...fctx,
|
logDomainEvent({
|
||||||
userId,
|
event: "account.ensure_exists.started",
|
||||||
|
fctx,
|
||||||
|
meta: { userId },
|
||||||
});
|
});
|
||||||
|
|
||||||
return ResultAsync.fromPromise(
|
return ResultAsync.fromPromise(
|
||||||
@@ -50,44 +52,40 @@ export class AccountRepository {
|
|||||||
where: eq(account.userId, userId),
|
where: eq(account.userId, userId),
|
||||||
}),
|
}),
|
||||||
(error) => {
|
(error) => {
|
||||||
logger.error("Failed to check account existence", {
|
logDomainEvent({
|
||||||
...fctx,
|
level: "error",
|
||||||
|
event: "account.ensure_exists.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
error,
|
error,
|
||||||
|
meta: { userId },
|
||||||
});
|
});
|
||||||
return this.dbError(
|
return this.dbError(
|
||||||
fctx,
|
fctx,
|
||||||
error instanceof Error
|
error instanceof Error ? error.message : String(error),
|
||||||
? error.message
|
|
||||||
: String(error),
|
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
).andThen((existingAccount) => {
|
).andThen((existingAccount) => {
|
||||||
if (existingAccount) {
|
if (existingAccount) {
|
||||||
logger.info("Account already exists for user", {
|
logDomainEvent({
|
||||||
...fctx,
|
event: "account.ensure_exists.succeeded",
|
||||||
userId,
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
meta: { userId, existed: true },
|
||||||
});
|
});
|
||||||
return ResultAsync.fromSafePromise(
|
return okAsync(true);
|
||||||
Promise.resolve(true),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(
|
|
||||||
"Account does not exist, creating new account for user",
|
|
||||||
{
|
|
||||||
...fctx,
|
|
||||||
userId,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
return ResultAsync.fromPromise(
|
return ResultAsync.fromPromise(
|
||||||
auth.$context.then((ctx) =>
|
auth.$context.then((ctx) => ctx.password.hash(nanoid())),
|
||||||
ctx.password.hash(nanoid()),
|
|
||||||
),
|
|
||||||
(error) => {
|
(error) => {
|
||||||
logger.error("Failed to hash password", {
|
logDomainEvent({
|
||||||
...fctx,
|
level: "error",
|
||||||
|
event: "account.ensure_exists.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
error,
|
error,
|
||||||
|
meta: { userId, stage: "hash_password" },
|
||||||
});
|
});
|
||||||
return this.dbError(
|
return this.dbError(
|
||||||
fctx,
|
fctx,
|
||||||
@@ -106,16 +104,20 @@ export class AccountRepository {
|
|||||||
id: aid,
|
id: aid,
|
||||||
accountId: userId,
|
accountId: userId,
|
||||||
providerId: "credential",
|
providerId: "credential",
|
||||||
userId: userId,
|
userId,
|
||||||
password,
|
password,
|
||||||
createdAt: new Date(),
|
createdAt: new Date(),
|
||||||
updatedAt: new Date(),
|
updatedAt: new Date(),
|
||||||
})
|
})
|
||||||
.execute(),
|
.execute(),
|
||||||
(error) => {
|
(error) => {
|
||||||
logger.error("Failed to create account", {
|
logDomainEvent({
|
||||||
...fctx,
|
level: "error",
|
||||||
|
event: "account.ensure_exists.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
error,
|
error,
|
||||||
|
meta: { userId, stage: "create_account" },
|
||||||
});
|
});
|
||||||
return this.dbError(
|
return this.dbError(
|
||||||
fctx,
|
fctx,
|
||||||
@@ -125,13 +127,12 @@ export class AccountRepository {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
).map(() => {
|
).map(() => {
|
||||||
logger.info(
|
logDomainEvent({
|
||||||
"Account created successfully for user",
|
event: "account.ensure_exists.succeeded",
|
||||||
{
|
fctx,
|
||||||
...fctx,
|
durationMs: Date.now() - startedAt,
|
||||||
userId,
|
meta: { userId, existed: false },
|
||||||
},
|
});
|
||||||
);
|
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -150,9 +151,11 @@ export class AccountRepository {
|
|||||||
fctx,
|
fctx,
|
||||||
attributes: { "app.user.id": userId },
|
attributes: { "app.user.id": userId },
|
||||||
fn: () => {
|
fn: () => {
|
||||||
logger.info("Starting password rotation for user", {
|
const startedAt = Date.now();
|
||||||
...fctx,
|
logDomainEvent({
|
||||||
userId,
|
event: "account.rotate_password.started",
|
||||||
|
fctx,
|
||||||
|
meta: { userId },
|
||||||
});
|
});
|
||||||
|
|
||||||
return ResultAsync.fromPromise(
|
return ResultAsync.fromPromise(
|
||||||
@@ -160,10 +163,14 @@ export class AccountRepository {
|
|||||||
where: eq(account.userId, userId),
|
where: eq(account.userId, userId),
|
||||||
}),
|
}),
|
||||||
(error) => {
|
(error) => {
|
||||||
logger.error(
|
logDomainEvent({
|
||||||
"Failed to check account existence for password rotation",
|
level: "error",
|
||||||
{ ...fctx, error },
|
event: "account.rotate_password.failed",
|
||||||
);
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
error,
|
||||||
|
meta: { userId, stage: "check_exists" },
|
||||||
|
});
|
||||||
return this.dbError(
|
return this.dbError(
|
||||||
fctx,
|
fctx,
|
||||||
error instanceof Error
|
error instanceof Error
|
||||||
@@ -173,29 +180,28 @@ export class AccountRepository {
|
|||||||
},
|
},
|
||||||
).andThen((existingAccount) => {
|
).andThen((existingAccount) => {
|
||||||
if (!existingAccount) {
|
if (!existingAccount) {
|
||||||
logger.error("Account not found for user", {
|
logDomainEvent({
|
||||||
...fctx,
|
level: "warn",
|
||||||
userId,
|
event: "account.rotate_password.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
error: { code: "NOT_FOUND", message: "Account not found" },
|
||||||
|
meta: { userId },
|
||||||
});
|
});
|
||||||
return ResultAsync.fromSafePromise(
|
return errAsync(this.accountNotFound(fctx));
|
||||||
Promise.resolve(this.accountNotFound(fctx)),
|
|
||||||
).andThen((err) =>
|
|
||||||
ResultAsync.fromSafePromise(Promise.reject(err)),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ResultAsync.fromPromise(
|
return ResultAsync.fromPromise(
|
||||||
auth.$context.then((ctx) =>
|
auth.$context.then((ctx) => ctx.password.hash(password)),
|
||||||
ctx.password.hash(password),
|
|
||||||
),
|
|
||||||
(error) => {
|
(error) => {
|
||||||
logger.error(
|
logDomainEvent({
|
||||||
"Failed to hash password for rotation",
|
level: "error",
|
||||||
{
|
event: "account.rotate_password.failed",
|
||||||
...fctx,
|
fctx,
|
||||||
error,
|
durationMs: Date.now() - startedAt,
|
||||||
},
|
error,
|
||||||
);
|
meta: { userId, stage: "hash_password" },
|
||||||
|
});
|
||||||
return this.dbError(
|
return this.dbError(
|
||||||
fctx,
|
fctx,
|
||||||
error instanceof Error
|
error instanceof Error
|
||||||
@@ -204,10 +210,6 @@ export class AccountRepository {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
).andThen((hashed) => {
|
).andThen((hashed) => {
|
||||||
logger.info("Updating user's password in database", {
|
|
||||||
...fctx,
|
|
||||||
});
|
|
||||||
|
|
||||||
return ResultAsync.fromPromise(
|
return ResultAsync.fromPromise(
|
||||||
this.db
|
this.db
|
||||||
.update(account)
|
.update(account)
|
||||||
@@ -216,9 +218,13 @@ export class AccountRepository {
|
|||||||
.returning()
|
.returning()
|
||||||
.execute(),
|
.execute(),
|
||||||
(error) => {
|
(error) => {
|
||||||
logger.error("Failed to update password", {
|
logDomainEvent({
|
||||||
...fctx,
|
level: "error",
|
||||||
|
event: "account.rotate_password.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
error,
|
error,
|
||||||
|
meta: { userId, stage: "update_password" },
|
||||||
});
|
});
|
||||||
return this.dbError(
|
return this.dbError(
|
||||||
fctx,
|
fctx,
|
||||||
@@ -227,14 +233,12 @@ export class AccountRepository {
|
|||||||
: String(error),
|
: String(error),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
).map((result) => {
|
).map(() => {
|
||||||
logger.info(
|
logDomainEvent({
|
||||||
"User's password updated successfully",
|
event: "account.rotate_password.succeeded",
|
||||||
{ ...fctx },
|
fctx,
|
||||||
);
|
durationMs: Date.now() - startedAt,
|
||||||
logger.debug("Password rotation result", {
|
meta: { userId },
|
||||||
...fctx,
|
|
||||||
result,
|
|
||||||
});
|
});
|
||||||
return password;
|
return password;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { Database, eq } from "@pkg/db";
|
|||||||
import { BanInfo, User } from "./data";
|
import { BanInfo, User } from "./data";
|
||||||
import { user } from "@pkg/db/schema";
|
import { user } from "@pkg/db/schema";
|
||||||
import { userErrors } from "./errors";
|
import { userErrors } from "./errors";
|
||||||
import { logger } from "@pkg/logger";
|
import { logDomainEvent } from "@pkg/logger";
|
||||||
|
|
||||||
export class UserRepository {
|
export class UserRepository {
|
||||||
constructor(private db: Database) {}
|
constructor(private db: Database) {}
|
||||||
@@ -17,9 +17,11 @@ export class UserRepository {
|
|||||||
fctx,
|
fctx,
|
||||||
attributes: { "app.user.id": userId },
|
attributes: { "app.user.id": userId },
|
||||||
fn: () => {
|
fn: () => {
|
||||||
logger.info("Getting user info for user", {
|
const startedAt = Date.now();
|
||||||
flowId: fctx.flowId,
|
logDomainEvent({
|
||||||
userId,
|
event: "user.get_info.started",
|
||||||
|
fctx,
|
||||||
|
meta: { userId },
|
||||||
});
|
});
|
||||||
|
|
||||||
return ResultAsync.fromPromise(
|
return ResultAsync.fromPromise(
|
||||||
@@ -27,9 +29,13 @@ export class UserRepository {
|
|||||||
where: eq(user.id, userId),
|
where: eq(user.id, userId),
|
||||||
}),
|
}),
|
||||||
(error) => {
|
(error) => {
|
||||||
logger.error("Failed to get user info", {
|
logDomainEvent({
|
||||||
flowId: fctx.flowId,
|
level: "error",
|
||||||
|
event: "user.get_info.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
error,
|
error,
|
||||||
|
meta: { userId },
|
||||||
});
|
});
|
||||||
return userErrors.getUserInfoFailed(
|
return userErrors.getUserInfoFailed(
|
||||||
fctx,
|
fctx,
|
||||||
@@ -38,16 +44,22 @@ export class UserRepository {
|
|||||||
},
|
},
|
||||||
).andThen((userData) => {
|
).andThen((userData) => {
|
||||||
if (!userData) {
|
if (!userData) {
|
||||||
logger.error("User not found with id", {
|
logDomainEvent({
|
||||||
flowId: fctx.flowId,
|
level: "warn",
|
||||||
userId,
|
event: "user.get_info.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
error: { code: "NOT_FOUND", message: "User not found" },
|
||||||
|
meta: { userId },
|
||||||
});
|
});
|
||||||
return errAsync(userErrors.userNotFound(fctx));
|
return errAsync(userErrors.userNotFound(fctx));
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("User info retrieved successfully for user", {
|
logDomainEvent({
|
||||||
flowId: fctx.flowId,
|
event: "user.get_info.succeeded",
|
||||||
userId,
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
meta: { userId },
|
||||||
});
|
});
|
||||||
return okAsync(userData as User);
|
return okAsync(userData as User);
|
||||||
});
|
});
|
||||||
@@ -64,9 +76,10 @@ export class UserRepository {
|
|||||||
fctx,
|
fctx,
|
||||||
attributes: { "app.user.username": username },
|
attributes: { "app.user.username": username },
|
||||||
fn: () => {
|
fn: () => {
|
||||||
logger.info("Checking username availability", {
|
const startedAt = Date.now();
|
||||||
...fctx,
|
logDomainEvent({
|
||||||
username,
|
event: "user.username_check.started",
|
||||||
|
fctx,
|
||||||
});
|
});
|
||||||
|
|
||||||
return ResultAsync.fromPromise(
|
return ResultAsync.fromPromise(
|
||||||
@@ -74,8 +87,11 @@ export class UserRepository {
|
|||||||
where: eq(user.username, username),
|
where: eq(user.username, username),
|
||||||
}),
|
}),
|
||||||
(error) => {
|
(error) => {
|
||||||
logger.error("Failed to check username availability", {
|
logDomainEvent({
|
||||||
...fctx,
|
level: "error",
|
||||||
|
event: "user.username_check.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
error,
|
error,
|
||||||
});
|
});
|
||||||
return userErrors.usernameCheckFailed(
|
return userErrors.usernameCheckFailed(
|
||||||
@@ -85,10 +101,11 @@ export class UserRepository {
|
|||||||
},
|
},
|
||||||
).map((existingUser) => {
|
).map((existingUser) => {
|
||||||
const isAvailable = !existingUser?.id;
|
const isAvailable = !existingUser?.id;
|
||||||
logger.info("Username availability checked", {
|
logDomainEvent({
|
||||||
...fctx,
|
event: "user.username_check.succeeded",
|
||||||
username,
|
fctx,
|
||||||
isAvailable,
|
durationMs: Date.now() - startedAt,
|
||||||
|
meta: { isAvailable },
|
||||||
});
|
});
|
||||||
return isAvailable;
|
return isAvailable;
|
||||||
});
|
});
|
||||||
@@ -105,9 +122,11 @@ export class UserRepository {
|
|||||||
fctx,
|
fctx,
|
||||||
attributes: { "app.user.id": userId },
|
attributes: { "app.user.id": userId },
|
||||||
fn: () => {
|
fn: () => {
|
||||||
logger.info("Updating last 2FA verified timestamp for user", {
|
const startedAt = Date.now();
|
||||||
flowId: fctx.flowId,
|
logDomainEvent({
|
||||||
userId,
|
event: "user.update_last_2fa.started",
|
||||||
|
fctx,
|
||||||
|
meta: { userId },
|
||||||
});
|
});
|
||||||
|
|
||||||
return ResultAsync.fromPromise(
|
return ResultAsync.fromPromise(
|
||||||
@@ -117,25 +136,26 @@ export class UserRepository {
|
|||||||
.where(eq(user.id, userId))
|
.where(eq(user.id, userId))
|
||||||
.execute(),
|
.execute(),
|
||||||
(error) => {
|
(error) => {
|
||||||
logger.error(
|
logDomainEvent({
|
||||||
"Failed to update last 2FA verified timestamp",
|
level: "error",
|
||||||
{
|
event: "user.update_last_2fa.failed",
|
||||||
...fctx,
|
fctx,
|
||||||
error,
|
durationMs: Date.now() - startedAt,
|
||||||
},
|
error,
|
||||||
);
|
meta: { userId },
|
||||||
|
});
|
||||||
return userErrors.updateFailed(
|
return userErrors.updateFailed(
|
||||||
fctx,
|
fctx,
|
||||||
error instanceof Error ? error.message : String(error),
|
error instanceof Error ? error.message : String(error),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
).map(() => {
|
).map(() => {
|
||||||
logger.info(
|
logDomainEvent({
|
||||||
"Last 2FA verified timestamp updated successfully",
|
event: "user.update_last_2fa.succeeded",
|
||||||
{
|
fctx,
|
||||||
...fctx,
|
durationMs: Date.now() - startedAt,
|
||||||
},
|
meta: { userId },
|
||||||
);
|
});
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@@ -153,11 +173,15 @@ export class UserRepository {
|
|||||||
fctx,
|
fctx,
|
||||||
attributes: { "app.user.id": userId },
|
attributes: { "app.user.id": userId },
|
||||||
fn: () => {
|
fn: () => {
|
||||||
logger.info("Banning user", {
|
const startedAt = Date.now();
|
||||||
...fctx,
|
logDomainEvent({
|
||||||
userId,
|
event: "user.ban.started",
|
||||||
banExpiresAt: banExpiresAt.toISOString(),
|
fctx,
|
||||||
reason,
|
meta: {
|
||||||
|
userId,
|
||||||
|
reasonLength: reason.length,
|
||||||
|
banExpiresAt: banExpiresAt.toISOString(),
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return ResultAsync.fromPromise(
|
return ResultAsync.fromPromise(
|
||||||
@@ -171,17 +195,25 @@ export class UserRepository {
|
|||||||
.where(eq(user.id, userId))
|
.where(eq(user.id, userId))
|
||||||
.execute(),
|
.execute(),
|
||||||
(error) => {
|
(error) => {
|
||||||
logger.error("Failed to ban user", { ...fctx, error });
|
logDomainEvent({
|
||||||
|
level: "error",
|
||||||
|
event: "user.ban.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
error,
|
||||||
|
meta: { userId },
|
||||||
|
});
|
||||||
return userErrors.banOperationFailed(
|
return userErrors.banOperationFailed(
|
||||||
fctx,
|
fctx,
|
||||||
error instanceof Error ? error.message : String(error),
|
error instanceof Error ? error.message : String(error),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
).map(() => {
|
).map(() => {
|
||||||
logger.info("User has been banned", {
|
logDomainEvent({
|
||||||
...fctx,
|
event: "user.ban.succeeded",
|
||||||
userId,
|
fctx,
|
||||||
banExpiresAt: banExpiresAt.toISOString(),
|
durationMs: Date.now() - startedAt,
|
||||||
|
meta: { userId },
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
@@ -195,7 +227,12 @@ export class UserRepository {
|
|||||||
fctx,
|
fctx,
|
||||||
attributes: { "app.user.id": userId },
|
attributes: { "app.user.id": userId },
|
||||||
fn: () => {
|
fn: () => {
|
||||||
logger.info("Checking ban status for user", { ...fctx, userId });
|
const startedAt = Date.now();
|
||||||
|
logDomainEvent({
|
||||||
|
event: "user.is_banned.started",
|
||||||
|
fctx,
|
||||||
|
meta: { userId },
|
||||||
|
});
|
||||||
|
|
||||||
return ResultAsync.fromPromise(
|
return ResultAsync.fromPromise(
|
||||||
this.db.query.user.findFirst({
|
this.db.query.user.findFirst({
|
||||||
@@ -206,9 +243,13 @@ export class UserRepository {
|
|||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
(error) => {
|
(error) => {
|
||||||
logger.error("Failed to check ban status", {
|
logDomainEvent({
|
||||||
...fctx,
|
level: "error",
|
||||||
|
event: "user.is_banned.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
error,
|
error,
|
||||||
|
meta: { userId },
|
||||||
});
|
});
|
||||||
return userErrors.dbError(
|
return userErrors.dbError(
|
||||||
fctx,
|
fctx,
|
||||||
@@ -217,29 +258,39 @@ export class UserRepository {
|
|||||||
},
|
},
|
||||||
).andThen((userData) => {
|
).andThen((userData) => {
|
||||||
if (!userData) {
|
if (!userData) {
|
||||||
logger.error("User not found when checking ban status", {
|
logDomainEvent({
|
||||||
...fctx,
|
level: "warn",
|
||||||
|
event: "user.is_banned.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
error: { code: "NOT_FOUND", message: "User not found" },
|
||||||
|
meta: { userId },
|
||||||
});
|
});
|
||||||
return errAsync(userErrors.userNotFound(fctx));
|
return errAsync(userErrors.userNotFound(fctx));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!userData.banned) {
|
if (!userData.banned) {
|
||||||
logger.info("User is not banned", { ...fctx, userId });
|
logDomainEvent({
|
||||||
|
event: "user.is_banned.succeeded",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
meta: { userId, isBanned: false },
|
||||||
|
});
|
||||||
return okAsync(false);
|
return okAsync(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!userData.banExpires) {
|
if (!userData.banExpires) {
|
||||||
logger.info("User is permanently banned", { ...fctx, userId });
|
logDomainEvent({
|
||||||
|
event: "user.is_banned.succeeded",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
meta: { userId, isBanned: true, isPermanent: true },
|
||||||
|
});
|
||||||
return okAsync(true);
|
return okAsync(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
if (userData.banExpires <= now) {
|
if (userData.banExpires <= now) {
|
||||||
logger.info("User ban has expired, removing ban status", {
|
|
||||||
...fctx,
|
|
||||||
userId,
|
|
||||||
});
|
|
||||||
|
|
||||||
return ResultAsync.fromPromise(
|
return ResultAsync.fromPromise(
|
||||||
this.db
|
this.db
|
||||||
.update(user)
|
.update(user)
|
||||||
@@ -251,9 +302,13 @@ export class UserRepository {
|
|||||||
.where(eq(user.id, userId))
|
.where(eq(user.id, userId))
|
||||||
.execute(),
|
.execute(),
|
||||||
(error) => {
|
(error) => {
|
||||||
logger.error("Failed to unban user after expiry", {
|
logDomainEvent({
|
||||||
...fctx,
|
level: "error",
|
||||||
|
event: "user.unban_after_expiry.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
error,
|
error,
|
||||||
|
meta: { userId },
|
||||||
});
|
});
|
||||||
return userErrors.unbanFailed(
|
return userErrors.unbanFailed(
|
||||||
fctx,
|
fctx,
|
||||||
@@ -264,25 +319,36 @@ export class UserRepository {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
.map(() => {
|
.map(() => {
|
||||||
logger.info("User has been unbanned after expiry", {
|
logDomainEvent({
|
||||||
...fctx,
|
event: "user.unban_after_expiry.succeeded",
|
||||||
userId,
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
meta: { userId },
|
||||||
});
|
});
|
||||||
return false;
|
return false;
|
||||||
})
|
})
|
||||||
.orElse((error) => {
|
.orElse((error) => {
|
||||||
logger.error(
|
logDomainEvent({
|
||||||
"Failed to unban user after expiry, still returning banned status",
|
level: "warn",
|
||||||
{ ...fctx, userId, error },
|
event: "user.is_banned.succeeded",
|
||||||
);
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
error,
|
||||||
|
meta: { userId, degraded: true, isBanned: true },
|
||||||
|
});
|
||||||
return okAsync(true);
|
return okAsync(true);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("User is banned", {
|
logDomainEvent({
|
||||||
...fctx,
|
event: "user.is_banned.succeeded",
|
||||||
userId,
|
fctx,
|
||||||
banExpires: userData.banExpires.toISOString(),
|
durationMs: Date.now() - startedAt,
|
||||||
|
meta: {
|
||||||
|
userId,
|
||||||
|
isBanned: true,
|
||||||
|
banExpires: userData.banExpires.toISOString(),
|
||||||
|
},
|
||||||
});
|
});
|
||||||
return okAsync(true);
|
return okAsync(true);
|
||||||
});
|
});
|
||||||
@@ -296,7 +362,12 @@ export class UserRepository {
|
|||||||
fctx,
|
fctx,
|
||||||
attributes: { "app.user.id": userId },
|
attributes: { "app.user.id": userId },
|
||||||
fn: () => {
|
fn: () => {
|
||||||
logger.info("Getting ban info for user", { ...fctx, userId });
|
const startedAt = Date.now();
|
||||||
|
logDomainEvent({
|
||||||
|
event: "user.ban_info.started",
|
||||||
|
fctx,
|
||||||
|
meta: { userId },
|
||||||
|
});
|
||||||
|
|
||||||
return ResultAsync.fromPromise(
|
return ResultAsync.fromPromise(
|
||||||
this.db.query.user.findFirst({
|
this.db.query.user.findFirst({
|
||||||
@@ -304,7 +375,14 @@ export class UserRepository {
|
|||||||
columns: { banned: true, banReason: true, banExpires: true },
|
columns: { banned: true, banReason: true, banExpires: true },
|
||||||
}),
|
}),
|
||||||
(error) => {
|
(error) => {
|
||||||
logger.error("Failed to get ban info", { ...fctx, error });
|
logDomainEvent({
|
||||||
|
level: "error",
|
||||||
|
event: "user.ban_info.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
error,
|
||||||
|
meta: { userId },
|
||||||
|
});
|
||||||
return userErrors.getBanInfoFailed(
|
return userErrors.getBanInfoFailed(
|
||||||
fctx,
|
fctx,
|
||||||
error instanceof Error ? error.message : String(error),
|
error instanceof Error ? error.message : String(error),
|
||||||
@@ -312,15 +390,22 @@ export class UserRepository {
|
|||||||
},
|
},
|
||||||
).andThen((userData) => {
|
).andThen((userData) => {
|
||||||
if (!userData) {
|
if (!userData) {
|
||||||
logger.error("User not found when getting ban info", {
|
logDomainEvent({
|
||||||
...fctx,
|
level: "warn",
|
||||||
|
event: "user.ban_info.failed",
|
||||||
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
error: { code: "NOT_FOUND", message: "User not found" },
|
||||||
|
meta: { userId },
|
||||||
});
|
});
|
||||||
return errAsync(userErrors.userNotFound(fctx));
|
return errAsync(userErrors.userNotFound(fctx));
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("Ban info retrieved successfully for user", {
|
logDomainEvent({
|
||||||
...fctx,
|
event: "user.ban_info.succeeded",
|
||||||
userId,
|
fctx,
|
||||||
|
durationMs: Date.now() - startedAt,
|
||||||
|
meta: { userId, banned: userData.banned || false },
|
||||||
});
|
});
|
||||||
|
|
||||||
return okAsync({
|
return okAsync({
|
||||||
|
|||||||
Reference in New Issue
Block a user