Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Map forms to fields #255

Closed
wants to merge 3 commits into from
Closed
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 convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type * as constants from "../constants.js";
import type * as forms from "../forms.js";
import type * as helpers from "../helpers.js";
import type * as http from "../http.js";
import type * as parseForm from "../parseForm.js";
import type * as passwordReset from "../passwordReset.js";
import type * as questFields from "../questFields.js";
import type * as questions from "../questions.js";
Expand All @@ -44,6 +45,7 @@ declare const fullApi: ApiFromModules<{
forms: typeof forms;
helpers: typeof helpers;
http: typeof http;
parseForm: typeof parseForm;
passwordReset: typeof passwordReset;
questFields: typeof questFields;
questions: typeof questions;
Expand Down
7 changes: 7 additions & 0 deletions convex/forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ export const create = userMutation({
},
});

export const setJSON = mutation({
args: { formId: v.id("forms"), json: v.string() },
handler: async (ctx, args) => {
await ctx.db.patch(args.formId, { json: args.json });
},
});

export const softDelete = userMutation({
args: { formId: v.id("forms") },
handler: async (ctx, args) => {
Expand Down
35 changes: 35 additions & 0 deletions convex/parseForm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"use node";

import { v } from "convex/values";
import PDFParser from "pdf2json";
import { api } from "./_generated/api";
import { action } from "./_generated/server";

export const parse = action({
args: { formId: v.id("forms") },
handler: async (ctx, args) => {
console.log("Parsing form", args.formId);
const form = await ctx.runQuery(api.forms.getById, { formId: args.formId });
if (!form || !form.file) throw Error("Form not found");

const url = await ctx.storage.getUrl(form.file);
if (!url) throw Error("Form URL not found");
console.log("URL", url);

const pdfParser = new PDFParser(this, true);

pdfParser.loadPDF(url);

pdfParser.on("pdfParser_dataError", (errData) =>
console.error(errData.parserError),
);

pdfParser.on("pdfParser_dataReady", (pdfData) => {
console.log("Form parsed", pdfData);
ctx.runMutation(api.forms.setJSON, {
formId: args.formId,
json: JSON.stringify(pdfData),
});
});
},
});
2 changes: 2 additions & 0 deletions convex/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ const forms = defineTable({
creationUser: v.id("users"),
/** The storageId for the PDF file. */
file: v.optional(v.id("_storage")),
/** The parsed data from the form. */
json: v.optional(v.string()),
/** The US State the form applies to. */
jurisdiction: jurisdiction,
/** Time in ms since epoch when the form was deleted. */
Expand Down
2 changes: 1 addition & 1 deletion convex/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const seed = internalMutation(async (ctx) => {
const users = await ctx.db.query("users").collect();
const quests = await ctx.db.query("quests").collect();
if (users.length > 0 || quests.length > 0) {
console.error("Data already exists, skipping seeding");
console.info("Data already exists, skipping seeding");
return;
}

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@
"@tiptap/react": "^2.10.3",
"convex": "^1.17.3",
"convex-helpers": "^0.1.65",
"jspdf": "^2.5.2",
"lucide-react": "^0.462.0",
"motion": "^11.13.1",
"next-themes": "^0.4.3",
"oslo": "^1.2.1",
"pdf2json": "^3.1.4",
"postcss": "^8.4.49",
"pretty-bytes": "^6.1.1",
"react": "^18.3.1",
Expand Down
Loading
Loading