Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for set product price action #198

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 48 additions & 4 deletions src/repositories/product/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import type {
ProductSetMetaDescriptionAction,
ProductSetMetaKeywordsAction,
ProductSetMetaTitleAction,
ProductSetProductPriceCustomFieldAction,
ProductSetProductPriceCustomTypeAction,
ProductSetTaxCategoryAction,
ProductTransitionStateAction,
ProductUpdateAction,
Expand All @@ -35,8 +37,11 @@ import type {
} from "@commercetools/platform-sdk";
import { CommercetoolsError } from "~src/exceptions";
import type { Writable } from "~src/types";
import { AbstractUpdateHandler, RepositoryContext } from "../abstract";
import { getReferenceFromResourceIdentifier } from "../helpers";
import { AbstractUpdateHandler, type RepositoryContext } from "../abstract";
import {
createCustomFields,
getReferenceFromResourceIdentifier,
} from "../helpers";
import {
checkForStagedChanges,
getVariant,
Expand Down Expand Up @@ -853,6 +858,47 @@ export class ProductUpdateHandler
return resource;
}

setProductPriceCustomField(
context: RepositoryContext,
resource: Writable<Product>,
{ name, value }: ProductSetProductPriceCustomFieldAction,
) {
resource.masterData.staged.masterVariant.prices?.map((price) => {
console.info("here for price", price);
if (!price.custom) {
return;
}
if (value === null) {
delete price.custom.fields[name];
} else {
price.custom.fields[name] = value;
}
});
return resource;
}

setProductPriceCustomType(
context: RepositoryContext,
resource: Writable<Product>,
{ type, fields }: ProductSetProductPriceCustomTypeAction,
) {
resource.masterData.staged.masterVariant.prices?.map((price) => {
console.info("im hereeee for type", type);
console.info("and for price", price);
if (type) {
price.custom = createCustomFields(
{ type, fields },
context.projectKey,
this._storage,
);
} else {
price.custom = undefined;
}
});
console.info("resourceeeeee", resource);
return resource;
}

setTaxCategory(
context: RepositoryContext,
resource: Writable<Product>,
Expand Down Expand Up @@ -919,8 +965,6 @@ export class ProductUpdateHandler
}

// 'setPrices': () => {},
// 'setProductPriceCustomType': () => {},
// 'setProductPriceCustomField': () => {},
// 'setDiscountedPrice': () => {},
// 'setAttributeInAllVariants': () => {},
// 'setCategoryOrderHint': () => {},
Expand Down
69 changes: 69 additions & 0 deletions src/services/product.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Category,
CategoryDraft,
Image,
Price,
PriceDraft,
Product,
ProductData,
Expand Down Expand Up @@ -120,6 +121,12 @@
currencyCode: "EUR",
centAmount: 1000,
},
custom: {
type: { typeId: "type", id: "product-price-custom-type" },
fields: {
myCustomField: null,
},
},
},
],
},
Expand All @@ -141,6 +148,12 @@
currencyCode: "EUR",
centAmount: 2000,
},
custom: {
type: { typeId: "type", id: "product-price-custom-type" },
fields: {
myCustomField: null,
},
},
},
],
},
Expand Down Expand Up @@ -203,6 +216,12 @@
currencyCode: "EUR",
centAmount: 1000,
},
custom: {
type: { typeId: "type", id: "product-price-custom-type" },
fields: {
myCustomField: null,
},
},
},
],
},
Expand All @@ -224,6 +243,12 @@
currencyCode: "EUR",
centAmount: 2000,
},
custom: {
type: { typeId: "type", id: "product-price-custom-type" },
fields: {
myCustomField: null,
},
},
},
],
},
Expand Down Expand Up @@ -1418,4 +1443,48 @@
id: productState2.id,
});
});

test("setProductPriceCustomField", async () => {
assert(productPublished, "product not created");

productPublished.masterData.current.masterVariant.prices?.push({
key: "new_price_eur",
country: "NL",
value: {
currencyCode: "EUR",
centAmount: 9999,
},
custom: {
type: { typeId: "type", id: "product-price-custom-type" },
fields: {
myCustomField: null,
},
},
} as unknown as Price);

const priceId =
productPublished.masterData.current.masterVariant.prices?.find((price) =>
price.key?.startsWith("new_price"),
)?.id;

const response = await supertest(ctMock.app)
.post(`/dummy/products/${productPublished.id}`)
.send({
version: 1,
actions: [
{
action: "setProductPriceCustomField",
name: "myCustomField",
value: "123",
priceId,
},
],
});
expect(response.status).toBe(200);
console.info(JSON.stringify(response.body.masterData.current, null, 2));
expect(
response.body.masterData.staged.masterVariant.prices?.[0].custom.fields

Check failure on line 1486 in src/services/product.test.ts

View workflow job for this annotation

GitHub Actions / Validate on Node 18.x and ubuntu-latest

src/services/product.test.ts > Product update actions > setProductPriceCustomField

TypeError: Cannot read properties of undefined (reading 'fields') ❯ src/services/product.test.ts:1486:62

Check failure on line 1486 in src/services/product.test.ts

View workflow job for this annotation

GitHub Actions / Validate on Node 18.x and macOS-latest

src/services/product.test.ts > Product update actions > setProductPriceCustomField

TypeError: Cannot read properties of undefined (reading 'fields') ❯ src/services/product.test.ts:1486:62

Check failure on line 1486 in src/services/product.test.ts

View workflow job for this annotation

GitHub Actions / Validate on Node 20.x and ubuntu-latest

src/services/product.test.ts > Product update actions > setProductPriceCustomField

TypeError: Cannot read properties of undefined (reading 'fields') ❯ src/services/product.test.ts:1486:62

Check failure on line 1486 in src/services/product.test.ts

View workflow job for this annotation

GitHub Actions / Validate on Node 20.x and macOS-latest

src/services/product.test.ts > Product update actions > setProductPriceCustomField

TypeError: Cannot read properties of undefined (reading 'fields') ❯ src/services/product.test.ts:1486:62

Check failure on line 1486 in src/services/product.test.ts

View workflow job for this annotation

GitHub Actions / Validate on Node 22.x and ubuntu-latest

src/services/product.test.ts > Product update actions > setProductPriceCustomField

TypeError: Cannot read properties of undefined (reading 'fields') ❯ src/services/product.test.ts:1486:62

Check failure on line 1486 in src/services/product.test.ts

View workflow job for this annotation

GitHub Actions / Validate on Node 22.x and macOS-latest

src/services/product.test.ts > Product update actions > setProductPriceCustomField

TypeError: Cannot read properties of undefined (reading 'fields') ❯ src/services/product.test.ts:1486:62
?.myCustomField,
).toBe("123");
});
});
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as ctp from "@commercetools/platform-sdk";
import { RepositoryMap } from "./repositories";
import AbstractService from "./services/abstract";
import type { RepositoryMap } from "./repositories";
import type AbstractService from "./services/abstract";

export const isType = <T>(x: T) => x;

Expand Down
Loading