Skip to content

Commit

Permalink
feat(business-unit): fixes for business units (#202)
Browse files Browse the repository at this point in the history
- updated sdk
- added better business-unit handling
- added approval rule mode updating
  • Loading branch information
demeyerthom authored Aug 16, 2024
1 parent 48e6236 commit 8737a6a
Show file tree
Hide file tree
Showing 17 changed files with 173 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-walls-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/commercetools-mock": minor
---

Fixed issues with business units, updated sdk to latest
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
60 changes: 51 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/oauth/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ describe("OAuth2Server", () => {
addresses: [],
authenticationMode: "password",
isEmailVerified: true,
stores: [],
});

const response = await supertest(app)
Expand Down
48 changes: 40 additions & 8 deletions src/repositories/business-unit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
BusinessUnitChangeApprovalRuleModeAction,
BusinessUnitChangeAssociateModeAction,
BusinessUnitChangeStatusAction,
BusinessUnitUpdateAction,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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),
),
Expand Down Expand Up @@ -170,6 +194,14 @@ class BusinessUnitUpdateHandler
}
}

changeApprovalRuleMode(
context: RepositoryContext,
resource: Writable<BusinessUnit>,
{ approvalRuleMode }: BusinessUnitChangeApprovalRuleModeAction,
) {
resource.approvalRuleMode = approvalRuleMode;
}

changeAssociateMode(
context: RepositoryContext,
resource: Writable<BusinessUnit>,
Expand Down
12 changes: 8 additions & 4 deletions src/repositories/cart/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions src/repositories/customer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> {
context.projectKey,
this._storage,
),
stores: [],
};
return this.saveNew(context, resource);
}
Expand Down
3 changes: 1 addition & 2 deletions src/repositories/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
type StoreReference,
type StoreResourceIdentifier,
type Type,
type TypedMoney,
type _Money,
} from "@commercetools/platform-sdk";
import type { Request } from "express";
Expand Down Expand Up @@ -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;
};
Expand Down
2 changes: 2 additions & 0 deletions src/repositories/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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),
Expand Down
34 changes: 34 additions & 0 deletions src/repositories/product-tailoring.ts
Original file line number Diff line number Diff line change
@@ -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<ProductTailoring, ProductTailoringUpdateAction>
>
{
setSlug() {
throw new Error("SetSlug method for product-tailoring not implemented.");
}
}
Loading

0 comments on commit 8737a6a

Please sign in to comment.