|
1 | 1 | import type { |
| 2 | + BusinessUnitAddBillingAddressIdAction, |
2 | 3 | BusinessUnitAddShippingAddressIdAction, |
3 | 4 | BusinessUnitChangeApprovalRuleModeAction, |
| 5 | + BusinessUnitChangeAssociateAction, |
4 | 6 | BusinessUnitChangeAssociateModeAction, |
5 | 7 | BusinessUnitChangeStatusAction, |
| 8 | + BusinessUnitRemoveAssociateAction, |
| 9 | + BusinessUnitRemoveBillingAddressIdAction, |
| 10 | + BusinessUnitRemoveShippingAddressIdAction, |
| 11 | + BusinessUnitSetAddressCustomFieldAction, |
| 12 | + BusinessUnitSetAddressCustomTypeAction, |
| 13 | + BusinessUnitSetCustomFieldAction, |
6 | 14 | BusinessUnitSetCustomTypeAction, |
| 15 | + BusinessUnitSetDefaultBillingAddressAction, |
7 | 16 | BusinessUnitSetDefaultShippingAddressAction, |
8 | 17 | BusinessUnitUpdateAction, |
9 | 18 | CompanyDraft, |
@@ -194,15 +203,25 @@ class BusinessUnitUpdateHandler |
194 | 203 | changeAddress( |
195 | 204 | context: RepositoryContext, |
196 | 205 | resource: Writable<BusinessUnit>, |
197 | | - { address }: BusinessUnitChangeAddressAction, |
| 206 | + { addressId, address }: BusinessUnitChangeAddressAction, |
198 | 207 | ) { |
| 208 | + const existingAddressIndex = resource.addresses.findIndex( |
| 209 | + (addr) => addr.id === addressId, |
| 210 | + ); |
| 211 | + if (existingAddressIndex === -1) { |
| 212 | + throw new Error(`Address with id ${addressId} not found`); |
| 213 | + } |
| 214 | + |
199 | 215 | const newAddress = createAddress( |
200 | 216 | address, |
201 | 217 | context.projectKey, |
202 | 218 | this._storage, |
203 | 219 | ); |
204 | 220 | if (newAddress) { |
205 | | - resource.addresses.push(newAddress); |
| 221 | + resource.addresses[existingAddressIndex] = { |
| 222 | + ...newAddress, |
| 223 | + id: addressId, |
| 224 | + }; |
206 | 225 | } |
207 | 226 | } |
208 | 227 |
|
@@ -261,6 +280,40 @@ class BusinessUnitUpdateHandler |
261 | 280 | resource.associates = newAssociates || undefined; |
262 | 281 | } |
263 | 282 |
|
| 283 | + removeAssociate( |
| 284 | + context: RepositoryContext, |
| 285 | + resource: Writable<BusinessUnit>, |
| 286 | + { customer }: BusinessUnitRemoveAssociateAction, |
| 287 | + ) { |
| 288 | + resource.associates = resource.associates.filter( |
| 289 | + (associate) => associate.customer.id !== customer.id, |
| 290 | + ); |
| 291 | + } |
| 292 | + |
| 293 | + changeAssociate( |
| 294 | + context: RepositoryContext, |
| 295 | + resource: Writable<BusinessUnit>, |
| 296 | + { associate }: BusinessUnitChangeAssociateAction, |
| 297 | + ) { |
| 298 | + const existingAssociateIndex = resource.associates.findIndex( |
| 299 | + (a) => a.customer.id === associate.customer.id, |
| 300 | + ); |
| 301 | + if (existingAssociateIndex === -1) { |
| 302 | + throw new Error( |
| 303 | + `Associate with customer id ${associate.customer.id} not found`, |
| 304 | + ); |
| 305 | + } |
| 306 | + |
| 307 | + const newAssociate = createAssociate( |
| 308 | + associate, |
| 309 | + context.projectKey, |
| 310 | + this._storage, |
| 311 | + ); |
| 312 | + if (newAssociate) { |
| 313 | + resource.associates[existingAssociateIndex] = newAssociate; |
| 314 | + } |
| 315 | + } |
| 316 | + |
264 | 317 | setContactEmail( |
265 | 318 | context: RepositoryContext, |
266 | 319 | resource: Writable<BusinessUnit>, |
@@ -314,6 +367,108 @@ class BusinessUnitUpdateHandler |
314 | 367 | } |
315 | 368 | } |
316 | 369 |
|
| 370 | + removeShippingAddressId( |
| 371 | + context: RepositoryContext, |
| 372 | + resource: Writable<BusinessUnit>, |
| 373 | + { addressId }: BusinessUnitRemoveShippingAddressIdAction, |
| 374 | + ) { |
| 375 | + if (resource.shippingAddressIds) { |
| 376 | + resource.shippingAddressIds = resource.shippingAddressIds.filter( |
| 377 | + (id) => id !== addressId, |
| 378 | + ); |
| 379 | + } |
| 380 | + if (resource.defaultShippingAddressId === addressId) { |
| 381 | + resource.defaultShippingAddressId = undefined; |
| 382 | + } |
| 383 | + } |
| 384 | + |
| 385 | + addBillingAddressId( |
| 386 | + context: RepositoryContext, |
| 387 | + resource: Writable<BusinessUnit>, |
| 388 | + { addressId }: BusinessUnitAddBillingAddressIdAction, |
| 389 | + ) { |
| 390 | + if (!resource.billingAddressIds) { |
| 391 | + resource.billingAddressIds = []; |
| 392 | + } |
| 393 | + if (addressId) { |
| 394 | + resource.billingAddressIds.push(addressId); |
| 395 | + } |
| 396 | + } |
| 397 | + |
| 398 | + removeBillingAddressId( |
| 399 | + context: RepositoryContext, |
| 400 | + resource: Writable<BusinessUnit>, |
| 401 | + { addressId }: BusinessUnitRemoveBillingAddressIdAction, |
| 402 | + ) { |
| 403 | + if (resource.billingAddressIds) { |
| 404 | + resource.billingAddressIds = resource.billingAddressIds.filter( |
| 405 | + (id) => id !== addressId, |
| 406 | + ); |
| 407 | + } |
| 408 | + if (resource.defaultBillingAddressId === addressId) { |
| 409 | + resource.defaultBillingAddressId = undefined; |
| 410 | + } |
| 411 | + } |
| 412 | + |
| 413 | + setDefaultBillingAddress( |
| 414 | + context: RepositoryContext, |
| 415 | + resource: Writable<BusinessUnit>, |
| 416 | + { addressId }: BusinessUnitSetDefaultBillingAddressAction, |
| 417 | + ) { |
| 418 | + resource.defaultBillingAddressId = addressId; |
| 419 | + } |
| 420 | + |
| 421 | + setCustomField( |
| 422 | + context: RepositoryContext, |
| 423 | + resource: Writable<BusinessUnit>, |
| 424 | + { name, value }: BusinessUnitSetCustomFieldAction, |
| 425 | + ) { |
| 426 | + if (!resource.custom) { |
| 427 | + throw new Error("Resource has no custom type"); |
| 428 | + } |
| 429 | + resource.custom.fields[name] = value; |
| 430 | + } |
| 431 | + |
| 432 | + setAddressCustomField( |
| 433 | + context: RepositoryContext, |
| 434 | + resource: Writable<BusinessUnit>, |
| 435 | + { addressId, name, value }: BusinessUnitSetAddressCustomFieldAction, |
| 436 | + ) { |
| 437 | + const address = resource.addresses.find((addr) => addr.id === addressId); |
| 438 | + if (!address) { |
| 439 | + throw new Error(`Address with id ${addressId} not found`); |
| 440 | + } |
| 441 | + if (!address.custom) { |
| 442 | + // If the address doesn't have custom fields, we need to initialize them |
| 443 | + // This might require a type to be set first, but we'll just create minimal structure |
| 444 | + throw new Error( |
| 445 | + "Address has no custom type set. Use setAddressCustomType first.", |
| 446 | + ); |
| 447 | + } |
| 448 | + address.custom.fields[name] = value; |
| 449 | + } |
| 450 | + |
| 451 | + setAddressCustomType( |
| 452 | + context: RepositoryContext, |
| 453 | + resource: Writable<BusinessUnit>, |
| 454 | + { addressId, type, fields }: BusinessUnitSetAddressCustomTypeAction, |
| 455 | + ) { |
| 456 | + const address = resource.addresses.find((addr) => addr.id === addressId); |
| 457 | + if (!address) { |
| 458 | + throw new Error(`Address with id ${addressId} not found`); |
| 459 | + } |
| 460 | + |
| 461 | + if (!type) { |
| 462 | + address.custom = undefined; |
| 463 | + } else { |
| 464 | + address.custom = createCustomFields( |
| 465 | + { type, fields }, |
| 466 | + context.projectKey, |
| 467 | + this._storage, |
| 468 | + ); |
| 469 | + } |
| 470 | + } |
| 471 | + |
317 | 472 | removeAddress( |
318 | 473 | context: RepositoryContext, |
319 | 474 | resource: Writable<BusinessUnit>, |
|
0 commit comments