@labdigital/commercetools-mock 1.8.1 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1588,6 +1588,71 @@ var getRepositoryContext = (request) => ({
1588
1588
  projectKey: request.params.projectKey,
1589
1589
  storeKey: request.params.storeKey
1590
1590
  });
1591
+ var createAssociate = (a, projectKey, storage) => {
1592
+ if (!a)
1593
+ return void 0;
1594
+ if (!a.associateRoleAssignments) {
1595
+ throw new Error("AssociateRoleAssignments is required");
1596
+ }
1597
+ return {
1598
+ customer: getReferenceFromResourceIdentifier(
1599
+ a.customer,
1600
+ projectKey,
1601
+ storage
1602
+ ),
1603
+ associateRoleAssignments: a.associateRoleAssignments?.map(
1604
+ (a2) => ({
1605
+ associateRole: getAssociateRoleKeyReference(
1606
+ a2.associateRole,
1607
+ projectKey,
1608
+ storage
1609
+ ),
1610
+ inheritance: a2.inheritance
1611
+ })
1612
+ ),
1613
+ roles: a.roles
1614
+ };
1615
+ };
1616
+ var getAssociateRoleKeyReference = (id, projectKey, storage) => {
1617
+ if (id.key) {
1618
+ return {
1619
+ typeId: "associate-role",
1620
+ key: id.key
1621
+ };
1622
+ }
1623
+ const value = getReferenceFromResourceIdentifier(
1624
+ id,
1625
+ projectKey,
1626
+ storage
1627
+ );
1628
+ if (!value.obj?.key) {
1629
+ throw new Error("No associate-role found for reference");
1630
+ }
1631
+ return {
1632
+ typeId: "associate-role",
1633
+ key: value.obj?.key
1634
+ };
1635
+ };
1636
+ var getBusinessUnitKeyReference = (id, projectKey, storage) => {
1637
+ if (id.key) {
1638
+ return {
1639
+ typeId: "business-unit",
1640
+ key: id.key
1641
+ };
1642
+ }
1643
+ const value = getReferenceFromResourceIdentifier(
1644
+ id,
1645
+ projectKey,
1646
+ storage
1647
+ );
1648
+ if (!value.obj?.key) {
1649
+ throw new Error("No business-unit found for reference");
1650
+ }
1651
+ return {
1652
+ typeId: "business-unit",
1653
+ key: value.obj?.key
1654
+ };
1655
+ };
1591
1656
 
1592
1657
  // src/services/project.ts
