refactor(modules): propagate UserError in quest, trade, and class services

This commit is contained in:
syntaxbullet
2025-12-22 12:58:47 +01:00
parent 75e586cee8
commit fbcac51370
3 changed files with 14 additions and 10 deletions

View File

@@ -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();

View File

@@ -1,6 +1,7 @@
```typescript
import { quests, userQuests, users } from "@/db/schema";
import { eq, and, sql } from "drizzle-orm";
import { UserError } from "@/lib/errors";
import { DrizzleClient } from "@/lib/DrizzleClient";
import { economyService } from "@/modules/economy/economy.service";
import { levelingService } from "@/modules/leveling/leveling.service";
@@ -45,8 +46,8 @@ export const questService = {
}
});
if (!userQuest) throw new Error("Quest not assigned");
if (userQuest.completedAt) throw new Error("Quest already completed");
if (!userQuest) throw new UserError("Quest not assigned");
if (userQuest.completedAt) throw new UserError("Quest already completed");
// Mark completed
await txFn.update(userQuests)
@@ -62,7 +63,7 @@ export const questService = {
if (rewards?.balance) {
const bal = BigInt(rewards.balance);
await economyService.modifyUserBalance(userId, bal, 'QUEST_REWARD', `Reward for quest ${questId}`, null, txFn);
await economyService.modifyUserBalance(userId, bal, 'QUEST_REWARD', `Reward for quest ${ questId }`, null, txFn);
results.balance = bal;
}

View File

@@ -1,5 +1,7 @@
import type { TradeSession, TradeParticipant } from "./trade.types";
import { DrizzleClient } from "@/lib/DrizzleClient";
import { withTransaction } from "@/lib/db";
import { UserError } from "@/lib/errors";
import { economyService } from "@/modules/economy/economy.service";
import { inventoryService } from "@/modules/inventory/inventory.service";
import { itemTransactions } from "@/db/schema";