Refactor 4 with bugfixes

This commit is contained in:
2026-03-04 03:56:23 +01:00
parent be91b6dfe8
commit 0ed9ca93d3
51 changed files with 1552 additions and 996 deletions

View File

@@ -0,0 +1,19 @@
import { DataSource } from 'typeorm';
import { JoinRequestEntity } from '../../../entities';
import { CreateJoinRequestCommand } from '../../types';
export async function handleCreateJoinRequest(command: CreateJoinRequestCommand, dataSource: DataSource): Promise<void> {
const repo = dataSource.getRepository(JoinRequestEntity);
const { request } = command.payload;
const entity = repo.create({
id: request.id,
serverId: request.serverId,
userId: request.userId,
userPublicKey: request.userPublicKey,
displayName: request.displayName,
status: request.status,
createdAt: request.createdAt
});
await repo.save(entity);
}

View File

@@ -0,0 +1,10 @@
import { DataSource } from 'typeorm';
import { ServerEntity, JoinRequestEntity } from '../../../entities';
import { DeleteServerCommand } from '../../types';
export async function handleDeleteServer(command: DeleteServerCommand, dataSource: DataSource): Promise<void> {
const { serverId } = command.payload;
await dataSource.getRepository(JoinRequestEntity).delete({ serverId });
await dataSource.getRepository(ServerEntity).delete(serverId);
}

View File

@@ -0,0 +1,10 @@
import { DataSource, LessThan } from 'typeorm';
import { JoinRequestEntity } from '../../../entities';
import { DeleteStaleJoinRequestsCommand } from '../../types';
export async function handleDeleteStaleJoinRequests(command: DeleteStaleJoinRequestsCommand, dataSource: DataSource): Promise<void> {
const repo = dataSource.getRepository(JoinRequestEntity);
const cutoff = Date.now() - command.payload.maxAgeMs;
await repo.delete({ createdAt: LessThan(cutoff) });
}

View File

@@ -0,0 +1,17 @@
import { DataSource } from 'typeorm';
import { AuthUserEntity } from '../../../entities';
import { RegisterUserCommand } from '../../types';
export async function handleRegisterUser(command: RegisterUserCommand, dataSource: DataSource): Promise<void> {
const repo = dataSource.getRepository(AuthUserEntity);
const { user } = command.payload;
const entity = repo.create({
id: user.id,
username: user.username,
passwordHash: user.passwordHash,
displayName: user.displayName,
createdAt: user.createdAt
});
await repo.save(entity);
}

View File

@@ -0,0 +1,10 @@
import { DataSource } from 'typeorm';
import { JoinRequestEntity } from '../../../entities';
import { UpdateJoinRequestStatusCommand } from '../../types';
export async function handleUpdateJoinRequestStatus(command: UpdateJoinRequestStatusCommand, dataSource: DataSource): Promise<void> {
const repo = dataSource.getRepository(JoinRequestEntity);
const { requestId, status } = command.payload;
await repo.update(requestId, { status });
}

View File

@@ -0,0 +1,23 @@
import { DataSource } from 'typeorm';
import { ServerEntity } from '../../../entities';
import { UpsertServerCommand } from '../../types';
export async function handleUpsertServer(command: UpsertServerCommand, dataSource: DataSource): Promise<void> {
const repo = dataSource.getRepository(ServerEntity);
const { server } = command.payload;
const entity = repo.create({
id: server.id,
name: server.name,
description: server.description ?? null,
ownerId: server.ownerId,
ownerPublicKey: server.ownerPublicKey,
isPrivate: server.isPrivate ? 1 : 0,
maxUsers: server.maxUsers,
currentUsers: server.currentUsers,
tags: JSON.stringify(server.tags),
createdAt: server.createdAt,
lastSeen: server.lastSeen
});
await repo.save(entity);
}

View File

@@ -0,0 +1,27 @@
import { DataSource } from 'typeorm';
import {
CommandType,
CommandTypeKey,
Command,
RegisterUserCommand,
UpsertServerCommand,
DeleteServerCommand,
CreateJoinRequestCommand,
UpdateJoinRequestStatusCommand,
DeleteStaleJoinRequestsCommand
} from '../types';
import { handleRegisterUser } from './handlers/registerUser';
import { handleUpsertServer } from './handlers/upsertServer';
import { handleDeleteServer } from './handlers/deleteServer';
import { handleCreateJoinRequest } from './handlers/createJoinRequest';
import { handleUpdateJoinRequestStatus } from './handlers/updateJoinRequestStatus';
import { handleDeleteStaleJoinRequests } from './handlers/deleteStaleJoinRequests';
export const buildCommandHandlers = (dataSource: DataSource): Record<CommandTypeKey, (command: Command) => Promise<unknown>> => ({
[CommandType.RegisterUser]: (cmd) => handleRegisterUser(cmd as RegisterUserCommand, dataSource),
[CommandType.UpsertServer]: (cmd) => handleUpsertServer(cmd as UpsertServerCommand, dataSource),
[CommandType.DeleteServer]: (cmd) => handleDeleteServer(cmd as DeleteServerCommand, dataSource),
[CommandType.CreateJoinRequest]: (cmd) => handleCreateJoinRequest(cmd as CreateJoinRequestCommand, dataSource),
[CommandType.UpdateJoinRequestStatus]: (cmd) => handleUpdateJoinRequestStatus(cmd as UpdateJoinRequestStatusCommand, dataSource),
[CommandType.DeleteStaleJoinRequests]: (cmd) => handleDeleteStaleJoinRequests(cmd as DeleteStaleJoinRequestsCommand, dataSource)
});

