@labdigital/commercetools-mock 1.8.1 → 1.10.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
@@ -2145,6 +2329,11 @@ var CartRepository = class extends AbstractResourceRepository {
2145
2329
  ...address,
2146
2330
  custom
2147
2331
  };
2332
+ },
2333
+ removeDiscountCode: (context, resource, { discountCode }) => {
2334
+ resource.discountCodes = resource.discountCodes.filter(
2335
+ (code) => code.discountCode.id !== discountCode.id
2336
+ );
2148
2337
  }
2149
2338
  };
2150
2339
  draftLineItemtoLineItem = (projectKey, draftLineItem, currency, country) => {
@@ -2337,6 +2526,26 @@ var CartDiscountRepository = class extends AbstractResourceRepository {
2337
2526
  } else {
2338
2527
  resource.custom.fields[name] = value;
2339
2528
  }
2529
+ },
2530
+ setCustomType: (context, resource, { type, fields }) => {
2531
+ if (!type) {
2532
+ resource.custom = void 0;
2533
+ } else {
2534
+ const resolvedType = this._storage.getByResourceIdentifier(
2535
+ context.projectKey,
2536
+ type
2537
+ );
2538
+ if (!resolvedType) {
2539
+ throw new Error(`Type ${type} not found`);
2540
+ }
2541
+ resource.custom = {
2542
+ type: {
2543
+ typeId: "type",
2544
+ id: resolvedType.id
2545
+ },
2546
+ fields: fields || {}
2547
+ };
2548
+ }
2340
2549
  }
2341
2550
  };
2342
2551
  };
@@ -5477,6 +5686,18 @@ var AssociateRoleServices = class extends AbstractService {
5477
5686
  }
5478
5687
  };
5479
5688
 
5689
+ // src/services/business-units.ts
5690
+ var BusinessUnitServices = class extends AbstractService {
5691
+ repository;
5692
+ constructor(parent, repository) {
5693
+ super(parent);
5694
+ this.repository = repository;
5695
+ }
5696
+ getBasePath() {
5697
+ return "business-units";
5698
+ }
5699
+ };
5700
+
5480
5701
  // src/services/cart.ts
5481
5702
  var CartService = class extends AbstractService {
5482
5703
  repository;
@@ -6067,6 +6288,7 @@ var AttributeGroupService = class extends AbstractService {
6067
6288
  // src/services/index.ts
6068
6289
  var createServices = (router, repos) => ({
6069
6290
  "associate-role": new AssociateRoleServices(router, repos["associate-role"]),
6291
+ "business-unit": new BusinessUnitServices(router, repos["business-unit"]),
6070
6292
  category: new CategoryServices(router, repos["category"]),
6071
6293
  cart: new CartService(router, repos["cart"], repos["order"]),
6072
6294
  "cart-discount": new CartDiscountService(router, repos["cart-discount"]),