Skip to content

Commit 564b3f8

Browse files
authored
fix: match customer and business unit address actions (#359)
1 parent b07c7c9 commit 564b3f8

File tree

3 files changed

+142
-42
lines changed

3 files changed

+142
-42
lines changed

.changeset/polite-teeth-appear.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@labdigital/commercetools-mock": patch
3+
---
4+
5+
fix: allow business unit address actions to work with key as well as ID

src/repositories/business-unit.ts

Lines changed: 119 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import assert from "node:assert";
12
import type {
3+
Address,
24
Associate,
35
BusinessUnit,
46
BusinessUnitAddAddressAction,
@@ -32,8 +34,10 @@ import type {
3234
CompanyDraft,
3335
Division,
3436
DivisionDraft,
37+
InvalidOperationError,
3538
} from "@commercetools/platform-sdk";
3639
import type { Config } from "#src/config.ts";
40+
import { CommercetoolsError } from "#src/exceptions.ts";
3741
import { generateRandomString, getBaseResourceProperties } from "../helpers.ts";
3842
import type { Writable } from "../types.ts";
3943
import type { UpdateHandlerInterface } from "./abstract.ts";
@@ -201,22 +205,23 @@ class BusinessUnitUpdateHandler
201205
changeAddress(
202206
context: RepositoryContext,
203207
resource: Writable<BusinessUnit>,
204-
{ addressId, address }: BusinessUnitChangeAddressAction,
208+
{ addressId, addressKey, address }: BusinessUnitChangeAddressAction,
205209
) {
206-
const existingAddressIndex = resource.addresses.findIndex(
207-
(addr) => addr.id === addressId,
210+
const current = this._findAddress(resource, addressId, addressKey, true);
211+
assert(current?.id); // always true since we set required to true
212+
213+
const oldAddressIndex = resource.addresses.findIndex(
214+
(a) => a.id === current.id,
208215
);
209-
if (existingAddressIndex === -1) {
210-
throw new Error(`Address with id ${addressId} not found`);
211-
}
212216

213217
const newAddress = createAddress(
214-
{ ...address, id: addressId },
218+
{ ...address, id: current.id },
215219
context.projectKey,
216220
this._storage,
217221
);
222+
218223
if (newAddress) {
219-
resource.addresses[existingAddressIndex] = newAddress;
224+
resource.addresses[oldAddressIndex] = newAddress;
220225
}
221226
}
222227

@@ -344,73 +349,100 @@ class BusinessUnitUpdateHandler
344349
setDefaultShippingAddress(
345350
context: RepositoryContext,
346351
resource: Writable<BusinessUnit>,
347-
{ addressId }: BusinessUnitSetDefaultShippingAddressAction,
352+
{ addressId, addressKey }: BusinessUnitSetDefaultShippingAddressAction,
348353
) {
349-
resource.defaultShippingAddressId = addressId;
354+
const address = this._findAddress(resource, addressId, addressKey, true);
355+
assert(address?.id); // always true since we set required to true
356+
357+
resource.defaultShippingAddressId = address.id;
358+
if (resource.shippingAddressIds === undefined) {
359+
resource.shippingAddressIds = [];
360+
}
361+
if (!resource.shippingAddressIds.includes(address.id)) {
362+
resource.shippingAddressIds.push(address.id);
363+
}
350364
}
351365

352366
addShippingAddressId(
353367
context: RepositoryContext,
354368
resource: Writable<BusinessUnit>,
355-
{ addressId }: BusinessUnitAddShippingAddressIdAction,
369+
{ addressId, addressKey }: BusinessUnitAddShippingAddressIdAction,
356370
) {
357-
if (!resource.shippingAddressIds) {
371+
const address = this._findAddress(resource, addressId, addressKey, true);
372+
assert(address?.id); // always true since we set required to true
373+
374+
if (resource.shippingAddressIds === undefined) {
358375
resource.shippingAddressIds = [];
359376
}
360-
if (addressId) {
361-
resource.shippingAddressIds.push(addressId);
377+
378+
if (!resource.shippingAddressIds.includes(address.id)) {
379+
resource.shippingAddressIds.push(address.id);
362380
}
381+
return resource;
363382
}
364383

365384
removeShippingAddressId(
366385
context: RepositoryContext,
367386
resource: Writable<BusinessUnit>,
368-
{ addressId }: BusinessUnitRemoveShippingAddressIdAction,
387+
{ addressId, addressKey }: BusinessUnitRemoveShippingAddressIdAction,
369388
) {
370-
if (resource.shippingAddressIds) {
371-
resource.shippingAddressIds = resource.shippingAddressIds.filter(
372-
(id) => id !== addressId,
373-
);
374-
}
375-
if (resource.defaultShippingAddressId === addressId) {
389+
const address = this._findAddress(resource, addressId, addressKey, true);
390+
assert(address?.id); // always true since we set required to true
391+
resource.shippingAddressIds = resource.shippingAddressIds?.filter(
392+
(id) => id !== address.id,
393+
);
394+
if (resource.defaultShippingAddressId === address.id) {
376395
resource.defaultShippingAddressId = undefined;
377396
}
378397
}
379398

380399
addBillingAddressId(
381400
context: RepositoryContext,
382401
resource: Writable<BusinessUnit>,
383-
{ addressId }: BusinessUnitAddBillingAddressIdAction,
402+
{ addressId, addressKey }: BusinessUnitAddBillingAddressIdAction,
384403
) {
385-
if (!resource.billingAddressIds) {
404+
const address = this._findAddress(resource, addressId, addressKey, true);
405+
assert(address?.id); // always true since we set required to true
406+
407+
if (resource.billingAddressIds === undefined) {
386408
resource.billingAddressIds = [];
387409
}
388-
if (addressId) {
389-
resource.billingAddressIds.push(addressId);
410+
411+
if (!resource.billingAddressIds.includes(address.id)) {
412+
resource.billingAddressIds.push(address.id);
390413
}
391414
}
392415

393416
removeBillingAddressId(
394417
context: RepositoryContext,
395418
resource: Writable<BusinessUnit>,
396-
{ addressId }: BusinessUnitRemoveBillingAddressIdAction,
419+
{ addressId, addressKey }: BusinessUnitRemoveBillingAddressIdAction,
397420
) {
398-
if (resource.billingAddressIds) {
399-
resource.billingAddressIds = resource.billingAddressIds.filter(
400-
(id) => id !== addressId,
401-
);
402-
}
403-
if (resource.defaultBillingAddressId === addressId) {
421+
const address = this._findAddress(resource, addressId, addressKey, true);
422+
assert(address?.id); // always true since we set required to true
423+
resource.billingAddressIds = resource.billingAddressIds?.filter(
424+
(id) => id !== address.id,
425+
);
426+
if (resource.defaultBillingAddressId === address.id) {
404427
resource.defaultBillingAddressId = undefined;
405428
}
406429
}
407430

408431
setDefaultBillingAddress(
409432
context: RepositoryContext,
410433
resource: Writable<BusinessUnit>,
411-
{ addressId }: BusinessUnitSetDefaultBillingAddressAction,
434+
{ addressId, addressKey }: BusinessUnitSetDefaultBillingAddressAction,
412435
) {
413-
resource.defaultBillingAddressId = addressId;
436+
const address = this._findAddress(resource, addressId, addressKey, true);
437+
assert(address?.id); // always true since we set required to true
438+
439+
resource.defaultBillingAddressId = address.id;
440+
if (resource.billingAddressIds === undefined) {
441+
resource.billingAddressIds = [];
442+
}
443+
if (!resource.billingAddressIds.includes(address.id)) {
444+
resource.billingAddressIds.push(address.id);
445+
}
414446
}
415447

416448
setCustomField(
@@ -467,28 +499,73 @@ class BusinessUnitUpdateHandler
467499
removeAddress(
468500
context: RepositoryContext,
469501
resource: Writable<BusinessUnit>,
470-
{ addressId }: BusinessUnitRemoveAddressAction,
502+
{ addressId, addressKey }: BusinessUnitRemoveAddressAction,
471503
) {
472-
resource.addresses = resource.addresses.filter(
473-
(addr) => addr.id !== addressId,
474-
);
504+
const address = this._findAddress(resource, addressId, addressKey, true);
505+
assert(address?.id); // always true since we set required to true
506+
resource.addresses = resource.addresses.filter((a) => a.id !== address.id);
475507

476508
if (resource.shippingAddressIds) {
477509
resource.shippingAddressIds = resource.shippingAddressIds.filter(
478-
(id) => id !== addressId,
510+
(id) => id !== address.id,
479511
);
480512
}
481513
if (resource.billingAddressIds) {
482514
resource.billingAddressIds = resource.billingAddressIds.filter(
483-
(id) => id !== addressId,
515+
(id) => id !== address.id,
484516
);
485517
}
486518

487-
if (resource.defaultShippingAddressId === addressId) {
519+
if (resource.defaultShippingAddressId === address.id) {
488520
resource.defaultShippingAddressId = undefined;
489521
}
490-
if (resource.defaultBillingAddressId === addressId) {
522+
if (resource.defaultBillingAddressId === address.id) {
491523
resource.defaultBillingAddressId = undefined;
492524
}
493525
}
526+
527+
private _findAddress(
528+
resource: Writable<BusinessUnit>,
529+
addressId: string | undefined,
530+
addressKey: string | undefined,
531+
required = false,
532+
): Address | undefined {
533+
if (addressKey) {
534+
const address = resource.addresses.find((a) => a.key === addressKey);
535+
if (!address) {
536+
throw new CommercetoolsError<InvalidOperationError>(
537+
{
538+
code: "InvalidOperation",
539+
message: `Business Unit does not contain an address with the key ${addressKey}.`,
540+
},
541+
400,
542+
);
543+
}
544+
return address;
545+
}
546+
547+
if (addressId) {
548+
const address = resource.addresses.find((a) => a.id === addressId);
549+
if (!address) {
550+
throw new CommercetoolsError<InvalidOperationError>(
551+
{
552+
code: "InvalidOperation",
553+
message: `Business Unit does not contain an address with the id ${addressId}.`,
554+
},
555+
400,
556+
);
557+
}
558+
return address;
559+
}
560+
561+
if (required) {
562+
throw new CommercetoolsError<InvalidOperationError>(
563+
{
564+
code: "InvalidOperation",
565+
message: "One of address 'addressId' or 'addressKey' is required.",
566+
},
567+
400,
568+
);
569+
}
570+
}
494571
}

src/repositories/customer/actions.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,24 @@ export class CustomerUpdateHandler
149149
);
150150
assert(address?.id); // always true since we set required to true
151151
resource.addresses = resource.addresses.filter((a) => a.id !== address.id);
152+
153+
if (resource.shippingAddressIds) {
154+
resource.shippingAddressIds = resource.shippingAddressIds.filter(
155+
(id) => id !== address.id,
156+
);
157+
}
158+
if (resource.billingAddressIds) {
159+
resource.billingAddressIds = resource.billingAddressIds.filter(
160+
(id) => id !== address.id,
161+
);
162+
}
163+
164+
if (resource.defaultShippingAddressId === address.id) {
165+
resource.defaultShippingAddressId = undefined;
166+
}
167+
if (resource.defaultBillingAddressId === address.id) {
168+
resource.defaultBillingAddressId = undefined;
169+
}
152170
}
153171

154172
removeBillingAddressId(

0 commit comments

Comments
 (0)