66
server/src/cqrs/index.ts Normal file
View File

@@ -0,0 +1,66 @@
/**
* Thin service layer - binds every CQRS handler to `getDataSource()` so
* routes can call one-liner functions instead of manually constructing
* command/query objects and passing the DataSource every time.
*/
import { getDataSource } from '../db';
import {
CommandType,
QueryType,
AuthUserPayload,
ServerPayload,
JoinRequestPayload
} from './types';
import { handleRegisterUser } from './commands/handlers/registerUser';
import { handleUpsertServer } from './commands/handlers/upsertServer';
import { handleDeleteServer } from './commands/handlers/deleteServer';
import { handleCreateJoinRequest } from './commands/handlers/createJoinRequest';
import { handleUpdateJoinRequestStatus } from './commands/handlers/updateJoinRequestStatus';
import { handleDeleteStaleJoinRequests } from './commands/handlers/deleteStaleJoinRequests';
import { handleGetUserByUsername } from './queries/handlers/getUserByUsername';
import { handleGetUserById } from './queries/handlers/getUserById';
import { handleGetAllPublicServers } from './queries/handlers/getAllPublicServers';
import { handleGetServerById } from './queries/handlers/getServerById';
import { handleGetJoinRequestById } from './queries/handlers/getJoinRequestById';
import { handleGetPendingRequestsForServer } from './queries/handlers/getPendingRequestsForServer';
// --------------- Commands ---------------
export const registerUser = (user: AuthUserPayload) =>
handleRegisterUser({ type: CommandType.RegisterUser, payload: { user } }, getDataSource());
export const upsertServer = (server: ServerPayload) =>
handleUpsertServer({ type: CommandType.UpsertServer, payload: { server } }, getDataSource());
export const deleteServer = (serverId: string) =>
handleDeleteServer({ type: CommandType.DeleteServer, payload: { serverId } }, getDataSource());
export const createJoinRequest = (request: JoinRequestPayload) =>
handleCreateJoinRequest({ type: CommandType.CreateJoinRequest, payload: { request } }, getDataSource());
export const updateJoinRequestStatus = (requestId: string, status: JoinRequestPayload['status']) =>
handleUpdateJoinRequestStatus({ type: CommandType.UpdateJoinRequestStatus, payload: { requestId, status } }, getDataSource());
export const deleteStaleJoinRequests = (maxAgeMs: number) =>
handleDeleteStaleJoinRequests({ type: CommandType.DeleteStaleJoinRequests, payload: { maxAgeMs } }, getDataSource());
// --------------- Queries ---------------
export const getUserByUsername = (username: string) =>
handleGetUserByUsername({ type: QueryType.GetUserByUsername, payload: { username } }, getDataSource());
export const getUserById = (userId: string) =>
handleGetUserById({ type: QueryType.GetUserById, payload: { userId } }, getDataSource());
export const getAllPublicServers = () =>
handleGetAllPublicServers(getDataSource());
export const getServerById = (serverId: string) =>
handleGetServerById({ type: QueryType.GetServerById, payload: { serverId } }, getDataSource());
export const getJoinRequestById = (requestId: string) =>
handleGetJoinRequestById({ type: QueryType.GetJoinRequestById, payload: { requestId } }, getDataSource());
export const getPendingRequestsForServer = (serverId: string) =>
handleGetPendingRequestsForServer({ type: QueryType.GetPendingRequestsForServer, payload: { serverId } }, getDataSource());

View File

