forked from syntaxbullet/AuroraBot-discord
refactor(modules): propagate UserError in quest, trade, and class services
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
|
||||
```typescript
|
||||
import { classes, users } from "@/db/schema";
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import { DrizzleClient } from "@/lib/DrizzleClient";
|
||||
import { UserError } from "@/lib/errors";
|
||||
|
||||
export const classService = {
|
||||
getAllClasses: async () => {
|
||||
@@ -14,7 +15,7 @@ export const classService = {
|
||||
where: eq(classes.id, classId),
|
||||
});
|
||||
|
||||
if (!cls) throw new Error("Class not found");
|
||||
if (!cls) throw new UserError("Class not found");
|
||||
|
||||
const [user] = await txFn.update(users)
|
||||
.set({ classId: classId })
|
||||
@@ -37,15 +38,15 @@ export const classService = {
|
||||
where: eq(classes.id, classId),
|
||||
});
|
||||
|
||||
if (!cls) throw new Error("Class not found");
|
||||
if (!cls) throw new UserError("Class not found");
|
||||
|
||||
if (amount < 0n && (cls.balance ?? 0n) < -amount) {
|
||||
throw new Error("Insufficient class funds");
|
||||
if ((cls.balance ?? 0n) < amount) {
|
||||
throw new UserError("Insufficient class funds");
|
||||
}
|
||||
|
||||
const [updatedClass] = await txFn.update(classes)
|
||||
.set({
|
||||
balance: sql`${classes.balance} + ${amount}`,
|
||||
balance: sql`${ classes.balance } + ${ amount } `,
|
||||
})
|
||||
.where(eq(classes.id, classId))
|
||||
.returning();
|
||||
|
||||
Reference in New Issue
Block a user