Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import type * as AppReviewOTP from "../AppReviewOTP.js";
import type * as ResendOTP from "../ResendOTP.js";
import type * as __tests___testUtils from "../__tests__/testUtils.js";
import type * as admin from "../admin.js";
import type * as auth from "../auth.js";
import type * as blocks from "../blocks.js";
import type * as http from "../http.js";
Expand All @@ -35,6 +36,7 @@ declare const fullApi: ApiFromModules<{
AppReviewOTP: typeof AppReviewOTP;
ResendOTP: typeof ResendOTP;
"__tests__/testUtils": typeof __tests___testUtils;
admin: typeof admin;
auth: typeof auth;
blocks: typeof blocks;
http: typeof http;
Expand Down
382 changes: 382 additions & 0 deletions backend/convex/admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,382 @@
import { v, ConvexError } from 'convex/values';
import { query, mutation } from './_generated/server';
import type { Id } from './_generated/dataModel';
import { requireAdmin } from './lib/authIdentity';

/**
* Admin moderation queries and mutations.
* All functions require the caller to have isAdmin === true on their user record.
*/

// --- Queries ---

/**
* Get paginated reports for the admin moderation queue.
* Supports filtering by status and targetType.
* Null status on a report is treated as 'pending'.
*/
export const getReports = query({
args: {
status: v.optional(
v.union(v.literal('pending'), v.literal('reviewed'), v.literal('dismissed'))
),
targetType: v.optional(v.union(v.literal('listing'), v.literal('profile'))),
limit: v.optional(v.number()),
},
handler: async (ctx, args) => {
await requireAdmin(ctx);

const limit = Math.min(args.limit ?? 50, 100);

// Fetch reports ordered by newest first
let allReports = await ctx.db.query('reports').order('desc').take(500);

// Filter by status (null treated as pending)
if (args.status) {
allReports = allReports.filter((r) => {
const reportStatus = r.status ?? 'pending';
return reportStatus === args.status;
});
}

// Filter by targetType
if (args.targetType) {
allReports = allReports.filter((r) => r.targetType === args.targetType);
}

// Limit results
const reports = allReports.slice(0, limit);
Comment on lines +26 to +48

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🌐 Web query:

Convex paginate paginationOpts usage with withIndex and order

💡 Result:

In Convex, use paginationOpts with .paginate(paginationOpts) on a database query chain that includes .withIndex and .order for efficient indexed pagination. Example from official docs: export const listWithExtraArg = query({ args: { paginationOpts: paginationOptsValidator, author: v.string }, handler: async (ctx, args) => { return await ctx.db .query("messages") .withIndex("by_author", (q) => q.eq("author", args.author)) .order("desc") .paginate(args.paginationOpts); }, }); Key points: - Import paginationOptsValidator from "convex/server" for args validation. - Chain .withIndex(indexName, filter) to use a predefined index for filtering/sorting. - Follow with .order("asc") or .order("desc") to specify sort direction (matches index order). - Call .paginate(args.paginationOpts) last; paginationOpts includes numItems, cursor, etc., for cursor-based pagination. - Use usePaginatedQuery hook on client for React integration. - Supports additional args beyond paginationOpts. - Post-paginate transformations (map/filter on page) allowed, but preserve continueCursor, isDone, status. This works for cursor-based, gap-less pagination with indexes for performance. For non-indexed queries, prefer indexes to avoid scans.

Citations:


🏁 Script executed:

fd -t f schema.ts | head -5

Repository: codebox-calpoly/PolyBuys

Length of output: 92


🏁 Script executed:

fd -t f admin.ts | head -5

Repository: codebox-calpoly/PolyBuys

Length of output: 91


🏁 Script executed:

# Check if reports table exists in schema and look for status field/index
rg -A 20 "reports.*defineTable\|defineTable.*reports" --type=ts

Repository: codebox-calpoly/PolyBuys

Length of output: 50


🏁 Script executed:

# Look for any existing indexes on the reports table
rg "index.*status|by_status" --type=ts

Repository: codebox-calpoly/PolyBuys

Length of output: 2084


🏁 Script executed:

# Look specifically for reports table definition in schema.ts
rg -A 30 "reports.*:.*defineTable|reports\s*:\s*{" backend/convex/schema.ts

Repository: codebox-calpoly/PolyBuys

Length of output: 209


🏁 Script executed:

# Check lines around the reports table to see all its fields and indexes
cat -n backend/convex/schema.ts | grep -A 40 "reports.*:"

Repository: codebox-calpoly/PolyBuys

Length of output: 1835


🏁 Script executed:

cat -n backend/convex/admin.ts | sed -n '1,80p'

Repository: codebox-calpoly/PolyBuys

Length of output: 3289


🏁 Script executed:

cat -n backend/convex/admin.ts | sed -n '26,60p'

Repository: codebox-calpoly/PolyBuys

Length of output: 1460


take(500) + in-memory filter causes silent data loss when reports exceed 500 and requested status is not in the latest 500.

The code fetches the 500 newest reports, then filters by status and targetType in memory before slicing to limit. If the table exceeds ~500 rows and the newest 500 happen to be reviewed/dismissed, a query for status: 'pending' returns an empty result despite pending reports existing. This is especially problematic for a moderation queue where most reports get resolved.

Additionally, this is not true pagination despite the function's intent; it returns a capped, unsorted result set once the table grows.

Recommended fix:

  1. Add index to reports table in schema.ts:
    .index('by_status_createdAt', ['status', 'createdAt'])
    
  2. Update the query to use the index with .withIndex(), then filter targetType in-memory if needed. Alternatively, implement proper cursor-based pagination with .paginate(paginationOpts) per Convex docs.

The current sketch requires the index to exist first; without schema changes, the fix cannot work as written.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/convex/admin.ts` around lines 26 - 48, The handler in admin.ts uses
ctx.db.query('reports').order('desc').take(500') and then does in-memory
filtering which causes silent data loss when >500 rows; add a DB index in
schema.ts (e.g., .index('by_status_createdAt', ['status','createdAt'])) and
change the query in the handler to use .withIndex('by_status_createdAt') and
query by status (so the DB returns correct filtered rows), then apply targetType
filtering in-memory if necessary or switch to Convex cursor pagination
(.paginate(paginationOpts)) to support proper paging; ensure requireAdmin and
the limit logic remain, but remove the fixed take(500) to rely on indexed
query/pagination.


// Enrich with target and reporter context
const enriched = await Promise.all(
reports.map(async (report) => {
let targetTitle: string | null = null;
let targetImage: string | null = null;
let targetIsHidden = false;

if (report.targetType === 'listing') {
const listing = await ctx.db.get(report.targetId as Id<'listings'>).catch(() => null);
if (listing) {
targetTitle = listing.title;
targetImage = listing.images?.[0] ?? null;
targetIsHidden = listing.isHidden === true;
}
} else if (report.targetType === 'profile') {
const profile = await ctx.db.get(report.targetId as Id<'profiles'>).catch(() => null);
if (profile) {
targetTitle = profile.name;
targetIsHidden = profile.isHidden === true;
}
}

// Get reporter profile name
const reporterProfile = await ctx.db
.query('profiles')
.withIndex('by_userId', (q) => q.eq('userId', report.reporterId))
.first();

return {
...report,
status: report.status ?? 'pending',
targetTitle,
targetImage,
targetIsHidden,
reporterName: reporterProfile?.name ?? 'Unknown user',
};
})
);

return enriched;
},
});

/**
* Get detailed view of a single report with full target context and all reports for that target.
*/
export const getReportDetail = query({
args: { reportId: v.id('reports') },
handler: async (ctx, args) => {
await requireAdmin(ctx);

const report = await ctx.db.get(args.reportId);
if (!report) {
throw new ConvexError('Report not found');
}

// Get full target data
let target: Record<string, unknown> | null = null;
if (report.targetType === 'listing') {
const listing = await ctx.db.get(report.targetId as Id<'listings'>).catch(() => null);
target = listing ? { ...listing } : null;
} else if (report.targetType === 'profile') {
const profile = await ctx.db.get(report.targetId as Id<'profiles'>).catch(() => null);
target = profile ? { ...profile } : null;
}

// Get all reports for this target
const allTargetReports = await ctx.db
.query('reports')
.withIndex('by_target', (q) =>
q.eq('targetId', report.targetId).eq('targetType', report.targetType)
)
.collect();

// Enrich each report with reporter name
const enrichedReports = await Promise.all(
allTargetReports.map(async (r) => {
const reporterProfile = await ctx.db
.query('profiles')
.withIndex('by_userId', (q) => q.eq('userId', r.reporterId))
.first();
return {
...r,
status: r.status ?? 'pending',
reporterName: reporterProfile?.name ?? 'Unknown user',
};
})
);

// Get reporter profile for the primary report
const reporterProfile = await ctx.db
.query('profiles')
.withIndex('by_userId', (q) => q.eq('userId', report.reporterId))
.first();

return {
report: {
...report,
status: report.status ?? 'pending',
reporterName: reporterProfile?.name ?? 'Unknown user',
},
target,
allReportsForTarget: enrichedReports,
uniqueReporterCount: new Set(allTargetReports.map((r) => r.reporterId)).size,
};
},
});

/**
* Get summary stats for the admin dashboard.
*/
export const getStats = query({
args: {},
handler: async (ctx) => {
await requireAdmin(ctx);

const allReports = await ctx.db.query('reports').collect();

const pending = allReports.filter((r) => (r.status ?? 'pending') === 'pending').length;
const reviewed = allReports.filter((r) => r.status === 'reviewed').length;
const dismissed = allReports.filter((r) => r.status === 'dismissed').length;

// Count hidden listings
const hiddenListings = await ctx.db
.query('listings')
.filter((q) => q.eq(q.field('isHidden'), true))
.collect();

// Count hidden profiles
const hiddenProfiles = await ctx.db
.query('profiles')
.filter((q) => q.eq(q.field('isHidden'), true))
.collect();

return {
pendingReports: pending,
reviewedReports: reviewed,
dismissedReports: dismissed,
totalReports: allReports.length,
hiddenListings: hiddenListings.length,
hiddenProfiles: hiddenProfiles.length,
};
},
});

/**
* Check if the current user is an admin.
*/
export const isCurrentUserAdmin = query({
args: {},
handler: async (ctx) => {
try {
await requireAdmin(ctx);
return true;
} catch {
return false;
}
},
});

// --- Mutations ---

/**
* Resolve a report by marking it as reviewed or dismissed.
* Optionally hides the target content.
*/
export const resolveReport = mutation({
args: {
reportId: v.id('reports'),
resolution: v.union(v.literal('reviewed'), v.literal('dismissed')),
hideTarget: v.optional(v.boolean()),
},
handler: async (ctx, args) => {
const adminId = await requireAdmin(ctx);

const report = await ctx.db.get(args.reportId);
if (!report) {
throw new ConvexError('Report not found');
}

// Update report status
await ctx.db.patch(args.reportId, {
status: args.resolution,
reviewedBy: adminId,
reviewedAt: Date.now(),
});

// Optionally hide the target
if (args.hideTarget) {
if (report.targetType === 'listing') {
const listing = await ctx.db.get(report.targetId as Id<'listings'>);
if (listing && !listing.isHidden) {
await ctx.db.patch(report.targetId as Id<'listings'>, {
isHidden: true,
hiddenAt: Date.now(),
hiddenReason: 'admin_action',
});
}
} else if (report.targetType === 'profile') {
const profile = await ctx.db.get(report.targetId as Id<'profiles'>);
if (profile && !profile.isHidden) {
await ctx.db.patch(report.targetId as Id<'profiles'>, {
isHidden: true,
hiddenAt: Date.now(),
hiddenReason: 'admin_action',
});
}
}
}
},
});

/**
* Bulk resolve all reports for a given target.
*/
export const resolveAllForTarget = mutation({
args: {
targetId: v.string(),
targetType: v.union(v.literal('listing'), v.literal('profile')),
resolution: v.union(v.literal('reviewed'), v.literal('dismissed')),
hideTarget: v.optional(v.boolean()),
},
handler: async (ctx, args) => {
const adminId = await requireAdmin(ctx);

const reports = await ctx.db
.query('reports')
.withIndex('by_target', (q) =>
q.eq('targetId', args.targetId).eq('targetType', args.targetType)
)
.collect();

// Update all pending reports for this target
for (const report of reports) {
if ((report.status ?? 'pending') === 'pending') {
await ctx.db.patch(report._id, {
status: args.resolution,
reviewedBy: adminId,
reviewedAt: Date.now(),
});
}
}

// Optionally hide the target
if (args.hideTarget) {
if (args.targetType === 'listing') {
const listing = await ctx.db.get(args.targetId as Id<'listings'>);
if (listing && !listing.isHidden) {
await ctx.db.patch(args.targetId as Id<'listings'>, {
isHidden: true,
hiddenAt: Date.now(),
hiddenReason: 'admin_action',
});
}
} else if (args.targetType === 'profile') {
const profile = await ctx.db.get(args.targetId as Id<'profiles'>);
if (profile && !profile.isHidden) {
await ctx.db.patch(args.targetId as Id<'profiles'>, {
isHidden: true,
hiddenAt: Date.now(),
hiddenReason: 'admin_action',
});
}
}
}
},
});

/**
* Manually hide a listing or profile.
*/
export const hideContent = mutation({
args: {
targetId: v.string(),
targetType: v.union(v.literal('listing'), v.literal('profile')),
},
handler: async (ctx, args) => {
await requireAdmin(ctx);

if (args.targetType === 'listing') {
const listing = await ctx.db.get(args.targetId as Id<'listings'>);
if (!listing) throw new ConvexError('Listing not found');
if (listing.isHidden) return; // Already hidden
await ctx.db.patch(args.targetId as Id<'listings'>, {
isHidden: true,
hiddenAt: Date.now(),
hiddenReason: 'admin_action',
});
} else {
const profile = await ctx.db.get(args.targetId as Id<'profiles'>);
if (!profile) throw new ConvexError('Profile not found');
if (profile.isHidden) return;
await ctx.db.patch(args.targetId as Id<'profiles'>, {
isHidden: true,
hiddenAt: Date.now(),
hiddenReason: 'admin_action',
});
}
},
});

/**
* Unhide a listing or profile.
*/
export const unhideContent = mutation({
args: {
targetId: v.string(),
targetType: v.union(v.literal('listing'), v.literal('profile')),
},
handler: async (ctx, args) => {
await requireAdmin(ctx);

if (args.targetType === 'listing') {
const listing = await ctx.db.get(args.targetId as Id<'listings'>);
if (!listing) throw new ConvexError('Listing not found');
if (!listing.isHidden) return;
await ctx.db.patch(args.targetId as Id<'listings'>, {
isHidden: false,
hiddenAt: undefined,
hiddenReason: undefined,
});
} else {
const profile = await ctx.db.get(args.targetId as Id<'profiles'>);
if (!profile) throw new ConvexError('Profile not found');
if (!profile.isHidden) return;
await ctx.db.patch(args.targetId as Id<'profiles'>, {
isHidden: false,
hiddenAt: undefined,
hiddenReason: undefined,
});
}
},
});
Loading
Loading