@@ -0,0 +1,46 @@
import { AuthUserEntity } from '../entities/AuthUserEntity';
import { ServerEntity } from '../entities/ServerEntity';
import { JoinRequestEntity } from '../entities/JoinRequestEntity';
import {
AuthUserPayload,
ServerPayload,
JoinRequestPayload
} from './types';
export function rowToAuthUser(row: AuthUserEntity): AuthUserPayload {
return {
id: row.id,
username: row.username,
passwordHash: row.passwordHash,
displayName: row.displayName,
createdAt: row.createdAt
};
}
export function rowToServer(row: ServerEntity): ServerPayload {
return {
id: row.id,
name: row.name,
description: row.description ?? undefined,
ownerId: row.ownerId,
ownerPublicKey: row.ownerPublicKey,
isPrivate: !!row.isPrivate,
maxUsers: row.maxUsers,
currentUsers: row.currentUsers,
tags: JSON.parse(row.tags || '[]'),
createdAt: row.createdAt,
lastSeen: row.lastSeen
};
}
export function rowToJoinRequest(row: JoinRequestEntity): JoinRequestPayload {
return {
id: row.id,
serverId: row.serverId,
userId: row.userId,
userPublicKey: row.userPublicKey,
displayName: row.displayName,
status: row.status as JoinRequestPayload['status'],
createdAt: row.createdAt
};
}

View File

@@ -0,0 +1,10 @@
import { DataSource } from 'typeorm';
import { ServerEntity } from '../../../entities';
import { rowToServer } from '../../mappers';
export async function handleGetAllPublicServers(dataSource: DataSource) {
const repo = dataSource.getRepository(ServerEntity);
const rows = await repo.find({ where: { isPrivate: 0 } });
return rows.map(rowToServer);
}

View File

@@ -0,0 +1,11 @@
import { DataSource } from 'typeorm';
import { JoinRequestEntity } from '../../../entities';
import { GetJoinRequestByIdQuery } from '../../types';
import { rowToJoinRequest } from '../../mappers';
export async function handleGetJoinRequestById(query: GetJoinRequestByIdQuery, dataSource: DataSource) {
const repo = dataSource.getRepository(JoinRequestEntity);
const row = await repo.findOne({ where: { id: query.payload.requestId } });
return row ? rowToJoinRequest(row) : null;
}

View File

@@ -0,0 +1,11 @@
import { DataSource } from 'typeorm';
import { JoinRequestEntity } from '../../../entities';
import { GetPendingRequestsForServerQuery } from '../../types';
import { rowToJoinRequest } from '../../mappers';
export async function handleGetPendingRequestsForServer(query: GetPendingRequestsForServerQuery, dataSource: DataSource) {
const repo = dataSource.getRepository(JoinRequestEntity);
const rows = await repo.find({ where: { serverId: query.payload.serverId, status: 'pending' } });
return rows.map(rowToJoinRequest);
}

View File

@@ -0,0 +1,11 @@
import { DataSource } from 'typeorm';
import { ServerEntity } from '../../../entities';
import { GetServerByIdQuery } from '../../types';
import { rowToServer } from '../../mappers';
export async function handleGetServerById(query: GetServerByIdQuery, dataSource: DataSource) {
const repo = dataSource.getRepository(ServerEntity);
const row = await repo.findOne({ where: { id: query.payload.serverId } });
return row ? rowToServer(row) : null;
}

View File

@@ -0,0 +1,11 @@
import { DataSource } from 'typeorm';
import { AuthUserEntity } from '../../../entities';
import { GetUserByIdQuery } from '../../types';
import { rowToAuthUser } from '../../mappers';
export async function handleGetUserById(query: GetUserByIdQuery, dataSource: DataSource) {
const repo = dataSource.getRepository(AuthUserEntity);
const row = await repo.findOne({ where: { id: query.payload.userId } });
return row ? rowToAuthUser(row) : null;
}

View File

@@ -0,0 +1,11 @@
import { DataSource } from 'typeorm';
import { AuthUserEntity } from '../../../entities';
import { GetUserByUsernameQuery } from '../../types';
import { rowToAuthUser } from '../../mappers';
export async function handleGetUserByUsername(query: GetUserByUsernameQuery, dataSource: DataSource) {
const repo = dataSource.getRepository(AuthUserEntity);
const row = await repo.findOne({ where: { username: query.payload.username } });
return row ? rowToAuthUser(row) : null;
}

View File

@@ -0,0 +1,26 @@
import { DataSource } from 'typeorm';
import {
QueryType,
QueryTypeKey,
Query,
GetUserByUsernameQuery,
GetUserByIdQuery,
GetServerByIdQuery,
GetJoinRequestByIdQuery,
GetPendingRequestsForServerQuery
} from '../types';
import { handleGetUserByUsername } from './handlers/getUserByUsername';
import { handleGetUserById } from './handlers/getUserById';
import { handleGetAllPublicServers } from './handlers/getAllPublicServers';
import { handleGetServerById } from './handlers/getServerById';
import { handleGetJoinRequestById } from './handlers/getJoinRequestById';
import { handleGetPendingRequestsForServer } from './handlers/getPendingRequestsForServer';
export const buildQueryHandlers = (dataSource: DataSource): Record<QueryTypeKey, (query: Query) => Promise<unknown>> => ({
[QueryType.GetUserByUsername]: (query) => handleGetUserByUsername(query as GetUserByUsernameQuery, dataSource),
[QueryType.GetUserById]: (query) => handleGetUserById(query as GetUserByIdQuery, dataSource),
[QueryType.GetAllPublicServers]: () => handleGetAllPublicServers(dataSource),
[QueryType.GetServerById]: (query) => handleGetServerById(query as GetServerByIdQuery, dataSource),
[QueryType.GetJoinRequestById]: (query) => handleGetJoinRequestById(query as GetJoinRequestByIdQuery, dataSource),
[QueryType.GetPendingRequestsForServer]: (query) => handleGetPendingRequestsForServer(query as GetPendingRequestsForServerQuery, dataSource)
});

