61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
import { v } from "convex/values";
|
|
|
|
import type { Doc, Id } from "./_generated/dataModel";
|
|
import { internalQuery } from "./_generated/server";
|
|
import { buildPageSpeedAuditInputs, type PageSpeedMinimalAuditResult } from "../lib/pagespeed-audit-input";
|
|
|
|
function normalizePageSpeedResultRow(
|
|
row: Doc<"pageSpeedResults">,
|
|
): PageSpeedMinimalAuditResult {
|
|
return {
|
|
strategy: row.strategy,
|
|
status: row.status,
|
|
sourceUrl: row.sourceUrl,
|
|
...(row.finalUrl ? { finalUrl: row.finalUrl } : {}),
|
|
...(row.normalized ? { normalized: row.normalized } : {}),
|
|
...(row.errorType ? { errorType: row.errorType } : {}),
|
|
...(row.errorSummary ? { errorSummary: row.errorSummary } : {}),
|
|
};
|
|
}
|
|
|
|
export const getPageSpeedAuditInputs = internalQuery({
|
|
args: {
|
|
leadId: v.optional(v.id("leads")),
|
|
auditId: v.optional(v.id("audits")),
|
|
},
|
|
handler: async (
|
|
ctx,
|
|
args,
|
|
): Promise<{
|
|
technicalSignals: string[];
|
|
customerImplications: string[];
|
|
internalNotes: string[];
|
|
}> => {
|
|
let results: Doc<"pageSpeedResults">[];
|
|
|
|
if (args.auditId) {
|
|
results = await ctx.db
|
|
.query("pageSpeedResults")
|
|
.withIndex("by_auditId", (q) => q.eq("auditId", args.auditId as Id<"audits">))
|
|
.order("desc")
|
|
.take(50);
|
|
return buildPageSpeedAuditInputs(results.map(normalizePageSpeedResultRow));
|
|
}
|
|
|
|
if (args.leadId) {
|
|
results = await ctx.db
|
|
.query("pageSpeedResults")
|
|
.withIndex("by_leadId", (q) => q.eq("leadId", args.leadId as Id<"leads">))
|
|
.order("desc")
|
|
.take(50);
|
|
return buildPageSpeedAuditInputs(results.map(normalizePageSpeedResultRow));
|
|
}
|
|
|
|
return {
|
|
technicalSignals: [],
|
|
customerImplications: [],
|
|
internalNotes: [],
|
|
};
|
|
},
|
|
});
|