Skip to content

Commit 43a4299

Browse files
authored
feat: add missing BusinessUnit update actions and fix changeAddress a… (#321)
…ction
1 parent c56b3e2 commit 43a4299

File tree

5 files changed

+827
-17
lines changed

5 files changed

+827
-17
lines changed

.changeset/tasty-cats-sneeze.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
"@labdigital/commercetools-mock": minor
3+
---
4+
5+
Implements the following previously missing BusinessUnit update action handlers:
6+
- `removeShippingAddressId` - Remove an address ID from shipping addresses
7+
- `addBillingAddressId` - Add an address ID to billing addresses
8+
- `removeBillingAddressId` - Remove an address ID from billing addresses
9+
- `setDefaultBillingAddress` - Set the default billing address
10+
- `setCustomField` - Set a custom field value on the business unit
11+
- `setAddressCustomField` - Set a custom field value on a specific address
12+
- `setAddressCustomType` - Set the custom type for a specific address
13+
- `removeAssociate` - Remove an associate from the business unit
14+
- `changeAssociate` - Change an existing associate's role assignments
15+
16+
Fixes the `changeAddress` action to properly replace an existing address instead of adding a new one.

src/repositories/business-unit.ts

Lines changed: 157 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
import type {
2+
BusinessUnitAddBillingAddressIdAction,
23
BusinessUnitAddShippingAddressIdAction,
34
BusinessUnitChangeApprovalRuleModeAction,
5+
BusinessUnitChangeAssociateAction,
46
BusinessUnitChangeAssociateModeAction,
57
BusinessUnitChangeStatusAction,
8+
BusinessUnitRemoveAssociateAction,
9+
BusinessUnitRemoveBillingAddressIdAction,
10+
BusinessUnitRemoveShippingAddressIdAction,
11+
BusinessUnitSetAddressCustomFieldAction,
12+
BusinessUnitSetAddressCustomTypeAction,
13+
BusinessUnitSetCustomFieldAction,
614
BusinessUnitSetCustomTypeAction,
15+
BusinessUnitSetDefaultBillingAddressAction,
716
BusinessUnitSetDefaultShippingAddressAction,
817
BusinessUnitUpdateAction,
918
CompanyDraft,
@@ -194,15 +203,25 @@ class BusinessUnitUpdateHandler
194203
changeAddress(
195204
context: RepositoryContext,
196205
resource: Writable<BusinessUnit>,
197-
{ address }: BusinessUnitChangeAddressAction,
206+
{ addressId, address }: BusinessUnitChangeAddressAction,
198207
) {
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+
199215
const newAddress = createAddress(
200216
address,
201217
context.projectKey,
202218
this._storage,
203219
);
204220
if (newAddress) {
205-
resource.addresses.push(newAddress);
221+
resource.addresses[existingAddressIndex] = {
222+
...newAddress,
223+
id: addressId,
224+
};
206225
}
207226
}
208227

@@ -261,6 +280,40 @@ class BusinessUnitUpdateHandler
261280
resource.associates = newAssociates || undefined;
262281
}
263282

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+
264317
setContactEmail(
265318
context: RepositoryContext,
266319
resource: Writable<BusinessUnit>,
@@ -314,6 +367,108 @@ class BusinessUnitUpdateHandler
314367
}
315368
}
316369

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+
317472
removeAddress(
318473
context: RepositoryContext,
319474
resource: Writable<BusinessUnit>,

0 commit comments

Comments
 (0)