added spanning methods for insights in logic + logger is fully otel-ified as well 🥳

This commit is contained in:
user
2026-03-01 03:11:21 +02:00
parent 9914218b81
commit 8004459d20
6 changed files with 564 additions and 360 deletions

View File

@@ -1,4 +1,5 @@
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 { auth } from "../auth/config.base";
@@ -34,72 +35,22 @@ export class AccountRepository {
fctx: FlowExecCtx,
userId: string,
): ResultAsync<boolean, Err> {
logger.info("Checking if account exists for user", {
...fctx,
userId,
});
return ResultAsync.fromPromise(
this.db.query.account.findFirst({
where: eq(account.userId, userId),
}),
(error) => {
logger.error("Failed to check account existence", {
...fctx,
error,
});
return this.dbError(
fctx,
error instanceof Error ? error.message : String(error),
);
},
).andThen((existingAccount) => {
if (existingAccount) {
logger.info("Account already exists for user", {
return traceResultAsync({
name: "logic.user.repository.ensureAccountExists",
fctx,
attributes: { "app.user.id": userId },
fn: () => {
logger.info("Checking if account exists for user", {
...fctx,
userId,
});
return ResultAsync.fromSafePromise(Promise.resolve(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())),
(error) => {
logger.error("Failed to hash password", {
...fctx,
error,
});
return this.dbError(
fctx,
error instanceof Error ? error.message : String(error),
);
},
).andThen((password) => {
const aid = nanoid();
return ResultAsync.fromPromise(
this.db
.insert(account)
.values({
id: aid,
accountId: userId,
providerId: "credential",
userId: userId,
password,
createdAt: new Date(),
updatedAt: new Date(),
})
.execute(),
this.db.query.account.findFirst({
where: eq(account.userId, userId),
}),
(error) => {
logger.error("Failed to create account", {
logger.error("Failed to check account existence", {
...fctx,
error,
});
@@ -110,14 +61,82 @@ export class AccountRepository {
: String(error),
);
},
).map(() => {
logger.info("Account created successfully for user", {
...fctx,
userId,
).andThen((existingAccount) => {
if (existingAccount) {
logger.info("Account already exists for user", {
...fctx,
userId,
});
return ResultAsync.fromSafePromise(
Promise.resolve(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()),
),
(error) => {
logger.error("Failed to hash password", {
...fctx,
error,
});
return this.dbError(
fctx,
error instanceof Error
? error.message
: String(error),
);
},
).andThen((password) => {
const aid = nanoid();
return ResultAsync.fromPromise(
this.db
.insert(account)
.values({
id: aid,
accountId: userId,
providerId: "credential",
userId: userId,
password,
createdAt: new Date(),
updatedAt: new Date(),
})
.execute(),
(error) => {
logger.error("Failed to create account", {
...fctx,
error,
});
return this.dbError(
fctx,
error instanceof Error
? error.message
: String(error),
);
},
).map(() => {
logger.info(
"Account created successfully for user",
{
...fctx,
userId,
},
);
return false;
});
});
return false;
});
});
},
});
}
@@ -126,70 +145,25 @@ export class AccountRepository {
userId: string,
password: string,
): ResultAsync<string, Err> {
logger.info("Starting password rotation for user", {
...fctx,
userId,
});
return ResultAsync.fromPromise(
this.db.query.account.findFirst({
where: eq(account.userId, userId),
}),
(error) => {
logger.error(
"Failed to check account existence for password rotation",
{
...fctx,
error,
},
);
return this.dbError(
fctx,
error instanceof Error ? error.message : String(error),
);
},
).andThen((existingAccount) => {
if (!existingAccount) {
logger.error("Account not found for user", {
return traceResultAsync({
name: "logic.user.repository.rotatePassword",
fctx,
attributes: { "app.user.id": userId },
fn: () => {
logger.info("Starting password rotation for user", {
...fctx,
userId,
});
return ResultAsync.fromSafePromise(
Promise.resolve(this.accountNotFound(fctx)),
).andThen((err) =>
ResultAsync.fromSafePromise(Promise.reject(err)),
);
}
return ResultAsync.fromPromise(
auth.$context.then((ctx) => ctx.password.hash(password)),
(error) => {
logger.error("Failed to hash password for rotation", {
...fctx,
error,
});
return this.dbError(
fctx,
error instanceof Error ? error.message : String(error),
);
},
).andThen((hashed) => {
logger.info("Updating user's password in database", {
...fctx,
});
return ResultAsync.fromPromise(
this.db
.update(account)
.set({ password: hashed })
.where(eq(account.userId, userId))
.returning()
.execute(),
this.db.query.account.findFirst({
where: eq(account.userId, userId),
}),
(error) => {
logger.error("Failed to update password", {
...fctx,
error,
});
logger.error(
"Failed to check account existence for password rotation",
{ ...fctx, error },
);
return this.dbError(
fctx,
error instanceof Error
@@ -197,17 +171,76 @@ export class AccountRepository {
: String(error),
);
},
).map((result) => {
logger.info("User's password updated successfully", {
...fctx,
).andThen((existingAccount) => {
if (!existingAccount) {
logger.error("Account not found for user", {
...fctx,
userId,
});
return ResultAsync.fromSafePromise(
Promise.resolve(this.accountNotFound(fctx)),
).andThen((err) =>
ResultAsync.fromSafePromise(Promise.reject(err)),
);
}
return ResultAsync.fromPromise(
auth.$context.then((ctx) =>
ctx.password.hash(password),
),
(error) => {
logger.error(
"Failed to hash password for rotation",
{
...fctx,
error,
},
);
return this.dbError(
fctx,
error instanceof Error
? error.message
: String(error),
);
},
).andThen((hashed) => {
logger.info("Updating user's password in database", {
...fctx,
});
return ResultAsync.fromPromise(
this.db
.update(account)
.set({ password: hashed })
.where(eq(account.userId, userId))
.returning()
.execute(),
(error) => {
logger.error("Failed to update password", {
...fctx,
error,
});
return this.dbError(
fctx,
error instanceof Error
? error.message
: String(error),
);
},
).map((result) => {
logger.info(
"User's password updated successfully",
{ ...fctx },
);
logger.debug("Password rotation result", {
...fctx,
result,
});
return password;
});
});
logger.debug("Password rotation result", {
...fctx,
result,
});
return password;
});
});
},
});
}
}