Skip to content

Commit

Permalink
Merge pull request #146 from jamalsoueidan/add-active-metafield
Browse files Browse the repository at this point in the history
Add active metafield for product
  • Loading branch information
jamalsoueidan authored May 17, 2024
2 parents 6e0e15f + aa59e19 commit a816c02
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/functions/customer/controllers/product/add.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ describe("CustomerProductControllerAdd", () => {
},
],
},
active: {
id: `gid://shopify/Metafield/3333`,
value: `False`,
},
user: {
id: `gid://shopify/Metafield/44429081510215`,
value: `gid://shopify/Metafield/44429081510215`,
Expand Down Expand Up @@ -184,6 +188,10 @@ describe("CustomerProductControllerAdd", () => {
id: `gid://shopify/Metafield/233`,
value: "false",
},
active: {
id: mockProduct.productDuplicate?.newProduct?.active?.id!,
value: "False",
},
parentId: {
id: mockProduct.productDuplicate?.newProduct?.parentId?.id!,
value: `gid://shopify/Product/${body.parentId}`,
Expand Down Expand Up @@ -320,6 +328,10 @@ describe("CustomerProductControllerAdd", () => {
id: mockProductUpdate.productUpdate?.product?.user?.id,
value: user.userMetaobjectId,
},
{
id: mockProductUpdate.productUpdate?.product?.active?.id,
value: "true",
},
{
id: mockProductUpdate.productUpdate?.product?.scheduleId?.id,
value: newSchedule._id.toString(),
Expand Down
12 changes: 12 additions & 0 deletions src/functions/customer/services/product/add.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ describe("CustomerProductServiceAdd", () => {
},
],
},
active: {
id: `gid://shopify/Metafield/3333`,
value: `False`,
},
user: {
id: `gid://shopify/Metafield/22`,
value: `gid://shopify/Metafield/111`,
Expand Down Expand Up @@ -157,6 +161,10 @@ describe("CustomerProductServiceAdd", () => {
variants: {
nodes: mockProduct.productDuplicate?.newProduct?.variants.nodes!,
},
active: {
id: mockProduct.productDuplicate?.newProduct?.active?.id!,
value: `True`,
},
hideFromProfile: {
id: mockProduct.productDuplicate?.newProduct?.hideFromProfile?.id!,
value: "true",
Expand Down Expand Up @@ -318,6 +326,10 @@ describe("CustomerProductServiceAdd", () => {
id: mockProductUpdate.productUpdate?.product?.user?.id,
value: mockProductUpdate.productUpdate?.product?.user?.value,
},
{
id: mockProductUpdate.productUpdate?.product?.active?.id,
value: "true",
},
{
id: mockProductUpdate.productUpdate?.product?.scheduleId?.id,
value: newSchedule._id.toString(),
Expand Down
6 changes: 6 additions & 0 deletions src/functions/customer/services/product/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export const CustomerProductServiceAdd = async (
metaobjectId: shopifyProduct.user?.id,
value: shopifyProduct.user?.value,
},
activeMetafieldId: shopifyProduct.active?.id,
active: false,
hideFromCombineMetafieldId: shopifyProduct.hideFromCombine?.id,
hideFromCombine: body.hideFromCombine || false,
hideFromProfileMetafieldId: shopifyProduct.hideFromProfile?.id,
Expand Down Expand Up @@ -127,6 +129,10 @@ export const PRODUCT_FRAGMENT = `#graphql
price
}
}
active: metafield(key: "active", namespace: "system") {
id
value
}
user: metafield(key: "user", namespace: "booking") {
id
value
Expand Down
15 changes: 14 additions & 1 deletion src/functions/customer/services/product/update.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createUser } from "~/library/jest/helpers";
import { getProductObject } from "~/library/jest/helpers/product";
import { createSchedule } from "~/library/jest/helpers/schedule";
import { shopifyAdmin } from "~/library/shopify";
import { GidFormat } from "~/library/zod";
import { BooleanOrString, GidFormat } from "~/library/zod";
import {
ProductPricepdateMutation,
ProductUpdateMutation,
Expand Down Expand Up @@ -56,6 +56,10 @@ describe("CustomerProductServiceUpdate", () => {
},
],
},
active: {
id: `gid://shopify/Metafield/3333`,
value: `False`,
},
user: {
id: `gid://shopify/Metafield/44429081510215`,
value: `gid://shopify/Metafield/123312`,
Expand Down Expand Up @@ -141,6 +145,11 @@ describe("CustomerProductServiceUpdate", () => {
metaobjectId: mockProductUpdate.productUpdate?.product?.user?.id,
value: mockProductUpdate.productUpdate?.product?.user?.value,
},
activeMetafieldId:
mockProductUpdate.productUpdate?.product?.active?.id,
active: BooleanOrString.parse(
mockProductUpdate.productUpdate?.product?.active?.value
),
hideFromProfileMetafieldId:
mockProductUpdate.productUpdate?.product?.hideFromProfile?.id,
hideFromProfile: false,
Expand Down Expand Up @@ -229,6 +238,10 @@ describe("CustomerProductServiceUpdate", () => {
id: mockProductUpdate.productUpdate?.product?.user?.id,
value: user.userMetaobjectId,
},
{
id: mockProductUpdate.productUpdate?.product?.active?.id,
value: user.active.toString(),
},
{
id: mockProductUpdate.productUpdate?.product?.scheduleId?.id,
value: newSchedule._id.toString(),
Expand Down
8 changes: 8 additions & 0 deletions src/functions/customer/services/product/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ export const CustomerProductServiceUpdate = async (
: []),
];

if (user.active !== oldProduct.active) {
metafields.push({
id: oldProduct.activeMetafieldId,
value: user.active.toString(),
});
}

const locations = oldProduct.locations.concat(
(body.locations || [])?.filter(
(item2) =>
Expand All @@ -146,6 +153,7 @@ export const CustomerProductServiceUpdate = async (
const newProduct = {
...oldProduct,
...body,
active: user.active,
user: {
...oldProduct.user,
value: user.images?.profile?.metaobjectId || oldProduct.user?.value,
Expand Down
2 changes: 2 additions & 0 deletions src/functions/schedule/schedule.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ export const ScheduleProductZodSchema = z.object({
value: z.string().optional(),
})
.optional(),
activeMetafieldId: z.string().optional(),
active: BooleanOrString,
hideFromProfileMetafieldId: z.string().optional(),
hideFromProfile: BooleanOrString,
hideFromCombineMetafieldId: z.string().optional(),
Expand Down
2 changes: 2 additions & 0 deletions src/functions/schedule/schemas/product.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export const ProductSchema = new mongoose.Schema<ScheduleProduct>(
metaobjectId: String,
value: String,
},
activeMetafieldId: String,
active: Boolean,
hideFromProfileMetafieldId: String,
hideFromProfile: Boolean,
hideFromCombineMetafieldId: String,
Expand Down
8 changes: 6 additions & 2 deletions src/library/zod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ export const objectIdValidator = z
export const BooleanOrString = z
.union([z.boolean(), z.string()])
.transform((value) => {
if (typeof value === "string" && value === "true") {
return true;
if (typeof value === "string") {
if (value.toLowerCase() === "true") {
return true;
} else {
return false;
}
}
return value;
})
Expand Down
Loading

0 comments on commit a816c02

Please sign in to comment.