Skip to content

Commit

Permalink
Merge pull request #169 from jamalsoueidan/add-product-type
Browse files Browse the repository at this point in the history
add product-type to products
  • Loading branch information
jamalsoueidan authored Jul 1, 2024
2 parents f13e31b + 87721f5 commit 58e5e42
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 9 deletions.
3 changes: 3 additions & 0 deletions openapi/paths/customer/product/add/body.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ type: object
properties:
title:
type: string
productType:
type: string
description:
type: string
descriptionHtml:
Expand Down Expand Up @@ -31,6 +33,7 @@ properties:

required:
- title
- productType
- description
- descriptionHtml
- scheduleId
Expand Down
2 changes: 2 additions & 0 deletions openapi/paths/customer/product/update/body.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ properties:
type: string
hideFromCombine:
type: string
productType:
type: string
price:
type: object
properties:
Expand Down
7 changes: 7 additions & 0 deletions openapi/paths/openai/product-title/response.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ properties:
payload:
type: object
properties:
collection:
type: object
properties:
id:
type: string
title:
type: string
title:
type: string
description:
Expand Down
3 changes: 2 additions & 1 deletion src/functions/customer/controllers/product/add.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { HttpRequest, InvocationContext } from "@azure/functions";

import {
HttpSuccessResponse,
createContext,
createHttpRequest,
HttpSuccessResponse,
} from "~/library/jest/azure";

import { createUser } from "~/library/jest/helpers";
Expand Down Expand Up @@ -142,6 +142,7 @@ describe("CustomerProductControllerAdd", () => {
hideFromProfile: true,
title,
description: "ok",
productType: "test",
descriptionHtml: "<b>test</b>",
scheduleId: newSchedule._id,
locations,
Expand Down
1 change: 1 addition & 0 deletions src/functions/customer/controllers/product/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const CustomerProductControllerAddQuerySchema = z.object({
const CustomerProductControllerAddBodySchema = ScheduleProductZodSchema.pick({
locations: true,
price: true,
productType: true,
compareAtPrice: true,
hideFromCombine: true,
hideFromProfile: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,6 @@ describe("CustomerProductUpdateOrchestration", () => {
`user-${user.username}`,
`userid-${user.customerId}`,
`treatments`,
`productid-${GidFormat.parse(
mockProductUpdate.productUpdate?.product?.id
)}`,
`product-${product.productHandle}`,
`scheduleid-${newSchedule._id}`,
`location_type-${location.locationType}`,
"active",
Expand All @@ -265,6 +261,7 @@ describe("CustomerProductUpdateOrchestration", () => {
variables: {
id: mockProductUpdate.productUpdate?.product?.id,
title: product.title,
productType: product.productType,
descriptionHtml: product.descriptionHtml,
metafields: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export const updateProduct = async ({
const variables = {
title: product.title,
descriptionHtml: product.descriptionHtml || "",
productType: product.productType || "Andet",
id: `gid://shopify/Product/${product.productId}`,
metafields: [
{
Expand Down Expand Up @@ -180,8 +181,6 @@ export const updateProduct = async ({
`user-${user.username}`,
`userid-${user.customerId}`,
"treatments",
`productid-${product.productId}`,
`product-${product.productHandle}`,
`scheduleid-${schedule._id}`,
]
.concat(tags)
Expand Down
1 change: 1 addition & 0 deletions src/functions/customer/services/product/add.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ describe("CustomerProductServiceAdd", () => {
},
{
...productBody,
productType: "Andet",
scheduleId: newSchedule._id,
title,
}
Expand Down
2 changes: 2 additions & 0 deletions src/functions/customer/services/product/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type CustomerProductServiceAdd = {

export type CustomerProductServiceAddBody = Pick<
ScheduleProduct,
| "productType"
| "description"
| "descriptionHtml"
| "locations"
Expand Down Expand Up @@ -62,6 +63,7 @@ export const CustomerProductServiceAdd = async (
productHandle: shopifyProduct.handle,
productId: shopifyProductId,
variantId: GidFormat.parse(variant.id),
productType: body.productType,
price: {
amount: body.price.amount,
currencyCode: "DKK",
Expand Down
2 changes: 1 addition & 1 deletion src/functions/openai/services/product-categorize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Respond with this JSON structure:
content,
},
],
max_tokens: 300,
max_tokens: 20000,
response_format: {
type: "json_object",
},
Expand Down
31 changes: 30 additions & 1 deletion src/functions/openai/services/product-title.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import OpenAI from "openai";
import { shopifyAdmin } from "~/library/shopify";
import { COLLECTIONS } from "./product-categorize";

const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});

type OpenAIServiceProductTitleReturn = {
collection: {
id: string;
title: string;
};
title: string;
description: string;
};
Expand All @@ -16,12 +22,35 @@ export const OpenAIServiceProductTitle = async ({
title: string;
description?: string;
}) => {
const { data } = await shopifyAdmin().request(COLLECTIONS);

const onlyRootcollections = data?.collections.nodes.filter(
(col: any) => col.ruleSet.rules.length > 1
);

const collectionsContext = JSON.stringify(onlyRootcollections, null, 2);

const response = await openai.chat.completions.create({
model: "gpt-4o-2024-05-13",
messages: [
{
role: "system",
content: `You are an expert in correcting treatment titles and descriptions. Please correct any grammatical errors in title and description. If the description is missing, add a short sentence that encourages customers to buy this treatment, the text should be from the indiviual beauty professional as seller of the treatment. Please responds with title, description json format and keep the language in danish.`,
content: `
You are an expert in correcting treatment titles and descriptions.
Please correct any grammatical errors in title and description. If the description is missing, add a short sentence that encourages customers to buy this treatment, the text should be from the indiviual beauty professional as seller of the treatment.
Also figure out the collection the treatment belongs to.
### Collections
${collectionsContext}
Respond with this JSON structure, and choose danish language for title and description.
{
"collection": {
id: "gid://shopify/Collection/1111",
title: "example",
},
title: "example",
description: "example",
}`,
},
{
role: "user",
Expand Down
1 change: 1 addition & 0 deletions src/functions/schedule/schedule.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export const ScheduleProductZodSchema = z.object({
productHandle: z.string(),
collectionIds: z.array(GidFormat),
productId: GidFormat,
productType: z.string(),
variantId: GidFormat,
user: z
.object({
Expand Down
1 change: 1 addition & 0 deletions src/functions/schedule/schemas/product.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const ProductSchema = new mongoose.Schema<ScheduleProduct>(
type: String,
index: true,
},
productType: String,
productId: {
type: Number,
index: true,
Expand Down
1 change: 1 addition & 0 deletions src/library/jest/helpers/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const getProductObject = (
): ScheduleProduct => ({
title: "test",
collectionIds: [],
productType: "test",
productHandle: faker.internet.displayName(),
productId: faker.number.int({ min: 1, max: 10000000 }),
scheduleIdMetafieldId: String(faker.number.int({ min: 1, max: 10000000 })),
Expand Down

0 comments on commit 58e5e42

Please sign in to comment.