|
| 1 | +import assert from "node:assert"; |
1 | 2 | import type { |
| 3 | + Address, |
2 | 4 | Associate, |
3 | 5 | BusinessUnit, |
4 | 6 | BusinessUnitAddAddressAction, |
@@ -32,8 +34,10 @@ import type { |
32 | 34 | CompanyDraft, |
33 | 35 | Division, |
34 | 36 | DivisionDraft, |
| 37 | + InvalidOperationError, |
35 | 38 | } from "@commercetools/platform-sdk"; |
36 | 39 | import type { Config } from "#src/config.ts"; |
| 40 | +import { CommercetoolsError } from "#src/exceptions.ts"; |
37 | 41 | import { generateRandomString, getBaseResourceProperties } from "../helpers.ts"; |
38 | 42 | import type { Writable } from "../types.ts"; |
39 | 43 | import type { UpdateHandlerInterface } from "./abstract.ts"; |
@@ -201,22 +205,23 @@ class BusinessUnitUpdateHandler |
201 | 205 | changeAddress( |
202 | 206 | context: RepositoryContext, |
203 | 207 | resource: Writable<BusinessUnit>, |
204 | | - { addressId, address }: BusinessUnitChangeAddressAction, |
| 208 | + { addressId, addressKey, address }: BusinessUnitChangeAddressAction, |
205 | 209 | ) { |
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, |
208 | 215 | ); |
209 | | - if (existingAddressIndex === -1) { |
210 | | - throw new Error(`Address with id ${addressId} not found`); |
211 | | - } |
212 | 216 |
|
213 | 217 | const newAddress = createAddress( |
214 | | - { ...address, id: addressId }, |
| 218 | + { ...address, id: current.id }, |
215 | 219 | context.projectKey, |
216 | 220 | this._storage, |
217 | 221 | ); |
| 222 | + |
218 | 223 | if (newAddress) { |
219 | | - resource.addresses[existingAddressIndex] = newAddress; |
| 224 | + resource.addresses[oldAddressIndex] = newAddress; |
220 | 225 | } |
221 | 226 | } |
222 | 227 |
|
@@ -344,73 +349,100 @@ class BusinessUnitUpdateHandler |
344 | 349 | setDefaultShippingAddress( |
345 | 350 | context: RepositoryContext, |
346 | 351 | resource: Writable<BusinessUnit>, |
347 | | - { addressId }: BusinessUnitSetDefaultShippingAddressAction, |
| 352 | + { addressId, addressKey }: BusinessUnitSetDefaultShippingAddressAction, |
348 | 353 | ) { |
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 | + } |
350 | 364 | } |
351 | 365 |
|
352 | 366 | addShippingAddressId( |
353 | 367 | context: RepositoryContext, |
354 | 368 | resource: Writable<BusinessUnit>, |
355 | | - { addressId }: BusinessUnitAddShippingAddressIdAction, |
| 369 | + { addressId, addressKey }: BusinessUnitAddShippingAddressIdAction, |
356 | 370 | ) { |
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) { |
358 | 375 | resource.shippingAddressIds = []; |
359 | 376 | } |
360 | | - if (addressId) { |
361 | | - resource.shippingAddressIds.push(addressId); |
| 377 | + |
| 378 | + if (!resource.shippingAddressIds.includes(address.id)) { |
| 379 | + resource.shippingAddressIds.push(address.id); |
362 | 380 | } |
| 381 | + return resource; |
363 | 382 | } |
364 | 383 |
|
365 | 384 | removeShippingAddressId( |
366 | 385 | context: RepositoryContext, |
367 | 386 | resource: Writable<BusinessUnit>, |
368 | | - { addressId }: BusinessUnitRemoveShippingAddressIdAction, |
| 387 | + { addressId, addressKey }: BusinessUnitRemoveShippingAddressIdAction, |
369 | 388 | ) { |
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) { |
376 | 395 | resource.defaultShippingAddressId = undefined; |
377 | 396 | } |
378 | 397 | } |
379 | 398 |
|
380 | 399 | addBillingAddressId( |
381 | 400 | context: RepositoryContext, |
382 | 401 | resource: Writable<BusinessUnit>, |
383 | | - { addressId }: BusinessUnitAddBillingAddressIdAction, |
| 402 | + { addressId, addressKey }: BusinessUnitAddBillingAddressIdAction, |
384 | 403 | ) { |
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) { |
386 | 408 | resource.billingAddressIds = []; |
387 | 409 | } |
388 | | - if (addressId) { |
389 | | - resource.billingAddressIds.push(addressId); |
| 410 | + |
| 411 | + if (!resource.billingAddressIds.includes(address.id)) { |
| 412 | + resource.billingAddressIds.push(address.id); |
390 | 413 | } |
391 | 414 | } |
392 | 415 |
|
393 | 416 | removeBillingAddressId( |
394 | 417 | context: RepositoryContext, |
395 | 418 | resource: Writable<BusinessUnit>, |
396 | | - { addressId }: BusinessUnitRemoveBillingAddressIdAction, |
| 419 | + { addressId, addressKey }: BusinessUnitRemoveBillingAddressIdAction, |
397 | 420 | ) { |
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) { |
404 | 427 | resource.defaultBillingAddressId = undefined; |
405 | 428 | } |
406 | 429 | } |
407 | 430 |
|
408 | 431 | setDefaultBillingAddress( |
409 | 432 | context: RepositoryContext, |
410 | 433 | resource: Writable<BusinessUnit>, |
411 | | - { addressId }: BusinessUnitSetDefaultBillingAddressAction, |
| 434 | + { addressId, addressKey }: BusinessUnitSetDefaultBillingAddressAction, |
412 | 435 | ) { |
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 | + } |
414 | 446 | } |
415 | 447 |
|
416 | 448 | setCustomField( |
@@ -467,28 +499,73 @@ class BusinessUnitUpdateHandler |
467 | 499 | removeAddress( |
468 | 500 | context: RepositoryContext, |
469 | 501 | resource: Writable<BusinessUnit>, |
470 | | - { addressId }: BusinessUnitRemoveAddressAction, |
| 502 | + { addressId, addressKey }: BusinessUnitRemoveAddressAction, |
471 | 503 | ) { |
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); |
475 | 507 |
|
476 | 508 | if (resource.shippingAddressIds) { |
477 | 509 | resource.shippingAddressIds = resource.shippingAddressIds.filter( |
478 | | - (id) => id !== addressId, |
| 510 | + (id) => id !== address.id, |
479 | 511 | ); |
480 | 512 | } |
481 | 513 | if (resource.billingAddressIds) { |
482 | 514 | resource.billingAddressIds = resource.billingAddressIds.filter( |
483 | | - (id) => id !== addressId, |
| 515 | + (id) => id !== address.id, |
484 | 516 | ); |
485 | 517 | } |
486 | 518 |
|
487 | | - if (resource.defaultShippingAddressId === addressId) { |
| 519 | + if (resource.defaultShippingAddressId === address.id) { |
488 | 520 | resource.defaultShippingAddressId = undefined; |
489 | 521 | } |
490 | | - if (resource.defaultBillingAddressId === addressId) { |
| 522 | + if (resource.defaultBillingAddressId === address.id) { |
491 | 523 | resource.defaultBillingAddressId = undefined; |
492 | 524 | } |
493 | 525 | } |
| 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 | + } |
494 | 571 | } |
0 commit comments