Skip to content

Commit aac9a01

Browse files
authored
Merge pull request #97 from chrispoupart/fix/assigned-quest-bug
feat(authorization): 🛂 add role-based quest filtering
2 parents 2e0b183 + a081738 commit aac9a01

File tree

2 files changed

+68
-11
lines changed

2 files changed

+68
-11
lines changed

backend/src/controllers/dashboardController.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ export class DashboardController {
180180
static async getQuestListing(req: Request, res: Response): Promise<void> {
181181
try {
182182
const userId = (req as any).user?.userId;
183+
const userRole = (req as any).user?.role;
183184
if (!userId) {
184185
res.status(401).json({
185186
success: false,
@@ -192,21 +193,40 @@ export class DashboardController {
192193
const skip = (parseInt(page as string) - 1) * parseInt(limit as string);
193194

194195
// Build where clause
195-
const whereClause: any = {};
196+
const whereConditions: any[] = [];
196197

197198
// Filter by status
198199
if (status && status !== 'all') {
199-
whereClause.status = status;
200+
whereConditions.push({ status: status });
200201
}
201202

202203
// Search functionality
203204
if (search && typeof search === 'string') {
204-
whereClause.OR = [
205-
{ title: { contains: search, mode: 'insensitive' } },
206-
{ description: { contains: search, mode: 'insensitive' } }
207-
];
205+
whereConditions.push({
206+
OR: [
207+
{ title: { contains: search, mode: 'insensitive' } },
208+
{ description: { contains: search, mode: 'insensitive' } }
209+
]
210+
});
211+
}
212+
213+
// Filter quests based on user role and assignment
214+
if (userRole !== 'ADMIN') {
215+
whereConditions.push({
216+
OR: [{ userId: null }, { userId: userId }],
217+
});
208218
}
209219

220+
// Exclude expired quests (dueDate in the past)
221+
whereConditions.push({
222+
OR: [
223+
{ dueDate: null },
224+
{ dueDate: { gte: new Date() } }
225+
]
226+
});
227+
228+
const whereClause = whereConditions.length > 0 ? { AND: whereConditions } : {};
229+
210230
// Get quests with pagination
211231
const [quests, totalCount] = await Promise.all([
212232
prisma.quest.findMany({
@@ -234,6 +254,14 @@ export class DashboardController {
234254
characterName: true,
235255
avatarUrl: true,
236256
}
257+
},
258+
personalizedFor: { // Assigned user
259+
select: {
260+
id: true,
261+
name: true,
262+
characterName: true,
263+
avatarUrl: true,
264+
}
237265
}
238266
}
239267
}),

backend/src/controllers/questController.ts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,7 @@ export class QuestController {
899899
static async getRepeatableQuests(req: Request, res: Response): Promise<void> {
900900
try {
901901
const userId = (req as any).user?.userId;
902+
const userRole = (req as any).user?.role;
902903
if (!userId) {
903904
res.status(401).json({ success: false, error: { message: 'User not authenticated' } });
904905
return;
@@ -908,12 +909,32 @@ export class QuestController {
908909
const limit = parseInt(req.query['limit'] as string) || 10;
909910
const skip = (page - 1) * limit;
910911

911-
// Get ALL repeatable quests (both available and on cooldown)
912+
// Build where conditions
913+
const whereConditions: any[] = [
914+
{ isRepeatable: true },
915+
{ status: { in: ['AVAILABLE', 'COOLDOWN'] } }
916+
];
917+
918+
// Filter quests based on user role and assignment
919+
if (userRole !== 'ADMIN') {
920+
whereConditions.push({
921+
OR: [{ userId: null }, { userId: userId }],
922+
});
923+
}
924+
925+
// Exclude expired quests (dueDate in the past)
926+
whereConditions.push({
927+
OR: [
928+
{ dueDate: null },
929+
{ dueDate: { gte: new Date() } }
930+
]
931+
});
932+
933+
const where = { AND: whereConditions };
934+
935+
// Get repeatable quests with proper filtering
912936
const repeatableQuests = await prisma.quest.findMany({
913-
where: {
914-
isRepeatable: true,
915-
status: { in: ['AVAILABLE', 'COOLDOWN'] },
916-
},
937+
where,
917938
include: {
918939
creator: {
919940
select: {
@@ -924,6 +945,14 @@ export class QuestController {
924945
characterName: true,
925946
avatarUrl: true,
926947
}
948+
},
949+
personalizedFor: { // Assigned user
950+
select: {
951+
id: true,
952+
name: true,
953+
characterName: true,
954+
avatarUrl: true,
955+
}
927956
}
928957
},
929958
orderBy: { createdAt: 'desc' },

0 commit comments

Comments
 (0)