144
server/src/cqrs/types.ts Normal file
View File

@@ -0,0 +1,144 @@
/* ------------------------------------------------------------------ */
/* CQRS type definitions for the MetoYou server process. */
/* Commands mutate state; queries read state. */
/* ------------------------------------------------------------------ */
// --------------- Command types ---------------
export const CommandType = {
RegisterUser: 'register-user',
UpsertServer: 'upsert-server',
DeleteServer: 'delete-server',
CreateJoinRequest: 'create-join-request',
UpdateJoinRequestStatus: 'update-join-request-status',
DeleteStaleJoinRequests: 'delete-stale-join-requests'
} as const;
export type CommandTypeKey = typeof CommandType[keyof typeof CommandType];
// --------------- Query types ---------------
export const QueryType = {
GetUserByUsername: 'get-user-by-username',
GetUserById: 'get-user-by-id',
GetAllPublicServers: 'get-all-public-servers',
GetServerById: 'get-server-by-id',
GetJoinRequestById: 'get-join-request-by-id',
GetPendingRequestsForServer: 'get-pending-requests-for-server'
} as const;
export type QueryTypeKey = typeof QueryType[keyof typeof QueryType];
// --------------- Payload interfaces ---------------
export interface AuthUserPayload {
id: string;
username: string;
passwordHash: string;
displayName: string;
createdAt: number;
}
export interface ServerPayload {
id: string;
name: string;
description?: string;
ownerId: string;
ownerPublicKey: string;
isPrivate: boolean;
maxUsers: number;
currentUsers: number;
tags: string[];
createdAt: number;
lastSeen: number;
}
export interface JoinRequestPayload {
id: string;
serverId: string;
userId: string;
userPublicKey: string;
displayName: string;
status: 'pending' | 'approved' | 'rejected';
createdAt: number;
}
// --------------- Command interfaces ---------------
export interface RegisterUserCommand {
type: typeof CommandType.RegisterUser;
payload: { user: AuthUserPayload };
}
export interface UpsertServerCommand {
type: typeof CommandType.UpsertServer;
payload: { server: ServerPayload };
}
export interface DeleteServerCommand {
type: typeof CommandType.DeleteServer;
payload: { serverId: string };
}
export interface CreateJoinRequestCommand {
type: typeof CommandType.CreateJoinRequest;
payload: { request: JoinRequestPayload };
}
export interface UpdateJoinRequestStatusCommand {
type: typeof CommandType.UpdateJoinRequestStatus;
payload: { requestId: string; status: JoinRequestPayload['status'] };
}
export interface DeleteStaleJoinRequestsCommand {
type: typeof CommandType.DeleteStaleJoinRequests;
payload: { maxAgeMs: number };
}
export type Command =
| RegisterUserCommand
| UpsertServerCommand
| DeleteServerCommand
| CreateJoinRequestCommand
| UpdateJoinRequestStatusCommand
| DeleteStaleJoinRequestsCommand;
// --------------- Query interfaces ---------------
export interface GetUserByUsernameQuery {
type: typeof QueryType.GetUserByUsername;
payload: { username: string };
}
export interface GetUserByIdQuery {
type: typeof QueryType.GetUserById;
payload: { userId: string };
}
export interface GetAllPublicServersQuery {
type: typeof QueryType.GetAllPublicServers;
payload: Record<string, never>;
}
export interface GetServerByIdQuery {
type: typeof QueryType.GetServerById;
payload: { serverId: string };
}
export interface GetJoinRequestByIdQuery {
type: typeof QueryType.GetJoinRequestById;
payload: { requestId: string };
}
export interface GetPendingRequestsForServerQuery {
type: typeof QueryType.GetPendingRequestsForServer;
payload: { serverId: string };
}
export type Query =
| GetUserByUsernameQuery
| GetUserByIdQuery
| GetAllPublicServersQuery
| GetServerByIdQuery
| GetJoinRequestByIdQuery
| GetPendingRequestsForServerQuery;