feat: enforce active quest limit in assignQuest

Adds a limit check to assignQuest that reads maxActiveQuests from game
settings and throws a UserError when the user has reached their active
quest limit. Completed quests are excluded from the count.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
syntaxbullet
2026-03-28 15:39:23 +01:00
parent 4ead7e60b1
commit 912ce5b942
2 changed files with 65 additions and 1 deletions

View File

@@ -4,6 +4,7 @@ import { UserError } from "@shared/lib/errors";
import { DrizzleClient } from "@shared/db/DrizzleClient";
import { economyService } from "@shared/modules/economy/economy.service";
import { levelingService } from "@shared/modules/leveling/leveling.service";
import { gameSettingsService } from "@shared/modules/game-settings/game-settings.service";
import { withTransaction } from "@/lib/db";
import type { Transaction } from "@shared/lib/types";
import { TransactionType } from "@shared/lib/constants";
@@ -12,13 +13,29 @@ import { systemEvents, EVENTS } from "@shared/lib/events";
export const questService = {
assignQuest: async (userId: string, questId: number, tx?: Transaction) => {
return await withTransaction(async (txFn) => {
// Check active quest limit
const settings = await gameSettingsService.getSettings();
const maxActive = settings?.quest?.maxActiveQuests ?? 3;
const userQuestList = await txFn.query.userQuests.findMany({
where: eq(userQuests.userId, BigInt(userId)),
});
const activeCount = userQuestList.filter(uq => !uq.completedAt).length;
if (activeCount >= maxActive) {
throw new UserError(
`You can only have ${maxActive} active quests at a time. Complete a quest before accepting a new one.`
);
}
return await txFn.insert(userQuests)
.values({
userId: BigInt(userId),
questId: questId,
progress: 0,
})
.onConflictDoNothing() // Ignore if already assigned
.onConflictDoNothing()
.returning();
}, tx);
},