Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Try to build out the survey worker better hooking everything up

Scott Petrovic27ac6a470be1
diff --git a/src/lib/components/download-success/DownloadSuccessDialog.ts b/src/lib/components/download-success/DownloadSuccessDialog.ts
index 860cd01..7a575d6 100644
--- a/src/lib/components/download-success/DownloadSuccessDialog.ts
+++ b/src/lib/components/download-success/DownloadSuccessDialog.ts
@@ -3,6 +3,7 @@ import { StarRating } from './StarRating'
export class DownloadSuccessDialog {
private dialog_element: HTMLDivElement | null = null
private star_rating: StarRating | null = null
+ private current_rating: number | null = null
private readonly content_html = `
@@ -20,7 +21,7 @@ export class DownloadSuccessDialog {
<h3>Share Your Feedback</h3>
<div class="star-rating-container"></div>
<textarea class="download-success-feedback-textarea" placeholder="Add optionalal feedback. (500 character max)" rows="4" maxlength="500"></textarea>
- <button class="download-success-survey-btn">Submit</button>
+ <button id="survey-submission-button">Submit</button>
</div>
<div class="download-success-section">
@@ -51,12 +52,53 @@ export class DownloadSuccessDialog {
const rating_container = this.dialog_element.querySelector('.star-rating-container')
if (rating_container) {
this.star_rating = new StarRating((rating) => {
+ this.current_rating = rating
console.log('User rated:', rating)
})
rating_container.innerHTML = this.star_rating.getHTML()
this.star_rating.attachEventListeners(rating_container as HTMLElement)
}
+ // Survey submission handler
+ const submit_button = this.dialog_element.querySelector('#survey-submission-button')
+ submit_button?.addEventListener('click', async () => {
+ const feedback_textarea = this.dialog_element?.querySelector('.download-success-feedback-textarea') as HTMLTextAreaElement
+ const feedback_text = feedback_textarea?.value?.trim() || ''
+
+ // Worker requires each submitted item to include a non-empty answer.
+ if (this.current_rating === null) {
+ console.error('Survey submission blocked: rating is required before submitting')
+ return
+ }
+
+ // Build payload with required rating and optional feedback.
+ const survey_data: Array<{ question: string, answer: string | number }> = [
+ { question: 'Rating', answer: this.current_rating }
+ ]
+
+ if (feedback_text.length > 0) {
+ survey_data.push({ question: 'Feedback', answer: feedback_text })
+ }
+
+ // Use the Cloudflare Worker endpoint to submit the survey data
+ const WORKER_URL = "https://mesh2motion-app.scottpetrovic.workers.dev"
+
+ try {
+ const res = await fetch(`${WORKER_URL}/submit`, {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({ survey: survey_data })
+ })
+ if (!res.ok) {
+ console.error('Survey submission failed:', res.statusText)
+ } else {
+ console.log('Survey submitted successfully')
+ }
+ } catch (error) {
+ console.error('Error submitting survey:', error)
+ }
+ })
+
// Close button handler
const close_button = this.dialog_element.querySelector('.download-success-dialog-close')
close_button?.addEventListener('click', () => { this.remove() })
diff --git a/src/survey-worker.js b/src/survey-worker.js
new file mode 100644
index 0000000..4e57a7b
--- /dev/null
+++ b/src/survey-worker.js
@@ -0,0 +1,180 @@
+/***
+ * A simple Cloudflare Worker to accept survey responses and store them in a D1 database.
+ *
+ * Expected payload format (JSON):
+ * {
+ * "survey": [
+ * { "question": "How satisfied are you?", "answer": 5 },
+ * { "question": "Any additional feedback?", "answer": "Great app!" }
+ * ],
+ * "session_id": "optional-session-id"
+ * }
+ */
+export default {
+ /**
+ * Routes incoming HTTP requests to the appropriate worker behavior.
+ * Keeps top-level control flow simple: preflight, submit, or not found.
+ */
+ async fetch(request, env) {
+ // Parse route data once so we can do simple method/path checks below.
+ const url = new URL(request.url);
+
+ // 1) Handle CORS preflight requests early.
+ if (request.method === "OPTIONS") {
+ return corsResponse(null, 204);
+ }
+
+ // 2) Main write endpoint: accept survey payload and store answers.
+ if (request.method === "POST" && url.pathname === "/submit") {
+ return handleSubmit(request, env);
+ }
+
+ // 3) Fallback for unknown routes/methods.
+ return corsResponse({ error: "Not found" }, 404);
+ },
+};
+
+const MAX_SURVEY_ITEMS = 20;
+const MAX_QUESTION_LENGTH = 200;
+const MAX_ANSWER_LENGTH = 2000;
+
+/**
+ * Handles POST /submit by validating payload shape, preparing DB writes,
+ * executing inserts in a batch, and returning a success response.
+ */
+async function handleSubmit(request, env) {
+ let body;
+ try {
+ // Parse JSON body and fail fast on malformed payloads.
+ body = await request.json();
+ } catch {
+ return badRequest("Invalid JSON");
+ }
+
+ const { survey, session_id } = body;
+
+ // The API expects: { survey: [{ question, answer }, ...], session_id?: string }
+ if (!Array.isArray(survey) || survey.length === 0 || survey.length > MAX_SURVEY_ITEMS) {
+ return badRequest("survey must be a non-empty array");
+ }
+
+ const normalized_session_id = normalizeSessionId(session_id);
+ const statements_or_error = buildSurveyInsertStatements(env.DB, survey, normalized_session_id);
+
+ if (statements_or_error.error) {
+ return badRequest(statements_or_error.error);
+ }
+
+ // Execute all inserts in one batch for consistency and fewer round trips.
+ await env.DB.batch(statements_or_error.statements);
+
+ return corsResponse(
+ {
+ success: true,
+ session_id: normalized_session_id,
+ inserted: statements_or_error.statements.length,
+ },
+ 201
+ );
+}
+
+/**
+ * Normalizes an optional session id from the client.
+ * Generates a UUID when no usable id is provided.
+ */
+function normalizeSessionId(session_id) {
+ // Reuse client session_id when provided; otherwise create one to group this submission.
+ return typeof session_id === "string" && session_id.trim().length > 0
+ ? session_id.trim()
+ : crypto.randomUUID();
+}
+
+/**
+ * Validates each survey item and converts valid entries into prepared insert
+ * statements so all writes remain parameterized and safe.
+ */
+function buildSurveyInsertStatements(db, survey, normalized_session_id) {
+ const statements = [];
+
+ for (const entry of survey) {
+ const validation_error = validateSurveyItem(entry);
+ if (validation_error) {
+ return { error: validation_error };
+ }
+
+ const question = entry.question.trim();
+ const answer_text = String(entry.answer).trim();
+
+ // submitted at insert automatically uses default to current timestamp
+ // the ID auto-increments, so don't specify that.
+ statements.push(
+ db.prepare(
+ "INSERT INTO responses (session_id, question, answer) VALUES (?, ?, ?)"
+ ).bind(normalized_session_id, question, answer_text)
+ );
+ }
+
+ return { statements };
+}
+
+/**
+ * Applies per-item validation rules and returns a user-facing error message
+ * when invalid, otherwise returns null.
+ */
+function validateSurveyItem(entry) {
+ if (!entry || typeof entry !== "object" || Array.isArray(entry)) {
+ return "Each survey item must be an object";
+ }
+
+ const question = typeof entry.question === "string" ? entry.question.trim() : "";
+ const answer = entry.answer;
+
+ if (!question) {
+ return "Each survey item must include a question";
+ }
+
+ if (question.length > MAX_QUESTION_LENGTH) {
+ return "Question is too long";
+ }
+
+ const answer_type = typeof answer;
+ if (answer_type !== "string" && answer_type !== "number" && answer_type !== "boolean") {
+ return "Each survey answer must be a string, number, or boolean";
+ }
+
+ if (answer_type === "number" && !Number.isFinite(answer)) {
+ return "Numeric answers must be finite";
+ }
+
+ const answer_text = String(answer).trim();
+ if (answer_text === "") {
+ return "Each survey item must include an answer";
+ }
+
+ if (answer_text.length > MAX_ANSWER_LENGTH) {
+ return "Answer is too long";
+ }
+
+ return null;
+}
+
+/**
+ * Convenience helper for consistent JSON 400 responses.
+ */
+function badRequest(message) {
+ return corsResponse({ error: message }, 400);
+}
+
+/**
+ * Builds a JSON response with CORS headers used by this worker.
+ */
+function corsResponse(body, status) {
+ const headers = {
+ "Content-Type": "application/json",
+ "Access-Control-Allow-Origin": "*", // restrict to your domain in production
+ "Access-Control-Allow-Methods": "GET, POST, OPTIONS",
+ "Access-Control-Allow-Headers": "Content-Type",
+ };
+
+ return new Response(body ? JSON.stringify(body) : null, { status, headers });
+}
\ No newline at end of file
diff --git a/wrangler.jsonc b/wrangler.jsonc
index 54a0250..58d2269 100644
--- a/wrangler.jsonc
+++ b/wrangler.jsonc
@@ -4,6 +4,7 @@
"assets": {
"directory": "./dist"
},
+ "main": "src/survey-worker.js",
"d1_databases": [
{
"binding": "DB",