import { v } from "convex/values"; import { normalizeListLimit } from "./domain"; import { mutation, query } from "./_generated/server"; const blacklistType = v.union( v.literal("domain"), v.literal("email"), v.literal("phone"), v.literal("company"), v.literal("google_place_id"), ); function normalizeBlacklistValue(value: string) { return value.trim().toLowerCase(); } export const create = mutation({ args: { type: blacklistType, value: v.string(), note: v.optional(v.string()), }, handler: async (ctx, args) => { return await ctx.db.insert("blacklistEntries", { ...args, normalizedValue: normalizeBlacklistValue(args.value), createdAt: Date.now(), }); }, }); export const list = query({ args: { type: v.optional(blacklistType), limit: v.optional(v.number()), }, handler: async (ctx, args) => { const limit = normalizeListLimit(args.limit); if (args.type) { const type = args.type; return await ctx.db .query("blacklistEntries") .withIndex("by_type_and_normalizedValue", (q) => q.eq("type", type)) .take(limit); } return await ctx.db.query("blacklistEntries").order("desc").take(limit); }, });