1593
1658
  var ProjectService = class {
@@ -1847,9 +1912,128 @@ var BusinessUnitRepository = class extends AbstractResourceRepository {
1847
1912
  getTypeId() {
1848
1913
  return "business-unit";
1849
1914
  }
1915
+ _isCompanyDraft(draft) {
1916
+ return draft.unitType === "Company";
1917
+ }
1918
+ _isDivisionDraft(draft) {
1919
+ return draft.unitType === "Division";
1920
+ }
1850
1921
  create(context, draft) {
1851
- throw new Error("Method not implemented.");
1922
+ const resource = {
1923
+ ...getBaseResourceProperties(),
1924
+ key: draft.key,
1925
+ status: draft.status,
1926
+ stores: draft.stores?.map(
1927
+ (s) => getStoreKeyReference(s, context.projectKey, this._storage)
1928
+ ),
1929
+ storeMode: draft.storeMode,
1930
+ name: draft.name,
1931
+ contactEmail: draft.contactEmail,
1932
+ addresses: draft.addresses?.map(
1933
+ (a) => createAddress(a, context.projectKey, this._storage)
1934
+ ),
1935
+ custom: createCustomFields(
1936
+ draft.custom,
1937
+ context.projectKey,
1938
+ this._storage
1939
+ ),
1940
+ shippingAddressIds: draft.shippingAddresses,
1941
+ defaultShippingAddressId: draft.defaultShippingAddress,
1942
+ billingAddressIds: draft.billingAddresses,
1943
+ associateMode: draft.associateMode,
1944
+ associates: draft.associates?.map(
1945
+ (a) => createAssociate(a, context.projectKey, this._storage)
1946
+ )
1947
+ };
1948
+ if (this._isDivisionDraft(draft)) {
1949
+ const division = {
1950
+ ...resource,
1951
+ parentUnit: getBusinessUnitKeyReference(
1952
+ draft.parentUnit,
1953
+ context.projectKey,
1954
+ this._storage
1955
+ )
1956
+ };
1957
+ this.saveNew(context, division);
1958
+ return division;
1959
+ } else if (this._isCompanyDraft(draft)) {
1960
+ const company = resource;
1961
+ this.saveNew(context, company);
1962
+ return company;
1963
+ }
1964
+ throw new Error("Invalid business unit type");
1852
1965
  }
1966
+ actions = {
1967
+ addAddress: (context, resource, { address }) => {
1968
+ const newAddress = createAddress(
1969
+ address,
1970
+ context.projectKey,
1971
+ this._storage
1972
+ );
1973
+ if (newAddress) {
1974
+ resource.addresses.push(newAddress);
1975
+ }
1976
+ },
1977
+ addAssociate: (context, resource, { associate }) => {
1978
+ const newAssociate = createAssociate(
1979
+ associate,
1980
+ context.projectKey,
1981
+ this._storage
1982
+ );
1983
+ if (newAssociate) {
1984
+ resource.associates.push(newAssociate);
1985
+ }
1986
+ },
1987
+ setAssociates: (context, resource, { associates }) => {
1988
+ const newAssociates = associates.map((a) => createAssociate(a, context.projectKey, this._storage)).filter((a) => a !== void 0);
1989
+ resource.associates = newAssociates || void 0;
1990
+ },
1991
+ setContactEmail: (context, resource, { contactEmail }) => {
1992
+ resource.contactEmail = contactEmail;
1993
+ },
1994
+ setStoreMode: (context, resource, { storeMode }) => {
1995
+ resource.storeMode = storeMode;
1996
+ },
1997
+ changeAssociateMode: (context, resource, { storeMode }) => {
1998
+ resource.associateMode = storeMode;
1999
+ },
2000
+ changeName: (context, resource, { name }) => {
2001
+ resource.name = name;
2002
+ },
2003
+ changeAddress: (context, resource, { address }) => {
2004
+ const newAddress = createAddress(
2005
+ address,
2006
+ context.projectKey,
2007
+ this._storage
2008
+ );
2009
+ if (newAddress) {
2010
+ resource.addresses.push(newAddress);
2011
+ }
2012
+ },
2013
+ addStore: (context, resource, { store }) => {
2014
+ const newStore = getStoreKeyReference(
2015
+ store,
2016
+ context.projectKey,
2017
+ this._storage
2018
+ );
2019
+ if (newStore) {
2020
+ if (!resource.stores) {
2021
+ resource.stores = [];
2022
+ }
2023
+ resource.stores.push(newStore);
2024
+ }
2025
+ },
2026
+ changeParentUnit: (context, resource, { parentUnit }) => {
2027
+ resource.parentUnit = getBusinessUnitKeyReference(
2028
+ parentUnit,
2029
+ context.projectKey,
2030
+ this._storage
2031
+ );
2032
+ },
2033
+ changeStatus: (context, resource, { status }) => {
2034
+ resource.status = status;
2035
+ }
2036
+ };
1853
2037
  };
1854
2038
 
1855
2039
  // src/repositories/cart.ts
@@ -2337,6 +2521,26 @@ var CartDiscountRepository = class extends AbstractResourceRepository {
2337
2521
  } else {
2338
2522
  resource.custom.fields[name] = value;
2339
2523
  }
2524
+ },
2525
+ setCustomType: (context, resource, { type, fields }) => {
2526
+ if (!type) {
2527
+ resource.custom = void 0;
2528
+ } else {
2529
+ const resolvedType = this._storage.getByResourceIdentifier(
2530
+ context.projectKey,
2531
+ type
2532
+ );
2533
+ if (!resolvedType) {
2534
+ throw new Error(`Type ${type} not found`);
2535
+ }
2536
+ resource.custom = {
2537
+ type: {
2538
+ typeId: "type",
2539
+ id: resolvedType.id
2540
+ },
2541
+ fields: fields || {}
2542
+ };
2543
+ }
2340
2544
  }
2341
2545
  };
2342
2546
  };
@@ -5477,6 +5681,18 @@ var AssociateRoleServices = class extends AbstractService {
5477
5681
  }
5478
5682
  };
5479
5683
 
5684
+ // src/services/business-units.ts
5685
+ var BusinessUnitServices = class extends AbstractService {
5686
+ repository;
5687
+ constructor(parent, repository) {
5688
+ super(parent);
5689
+ this.repository = repository;
5690
+ }
5691
+ getBasePath() {
5692
+ return "business-units";
5693
+ }
5694
+ };
5695
+
5480
5696
  // src/services/cart.ts
5481
5697
  var CartService = class extends AbstractService {
5482
5698
  repository;
@@ -6067,6 +6283,7 @@ var AttributeGroupService = class extends AbstractService {
6067
6283
  // src/services/index.ts
6068
6284
  var createServices = (router, repos) => ({
6069
6285
  "associate-role": new AssociateRoleServices(router, repos["associate-role"]),
6286
+ "business-unit": new BusinessUnitServices(router, repos["business-unit"]),
6070
6287
  category: new CategoryServices(router, repos["category"]),
6071
6288
  cart: new CartService(router, repos["cart"], repos["order"]),
6072
6289
  "cart-discount": new CartDiscountService(router, repos["cart-discount"]),