levelled up logging, albeit with a bit of verbosity...
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import { FlowExecCtx } from "@/core/flow.execution.context";
|
||||
import { traceResultAsync } from "@core/observability";
|
||||
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 { account } from "@pkg/db/schema";
|
||||
import { ResultAsync } from "neverthrow";
|
||||
import { ResultAsync, errAsync, okAsync } from "neverthrow";
|
||||
import { Database, eq } from "@pkg/db";
|
||||
import { nanoid } from "nanoid";
|
||||
|
||||
@@ -40,9 +40,11 @@ export class AccountRepository {
|
||||
fctx,
|
||||
attributes: { "app.user.id": userId },
|
||||
fn: () => {
|
||||
logger.info("Checking if account exists for user", {
|
||||
...fctx,
|
||||
userId,
|
||||
const startedAt = Date.now();
|
||||
logDomainEvent({
|
||||
event: "account.ensure_exists.started",
|
||||
fctx,
|
||||
meta: { userId },
|
||||
});
|
||||
|
||||
return ResultAsync.fromPromise(
|
||||
@@ -50,44 +52,40 @@ export class AccountRepository {
|
||||
where: eq(account.userId, userId),
|
||||
}),
|
||||
(error) => {
|
||||
logger.error("Failed to check account existence", {
|
||||
...fctx,
|
||||
logDomainEvent({
|
||||
level: "error",
|
||||
event: "account.ensure_exists.failed",
|
||||
fctx,
|
||||
durationMs: Date.now() - startedAt,
|
||||
error,
|
||||
meta: { userId },
|
||||
});
|
||||
return this.dbError(
|
||||
fctx,
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: String(error),
|
||||
error instanceof Error ? error.message : String(error),
|
||||
);
|
||||
},
|
||||
).andThen((existingAccount) => {
|
||||
if (existingAccount) {
|
||||
logger.info("Account already exists for user", {
|
||||
...fctx,
|
||||
userId,
|
||||
logDomainEvent({
|
||||
event: "account.ensure_exists.succeeded",
|
||||
fctx,
|
||||
durationMs: Date.now() - startedAt,
|
||||
meta: { userId, existed: true },
|
||||
});
|
||||
return ResultAsync.fromSafePromise(
|
||||
Promise.resolve(true),
|
||||
);
|
||||
return okAsync(true);
|
||||
}
|
||||
|
||||
logger.info(
|
||||
"Account does not exist, creating new account for user",
|
||||
{
|
||||
...fctx,
|
||||
userId,
|
||||
},
|
||||
);
|
||||
|
||||
return ResultAsync.fromPromise(
|
||||
auth.$context.then((ctx) =>
|
||||
ctx.password.hash(nanoid()),
|
||||
),
|
||||
auth.$context.then((ctx) => ctx.password.hash(nanoid())),
|
||||
(error) => {
|
||||
logger.error("Failed to hash password", {
|
||||
...fctx,
|
||||
logDomainEvent({
|
||||
level: "error",
|
||||
event: "account.ensure_exists.failed",
|
||||
fctx,
|
||||
durationMs: Date.now() - startedAt,
|
||||
error,
|
||||
meta: { userId, stage: "hash_password" },
|
||||
});
|
||||
return this.dbError(
|
||||
fctx,
|
||||
@@ -106,16 +104,20 @@ export class AccountRepository {
|
||||
id: aid,
|
||||
accountId: userId,
|
||||
providerId: "credential",
|
||||
userId: userId,
|
||||
userId,
|
||||
password,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.execute(),
|
||||
(error) => {
|
||||
logger.error("Failed to create account", {
|
||||
...fctx,
|
||||
logDomainEvent({
|
||||
level: "error",
|
||||
event: "account.ensure_exists.failed",
|
||||
fctx,
|
||||
durationMs: Date.now() - startedAt,
|
||||
error,
|
||||
meta: { userId, stage: "create_account" },
|
||||
});
|
||||
return this.dbError(
|
||||
fctx,
|
||||
@@ -125,13 +127,12 @@ export class AccountRepository {
|
||||
);
|
||||
},
|
||||
).map(() => {
|
||||
logger.info(
|
||||
"Account created successfully for user",
|
||||
{
|
||||
...fctx,
|
||||
userId,
|
||||
},
|
||||
);
|
||||
logDomainEvent({
|
||||
event: "account.ensure_exists.succeeded",
|
||||
fctx,
|
||||
durationMs: Date.now() - startedAt,
|
||||
meta: { userId, existed: false },
|
||||
});
|
||||
return false;
|
||||
});
|
||||
});
|
||||
@@ -150,9 +151,11 @@ export class AccountRepository {
|
||||
fctx,
|
||||
attributes: { "app.user.id": userId },
|
||||
fn: () => {
|
||||
logger.info("Starting password rotation for user", {
|
||||
...fctx,
|
||||
userId,
|
||||
const startedAt = Date.now();
|
||||
logDomainEvent({
|
||||
event: "account.rotate_password.started",
|
||||
fctx,
|
||||
meta: { userId },
|
||||
});
|
||||
|
||||
return ResultAsync.fromPromise(
|
||||
@@ -160,10 +163,14 @@ export class AccountRepository {
|
||||
where: eq(account.userId, userId),
|
||||
}),
|
||||
(error) => {
|
||||
logger.error(
|
||||
"Failed to check account existence for password rotation",
|
||||
{ ...fctx, error },
|
||||
);
|
||||
logDomainEvent({
|
||||
level: "error",
|
||||
event: "account.rotate_password.failed",
|
||||
fctx,
|
||||
durationMs: Date.now() - startedAt,
|
||||
error,
|
||||
meta: { userId, stage: "check_exists" },
|
||||
});
|
||||
return this.dbError(
|
||||
fctx,
|
||||
error instanceof Error
|
||||
@@ -173,29 +180,28 @@ export class AccountRepository {
|
||||
},
|
||||
).andThen((existingAccount) => {
|
||||
if (!existingAccount) {
|
||||
logger.error("Account not found for user", {
|
||||
...fctx,
|
||||
userId,
|
||||
logDomainEvent({
|
||||
level: "warn",
|
||||
event: "account.rotate_password.failed",
|
||||
fctx,
|
||||
durationMs: Date.now() - startedAt,
|
||||
error: { code: "NOT_FOUND", message: "Account not found" },
|
||||
meta: { userId },
|
||||
});
|
||||
return ResultAsync.fromSafePromise(
|
||||
Promise.resolve(this.accountNotFound(fctx)),
|
||||
).andThen((err) =>
|
||||
ResultAsync.fromSafePromise(Promise.reject(err)),
|
||||
);
|
||||
return errAsync(this.accountNotFound(fctx));
|
||||
}
|
||||
|
||||
return ResultAsync.fromPromise(
|
||||
auth.$context.then((ctx) =>
|
||||
ctx.password.hash(password),
|
||||
),
|
||||
auth.$context.then((ctx) => ctx.password.hash(password)),
|
||||
(error) => {
|
||||
logger.error(
|
||||
"Failed to hash password for rotation",
|
||||
{
|
||||
...fctx,
|
||||
error,
|
||||
},
|
||||
);
|
||||
logDomainEvent({
|
||||
level: "error",
|
||||
event: "account.rotate_password.failed",
|
||||
fctx,
|
||||
durationMs: Date.now() - startedAt,
|
||||
error,
|
||||
meta: { userId, stage: "hash_password" },
|
||||
});
|
||||
return this.dbError(
|
||||
fctx,
|
||||
error instanceof Error
|
||||
@@ -204,10 +210,6 @@ export class AccountRepository {
|
||||
);
|
||||
},
|
||||
).andThen((hashed) => {
|
||||
logger.info("Updating user's password in database", {
|
||||
...fctx,
|
||||
});
|
||||
|
||||
return ResultAsync.fromPromise(
|
||||
this.db
|
||||
.update(account)
|
||||
@@ -216,9 +218,13 @@ export class AccountRepository {
|
||||
.returning()
|
||||
.execute(),
|
||||
(error) => {
|
||||
logger.error("Failed to update password", {
|
||||
...fctx,
|
||||
logDomainEvent({
|
||||
level: "error",
|
||||
event: "account.rotate_password.failed",
|
||||
fctx,
|
||||
durationMs: Date.now() - startedAt,
|
||||
error,
|
||||
meta: { userId, stage: "update_password" },
|
||||
});
|
||||
return this.dbError(
|
||||
fctx,
|
||||
@@ -227,14 +233,12 @@ export class AccountRepository {
|
||||
: String(error),
|
||||
);
|
||||
},
|
||||
).map((result) => {
|
||||
logger.info(
|
||||
"User's password updated successfully",
|
||||
{ ...fctx },
|
||||
);
|
||||
logger.debug("Password rotation result", {
|
||||
...fctx,
|
||||
result,
|
||||
).map(() => {
|
||||
logDomainEvent({
|
||||
event: "account.rotate_password.succeeded",
|
||||
fctx,
|
||||
durationMs: Date.now() - startedAt,
|
||||
meta: { userId },
|
||||
});
|
||||
return password;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user