Skip to content

Commit

Permalink
Moving the template to ts and fixing issues from the pr review
Browse files Browse the repository at this point in the history
  • Loading branch information
DevAOC committed Nov 12, 2024
1 parent 6d36b93 commit bd9ef09
Show file tree
Hide file tree
Showing 62 changed files with 190 additions and 165 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { CreateReviewMetaobjectGlobalActionContext } from "gadget-server";
export const run: ActionRun = async ({ params, logger, api, connections }) => {
const { shopId, review } = params;

/**
* @param { CreateReviewMetaobjectGlobalActionContext } context
*/
export async function run({ params, logger, api, connections }) {
const {
shopId,
review: { id, rating, content, productId },
} = params;
if (!shopId) throw new Error("shopId is required");
if (!review || !review?.id) throw new Error("review is required");

const shopify = connections.shopify.forShop(shopId);
const shopify = await connections.shopify.forShopId(shopId);

// Create the metaobject
const metaobjectCreateResponse = await shopify.graphql(
Expand All @@ -35,18 +30,18 @@ export async function run({ params, logger, api, connections }) {
{
key: "rating",
value: JSON.stringify({
value: rating,
value: review.rating,
scale_max: "5",
scale_min: "0",
}),
},
{
key: "content",
value: content,
value: review.content,
},
{
key: "product",
value: `gid://shopify/Product/${productId}`,
value: `gid://shopify/Product/${review.productId}`,
},
],
},
Expand All @@ -59,10 +54,10 @@ export async function run({ params, logger, api, connections }) {
metaobjectCreateResponse.metaobjectCreate.userErrors[0].message
);

await api.internal.review.update(id, {
await api.internal.review.update(review.id, {
metaobjectId: metaobjectCreateResponse.metaobjectCreate.metaobject.id,
});
}
};

export const params = {
shopId: { type: "string" },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { CreateReviewsMetafieldGlobalActionContext } from "gadget-server";

/**
* @param { CreateReviewsMetafieldGlobalActionContext } context
*/
export async function run({ params, logger, api, connections }) {
export const run: ActionRun = async ({ params, logger, api, connections }) => {
const { shopId, productId } = params;

if (!shopId) throw new Error("shopId is required");

const shopify = await connections.shopify.forShopId(shopId);

const metafieldsSetResponse = await shopify.graphql(
Expand Down Expand Up @@ -34,7 +31,7 @@ export async function run({ params, logger, api, connections }) {

if (metafieldsSetResponse?.metafieldsSet?.userErrors?.length)
throw new Error(metafieldsSetResponse.metafieldsSet.userErrors[0].message);
}
};

export const params = {
shopId: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
import { EnqueueEmailsGlobalActionContext } from "gadget-server";

/**
* @param { EnqueueEmailsGlobalActionContext } context
*/
export async function run({ params, logger, api, connections }) {
export const run: ActionRun = async ({ params, logger, api, connections }) => {
const { allOrders } = params;

const options = {
queue: {
name: params.options?.queue?.name ?? "",
maxConcurrency: params.options?.queue?.maxConcurrency,
},
retries: params.options?.retries,
};

if (!allOrders?.length) {
logger.info("No orders to process");
return;
}

const orders = allOrders.splice(0, 80);

for (const {
singleUseCode,
customer: { email },
} of orders) {
await api.enqueue(api.sendEmail, { singleUseCode, email }, options);
for (const { singleUseCode, customer } of orders) {
await api.enqueue(
api.sendEmail,
{ singleUseCode, email: customer?.email },
options
);
}

if (allOrders.length)
await api.enqueue(api.enqueueEmails, { allOrders, options }, options);
}
};

export const params = {
allOrders: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import { FetchOrderDataGlobalActionContext } from "gadget-server";

/**
* @param { FetchOrderDataGlobalActionContext } context
*/
export async function run({ params, logger, api, connections }) {
export const run: ActionRun = async ({ params, logger, api, connections }) => {
const order = await api.shopifyOrder.maybeFindFirst({
filter: {
singleUseCode: {
Expand Down Expand Up @@ -42,28 +37,27 @@ export async function run({ params, logger, api, connections }) {

while (lineItems.hasNextPage) {
lineItems = await lineItems.nextPage();
allLineItems = allLineItems.concat(lineItems);
allLineItems.push(...lineItems);
}

const seen = {};
const products = [];
const seen: { [key: string]: boolean } = {};
const products: {
id: string;
title: string;
image: string;
alt: string;
}[] = [];

for (const {
product: {
id,
title,
featuredMedia: {
file: { alt, url },
},
},
} of allLineItems) {
if (!seen[id]) {
seen[id] = true;
for (const { product } of allLineItems) {
if (!product?.id) continue;

if (!seen[product?.id]) {
seen[product?.id] = true;
products.push({
id: id,
title: title,
image: url,
alt,
id: product?.id,
title: product.title ?? "",
image: product.featuredMedia?.file?.url ?? "",
alt: product.featuredMedia?.file?.alt ?? "",
});
}
}
Expand All @@ -76,7 +70,7 @@ export async function run({ params, logger, api, connections }) {
} else {
throw new Error(`Single use code not found: ${params.code}`);
}
}
};

export const params = {
code: {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ActionOptions } from "gadget-server";
import { globalShopifySync } from "gadget-server/shopify";

const HourInMs = 60 * 60 * 1000;

export const run: ActionRun = async ({ params, logger, api, connections }) => {
const syncOnlyModels = connections.shopify.enabledModels
.filter((model) => model.syncOnly)
.map((model) => model.apiIdentifier);

const syncSince = new Date(Date.now() - 25 * HourInMs);

await globalShopifySync({
apiKeys: connections.shopify.apiKeys,
syncSince,
models: syncOnlyModels,
});
};

export const options: ActionOptions = {
triggers: {
scheduler: [
{
every: "day",
at: "14:21 UTC",
},
],
},
};
43 changes: 0 additions & 43 deletions shopify/product-reviews-template/api/actions/sendEmail.js

This file was deleted.

29 changes: 29 additions & 0 deletions shopify/product-reviews-template/api/actions/sendEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import renderEmail from "../utilities/renderEmail";

export const run: ActionRun = async ({
params,
logger,
api,
connections,
emails,
currentAppUrl,
}) => {
const { singleUseCode, email } = params;

if (!email || !singleUseCode) throw new Error("Missing required params");

await emails.sendMail({
to: email,
subject: "Review your purchase",
html: await renderEmail({ currentAppUrl, singleUseCode }),
});
};

export const params = {
email: {
type: "string",
},
singleUseCode: {
type: "string",
},
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { SendReviewRequestsGlobalActionContext } from "gadget-server";
import { v4 as uuidv4 } from "uuid";

/**
* @param { SendReviewRequestsGlobalActionContext } context
*/
export async function run({ params, logger, api, connections }) {
export const run: ActionRun = async ({ params, logger, api, connections }) => {
let orders = await api.shopifyOrder.findMany({
first: 250,
filter: {
Expand All @@ -27,7 +23,7 @@ export async function run({ params, logger, api, connections }) {

while (orders.hasNextPage) {
orders = await orders.nextPage();
allOrders = allOrders.concat(orders);
allOrders.push(...orders);
}

const options = {
Expand All @@ -44,4 +40,4 @@ export async function run({ params, logger, api, connections }) {
{ allOrders: allOrders.map(({ __typeName, ...rest }) => rest), options },
options
);
}
};
23 changes: 23 additions & 0 deletions shopify/product-reviews-template/api/utilities/renderEmail.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from "react";
import { render } from "@react-email/render";
import { Container, Button } from "@react-email/components";

export default async ({
singleUseCode,
currentAppUrl,
}: {
singleUseCode: string;
currentAppUrl: string;
}) => {
return await render(
<Container>
{/* Add more text in here */}
<Button
href={`${currentAppUrl}/review/${singleUseCode}`}
style={{ color: "#61dafb", padding: "10px 20px" }}
>
Review
</Button>
</Container>
);
};
Loading

0 comments on commit bd9ef09

Please sign in to comment.