Skip to content

Commit 3e9da1f

Browse files
authored
Merge pull request #164 from gadget-inc/devaoc/fixing-product-quiz
fixing the product quiz by readding the routes
2 parents 31b3590 + 8e47805 commit 3e9da1f

File tree

4 files changed

+165
-3
lines changed

4 files changed

+165
-3
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { RouteHandler } from "gadget-server";
2+
3+
/**
4+
* Route handler for GET proxy/quiz
5+
*
6+
* See: https://docs.gadget.dev/guides/http-routes/route-configuration#route-context
7+
*/
8+
const route: RouteHandler<{ Querystring: { slug: string } }> = async ({
9+
request,
10+
reply,
11+
api,
12+
logger,
13+
connections,
14+
}) => {
15+
if (!connections.shopify.current) {
16+
return await reply.code(401).send({ error: { message: "Unauthorized" } });
17+
}
18+
19+
const { slug } = request.query;
20+
21+
const quiz = await api.quiz.findFirst({
22+
filter: {
23+
slug: {
24+
equals: slug,
25+
},
26+
shopId: {
27+
equals: String(connections.shopify.currentShop?.id),
28+
},
29+
},
30+
select: {
31+
id: true,
32+
title: true,
33+
body: true,
34+
questions: {
35+
edges: {
36+
node: {
37+
id: true,
38+
text: true,
39+
answers: {
40+
edges: {
41+
node: {
42+
id: true,
43+
text: true,
44+
},
45+
},
46+
},
47+
},
48+
},
49+
},
50+
},
51+
});
52+
53+
await reply.send(quiz);
54+
};
55+
56+
export default route;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { RouteHandler } from "gadget-server";
2+
3+
/**
4+
* Route handler for POST proxy/recommendations
5+
*
6+
* See: https://docs.gadget.dev/guides/http-routes/route-configuration#route-context
7+
*/
8+
const route: RouteHandler<{
9+
Body: { answerIdFilters: [{ id: { equals: string } }] };
10+
}> = async ({ request, reply, api, logger, connections }) => {
11+
if (!connections.shopify.current) {
12+
return await reply.code(401).send({ error: { message: "Unauthorized" } });
13+
}
14+
15+
const { answerIdFilters } = request.body;
16+
17+
const answers = await api.answer.findMany({
18+
filter: {
19+
OR: answerIdFilters,
20+
shopId: {
21+
equals: String(connections.shopify.currentShop?.id),
22+
},
23+
},
24+
select: {
25+
recommendedProduct: {
26+
id: true,
27+
productSuggestion: {
28+
id: true,
29+
title: true,
30+
body: true,
31+
handle: true,
32+
media: {
33+
edges: {
34+
node: {
35+
image: true,
36+
},
37+
},
38+
},
39+
},
40+
},
41+
},
42+
});
43+
44+
await reply.send(answers);
45+
};
46+
47+
export default route;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { RouteHandler } from "gadget-server";
2+
3+
/**
4+
* Route handler for POST proxy/save
5+
*
6+
* See: https://docs.gadget.dev/guides/http-routes/route-configuration#route-context
7+
*/
8+
const route: RouteHandler<{
9+
Body: {
10+
quizId: string;
11+
email: string;
12+
recommendedProducts: string[];
13+
};
14+
}> = async ({ request, reply, api, logger, connections }) => {
15+
if (!connections.shopify.current) {
16+
return await reply.code(401).send({ error: { message: "Unauthorized" } });
17+
}
18+
19+
const { quizId, email, recommendedProducts } = request.body;
20+
21+
await api.quizResult.create({
22+
quiz: {
23+
_link: quizId,
24+
},
25+
shop: {
26+
_link: String(connections.shopify.currentShop?.id),
27+
},
28+
email: email,
29+
shopperSuggestions: recommendedProducts.map((recommendedProductId) => ({
30+
create: {
31+
product: {
32+
_link: recommendedProductId,
33+
},
34+
shop: {
35+
_link: String(connections.shopify.currentShop?.id),
36+
},
37+
},
38+
})),
39+
});
40+
41+
await reply.send();
42+
};
43+
44+
export default route;
Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1-
# This toml file is used for production. Only edit this file if you have a need for it
2-
# When you add a production config, values will be auto-populated here
3-
# Read more at https://docs.gadget.dev/guides/plugins/shopify/advanced-topics/shopify-app-toml
1+
# Learn more about working with Shopify TOML files with Gadget at https://docs.gadget.dev/guides/plugins/shopify/advanced-topics/shopify-app-toml
2+
3+
client_id = "52b23048a3e0a4ee6e7655cf10333cf1"
4+
name = "product-quiz-public-remix-ssr"
5+
application_url = "https://product-quiz-public-remix-ssr.gadget.app/api/shopify/install-or-render"
6+
embedded = true
7+
8+
[build]
9+
# Gadget manages all your scopes, webhooks and api versioning for your app
10+
# As such, this line should not be changed unless you are working with
11+
# Shopify managed installations. Read more at https://docs.gadget.dev/guides/plugins/shopify/advanced-topics/shopify-app-toml
12+
include_config_on_deploy = false
13+
14+
[auth]
15+
redirect_urls = ["https://product-quiz-public-remix-ssr.gadget.app/api/connections/auth/shopify/callback"]
16+
17+
[webhooks]
18+
api_version = "2025-04"

0 commit comments

Comments
 (0)