feat: wire convex data foundations
This commit is contained in:
95
convex/audits.ts
Normal file
95
convex/audits.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
import { v } from "convex/values";
|
||||
|
||||
import { normalizeListLimit } from "./domain";
|
||||
import { mutation, query } from "./_generated/server";
|
||||
|
||||
const auditStatus = v.union(
|
||||
v.literal("draft"),
|
||||
v.literal("approved"),
|
||||
v.literal("published"),
|
||||
v.literal("deactivated"),
|
||||
);
|
||||
|
||||
export const create = mutation({
|
||||
args: {
|
||||
leadId: v.id("leads"),
|
||||
slug: v.string(),
|
||||
checkedDomain: v.string(),
|
||||
checkedPages: v.array(v.string()),
|
||||
status: v.optional(auditStatus),
|
||||
internalSummary: v.optional(v.string()),
|
||||
publicSummary: v.optional(v.string()),
|
||||
publicBody: v.optional(v.string()),
|
||||
ctaType: v.optional(v.string()),
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
const now = Date.now();
|
||||
const existing = await ctx.db
|
||||
.query("audits")
|
||||
.withIndex("by_slug", (q) => q.eq("slug", args.slug))
|
||||
.take(1);
|
||||
|
||||
if (existing.length > 0) {
|
||||
throw new Error("Audit slug already exists.");
|
||||
}
|
||||
|
||||
return await ctx.db.insert("audits", {
|
||||
...args,
|
||||
status: args.status ?? "draft",
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
export const get = query({
|
||||
args: { id: v.id("audits") },
|
||||
handler: async (ctx, args) => {
|
||||
return await ctx.db.get(args.id);
|
||||
},
|
||||
});
|
||||
|
||||
export const getBySlug = query({
|
||||
args: { slug: v.string() },
|
||||
handler: async (ctx, args) => {
|
||||
const audits = await ctx.db
|
||||
.query("audits")
|
||||
.withIndex("by_slug", (q) => q.eq("slug", args.slug))
|
||||
.take(1);
|
||||
|
||||
return audits[0] ?? null;
|
||||
},
|
||||
});
|
||||
|
||||
export const list = query({
|
||||
args: {
|
||||
leadId: v.optional(v.id("leads")),
|
||||
status: v.optional(auditStatus),
|
||||
limit: v.optional(v.number()),
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
const limit = normalizeListLimit(args.limit);
|
||||
|
||||
if (args.leadId) {
|
||||
const leadId = args.leadId;
|
||||
|
||||
return await ctx.db
|
||||
.query("audits")
|
||||
.withIndex("by_leadId", (q) => q.eq("leadId", leadId))
|
||||
.order("desc")
|
||||
.take(limit);
|
||||
}
|
||||
|
||||
if (args.status) {
|
||||
const status = args.status;
|
||||
|
||||
return await ctx.db
|
||||
.query("audits")
|
||||
.withIndex("by_status", (q) => q.eq("status", status))
|
||||
.order("desc")
|
||||
.take(limit);
|
||||
}
|
||||
|
||||
return await ctx.db.query("audits").order("desc").take(limit);
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user