- Replace Bun with pnpm 9 + Turborepo + tsx; apps/api renamed to apps/backend - Split backend into http/, modules/, lib/ layers mirroring tai-specguard - Add use-case + Result pattern, Problem Details RFC 7807, basePath /api/v1 - Mount Better Auth at /api/v1/auth, keep session-auth whitelist - Split Prisma schema into prisma/models/*, generate into generated/ - Rework packages/shared into lib/ + schemas/ with pagination DTOs - Implement health, groups, students, payments, waitlist modules - Add vitest suite with prisma mocks (21 tests), biome lint - Point web client to /api/v1/auth and /api/v1/groups/from-organization
51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import type { Prisma } from '@generated/prisma/client';
|
|
import type {
|
|
GroupList,
|
|
GroupQuery,
|
|
ProblemDetails,
|
|
Result,
|
|
} from '@gruperly/shared';
|
|
import { ok } from '@gruperly/shared';
|
|
import { getPaginationMetadata, getPaginationOffset } from '@/lib/pagination';
|
|
import prisma from '@/lib/prisma';
|
|
import { buildGroupWhereForUser, type GroupDb, toGroupDto } from '../../lib';
|
|
|
|
type ListGroupsDeps = {
|
|
db?: Pick<GroupDb, 'group'>;
|
|
};
|
|
|
|
type GroupRecord = {
|
|
id: string;
|
|
name: string;
|
|
description: string | null;
|
|
createdById: string;
|
|
createdAt: Date;
|
|
updatedAt: Date;
|
|
};
|
|
|
|
export class ListGroups {
|
|
constructor(private readonly deps: ListGroupsDeps = {}) {}
|
|
|
|
async execute(
|
|
query: GroupQuery,
|
|
userId: string,
|
|
): Promise<Result<GroupList, ProblemDetails>> {
|
|
const db = this.deps.db ?? prisma;
|
|
const where = buildGroupWhereForUser(userId) as Prisma.GroupWhereInput;
|
|
|
|
const [records, total] = await Promise.all([
|
|
db.group.findMany({
|
|
where,
|
|
skip: getPaginationOffset(query),
|
|
take: query.pageSize,
|
|
orderBy: { createdAt: 'desc' },
|
|
}),
|
|
db.group.count({ where }),
|
|
]);
|
|
|
|
return ok({
|
|
data: records.map((record: GroupRecord) => toGroupDto(record)),
|
|
pagination: getPaginationMetadata(query, total),
|
|
});
|
|
}
|
|
} |