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(business-unit): fixes for business units #202

Merged
merged 4 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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.

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
Loading