@labdigital/commercetools-mock 1.8.0 → 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
@@ -82,14 +82,22 @@ var nestedLookup = (obj, path) => {
82
82
  }
83
83
  return val;
84
84
  };
85
- var QueryParamsAsArray = (input) => {
85
+ var queryParamsArray = (input) => {
86
86
  if (input == void 0) {
87
- return [];
87
+ return void 0;
88
88
  }
89
- if (Array.isArray(input)) {
90
- return input;
89
+ const values = Array.isArray(input) ? input : [input];
90
+ if (values.length < 1) {
91
+ return void 0;
91
92
  }
92
- return [input];
93
+ return values;
94
+ };
95
+ var queryParamsValue = (value) => {
96
+ const values = queryParamsArray(value);
97
+ if (values && values.length > 0) {
98
+ return values[0];
99
+ }
100
+ return void 0;
93
101
  };
94
102
  var cloneObject = (o) => JSON.parse(JSON.stringify(o));
95
103
 
@@ -1580,6 +1588,71 @@ var getRepositoryContext = (request) => ({
1580
1588
  projectKey: request.params.projectKey,
1581
1589
  storeKey: request.params.storeKey
1582
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
+ };
1583
1656
 
1584
1657
  // src/services/project.ts
1585
1658
  var ProjectService = class {
@@ -1839,9 +1912,128 @@ var BusinessUnitRepository = class extends AbstractResourceRepository {
1839
1912
  getTypeId() {
1840
1913
  return "business-unit";
1841
1914
  }
1915
+ _isCompanyDraft(draft) {
1916
+ return draft.unitType === "Company";
1917
+ }
1918
+ _isDivisionDraft(draft) {
1919
+ return draft.unitType === "Division";
1920
+ }
1842
1921
  create(context, draft) {
1843
- 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");
1844
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
+ };
1845
2037
  };
1846
2038
 
1847
2039
  // src/repositories/cart.ts
@@ -2329,6 +2521,26 @@ var CartDiscountRepository = class extends AbstractResourceRepository {
2329
2521
  } else {
2330
2522
  resource.custom.fields[name] = value;
2331
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
+ }
2332
2544
  }
2333
2545
  };
2334
2546
  };
@@ -4222,25 +4434,36 @@ var ProductProjectionSearch = class {
4222
4434
  getFacets(params, products) {
4223
4435
  if (!params.facet)
4224
4436
  return {};
4225
- const staged = false;
4226
4437
  const result = {};
4227
- for (const facet of params.facet) {
4438
+ const regexp = new RegExp(/ counting products$/);
4439
+ for (let facet of params.facet) {
4440
+ let countProducts = false;
4441
+ if (facet.endsWith(" counting products")) {
4442
+ facet = facet.replace(regexp, "");
4443
+ countProducts = true;
4444
+ }
4228
4445
  const expression = generateFacetFunc(facet);
4229
4446
  if (expression.type === "TermExpression") {
4230
- result[facet] = this.termFacet(expression.source, products);
4447
+ result[facet] = this.termFacet(
4448
+ expression.source,
4449
+ products,
4450
+ countProducts
4451
+ );
4231
4452
  }
4232
4453
  if (expression.type === "RangeExpression") {
4233
4454
  result[expression.source] = this.rangeFacet(
4234
4455
  expression.source,
4235
4456
  expression.children,
4236
- products
4457
+ products,
4458
+ countProducts
4237
4459
  );
4238
4460
  }
4239
4461
  if (expression.type === "FilterExpression") {
4240
4462
  result[expression.source] = this.filterFacet(
4241
4463
  expression.source,
4242
4464
  expression.children,
4243
- products
4465
+ products,
4466
+ countProducts
4244
4467
  );
4245
4468
  }
4246
4469
  }
@@ -4251,7 +4474,7 @@ var ProductProjectionSearch = class {
4251
4474
  * - counting products
4252
4475
  * - correct dataType
4253
4476
  */
4254
- termFacet(facet, products) {
4477
+ termFacet(facet, products, countProducts) {
4255
4478
  const result = {
4256
4479
  type: "terms",
4257
4480
  dataType: "text",
@@ -4296,7 +4519,7 @@ var ProductProjectionSearch = class {
4296
4519
  }
4297
4520
  return result;
4298
4521
  }
4299
- filterFacet(source, filters, products) {
4522
+ filterFacet(source, filters, products, countProducts) {
4300
4523
  let count = 0;
4301
4524
  if (source.startsWith("variants.")) {
4302
4525
  for (const p of products) {
@@ -4315,7 +4538,7 @@ var ProductProjectionSearch = class {
4315
4538
  count
4316
4539
  };
4317
4540
  }
4318
- rangeFacet(source, ranges, products) {
4541
+ rangeFacet(source, ranges, products, countProducts) {
4319
4542
  const counts = ranges?.map((range) => {
4320
4543
  if (source.startsWith("variants.")) {
4321
4544
  const values = [];
@@ -4437,16 +4660,7 @@ var ProductProjectionRepository = class extends AbstractResourceRepository {
4437
4660
  };
4438
4661
  }
4439
4662
  search(context, query) {
4440
- const results = this._searchService.search(context.projectKey, {
4441
- filter: QueryParamsAsArray(query.filter),
4442
- "filter.query": QueryParamsAsArray(query["filter.query"]),
4443
- facet: QueryParamsAsArray(query.facet),
4444
- offset: query.offset ? Number(query.offset) : void 0,
4445
- limit: query.limit ? Number(query.limit) : void 0,
4446
- expand: QueryParamsAsArray(query.expand),
4447
- staged: query.staged === "true"
4448
- });
4449
- return results;
4663
+ return this._searchService.search(context.projectKey, query);
4450
4664
  }
4451
4665
  actions = {};
4452
4666
  };
@@ -5451,12 +5665,7 @@ var AbstractService = class {
5451
5665
  }
5452
5666
  // No idea what i'm doing
5453
5667
  _parseParam(value) {
5454
- if (Array.isArray(value)) {
5455
- return value;
5456
- } else if (value !== void 0) {
5457
- return [`${value}`];
5458
- }
5459
- return void 0;
5668
+ return queryParamsArray(value);
5460
5669
  }
5461
5670
  };
5462
5671
 
@@ -5472,6 +5681,18 @@ var AssociateRoleServices = class extends AbstractService {
5472
5681
  }
5473
5682
  };
5474
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
+
5475
5696
  // src/services/cart.ts
5476
5697
  var CartService = class extends AbstractService {
5477
5698
  repository;
@@ -5899,9 +6120,25 @@ var ProductProjectionService = class extends AbstractService {
5899
6120
  return response.status(200).send(result);
5900
6121
  }
5901
6122
  search(request, response) {
6123
+ const query = request.query;
6124
+ const searchParams = {
6125
+ filter: queryParamsArray(query.filter),
6126
+ "filter.query": queryParamsArray(query["filter.query"]),
6127
+ facet: queryParamsArray(query.facet),
6128
+ expand: queryParamsArray(query.expand),
6129
+ staged: queryParamsValue(query.staged) === "true",
6130
+ localeProjection: queryParamsValue(query.localeProjection),
6131
+ storeProjection: queryParamsValue(query.storeProjection),
6132
+ priceChannel: queryParamsValue(query.priceChannel),
6133
+ priceCountry: queryParamsValue(query.priceCountry),
6134
+ priceCurrency: queryParamsValue(query.priceCurrency),
6135
+ priceCustomerGroup: queryParamsValue(query.priceCustomerGroup),
6136
+ offset: query.offset ? Number(queryParamsValue(query.offset)) : void 0,
6137
+ limit: query.limit ? Number(queryParamsValue(query.limit)) : void 0
6138
+ };
5902
6139
  const resource = this.repository.search(
5903
6140
  getRepositoryContext(request),
5904
- request.query
6141
+ searchParams
5905
6142
  );
5906
6143
  return response.status(200).send(resource);
5907
6144
  }
@@ -6046,6 +6283,7 @@ var AttributeGroupService = class extends AbstractService {
6046
6283
  // src/services/index.ts
6047
6284
  var createServices = (router, repos) => ({
6048
6285
  "associate-role": new AssociateRoleServices(router, repos["associate-role"]),
6286
+ "business-unit": new BusinessUnitServices(router, repos["business-unit"]),
6049
6287
  category: new CategoryServices(router, repos["category"]),
6050
6288
  cart: new CartService(router, repos["cart"], repos["order"]),
6051
6289
  "cart-discount": new CartDiscountService(router, repos["cart-discount"]),