& so it begins
This commit is contained in:
213
packages/logic/domains/user/account.repository.ts
Normal file
213
packages/logic/domains/user/account.repository.ts
Normal file
@@ -0,0 +1,213 @@
|
||||
import { FlowExecCtx } from "@/core/flow.execution.context";
|
||||
import { ERROR_CODES, type Err } from "@pkg/result";
|
||||
import { getError, logger } from "@pkg/logger";
|
||||
import { auth } from "../auth/config.base";
|
||||
import { account } from "@pkg/db/schema";
|
||||
import { ResultAsync } from "neverthrow";
|
||||
import { Database, eq } from "@pkg/db";
|
||||
import { nanoid } from "nanoid";
|
||||
|
||||
export class AccountRepository {
|
||||
constructor(private db: Database) {}
|
||||
|
||||
private dbError(fctx: FlowExecCtx, detail: string): Err {
|
||||
return getError({
|
||||
flowId: fctx.flowId,
|
||||
code: ERROR_CODES.DATABASE_ERROR,
|
||||
message: "Database operation failed",
|
||||
description: "Please try again later",
|
||||
detail,
|
||||
});
|
||||
}
|
||||
|
||||
private accountNotFound(fctx: FlowExecCtx): Err {
|
||||
return getError({
|
||||
flowId: fctx.flowId,
|
||||
code: ERROR_CODES.NOT_FOUND,
|
||||
message: "Account not found",
|
||||
description: "Please try again later",
|
||||
detail: "Account not found for user",
|
||||
});
|
||||
}
|
||||
|
||||
ensureAccountExists(
|
||||
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", {
|
||||
...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;
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
rotatePassword(
|
||||
fctx: FlowExecCtx,
|
||||
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", {
|
||||
...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;
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
55
packages/logic/domains/user/controller.ts
Normal file
55
packages/logic/domains/user/controller.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { FlowExecCtx } from "@/core/flow.execution.context";
|
||||
import { AccountRepository } from "./account.repository";
|
||||
import { UserRepository } from "./repository";
|
||||
import { db } from "@pkg/db";
|
||||
|
||||
export class UserController {
|
||||
constructor(
|
||||
private userRepository: UserRepository,
|
||||
private accountRepo: AccountRepository,
|
||||
) {}
|
||||
|
||||
getUserInfo(fctx: FlowExecCtx, userId: string) {
|
||||
return this.userRepository.getUserInfo(fctx, userId);
|
||||
}
|
||||
|
||||
ensureAccountExists(fctx: FlowExecCtx, userId: string) {
|
||||
return this.accountRepo.ensureAccountExists(fctx, userId);
|
||||
}
|
||||
|
||||
isUsernameAvailable(fctx: FlowExecCtx, username: string) {
|
||||
return this.userRepository.isUsernameAvailable(fctx, username);
|
||||
}
|
||||
|
||||
updateLastVerified2FaAtToNow(fctx: FlowExecCtx, userId: string) {
|
||||
return this.userRepository.updateLastVerified2FaAtToNow(fctx, userId);
|
||||
}
|
||||
|
||||
banUser(
|
||||
fctx: FlowExecCtx,
|
||||
userId: string,
|
||||
reason: string,
|
||||
banExpiresAt: Date,
|
||||
) {
|
||||
return this.userRepository.banUser(fctx, userId, reason, banExpiresAt);
|
||||
}
|
||||
|
||||
isUserBanned(fctx: FlowExecCtx, userId: string) {
|
||||
return this.userRepository.isUserBanned(fctx, userId);
|
||||
}
|
||||
|
||||
getBanInfo(fctx: FlowExecCtx, userId: string) {
|
||||
return this.userRepository.getBanInfo(fctx, userId);
|
||||
}
|
||||
|
||||
rotatePassword(fctx: FlowExecCtx, userId: string, password: string) {
|
||||
return this.accountRepo.rotatePassword(fctx, userId, password);
|
||||
}
|
||||
}
|
||||
|
||||
export function getUserController(): UserController {
|
||||
return new UserController(
|
||||
new UserRepository(db),
|
||||
new AccountRepository(db),
|
||||
);
|
||||
}
|
||||
159
packages/logic/domains/user/data.ts
Normal file
159
packages/logic/domains/user/data.ts
Normal file
@@ -0,0 +1,159 @@
|
||||
import { Session } from "better-auth";
|
||||
import * as v from "valibot";
|
||||
|
||||
export type { Session } from "better-auth";
|
||||
|
||||
export type ModifiedSession = Session & { isCurrent?: boolean };
|
||||
|
||||
// User role enum
|
||||
export enum UserRoleMap {
|
||||
user = "user",
|
||||
admin = "admin",
|
||||
}
|
||||
|
||||
// User role schema
|
||||
export const userRoleSchema = v.picklist(["user", "admin"]);
|
||||
export type UserRole = v.InferOutput<typeof userRoleSchema>;
|
||||
|
||||
// User schema
|
||||
export const userSchema = v.object({
|
||||
id: v.string(),
|
||||
name: v.string(),
|
||||
email: v.string(),
|
||||
emailVerified: v.boolean(),
|
||||
image: v.optional(v.string()),
|
||||
createdAt: v.date(),
|
||||
updatedAt: v.date(),
|
||||
username: v.optional(v.string()),
|
||||
displayUsername: v.optional(v.string()),
|
||||
role: v.optional(v.string()),
|
||||
banned: v.optional(v.boolean()),
|
||||
banReason: v.optional(v.string()),
|
||||
banExpires: v.optional(v.date()),
|
||||
onboardingDone: v.optional(v.boolean()),
|
||||
last2FAVerifiedAt: v.optional(v.date()),
|
||||
parentId: v.optional(v.string()),
|
||||
});
|
||||
export type User = v.InferOutput<typeof userSchema>;
|
||||
|
||||
// Account schema
|
||||
export const accountSchema = v.object({
|
||||
id: v.string(),
|
||||
accountId: v.string(),
|
||||
providerId: v.string(),
|
||||
userId: v.string(),
|
||||
accessToken: v.string(),
|
||||
refreshToken: v.string(),
|
||||
idToken: v.string(),
|
||||
accessTokenExpiresAt: v.date(),
|
||||
refreshTokenExpiresAt: v.date(),
|
||||
scope: v.string(),
|
||||
password: v.string(),
|
||||
createdAt: v.date(),
|
||||
updatedAt: v.date(),
|
||||
});
|
||||
export type Account = v.InferOutput<typeof accountSchema>;
|
||||
|
||||
// Ensure account exists schema
|
||||
export const ensureAccountExistsSchema = v.object({
|
||||
userId: v.string(),
|
||||
});
|
||||
export type EnsureAccountExists = v.InferOutput<
|
||||
typeof ensureAccountExistsSchema
|
||||
>;
|
||||
|
||||
// Ban info schema
|
||||
export const banInfoSchema = v.object({
|
||||
banned: v.boolean(),
|
||||
reason: v.optional(v.string()),
|
||||
expires: v.optional(v.date()),
|
||||
});
|
||||
export type BanInfo = v.InferOutput<typeof banInfoSchema>;
|
||||
|
||||
// Ban user schema
|
||||
export const banUserSchema = v.object({
|
||||
userId: v.string(),
|
||||
reason: v.string(),
|
||||
banExpiresAt: v.date(),
|
||||
});
|
||||
export type BanUser = v.InferOutput<typeof banUserSchema>;
|
||||
|
||||
// Check username availability schema
|
||||
export const checkUsernameSchema = v.object({
|
||||
username: v.string(),
|
||||
});
|
||||
export type CheckUsername = v.InferOutput<typeof checkUsernameSchema>;
|
||||
|
||||
// Rotate password schema
|
||||
export const rotatePasswordSchema = v.object({
|
||||
userId: v.string(),
|
||||
password: v.string(),
|
||||
});
|
||||
export type RotatePassword = v.InferOutput<typeof rotatePasswordSchema>;
|
||||
|
||||
// View Model specific types
|
||||
|
||||
// Search and filter types
|
||||
export const searchFieldSchema = v.picklist(["email", "name", "username"]);
|
||||
export type SearchField = v.InferOutput<typeof searchFieldSchema>;
|
||||
|
||||
export const searchOperatorSchema = v.picklist([
|
||||
"contains",
|
||||
"starts_with",
|
||||
"ends_with",
|
||||
]);
|
||||
export type SearchOperator = v.InferOutput<typeof searchOperatorSchema>;
|
||||
|
||||
export const filterOperatorSchema = v.picklist([
|
||||
"eq",
|
||||
"ne",
|
||||
"lt",
|
||||
"lte",
|
||||
"gt",
|
||||
"gte",
|
||||
]);
|
||||
export type FilterOperator = v.InferOutput<typeof filterOperatorSchema>;
|
||||
|
||||
export const sortDirectionSchema = v.picklist(["asc", "desc"]);
|
||||
export type SortDirection = v.InferOutput<typeof sortDirectionSchema>;
|
||||
|
||||
// Users query state
|
||||
export const usersQueryStateSchema = v.object({
|
||||
// searching
|
||||
searchValue: v.optional(v.string()),
|
||||
searchField: v.optional(searchFieldSchema),
|
||||
searchOperator: v.optional(searchOperatorSchema),
|
||||
|
||||
// pagination
|
||||
limit: v.pipe(v.number(), v.integer()),
|
||||
offset: v.pipe(v.number(), v.integer()),
|
||||
|
||||
// sorting
|
||||
sortBy: v.optional(v.string()),
|
||||
sortDirection: v.optional(sortDirectionSchema),
|
||||
|
||||
// filtering
|
||||
filterField: v.optional(v.string()),
|
||||
filterValue: v.optional(v.union([v.string(), v.number(), v.boolean()])),
|
||||
filterOperator: v.optional(filterOperatorSchema),
|
||||
});
|
||||
export type UsersQueryState = v.InferOutput<typeof usersQueryStateSchema>;
|
||||
|
||||
// UI View Model types
|
||||
|
||||
export const banExpiryModeSchema = v.picklist([
|
||||
"never",
|
||||
"1d",
|
||||
"7d",
|
||||
"30d",
|
||||
"custom",
|
||||
]);
|
||||
export type BanExpiryMode = v.InferOutput<typeof banExpiryModeSchema>;
|
||||
|
||||
export const createUserFormSchema = v.object({
|
||||
email: v.string(),
|
||||
password: v.string(),
|
||||
name: v.string(),
|
||||
role: v.union([userRoleSchema, v.array(userRoleSchema)]),
|
||||
});
|
||||
export type CreateUserForm = v.InferOutput<typeof createUserFormSchema>;
|
||||
77
packages/logic/domains/user/errors.ts
Normal file
77
packages/logic/domains/user/errors.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { FlowExecCtx } from "@/core/flow.execution.context";
|
||||
import { ERROR_CODES, type Err } from "@pkg/result";
|
||||
import { getError } from "@pkg/logger";
|
||||
|
||||
export const userErrors = {
|
||||
dbError: (fctx: FlowExecCtx, detail: string): Err =>
|
||||
getError({
|
||||
flowId: fctx.flowId,
|
||||
code: ERROR_CODES.DATABASE_ERROR,
|
||||
message: "Database operation failed",
|
||||
description: "Please try again later",
|
||||
detail,
|
||||
}),
|
||||
|
||||
userNotFound: (fctx: FlowExecCtx): Err =>
|
||||
getError({
|
||||
flowId: fctx.flowId,
|
||||
code: ERROR_CODES.NOT_FOUND,
|
||||
message: "User not found",
|
||||
description: "Try with a different user id",
|
||||
detail: "User not found in database",
|
||||
}),
|
||||
|
||||
usernameCheckFailed: (fctx: FlowExecCtx, detail: string): Err =>
|
||||
getError({
|
||||
flowId: fctx.flowId,
|
||||
code: ERROR_CODES.DATABASE_ERROR,
|
||||
message: "An error occurred while checking username availability",
|
||||
description: "Try again later",
|
||||
detail,
|
||||
}),
|
||||
|
||||
banOperationFailed: (fctx: FlowExecCtx, detail: string): Err =>
|
||||
getError({
|
||||
flowId: fctx.flowId,
|
||||
code: ERROR_CODES.DATABASE_ERROR,
|
||||
message: "Failed to perform ban operation",
|
||||
description: "Please try again later",
|
||||
detail,
|
||||
}),
|
||||
|
||||
unbanFailed: (fctx: FlowExecCtx, detail: string): Err =>
|
||||
getError({
|
||||
flowId: fctx.flowId,
|
||||
code: ERROR_CODES.DATABASE_ERROR,
|
||||
message: "Failed to unban user",
|
||||
description: "Please try again later",
|
||||
detail,
|
||||
}),
|
||||
|
||||
updateFailed: (fctx: FlowExecCtx, detail: string): Err =>
|
||||
getError({
|
||||
flowId: fctx.flowId,
|
||||
code: ERROR_CODES.DATABASE_ERROR,
|
||||
message: "Failed to update user",
|
||||
description: "Please try again later",
|
||||
detail,
|
||||
}),
|
||||
|
||||
getUserInfoFailed: (fctx: FlowExecCtx, detail: string): Err =>
|
||||
getError({
|
||||
flowId: fctx.flowId,
|
||||
code: ERROR_CODES.DATABASE_ERROR,
|
||||
message: "An error occurred while getting user info",
|
||||
description: "Try again later",
|
||||
detail,
|
||||
}),
|
||||
|
||||
getBanInfoFailed: (fctx: FlowExecCtx, detail: string): Err =>
|
||||
getError({
|
||||
flowId: fctx.flowId,
|
||||
code: ERROR_CODES.DATABASE_ERROR,
|
||||
message: "An error occurred while getting ban info",
|
||||
description: "Try again later",
|
||||
detail,
|
||||
}),
|
||||
};
|
||||
289
packages/logic/domains/user/repository.ts
Normal file
289
packages/logic/domains/user/repository.ts
Normal file
@@ -0,0 +1,289 @@
|
||||
import { ResultAsync, errAsync, okAsync } from "neverthrow";
|
||||
import { FlowExecCtx } from "@core/flow.execution.context";
|
||||
import { type Err } from "@pkg/result";
|
||||
import { Database, eq } from "@pkg/db";
|
||||
import { BanInfo, User } from "./data";
|
||||
import { user } from "@pkg/db/schema";
|
||||
import { userErrors } from "./errors";
|
||||
import { logger } from "@pkg/logger";
|
||||
|
||||
export class UserRepository {
|
||||
constructor(private db: Database) {}
|
||||
|
||||
getUserInfo(fctx: FlowExecCtx, userId: string): ResultAsync<User, Err> {
|
||||
logger.info("Getting user info for user", {
|
||||
flowId: fctx.flowId,
|
||||
userId,
|
||||
});
|
||||
|
||||
return ResultAsync.fromPromise(
|
||||
this.db.query.user.findFirst({
|
||||
where: eq(user.id, userId),
|
||||
}),
|
||||
(error) => {
|
||||
logger.error("Failed to get user info", {
|
||||
flowId: fctx.flowId,
|
||||
error,
|
||||
});
|
||||
return userErrors.getUserInfoFailed(
|
||||
fctx,
|
||||
error instanceof Error ? error.message : String(error),
|
||||
);
|
||||
},
|
||||
).andThen((userData) => {
|
||||
if (!userData) {
|
||||
logger.error("User not found with id", {
|
||||
flowId: fctx.flowId,
|
||||
userId,
|
||||
});
|
||||
return errAsync(userErrors.userNotFound(fctx));
|
||||
}
|
||||
|
||||
logger.info("User info retrieved successfully for user", {
|
||||
flowId: fctx.flowId,
|
||||
userId,
|
||||
});
|
||||
return okAsync(userData as User);
|
||||
});
|
||||
}
|
||||
|
||||
updateLastVerified2FaAtToNow(
|
||||
fctx: FlowExecCtx,
|
||||
userId: string,
|
||||
): ResultAsync<boolean, Err> {
|
||||
logger.info("Updating last 2FA verified timestamp for user", {
|
||||
flowId: fctx.flowId,
|
||||
userId,
|
||||
});
|
||||
|
||||
return ResultAsync.fromPromise(
|
||||
this.db
|
||||
.update(user)
|
||||
.set({ last2FAVerifiedAt: new Date() })
|
||||
.where(eq(user.id, userId))
|
||||
.execute(),
|
||||
(error) => {
|
||||
logger.error("Failed to update last 2FA verified timestamp", {
|
||||
...fctx,
|
||||
error,
|
||||
});
|
||||
return userErrors.updateFailed(
|
||||
fctx,
|
||||
error instanceof Error ? error.message : String(error),
|
||||
);
|
||||
},
|
||||
).map(() => {
|
||||
logger.info("Last 2FA verified timestamp updated successfully", {
|
||||
...fctx,
|
||||
});
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
isUsernameAvailable(
|
||||
fctx: FlowExecCtx,
|
||||
username: string,
|
||||
): ResultAsync<boolean, Err> {
|
||||
logger.info("Checking username availability", {
|
||||
...fctx,
|
||||
username,
|
||||
});
|
||||
|
||||
return ResultAsync.fromPromise(
|
||||
this.db.query.user.findFirst({
|
||||
where: eq(user.username, username),
|
||||
}),
|
||||
(error) => {
|
||||
logger.error("Failed to check username availability", {
|
||||
...fctx,
|
||||
error,
|
||||
});
|
||||
return userErrors.usernameCheckFailed(
|
||||
fctx,
|
||||
error instanceof Error ? error.message : String(error),
|
||||
);
|
||||
},
|
||||
).map((existingUser) => {
|
||||
const isAvailable = !existingUser?.id;
|
||||
logger.info("Username availability checked", {
|
||||
...fctx,
|
||||
username,
|
||||
isAvailable,
|
||||
});
|
||||
return isAvailable;
|
||||
});
|
||||
}
|
||||
|
||||
banUser(
|
||||
fctx: FlowExecCtx,
|
||||
userId: string,
|
||||
reason: string,
|
||||
banExpiresAt: Date,
|
||||
): ResultAsync<boolean, Err> {
|
||||
logger.info("Banning user", {
|
||||
...fctx,
|
||||
userId,
|
||||
banExpiresAt: banExpiresAt.toISOString(),
|
||||
reason,
|
||||
});
|
||||
|
||||
return ResultAsync.fromPromise(
|
||||
this.db
|
||||
.update(user)
|
||||
.set({
|
||||
banned: true,
|
||||
banReason: reason,
|
||||
banExpires: banExpiresAt,
|
||||
})
|
||||
.where(eq(user.id, userId))
|
||||
.execute(),
|
||||
(error) => {
|
||||
logger.error("Failed to ban user", { ...fctx, error });
|
||||
return userErrors.banOperationFailed(
|
||||
fctx,
|
||||
error instanceof Error ? error.message : String(error),
|
||||
);
|
||||
},
|
||||
).map(() => {
|
||||
logger.info("User has been banned", {
|
||||
...fctx,
|
||||
userId,
|
||||
banExpiresAt: banExpiresAt.toISOString(),
|
||||
});
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
isUserBanned(fctx: FlowExecCtx, userId: string): ResultAsync<boolean, Err> {
|
||||
logger.info("Checking ban status for user", { ...fctx, userId });
|
||||
|
||||
return ResultAsync.fromPromise(
|
||||
this.db.query.user.findFirst({
|
||||
where: eq(user.id, userId),
|
||||
columns: {
|
||||
banned: true,
|
||||
banExpires: true,
|
||||
},
|
||||
}),
|
||||
(error) => {
|
||||
logger.error("Failed to check ban status", {
|
||||
...fctx,
|
||||
error,
|
||||
});
|
||||
return userErrors.dbError(
|
||||
fctx,
|
||||
error instanceof Error ? error.message : String(error),
|
||||
);
|
||||
},
|
||||
).andThen((userData) => {
|
||||
if (!userData) {
|
||||
logger.error("User not found when checking ban status", {
|
||||
...fctx,
|
||||
});
|
||||
return errAsync(userErrors.userNotFound(fctx));
|
||||
}
|
||||
|
||||
// If not banned, return false
|
||||
if (!userData.banned) {
|
||||
logger.info("User is not banned", { ...fctx, userId });
|
||||
return okAsync(false);
|
||||
}
|
||||
|
||||
// If banned but no expiry date, consider permanently banned
|
||||
if (!userData.banExpires) {
|
||||
logger.info("User is permanently banned", { ...fctx, userId });
|
||||
return okAsync(true);
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
if (userData.banExpires <= now) {
|
||||
logger.info("User ban has expired, removing ban status", {
|
||||
...fctx,
|
||||
userId,
|
||||
});
|
||||
|
||||
return ResultAsync.fromPromise(
|
||||
this.db
|
||||
.update(user)
|
||||
.set({
|
||||
banned: false,
|
||||
banReason: null,
|
||||
banExpires: null,
|
||||
})
|
||||
.where(eq(user.id, userId))
|
||||
.execute(),
|
||||
(error) => {
|
||||
logger.error("Failed to unban user after expiry", {
|
||||
...fctx,
|
||||
error,
|
||||
});
|
||||
return userErrors.unbanFailed(
|
||||
fctx,
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: String(error),
|
||||
);
|
||||
},
|
||||
)
|
||||
.map(() => {
|
||||
logger.info("User has been unbanned after expiry", {
|
||||
...fctx,
|
||||
userId,
|
||||
});
|
||||
return false;
|
||||
})
|
||||
.orElse((error) => {
|
||||
logger.error(
|
||||
"Failed to unban user after expiry, still returning banned status",
|
||||
{ ...fctx, userId, error },
|
||||
);
|
||||
// Still return banned status since we couldn't update
|
||||
return okAsync(true);
|
||||
});
|
||||
}
|
||||
|
||||
logger.info("User is banned", {
|
||||
...fctx,
|
||||
userId,
|
||||
banExpires: userData.banExpires.toISOString(),
|
||||
});
|
||||
return okAsync(true);
|
||||
});
|
||||
}
|
||||
|
||||
getBanInfo(fctx: FlowExecCtx, userId: string): ResultAsync<BanInfo, Err> {
|
||||
logger.info("Getting ban info for user", { ...fctx, userId });
|
||||
|
||||
return ResultAsync.fromPromise(
|
||||
this.db.query.user.findFirst({
|
||||
where: eq(user.id, userId),
|
||||
columns: { banned: true, banReason: true, banExpires: true },
|
||||
}),
|
||||
(error) => {
|
||||
logger.error("Failed to get ban info", { ...fctx, error });
|
||||
return userErrors.getBanInfoFailed(
|
||||
fctx,
|
||||
error instanceof Error ? error.message : String(error),
|
||||
);
|
||||
},
|
||||
).andThen((userData) => {
|
||||
if (!userData) {
|
||||
logger.error("User not found when getting ban info", {
|
||||
...fctx,
|
||||
});
|
||||
return errAsync(userErrors.userNotFound(fctx));
|
||||
}
|
||||
|
||||
logger.info("Ban info retrieved successfully for user", {
|
||||
...fctx,
|
||||
userId,
|
||||
});
|
||||
|
||||
return okAsync({
|
||||
banned: userData.banned || false,
|
||||
reason: userData.banReason || undefined,
|
||||
expires: userData.banExpires || undefined,
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
165
packages/logic/domains/user/router.ts
Normal file
165
packages/logic/domains/user/router.ts
Normal file
@@ -0,0 +1,165 @@
|
||||
import {
|
||||
banUserSchema,
|
||||
checkUsernameSchema,
|
||||
ensureAccountExistsSchema,
|
||||
rotatePasswordSchema,
|
||||
} from "./data";
|
||||
import { HonoContext } from "@core/hono.helpers";
|
||||
import { sValidator } from "@hono/standard-validator";
|
||||
import { getUserController } from "./controller";
|
||||
import { Hono } from "hono";
|
||||
|
||||
const uc = getUserController();
|
||||
|
||||
export const usersRouter = new Hono<HonoContext>()
|
||||
// Get current user info
|
||||
.get("/me", async (c) => {
|
||||
const fctx = c.env.locals.fCtx;
|
||||
const userId = c.env.locals.user?.id;
|
||||
|
||||
if (!userId) {
|
||||
return c.json(
|
||||
{
|
||||
error: {
|
||||
code: "UNAUTHORIZED",
|
||||
message: "User not authenticated",
|
||||
description: "Please log in",
|
||||
detail: "No user ID found in session",
|
||||
},
|
||||
},
|
||||
401,
|
||||
);
|
||||
}
|
||||
|
||||
const res = await uc.getUserInfo(fctx, userId);
|
||||
return c.json(
|
||||
res.isOk()
|
||||
? { data: res.value, error: null }
|
||||
: { data: null, error: res.error },
|
||||
res.isOk() ? 200 : 400,
|
||||
);
|
||||
})
|
||||
|
||||
// Get user info by ID
|
||||
.get("/:userId", async (c) => {
|
||||
const fctx = c.env.locals.fCtx;
|
||||
const userId = c.req.param("userId");
|
||||
|
||||
const res = await uc.getUserInfo(fctx, userId);
|
||||
return c.json(
|
||||
res.isOk()
|
||||
? { data: res.value, error: null }
|
||||
: { data: null, error: res.error },
|
||||
res.isOk() ? 200 : 400,
|
||||
);
|
||||
})
|
||||
|
||||
// Ensure account exists
|
||||
.put(
|
||||
"/ensure-account-exists",
|
||||
sValidator("json", ensureAccountExistsSchema),
|
||||
async (c) => {
|
||||
const fctx = c.env.locals.fCtx;
|
||||
const data = c.req.valid("json");
|
||||
|
||||
const res = await uc.ensureAccountExists(fctx, data.userId);
|
||||
return c.json(
|
||||
res.isOk()
|
||||
? { data: res.value, error: null }
|
||||
: { data: null, error: res.error },
|
||||
res.isOk() ? 200 : 400,
|
||||
);
|
||||
},
|
||||
)
|
||||
|
||||
// Check username availability
|
||||
.post(
|
||||
"/check-username",
|
||||
sValidator("json", checkUsernameSchema),
|
||||
async (c) => {
|
||||
const fctx = c.env.locals.fCtx;
|
||||
const data = c.req.valid("json");
|
||||
|
||||
const res = await uc.isUsernameAvailable(fctx, data.username);
|
||||
return c.json(
|
||||
res.isOk()
|
||||
? { data: res.value, error: null }
|
||||
: { data: null, error: res.error },
|
||||
res.isOk() ? 200 : 400,
|
||||
);
|
||||
},
|
||||
)
|
||||
|
||||
// Update last 2FA verification time
|
||||
.put("/update-2fa-verified/:userId", async (c) => {
|
||||
const fctx = c.env.locals.fCtx;
|
||||
const userId = c.req.param("userId");
|
||||
|
||||
const res = await uc.updateLastVerified2FaAtToNow(fctx, userId);
|
||||
return c.json(
|
||||
res.isOk()
|
||||
? { data: res.value, error: null }
|
||||
: { data: null, error: res.error },
|
||||
res.isOk() ? 200 : 400,
|
||||
);
|
||||
})
|
||||
|
||||
// Ban user
|
||||
.post("/ban", sValidator("json", banUserSchema), async (c) => {
|
||||
const fctx = c.env.locals.fCtx;
|
||||
const data = c.req.valid("json");
|
||||
|
||||
const res = await uc.banUser(fctx, data.userId, data.reason, data.banExpiresAt);
|
||||
return c.json(
|
||||
res.isOk()
|
||||
? { data: res.value, error: null }
|
||||
: { data: null, error: res.error },
|
||||
res.isOk() ? 200 : 400,
|
||||
);
|
||||
})
|
||||
|
||||
// Check if user is banned
|
||||
.get("/:userId/is-banned", async (c) => {
|
||||
const fctx = c.env.locals.fCtx;
|
||||
const userId = c.req.param("userId");
|
||||
|
||||
const res = await uc.isUserBanned(fctx, userId);
|
||||
return c.json(
|
||||
res.isOk()
|
||||
? { data: res.value, error: null }
|
||||
: { data: null, error: res.error },
|
||||
res.isOk() ? 200 : 400,
|
||||
);
|
||||
})
|
||||
|
||||
// Get ban info
|
||||
.get("/:userId/ban-info", async (c) => {
|
||||
const fctx = c.env.locals.fCtx;
|
||||
const userId = c.req.param("userId");
|
||||
|
||||
const res = await uc.getBanInfo(fctx, userId);
|
||||
return c.json(
|
||||
res.isOk()
|
||||
? { data: res.value, error: null }
|
||||
: { data: null, error: res.error },
|
||||
res.isOk() ? 200 : 400,
|
||||
);
|
||||
})
|
||||
|
||||
// Rotate password
|
||||
.put(
|
||||
"/rotate-password",
|
||||
sValidator("json", rotatePasswordSchema),
|
||||
async (c) => {
|
||||
const fctx = c.env.locals.fCtx;
|
||||
const data = c.req.valid("json");
|
||||
|
||||
const res = await uc.rotatePassword(fctx, data.userId, data.password);
|
||||
return c.json(
|
||||
res.isOk()
|
||||
? { data: res.value, error: null }
|
||||
: { data: null, error: res.error },
|
||||
res.isOk() ? 200 : 400,
|
||||
);
|
||||
},
|
||||
);
|
||||
Reference in New Issue
Block a user