127 lines
3.3 KiB
TypeScript
127 lines
3.3 KiB
TypeScript
import { v } from "convex/values";
|
|
|
|
import { normalizeListLimit } from "./domain";
|
|
import { internalMutation, mutation, query } from "./_generated/server";
|
|
|
|
const strategy = v.union(
|
|
v.literal("call_first"),
|
|
v.literal("email_first"),
|
|
v.literal("defer"),
|
|
v.literal("do_not_contact"),
|
|
);
|
|
|
|
export const create = mutation({
|
|
args: {
|
|
leadId: v.id("leads"),
|
|
auditId: v.optional(v.id("audits")),
|
|
strategy,
|
|
phoneScript: v.optional(v.string()),
|
|
emailSubject: v.optional(v.string()),
|
|
emailBody: v.optional(v.string()),
|
|
followUpDraft: v.optional(v.string()),
|
|
},
|
|
handler: async (ctx, args) => {
|
|
const now = Date.now();
|
|
|
|
return await ctx.db.insert("outreachRecords", {
|
|
...args,
|
|
approvalStatus: "draft",
|
|
sendStatus: "not_sent",
|
|
responseStatus: "none",
|
|
salesStatus: "follow_up_planned",
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
});
|
|
},
|
|
});
|
|
|
|
export const upsertFromAuditGeneration = internalMutation({
|
|
args: {
|
|
leadId: v.id("leads"),
|
|
auditId: v.optional(v.id("audits")),
|
|
strategy: strategy,
|
|
phoneScript: v.optional(v.string()),
|
|
emailSubject: v.optional(v.string()),
|
|
emailBody: v.optional(v.string()),
|
|
followUpDraft: v.optional(v.string()),
|
|
},
|
|
handler: async (ctx, args) => {
|
|
const now = Date.now();
|
|
|
|
const existing = await ctx.db
|
|
.query("outreachRecords")
|
|
.withIndex("by_leadId", (q) => q.eq("leadId", args.leadId))
|
|
.order("desc")
|
|
.take(1);
|
|
|
|
if (existing.length > 0) {
|
|
const current = existing[0]!;
|
|
if (args.auditId) {
|
|
await ctx.db.patch(current._id, { auditId: args.auditId });
|
|
}
|
|
|
|
await ctx.db.patch(current._id, {
|
|
strategy: args.strategy,
|
|
...(args.phoneScript !== undefined ? { phoneScript: args.phoneScript } : {}),
|
|
...(args.emailSubject !== undefined
|
|
? { emailSubject: args.emailSubject }
|
|
: {}),
|
|
...(args.emailBody !== undefined ? { emailBody: args.emailBody } : {}),
|
|
...(args.followUpDraft !== undefined
|
|
? { followUpDraft: args.followUpDraft }
|
|
: {}),
|
|
updatedAt: now,
|
|
});
|
|
|
|
return current._id;
|
|
}
|
|
|
|
return await ctx.db.insert("outreachRecords", {
|
|
...args,
|
|
approvalStatus: "draft",
|
|
sendStatus: "not_sent",
|
|
responseStatus: "none",
|
|
salesStatus: "follow_up_planned",
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
});
|
|
},
|
|
});
|
|
|
|
export const list = query({
|
|
args: {
|
|
leadId: v.optional(v.id("leads")),
|
|
approvalStatus: v.optional(
|
|
v.union(v.literal("draft"), v.literal("approved"), v.literal("rejected")),
|
|
),
|
|
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("outreachRecords")
|
|
.withIndex("by_leadId", (q) => q.eq("leadId", leadId))
|
|
.order("desc")
|
|
.take(limit);
|
|
}
|
|
|
|
if (args.approvalStatus) {
|
|
const approvalStatus = args.approvalStatus;
|
|
|
|
return await ctx.db
|
|
.query("outreachRecords")
|
|
.withIndex("by_approvalStatus", (q) =>
|
|
q.eq("approvalStatus", approvalStatus),
|
|
)
|
|
.order("desc")
|
|
.take(limit);
|
|
}
|
|
|
|
return await ctx.db.query("outreachRecords").order("desc").take(limit);
|
|
},
|
|
});
|