Files
aurorabot/shared/modules/class/class.service.ts
syntaxbullet 5bd390b4ee docs: add JSDoc to service public methods
One-line JSDoc on 82 methods across 11 service files for quick
scanning without reading full implementations.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-02 11:36:18 +02:00

90 lines
3.2 KiB
TypeScript

import { classes, users } from "@db/schema";
import { eq, sql } from "drizzle-orm";
import { DrizzleClient } from "@shared/db/DrizzleClient";
import { UserError } from "@shared/lib/errors";
import { withTransaction } from "@/lib/db";
import type { Transaction } from "@shared/lib/types";
export const classService = {
/** Retrieve all available classes. */
getAllClasses: async () => {
return await DrizzleClient.query.classes.findMany();
},
/** Assign a class to a user; throws if the class does not exist. */
assignClass: async (userId: string, classId: bigint, tx?: Transaction) => {
return await withTransaction(async (txFn) => {
const cls = await txFn.query.classes.findFirst({
where: eq(classes.id, classId),
});
if (!cls) throw new UserError("Class not found");
const [user] = await txFn.update(users)
.set({ classId: classId })
.where(eq(users.id, BigInt(userId)))
.returning();
return user;
}, tx);
},
/** Get the current balance for a class, returning 0 if not found. */
getClassBalance: async (classId: bigint) => {
const cls = await DrizzleClient.query.classes.findFirst({
where: eq(classes.id, classId),
});
return cls?.balance || 0n;
},
/** Adjust a class balance by the given amount; throws if funds are insufficient for a deduction. */
modifyClassBalance: async (classId: bigint, amount: bigint, tx?: Transaction) => {
return await withTransaction(async (txFn) => {
const cls = await txFn.query.classes.findFirst({
where: eq(classes.id, classId),
});
if (!cls) throw new UserError("Class not found");
if ((cls.balance ?? 0n) + amount < 0n) {
throw new UserError("Insufficient class funds");
}
const [updatedClass] = await txFn.update(classes)
.set({
balance: sql`${classes.balance} + ${amount} `,
})
.where(eq(classes.id, classId))
.returning();
return updatedClass;
}, tx);
},
/** Update a class record with partial data. */
updateClass: async (id: bigint, data: Partial<typeof classes.$inferInsert>, tx?: Transaction) => {
return await withTransaction(async (txFn) => {
const [updatedClass] = await txFn.update(classes)
.set(data)
.where(eq(classes.id, id))
.returning();
return updatedClass;
}, tx);
},
/** Create a new class record. */
createClass: async (data: typeof classes.$inferInsert, tx?: Transaction) => {
return await withTransaction(async (txFn) => {
const [newClass] = await txFn.insert(classes)
.values(data)
.returning();
return newClass;
}, tx);
},
/** Delete a class by ID. */
deleteClass: async (id: bigint, tx?: Transaction) => {
return await withTransaction(async (txFn) => {
await txFn.delete(classes).where(eq(classes.id, id));
}, tx);
}
};