diff --git a/.changeset/dry-walls-dance.md b/.changeset/dry-walls-dance.md new file mode 100644 index 00000000..afe01c5a --- /dev/null +++ b/.changeset/dry-walls-dance.md @@ -0,0 +1,5 @@ +--- +"@labdigital/commercetools-mock": minor +--- + +Fixed issues with business units, updated sdk to latest diff --git a/package.json b/package.json index ef5d1680..2405e132 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "devDependencies": { "@changesets/changelog-github": "^0.5.0", "@changesets/cli": "^2.27.1", - "@commercetools/platform-sdk": "7.3.0", + "@commercetools/platform-sdk": "7.11.0", "@stylistic/eslint-plugin": "^1.6.2", "@types/basic-auth": "^1.1.8", "@types/body-parser": "^1.19.5", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e9560ade..8b6bc02a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -49,8 +49,8 @@ importers: specifier: ^2.27.1 version: 2.27.1 '@commercetools/platform-sdk': - specifier: 7.3.0 - version: 7.3.0 + specifier: 7.11.0 + version: 7.11.0 '@stylistic/eslint-plugin': specifier: ^1.6.2 version: 1.6.2(eslint@8.57.0)(typescript@5.3.3) @@ -251,12 +251,12 @@ packages: '@changesets/write@0.3.0': resolution: {integrity: sha512-slGLb21fxZVUYbyea+94uFiD6ntQW0M2hIKNznFizDhZPDgn2c/fv1UzzlW43RVzh1BEDuIqW6hzlJ1OflNmcw==} - '@commercetools/platform-sdk@7.3.0': - resolution: {integrity: sha512-MovSWsEk8f/ZwtxHPuYf8dMIf3R3bOU44UdKtha4JXWw/xrP03B0/MlEPnmHskGHedb2QMt2asvEfw2yLF34oA==} + '@commercetools/platform-sdk@7.11.0': + resolution: {integrity: sha512-8vlgiK6w8NewUZ60HUDJzdNUzDp2ByO+8fAH7aUTkt+suXYbG3Jl/k8brgX8JOn2AR4k7kC8g7sEHV6JUN8KzA==} engines: {node: '>=14'} - '@commercetools/sdk-client-v2@2.3.0': - resolution: {integrity: sha512-+vS6qRoKBbkunZCpdozung+7te97nFxaidLPqOTlz/9TmJaRfMMcYC/KUZCTS8S5a/4BFfY6DYujddolaU7e6Q==} + '@commercetools/sdk-client-v2@2.5.0': + resolution: {integrity: sha512-v1y++O6yllG+IRTYm9jPE8s667+GapnysyGIf8NJDZbVwhvcanixZL4d20imXjCpOr4u1iZrgRftc90mgYqblw==} engines: {node: '>=14'} '@commercetools/sdk-middleware-auth@7.0.1': @@ -271,6 +271,10 @@ packages: resolution: {integrity: sha512-DhMXAA2yIch/AaGxy7st85Z1HFmeLtHWGkr9z5rX4xKjan4PHGB/IE5saAR+SNGHhs6+1Lp8vZEHDo3tFqVLmg==} engines: {node: '>=14'} + '@commercetools/ts-client@2.0.2': + resolution: {integrity: sha512-4R+tOcshiDd3ikhwART2P58VFoZkMNIBCZYTidXIzpPrDvZta32B71xbp9mHVMROLBmEcCBU2jhzbKMOlMr/uw==} + engines: {node: '>=14'} + '@cspotcode/source-map-support@0.8.1': resolution: {integrity: sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==} engines: {node: '>=12'} @@ -995,6 +999,10 @@ packages: '@vitest/utils@1.5.0': resolution: {integrity: sha512-BDU0GNL8MWkRkSRdNFvCUCAVOeHaUlVJ9Tx0TYBZyXaaOTmGtUFObzchCivIBrIwKzvZA7A9sCejVhXM2aY98A==} + abort-controller@3.0.0: + resolution: {integrity: sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==} + engines: {node: '>=6.5'} + accepts@1.3.8: resolution: {integrity: sha512-PYAthTa2m2VKxuvSD3DPC/Gy+U+sOA1LAuT8mkmRuvw+NACSaeXEQ+NHcVF7rONl6qcaxV3Uuemwawk+7+SJLw==} engines: {node: '>= 0.6'} @@ -1093,6 +1101,9 @@ packages: assertion-error@1.1.0: resolution: {integrity: sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==} + async-mutex@0.5.0: + resolution: {integrity: sha512-1A94B18jkJ3DYq284ohPxoXbfTA5HsQ7/Mf4DEhcyLx3Bz27Rh59iScbB6EPiP+B+joue6YCxcMXSbFC1tZKwA==} + asynckit@0.4.0: resolution: {integrity: sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==} @@ -1537,6 +1548,10 @@ packages: resolution: {integrity: sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==} engines: {node: '>= 0.6'} + event-target-shim@5.0.1: + resolution: {integrity: sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==} + engines: {node: '>=6'} + execa@5.1.1: resolution: {integrity: sha512-8uSpZZocAZRBAPIEINJj3Lo9HyGitllczc27Eh5YYojjMFMn8yHMDMaUHE2Jqfq05D/wucwI4JGURyXt1vchyg==} engines: {node: '>=10'} @@ -3059,6 +3074,10 @@ packages: resolution: {integrity: sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA==} engines: {node: '>= 0.4.0'} + uuid@10.0.0: + resolution: {integrity: sha512-8XkAphELsDnEGrDxUOHB3RGvXz6TeuYSGEZBOjtTtPm2lwhGBjLgOzLHB63IUWfBpNucQjND6d3AOudO+H3RWQ==} + hasBin: true + uuid@9.0.1: resolution: {integrity: sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==} hasBin: true @@ -3457,16 +3476,17 @@ snapshots: human-id: 1.0.2 prettier: 2.8.8 - '@commercetools/platform-sdk@7.3.0': + '@commercetools/platform-sdk@7.11.0': dependencies: - '@commercetools/sdk-client-v2': 2.3.0 + '@commercetools/sdk-client-v2': 2.5.0 '@commercetools/sdk-middleware-auth': 7.0.1 '@commercetools/sdk-middleware-http': 7.0.4 '@commercetools/sdk-middleware-logger': 3.0.0 + '@commercetools/ts-client': 2.0.2 transitivePeerDependencies: - encoding - '@commercetools/sdk-client-v2@2.3.0': + '@commercetools/sdk-client-v2@2.5.0': dependencies: buffer: 6.0.3 node-fetch: 2.7.0 @@ -3483,6 +3503,16 @@ snapshots: '@commercetools/sdk-middleware-logger@3.0.0': {} + '@commercetools/ts-client@2.0.2': + dependencies: + abort-controller: 3.0.0 + async-mutex: 0.5.0 + buffer: 6.0.3 + node-fetch: 2.7.0 + uuid: 10.0.0 + transitivePeerDependencies: + - encoding + '@cspotcode/source-map-support@0.8.1': dependencies: '@jridgewell/trace-mapping': 0.3.9 @@ -4164,6 +4194,10 @@ snapshots: loupe: 2.3.7 pretty-format: 29.7.0 + abort-controller@3.0.0: + dependencies: + event-target-shim: 5.0.1 + accepts@1.3.8: dependencies: mime-types: 2.1.35 @@ -4254,6 +4288,10 @@ snapshots: assertion-error@1.1.0: {} + async-mutex@0.5.0: + dependencies: + tslib: 2.6.2 + asynckit@0.4.0: {} available-typed-arrays@1.0.7: @@ -4817,6 +4855,8 @@ snapshots: etag@1.8.1: {} + event-target-shim@5.0.1: {} + execa@5.1.1: dependencies: cross-spawn: 7.0.3 @@ -6369,6 +6409,8 @@ snapshots: utils-merge@1.0.1: {} + uuid@10.0.0: {} + uuid@9.0.1: {} v8-compile-cache-lib@3.0.1: {} diff --git a/src/oauth/server.test.ts b/src/oauth/server.test.ts index 86fe9814..65810fdd 100644 --- a/src/oauth/server.test.ts +++ b/src/oauth/server.test.ts @@ -106,6 +106,7 @@ describe("OAuth2Server", () => { addresses: [], authenticationMode: "password", isEmailVerified: true, + stores: [], }); const response = await supertest(app) diff --git a/src/repositories/business-unit.ts b/src/repositories/business-unit.ts index 0f694638..c608a3b6 100644 --- a/src/repositories/business-unit.ts +++ b/src/repositories/business-unit.ts @@ -1,4 +1,5 @@ import { + BusinessUnitChangeApprovalRuleModeAction, BusinessUnitChangeAssociateModeAction, BusinessUnitChangeStatusAction, BusinessUnitUpdateAction, @@ -19,8 +20,8 @@ import { type Company, type Division, } from "@commercetools/platform-sdk"; -import { getBaseResourceProperties } from "../helpers"; -import { AbstractStorage } from "../storage/abstract"; +import { AbstractStorage } from "~src/storage"; +import { generateRandomString, getBaseResourceProperties } from "../helpers"; import { Writable } from "../types"; import { AbstractResourceRepository, @@ -43,6 +44,28 @@ export class BusinessUnitRepository extends AbstractResourceRepository<"business } create(context: RepositoryContext, draft: BusinessUnitDraft): BusinessUnit { + const addresses = + draft.addresses?.map((address) => ({ + ...address, + id: generateRandomString(5), + })) ?? []; + + const defaultBillingAddressId = + addresses.length > 0 && draft.defaultBillingAddress !== undefined + ? addresses[draft.defaultBillingAddress].id + : undefined; + const defaultShippingAddressId = + addresses.length > 0 && draft.defaultShippingAddress !== undefined + ? addresses[draft.defaultShippingAddress].id + : undefined; + + const shippingAddressIds = draft.shippingAddresses?.map( + (i) => addresses[i].id, + ); + const billingAddressIds = draft.billingAddresses?.map( + (i) => addresses[i].id, + ); + const resource = { ...getBaseResourceProperties(), key: draft.key, @@ -53,18 +76,19 @@ export class BusinessUnitRepository extends AbstractResourceRepository<"business storeMode: draft.storeMode, name: draft.name, contactEmail: draft.contactEmail, - addresses: draft.addresses?.map((a) => - createAddress(a, context.projectKey, this._storage), - ), + addresses: addresses, custom: createCustomFields( draft.custom, context.projectKey, this._storage, ), - shippingAddressIds: draft.shippingAddresses, - defaultShippingAddressId: draft.defaultShippingAddress, - billingAddressIds: draft.billingAddresses, + shippingAddressIds: shippingAddressIds, + billingAddressIds: billingAddressIds, + defaultShippingAddressId: defaultShippingAddressId, + defaultBillingAddressId: defaultBillingAddressId, associateMode: draft.associateMode, + approvalRuleMode: draft.approvalRuleMode, + associates: draft.associates?.map((a) => createAssociate(a, context.projectKey, this._storage), ), @@ -170,6 +194,14 @@ class BusinessUnitUpdateHandler } } + changeApprovalRuleMode( + context: RepositoryContext, + resource: Writable, + { approvalRuleMode }: BusinessUnitChangeApprovalRuleModeAction, + ) { + resource.approvalRuleMode = approvalRuleMode; + } + changeAssociateMode( context: RepositoryContext, resource: Writable, diff --git a/src/repositories/cart/actions.ts b/src/repositories/cart/actions.ts index dd6a4c1c..cd338964 100644 --- a/src/repositories/cart/actions.ts +++ b/src/repositories/cart/actions.ts @@ -30,6 +30,7 @@ import { type ProductPagedQueryResponse, type ProductVariant, } from "@commercetools/platform-sdk"; +import { DirectDiscount } from "@commercetools/platform-sdk/dist/declarations/src/generated/models/cart"; import { v4 as uuidv4 } from "uuid"; import { CommercetoolsError } from "~src/exceptions"; import type { Writable } from "~src/types"; @@ -419,10 +420,13 @@ export class CartUpdateHandler { discounts }: CartSetDirectDiscountsAction, ) { // Doesn't apply any discounts logic, just sets the directDiscounts field - resource.directDiscounts = discounts.map((discount) => ({ - ...discount, - id: uuidv4(), - })); + resource.directDiscounts = discounts.map( + (discount) => + ({ + ...discount, + id: uuidv4(), + }) as DirectDiscount, + ); } setLineItemShippingDetails( diff --git a/src/repositories/customer/index.ts b/src/repositories/customer/index.ts index 2fded0c3..3203b37b 100644 --- a/src/repositories/customer/index.ts +++ b/src/repositories/customer/index.ts @@ -91,6 +91,7 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> { context.projectKey, this._storage, ), + stores: [], }; return this.saveNew(context, resource); } diff --git a/src/repositories/helpers.ts b/src/repositories/helpers.ts index ee184c2d..5c4fb1bc 100644 --- a/src/repositories/helpers.ts +++ b/src/repositories/helpers.ts @@ -27,7 +27,6 @@ import { type StoreReference, type StoreResourceIdentifier, type Type, - type TypedMoney, type _Money, } from "@commercetools/platform-sdk"; import type { Request } from "express"; @@ -133,7 +132,7 @@ export const createCentPrecisionMoney = (value: _Money): CentPrecisionMoney => { }; }; -export const createTypedMoney = (value: _Money): TypedMoney => { +export const createTypedMoney = (value: _Money): CentPrecisionMoney => { const result = createCentPrecisionMoney(value); return result; }; diff --git a/src/repositories/index.ts b/src/repositories/index.ts index 2ae4b0e0..a4fe6746 100644 --- a/src/repositories/index.ts +++ b/src/repositories/index.ts @@ -1,3 +1,4 @@ +import { ProductTailoringRepository } from "~src/repositories/product-tailoring"; import { AbstractStorage } from "../storage"; import { AssociateRoleRepository } from "./associate-role"; import { AttributeGroupRepository } from "./attribute-group"; @@ -66,6 +67,7 @@ export const createRepositories = (storage: AbstractStorage) => ({ "product-discount": new ProductDiscountRepository(storage), "product-projection": new ProductProjectionRepository(storage), "product-selection": new ProductSelectionRepository(storage), + "product-tailoring": new ProductTailoringRepository(storage), "project": new ProjectRepository(storage), "review": new ReviewRepository(storage), "quote": new QuoteRepository(storage), diff --git a/src/repositories/product-tailoring.ts b/src/repositories/product-tailoring.ts new file mode 100644 index 00000000..ec29b2ca --- /dev/null +++ b/src/repositories/product-tailoring.ts @@ -0,0 +1,34 @@ +import type { + ProductTailoring, + ProductTailoringUpdateAction, +} from "@commercetools/platform-sdk"; +import { AbstractStorage } from "~src/storage"; +import { + AbstractResourceRepository, + AbstractUpdateHandler, + RepositoryContext, + UpdateHandlerInterface, +} from "./abstract"; + +export class ProductTailoringRepository extends AbstractResourceRepository<"product-tailoring"> { + constructor(storage: AbstractStorage) { + super("product-tailoring", storage); + this.actions = new ProductTailoringUpdateHandler(this._storage); + } + + create(context: RepositoryContext, draft: any): ProductTailoring { + throw new Error("Create method for product-tailoring not implemented."); + } +} + +class ProductTailoringUpdateHandler + extends AbstractUpdateHandler + implements + Partial< + UpdateHandlerInterface + > +{ + setSlug() { + throw new Error("SetSlug method for product-tailoring not implemented."); + } +} diff --git a/src/repositories/shipping-method/actions.ts b/src/repositories/shipping-method/actions.ts index 2d844617..0fd3393a 100644 --- a/src/repositories/shipping-method/actions.ts +++ b/src/repositories/shipping-method/actions.ts @@ -4,6 +4,7 @@ import { type ShippingMethod, type ShippingMethodAddShippingRateAction, type ShippingMethodAddZoneAction, + type ShippingMethodChangeActiveAction, type ShippingMethodChangeIsDefaultAction, type ShippingMethodChangeNameAction, type ShippingMethodRemoveZoneAction, @@ -18,7 +19,7 @@ import { type ZoneReference, } from "@commercetools/platform-sdk"; import deepEqual from "deep-equal"; -import type { Writable } from "../../types"; +import type { Writable } from "~src/types"; import { AbstractUpdateHandler, RepositoryContext, @@ -83,6 +84,14 @@ export class ShippingMethodUpdateHandler }); } + changeActive( + _context: RepositoryContext, + resource: Writable, + { active }: ShippingMethodChangeActiveAction, + ) { + resource.active = active; + } + changeIsDefault( _context: RepositoryContext, resource: Writable, diff --git a/src/repositories/shipping-method/index.ts b/src/repositories/shipping-method/index.ts index a17e7cb3..28fcab63 100644 --- a/src/repositories/shipping-method/index.ts +++ b/src/repositories/shipping-method/index.ts @@ -35,6 +35,7 @@ export class ShippingMethodRepository extends AbstractResourceRepository<"shippi const resource: ShippingMethod = { ...getBaseResourceProperties(), ...draft, + active: draft.active ?? true, taxCategory: getReferenceFromResourceIdentifier( draft.taxCategory, context.projectKey, diff --git a/src/services/customer.test.ts b/src/services/customer.test.ts index 0c638175..f15b4e5d 100644 --- a/src/services/customer.test.ts +++ b/src/services/customer.test.ts @@ -20,6 +20,7 @@ describe("Customer Update Actions", () => { isEmailVerified: true, authenticationMode: "Password", //default in Commercetools version: 1, + stores: [], }; ctMock.project("dummy").add("customer", customer); }); @@ -467,6 +468,7 @@ describe("Customer Password Reset", () => { isEmailVerified: true, authenticationMode: "password", custom: { type: { typeId: "type", id: "" }, fields: {} }, + stores: [], }); }); diff --git a/src/services/my-customer.test.ts b/src/services/my-customer.test.ts index 8d1bc284..08f18836 100644 --- a/src/services/my-customer.test.ts +++ b/src/services/my-customer.test.ts @@ -1,4 +1,5 @@ import type { + Customer, CustomerChangePassword, CustomerToken, MyCustomerDraft, @@ -37,6 +38,7 @@ describe("Me", () => { id: expect.anything(), createdAt: expect.anything(), lastModifiedAt: expect.anything(), + stores: [], }, }); }); @@ -73,6 +75,7 @@ describe("/me", () => { isEmailVerified: true, authenticationMode: "password", custom: { type: { typeId: "type", id: "" }, fields: {} }, + stores: [], }); }); @@ -96,6 +99,7 @@ describe("/me", () => { typeId: "type", }, }, + stores: [], }); }); @@ -119,6 +123,7 @@ describe("/me", () => { typeId: "type", }, }, + stores: [], }); const newResponse = await supertest(ctMock.app).get("/dummy/me"); @@ -126,7 +131,7 @@ describe("/me", () => { }); test("Change my password", async () => { - const customer = { + const customer: Customer = { ...getBaseResourceProperties(), id: "customer-uuid", email: "user@example.com", @@ -135,6 +140,7 @@ describe("/me", () => { isEmailVerified: true, authenticationMode: "Password", //default in Commercetools version: 1, + stores: [], }; ctMock.project("dummy").add("customer", customer); @@ -176,7 +182,7 @@ describe("/me", () => { }); test("reset password flow", async () => { - const customer = { + const customer: Customer = { ...getBaseResourceProperties(), id: "customer-uuid", email: "user@example.com", @@ -185,6 +191,7 @@ describe("/me", () => { isEmailVerified: true, authenticationMode: "Password", //default in Commercetools version: 1, + stores: [], }; ctMock.project("dummy").add("customer", customer); @@ -225,7 +232,7 @@ describe("/me", () => { }); test("verify email flow", async () => { - const customer = { + const customer: Customer = { ...getBaseResourceProperties(), id: "customer-uuid", email: "user@example.com", @@ -234,6 +241,7 @@ describe("/me", () => { isEmailVerified: false, authenticationMode: "Password", //default in Commercetools version: 1, + stores: [], }; ctMock.project("dummy").add("customer", customer); diff --git a/src/services/shipping-method.test.ts b/src/services/shipping-method.test.ts index c477b7ca..70617efe 100644 --- a/src/services/shipping-method.test.ts +++ b/src/services/shipping-method.test.ts @@ -74,6 +74,7 @@ describe("Shipping method", () => { }, version: 1, zoneRates: [], + active: true, }); }); diff --git a/src/storage/in-memory.ts b/src/storage/in-memory.ts index 46618c35..944fb784 100644 --- a/src/storage/in-memory.ts +++ b/src/storage/in-memory.ts @@ -22,6 +22,7 @@ import { type Product, type ProductDiscount, type ProductProjection, + type ProductTailoring, type ProductType, type Project, type Quote, @@ -189,6 +190,7 @@ export class InMemoryStorage extends AbstractStorage { "product-selection": new Map(), "product-type": new Map(), "product-projection": new Map(), + "product-tailoring": new Map(), "review": new Map(), "shipping-method": new Map(), "staged-quote": new Map(), diff --git a/src/types.ts b/src/types.ts index 86541c28..de31c055 100644 --- a/src/types.ts +++ b/src/types.ts @@ -45,6 +45,7 @@ export type ResourceMap = { "product-price": ctp.StandalonePrice; "product-projection": ctp.ProductProjection; "product-selection": ctp.ProductSelection; + "product-tailoring": ctp.ProductTailoring; "product-type": ctp.ProductType; "product": ctp.Product; "quote-request": ctp.QuoteRequest; @@ -85,6 +86,7 @@ export type PagedQueryResponseMap = { "product-price": ctp.StandalonePricePagedQueryResponse; "product-projection": ctp.ProductProjectionPagedQueryResponse; "product-selection": ctp.ProductSelectionPagedQueryResponse; + "product-tailoring": ctp.ProductTailoringPagedQueryResponse; "product-type": ctp.ProductTypePagedQueryResponse; "product": ctp.ProductPagedQueryResponse; "quote-request": ctp.QuoteRequestPagedQueryResponse;