@labdigital/commercetools-mock 2.17.1 → 2.18.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.
Files changed (178) hide show
  1. package/dist/index.cjs +4186 -3974
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.cts +266 -413
  4. package/dist/index.d.ts +266 -413
  5. package/dist/index.js +4186 -3974
  6. package/dist/index.js.map +1 -1
  7. package/package.json +44 -46
  8. package/src/constants.ts +2 -2
  9. package/src/ctMock.test.ts +11 -11
  10. package/src/ctMock.ts +141 -127
  11. package/src/deprecation.ts +8 -0
  12. package/src/exceptions.ts +17 -15
  13. package/src/helpers.ts +32 -32
  14. package/src/index.test.ts +128 -128
  15. package/src/index.ts +3 -3
  16. package/src/lib/expandParser.ts +13 -13
  17. package/src/lib/haversine.test.ts +9 -9
  18. package/src/lib/haversine.ts +11 -11
  19. package/src/lib/masking.ts +11 -11
  20. package/src/lib/parser.ts +2 -2
  21. package/src/lib/password.ts +23 -3
  22. package/src/lib/predicateParser.test.ts +185 -183
  23. package/src/lib/predicateParser.ts +234 -234
  24. package/src/lib/projectionSearchFilter.test.ts +103 -101
  25. package/src/lib/projectionSearchFilter.ts +152 -150
  26. package/src/lib/proxy.ts +5 -5
  27. package/src/oauth/errors.ts +4 -4
  28. package/src/oauth/helpers.ts +6 -6
  29. package/src/oauth/server.test.ts +86 -86
  30. package/src/oauth/server.ts +158 -144
  31. package/src/oauth/store.ts +44 -43
  32. package/src/priceSelector.test.ts +35 -35
  33. package/src/priceSelector.ts +30 -30
  34. package/src/product-projection-search.ts +136 -134
  35. package/src/projectAPI.test.ts +7 -7
  36. package/src/projectAPI.ts +24 -22
  37. package/src/repositories/abstract.ts +168 -116
  38. package/src/repositories/associate-role.ts +90 -77
  39. package/src/repositories/attribute-group.ts +51 -40
  40. package/src/repositories/business-unit.ts +168 -148
  41. package/src/repositories/cart/actions.ts +489 -0
  42. package/src/repositories/cart/helpers.ts +30 -0
  43. package/src/repositories/cart/index.ts +180 -0
  44. package/src/repositories/cart-discount/actions.ts +148 -0
  45. package/src/repositories/cart-discount/index.ts +86 -0
  46. package/src/repositories/category/actions.ts +231 -0
  47. package/src/repositories/category/index.ts +52 -0
  48. package/src/repositories/channel.ts +88 -90
  49. package/src/repositories/custom-object.ts +46 -45
  50. package/src/repositories/customer/actions.ts +165 -0
  51. package/src/repositories/customer/index.ts +79 -0
  52. package/src/repositories/customer-group.ts +66 -55
  53. package/src/repositories/discount-code/actions.ts +149 -0
  54. package/src/repositories/discount-code/index.ts +50 -0
  55. package/src/repositories/errors.ts +10 -10
  56. package/src/repositories/extension.ts +64 -62
  57. package/src/repositories/helpers.ts +117 -118
  58. package/src/repositories/index.ts +80 -79
  59. package/src/repositories/inventory-entry/actions.ts +84 -0
  60. package/src/repositories/inventory-entry/index.ts +44 -0
  61. package/src/repositories/my-customer.ts +114 -0
  62. package/src/repositories/my-order.ts +8 -8
  63. package/src/repositories/order/actions.ts +281 -0
  64. package/src/repositories/{order.test.ts → order/index.test.ts} +77 -77
  65. package/src/repositories/order/index.ts +260 -0
  66. package/src/repositories/order-edit.ts +10 -23
  67. package/src/repositories/payment/actions.ts +305 -0
  68. package/src/repositories/payment/helpers.ts +17 -0
  69. package/src/repositories/payment/index.ts +56 -0
  70. package/src/repositories/product/actions.ts +943 -0
  71. package/src/repositories/product/helpers.ts +98 -0
  72. package/src/repositories/product/index.ts +130 -0
  73. package/src/repositories/product-discount.ts +127 -117
  74. package/src/repositories/product-projection.ts +56 -62
  75. package/src/repositories/product-selection.ts +31 -28
  76. package/src/repositories/product-type.ts +136 -134
  77. package/src/repositories/project.ts +133 -118
  78. package/src/repositories/quote-request.ts +7 -19
  79. package/src/repositories/quote.ts +7 -22
  80. package/src/repositories/review.ts +13 -26
  81. package/src/repositories/shipping-method/actions.ts +198 -0
  82. package/src/repositories/shipping-method/helpers.ts +10 -0
  83. package/src/repositories/shipping-method/index.ts +138 -0
  84. package/src/repositories/shopping-list/actions.ts +295 -0
  85. package/src/repositories/shopping-list/index.ts +122 -0
  86. package/src/repositories/staged-quote.ts +7 -20
  87. package/src/repositories/standalone-price.ts +57 -44
  88. package/src/repositories/state.ts +113 -68
  89. package/src/repositories/store.ts +106 -94
  90. package/src/repositories/subscription.ts +46 -22
  91. package/src/repositories/tax-category/actions.ts +94 -0
  92. package/src/repositories/tax-category/helpers.ts +8 -0
  93. package/src/repositories/tax-category/index.ts +25 -0
  94. package/src/repositories/type/actions.ts +162 -0
  95. package/src/repositories/type/index.ts +24 -0
  96. package/src/repositories/zone.ts +62 -58
  97. package/src/server.ts +9 -9
  98. package/src/services/abstract.ts +75 -72
  99. package/src/services/associate-roles.test.ts +27 -27
  100. package/src/services/associate-roles.ts +7 -7
  101. package/src/services/attribute-group.ts +7 -7
  102. package/src/services/business-units.test.ts +28 -28
  103. package/src/services/business-units.ts +7 -7
  104. package/src/services/cart-discount.test.ts +199 -199
  105. package/src/services/cart-discount.ts +7 -7
  106. package/src/services/cart.test.ts +261 -261
  107. package/src/services/cart.ts +22 -21
  108. package/src/services/category.test.ts +121 -121
  109. package/src/services/category.ts +7 -7
  110. package/src/services/channel.ts +7 -7
  111. package/src/services/custom-object.test.ts +130 -130
  112. package/src/services/custom-object.ts +34 -31
  113. package/src/services/customer-group.ts +7 -7
  114. package/src/services/customer.test.ts +205 -205
  115. package/src/services/customer.ts +23 -36
  116. package/src/services/discount-code.ts +7 -7
  117. package/src/services/extension.ts +7 -7
  118. package/src/services/index.ts +85 -81
  119. package/src/services/inventory-entry.test.ts +106 -106
  120. package/src/services/inventory-entry.ts +7 -7
  121. package/src/services/my-cart.test.ts +56 -56
  122. package/src/services/my-cart.ts +20 -20
  123. package/src/services/my-customer.test.ts +155 -104
  124. package/src/services/my-customer.ts +61 -75
  125. package/src/services/my-order.ts +16 -16
  126. package/src/services/my-payment.test.ts +40 -40
  127. package/src/services/my-payment.ts +7 -7
  128. package/src/services/my-shopping-list.ts +7 -7
  129. package/src/services/order.test.ts +243 -243
  130. package/src/services/order.ts +23 -18
  131. package/src/services/payment.test.ts +40 -40
  132. package/src/services/payment.ts +7 -7
  133. package/src/services/product-discount.ts +7 -7
  134. package/src/services/product-projection.test.ts +190 -190
  135. package/src/services/product-projection.ts +34 -32
  136. package/src/services/product-selection.test.ts +19 -19
  137. package/src/services/product-selection.ts +7 -7
  138. package/src/services/product-type.test.ts +38 -38
  139. package/src/services/product-type.ts +7 -7
  140. package/src/services/product.test.ts +658 -656
  141. package/src/services/product.ts +7 -7
  142. package/src/services/project.test.ts +24 -24
  143. package/src/services/project.ts +17 -17
  144. package/src/services/reviews.ts +7 -7
  145. package/src/services/shipping-method.test.ts +78 -78
  146. package/src/services/shipping-method.ts +16 -16
  147. package/src/services/shopping-list.test.ts +170 -170
  148. package/src/services/shopping-list.ts +7 -7
  149. package/src/services/standalone-price.test.ts +112 -112
  150. package/src/services/standalone-price.ts +7 -7
  151. package/src/services/state.test.ts +30 -30
  152. package/src/services/state.ts +7 -7
  153. package/src/services/store.test.ts +40 -40
  154. package/src/services/store.ts +7 -7
  155. package/src/services/subscription.ts +7 -7
  156. package/src/services/tax-category.test.ts +43 -43
  157. package/src/services/tax-category.ts +7 -7
  158. package/src/services/type.ts +7 -7
  159. package/src/services/zone.ts +7 -7
  160. package/src/shippingCalculator.test.ts +43 -43
  161. package/src/shippingCalculator.ts +23 -23
  162. package/src/storage/abstract.ts +36 -34
  163. package/src/storage/in-memory.ts +237 -233
  164. package/src/storage/index.ts +2 -2
  165. package/src/types.ts +91 -91
  166. package/src/repositories/cart-discount.ts +0 -219
  167. package/src/repositories/cart.ts +0 -659
  168. package/src/repositories/category.ts +0 -256
  169. package/src/repositories/customer.ts +0 -228
  170. package/src/repositories/discount-code.ts +0 -181
  171. package/src/repositories/inventory-entry.ts +0 -109
  172. package/src/repositories/order.ts +0 -514
  173. package/src/repositories/payment.ts +0 -342
  174. package/src/repositories/product.ts +0 -1106
  175. package/src/repositories/shipping-method.ts +0 -312
  176. package/src/repositories/shopping-list.ts +0 -392
  177. package/src/repositories/tax-category.ts +0 -111
  178. package/src/repositories/type.ts +0 -172
@@ -0,0 +1,98 @@
1
+ import {
2
+ ChannelReference,
3
+ Price,
4
+ PriceDraft,
5
+ Product,
6
+ ProductData,
7
+ ProductVariant,
8
+ ProductVariantDraft,
9
+ } from "@commercetools/platform-sdk";
10
+ import deepEqual from "deep-equal";
11
+ import { v4 as uuidv4 } from "uuid";
12
+ import { AbstractStorage } from "~src/storage";
13
+ import { Writable } from "~src/types";
14
+ import { RepositoryContext } from "../abstract";
15
+ import {
16
+ createTypedMoney,
17
+ getReferenceFromResourceIdentifier,
18
+ } from "../helpers";
19
+
20
+ interface VariantResult {
21
+ variant: Writable<ProductVariant> | undefined;
22
+ isMasterVariant: boolean;
23
+ variantIndex: number;
24
+ }
25
+
26
+ export const getVariant = (
27
+ productData: ProductData,
28
+ variantId?: number,
29
+ sku?: string,
30
+ ): VariantResult => {
31
+ const variants = [productData.masterVariant, ...productData.variants];
32
+ const foundVariant = variants.find((variant: ProductVariant) => {
33
+ if (variantId) {
34
+ return variant.id === variantId;
35
+ }
36
+ if (sku) {
37
+ return variant.sku === sku;
38
+ }
39
+ return false;
40
+ });
41
+
42
+ const isMasterVariant = foundVariant === productData.masterVariant;
43
+ return {
44
+ variant: foundVariant,
45
+ isMasterVariant,
46
+ variantIndex:
47
+ !isMasterVariant && foundVariant
48
+ ? productData.variants.indexOf(foundVariant)
49
+ : -1,
50
+ };
51
+ };
52
+
53
+ // Check if the product still has staged data that is different from the
54
+ // current data.
55
+ export const checkForStagedChanges = (product: Writable<Product>) => {
56
+ if (!product.masterData.staged) {
57
+ product.masterData.staged = product.masterData.current;
58
+ }
59
+
60
+ if (deepEqual(product.masterData.current, product.masterData.staged)) {
61
+ product.masterData.hasStagedChanges = false;
62
+ } else {
63
+ product.masterData.hasStagedChanges = true;
64
+ }
65
+ };
66
+
67
+ export const variantFromDraft = (
68
+ context: RepositoryContext,
69
+ storage: AbstractStorage,
70
+ variantId: number,
71
+ variant: ProductVariantDraft,
72
+ ): ProductVariant => ({
73
+ id: variantId,
74
+ sku: variant?.sku,
75
+ key: variant?.key,
76
+ attributes: variant?.attributes ?? [],
77
+ prices: variant?.prices?.map((p) => priceFromDraft(context, storage, p)),
78
+ assets: [],
79
+ images: [],
80
+ });
81
+
82
+ export const priceFromDraft = (
83
+ context: RepositoryContext,
84
+ storage: AbstractStorage,
85
+ draft: PriceDraft,
86
+ ): Price => ({
87
+ id: uuidv4(),
88
+ key: draft.key,
89
+ country: draft.country,
90
+ value: createTypedMoney(draft.value),
91
+ channel: draft.channel
92
+ ? getReferenceFromResourceIdentifier<ChannelReference>(
93
+ draft.channel,
94
+ context.projectKey,
95
+ storage,
96
+ )
97
+ : undefined,
98
+ });
@@ -0,0 +1,130 @@
1
+ import type {
2
+ CategoryReference,
3
+ InvalidJsonInputError,
4
+ Product,
5
+ ProductData,
6
+ ProductDraft,
7
+ ProductTypeReference,
8
+ StateReference,
9
+ TaxCategoryReference,
10
+ } from "@commercetools/platform-sdk";
11
+ import { CommercetoolsError } from "~src/exceptions";
12
+ import { getBaseResourceProperties } from "~src/helpers";
13
+ import { AbstractStorage } from "~src/storage/abstract";
14
+ import { AbstractResourceRepository, RepositoryContext } from "../abstract";
15
+ import { getReferenceFromResourceIdentifier } from "../helpers";
16
+ import { ProductUpdateHandler } from "./actions";
17
+ import { variantFromDraft } from "./helpers";
18
+
19
+ export class ProductRepository extends AbstractResourceRepository<"product"> {
20
+ constructor(storage: AbstractStorage) {
21
+ super("product", storage);
22
+ this.actions = new ProductUpdateHandler(storage);
23
+ }
24
+
25
+ create(context: RepositoryContext, draft: ProductDraft): Product {
26
+ if (!draft.masterVariant) {
27
+ throw new Error("Missing master variant");
28
+ }
29
+
30
+ let productType: ProductTypeReference | undefined = undefined;
31
+ try {
32
+ productType = getReferenceFromResourceIdentifier<ProductTypeReference>(
33
+ draft.productType,
34
+ context.projectKey,
35
+ this._storage,
36
+ );
37
+ } catch (err) {
38
+ // For now accept missing product types (but warn)
39
+ console.warn(
40
+ `Error resolving product-type '${draft.productType.id}'. This will be throw an error in later releases.`,
41
+ );
42
+ productType = {
43
+ typeId: "product-type",
44
+ id: draft.productType.id || "",
45
+ };
46
+ }
47
+
48
+ // Resolve Product categories
49
+ const categoryReferences: CategoryReference[] = [];
50
+ draft.categories?.forEach((category) => {
51
+ if (category) {
52
+ categoryReferences.push(
53
+ getReferenceFromResourceIdentifier<CategoryReference>(
54
+ category,
55
+ context.projectKey,
56
+ this._storage,
57
+ ),
58
+ );
59
+ } else {
60
+ throw new CommercetoolsError<InvalidJsonInputError>(
61
+ {
62
+ code: "InvalidJsonInput",
63
+ message: "Request body does not contain valid JSON.",
64
+ detailedErrorMessage: "categories: JSON object expected.",
65
+ },
66
+ 400,
67
+ );
68
+ }
69
+ });
70
+
71
+ // Resolve Tax category
72
+ let taxCategoryReference: TaxCategoryReference | undefined = undefined;
73
+ if (draft.taxCategory) {
74
+ taxCategoryReference =
75
+ getReferenceFromResourceIdentifier<TaxCategoryReference>(
76
+ draft.taxCategory,
77
+ context.projectKey,
78
+ this._storage,
79
+ );
80
+ }
81
+
82
+ // Resolve Product State
83
+ let productStateReference: StateReference | undefined = undefined;
84
+ if (draft.state) {
85
+ productStateReference =
86
+ getReferenceFromResourceIdentifier<StateReference>(
87
+ draft.state,
88
+ context.projectKey,
89
+ this._storage,
90
+ );
91
+ }
92
+
93
+ const productData: ProductData = {
94
+ name: draft.name,
95
+ slug: draft.slug,
96
+ description: draft.description,
97
+ categories: categoryReferences,
98
+ masterVariant: variantFromDraft(
99
+ context,
100
+ this._storage,
101
+ 1,
102
+ draft.masterVariant,
103
+ ),
104
+ variants:
105
+ draft.variants?.map((variant, index) =>
106
+ variantFromDraft(context, this._storage, index + 2, variant),
107
+ ) ?? [],
108
+ metaTitle: draft.metaTitle,
109
+ metaDescription: draft.metaDescription,
110
+ metaKeywords: draft.metaKeywords,
111
+ searchKeywords: draft.searchKeywords ?? {},
112
+ };
113
+
114
+ const resource: Product = {
115
+ ...getBaseResourceProperties(),
116
+ key: draft.key,
117
+ productType: productType,
118
+ taxCategory: taxCategoryReference,
119
+ state: productStateReference,
120
+ masterData: {
121
+ current: productData,
122
+ staged: productData,
123
+ hasStagedChanges: false,
124
+ published: draft.publish ?? false,
125
+ },
126
+ };
127
+
128
+ return this.saveNew(context, resource);
129
+ }
130
+ }
@@ -17,145 +17,155 @@ import type {
17
17
  ProductDiscountValueDraft,
18
18
  ProductDiscountValueExternal,
19
19
  ProductDiscountValueRelative,
20
- } from '@commercetools/platform-sdk'
21
- import { getBaseResourceProperties } from '../helpers.js'
22
- import type { Writable } from '../types.js'
23
- import { AbstractResourceRepository, RepositoryContext } from './abstract.js'
24
- import { createTypedMoney } from './helpers.js'
20
+ } from "@commercetools/platform-sdk";
21
+ import { getBaseResourceProperties } from "../helpers";
22
+ import { AbstractStorage } from "../storage/abstract";
23
+ import type { Writable } from "../types";
24
+ import {
25
+ AbstractResourceRepository,
26
+ AbstractUpdateHandler,
27
+ RepositoryContext,
28
+ UpdateHandlerInterface,
29
+ } from "./abstract";
30
+ import { createTypedMoney } from "./helpers";
25
31
 
26
- export class ProductDiscountRepository extends AbstractResourceRepository<'product-discount'> {
27
- getTypeId() {
28
- return 'product-discount' as const
32
+ export class ProductDiscountRepository extends AbstractResourceRepository<"product-discount"> {
33
+ constructor(storage: AbstractStorage) {
34
+ super("product-discount", storage);
35
+ this.actions = new ProductDiscountUpdateHandler(this._storage);
29
36
  }
30
37
 
31
38
  create(
32
39
  context: RepositoryContext,
33
- draft: ProductDiscountDraft
40
+ draft: ProductDiscountDraft,
34
41
  ): ProductDiscount {
35
42
  const resource: ProductDiscount = {
36
43
  ...getBaseResourceProperties(),
37
44
  key: draft.key,
38
45
  name: draft.name,
39
46
  description: draft.description,
40
- value: this.transformValueDraft(draft.value),
47
+ value: transformValueDraft(draft.value),
41
48
  predicate: draft.predicate,
42
49
  sortOrder: draft.sortOrder,
43
50
  isActive: draft.isActive || false,
44
51
  validFrom: draft.validFrom,
45
52
  validUntil: draft.validUntil,
46
53
  references: [],
54
+ };
55
+ return this.saveNew(context, resource);
56
+ }
57
+ }
58
+
59
+ const transformValueDraft = (
60
+ value: ProductDiscountValueDraft,
61
+ ): ProductDiscountValue => {
62
+ switch (value.type) {
63
+ case "absolute": {
64
+ return {
65
+ type: "absolute",
66
+ money: value.money.map(createTypedMoney),
67
+ } as ProductDiscountValueAbsolute;
68
+ }
69
+ case "external": {
70
+ return {
71
+ type: "external",
72
+ } as ProductDiscountValueExternal;
47
73
  }
48
- this.saveNew(context, resource)
49
- return resource
74
+ case "relative": {
75
+ return {
76
+ ...value,
77
+ } as ProductDiscountValueRelative;
78
+ }
79
+ }
80
+ };
81
+
82
+ export class ProductDiscountUpdateHandler
83
+ extends AbstractUpdateHandler
84
+ implements
85
+ UpdateHandlerInterface<ProductDiscount, ProductDiscountUpdateAction>
86
+ {
87
+ changeIsActive(
88
+ context: RepositoryContext,
89
+ resource: Writable<ProductDiscount>,
90
+ { isActive }: ProductDiscountChangeIsActiveAction,
91
+ ) {
92
+ resource.isActive = isActive;
50
93
  }
51
94
 
52
- private transformValueDraft(
53
- value: ProductDiscountValueDraft
54
- ): ProductDiscountValue {
55
- switch (value.type) {
56
- case 'absolute': {
57
- return {
58
- type: 'absolute',
59
- money: value.money.map(createTypedMoney),
60
- } as ProductDiscountValueAbsolute
61
- }
62
- case 'external': {
63
- return {
64
- type: 'external',
65
- } as ProductDiscountValueExternal
66
- }
67
- case 'relative': {
68
- return {
69
- ...value,
70
- } as ProductDiscountValueRelative
71
- }
95
+ changeName(
96
+ context: RepositoryContext,
97
+ resource: Writable<ProductDiscount>,
98
+ { name }: ProductDiscountChangeNameAction,
99
+ ) {
100
+ resource.name = name;
101
+ }
102
+
103
+ changePredicate(
104
+ context: RepositoryContext,
105
+ resource: Writable<ProductDiscount>,
106
+ { predicate }: ProductDiscountChangePredicateAction,
107
+ ) {
108
+ resource.predicate = predicate;
109
+ }
110
+
111
+ changeSortOrder(
112
+ context: RepositoryContext,
113
+ resource: Writable<ProductDiscount>,
114
+ { sortOrder }: ProductDiscountChangeSortOrderAction,
115
+ ) {
116
+ resource.sortOrder = sortOrder;
117
+ }
118
+
119
+ changeValue(
120
+ context: RepositoryContext,
121
+ resource: Writable<ProductDiscount>,
122
+ { value }: ProductDiscountChangeValueAction,
123
+ ) {
124
+ resource.value = transformValueDraft(value);
125
+ }
126
+
127
+ setDescription(
128
+ context: RepositoryContext,
129
+ resource: Writable<ProductDiscount>,
130
+ { description }: ProductDiscountSetDescriptionAction,
131
+ ) {
132
+ if (description && Object.keys(description).length > 0) {
133
+ resource.description = description;
134
+ } else {
135
+ resource.description = undefined;
72
136
  }
73
137
  }
74
138
 
75
- actions: Partial<
76
- Record<
77
- ProductDiscountUpdateAction['action'],
78
- (
79
- context: RepositoryContext,
80
- resource: Writable<ProductDiscount>,
81
- action: any
82
- ) => void
83
- >
84
- > = {
85
- setKey: (
86
- context: RepositoryContext,
87
- resource: Writable<ProductDiscount>,
88
- { key }: ProductDiscountSetKeyAction
89
- ) => {
90
- resource.key = key
91
- },
92
- setDescription: (
93
- context: RepositoryContext,
94
- resource: Writable<ProductDiscount>,
95
- { description }: ProductDiscountSetDescriptionAction
96
- ) => {
97
- if (description && Object.keys(description).length > 0) {
98
- resource.description = description
99
- } else {
100
- resource.description = undefined
101
- }
102
- },
103
- changeName: (
104
- context: RepositoryContext,
105
- resource: Writable<ProductDiscount>,
106
- { name }: ProductDiscountChangeNameAction
107
- ) => {
108
- resource.name = name
109
- },
110
- changeValue: (
111
- context: RepositoryContext,
112
- resource: Writable<ProductDiscount>,
113
- { value }: ProductDiscountChangeValueAction
114
- ) => {
115
- resource.value = this.transformValueDraft(value)
116
- },
117
- changePredicate: (
118
- context: RepositoryContext,
119
- resource: Writable<ProductDiscount>,
120
- { predicate }: ProductDiscountChangePredicateAction
121
- ) => {
122
- resource.predicate = predicate
123
- },
124
- changeSortOrder: (
125
- context: RepositoryContext,
126
- resource: Writable<ProductDiscount>,
127
- { sortOrder }: ProductDiscountChangeSortOrderAction
128
- ) => {
129
- resource.sortOrder = sortOrder
130
- },
131
- changeIsActive: (
132
- context: RepositoryContext,
133
- resource: Writable<ProductDiscount>,
134
- { isActive }: ProductDiscountChangeIsActiveAction
135
- ) => {
136
- resource.isActive = isActive
137
- },
138
- setValidFrom: (
139
- context: RepositoryContext,
140
- resource: Writable<ProductDiscount>,
141
- { validFrom }: ProductDiscountSetValidFromAction
142
- ) => {
143
- resource.validFrom = validFrom
144
- },
145
- setValidUntil: (
146
- context: RepositoryContext,
147
- resource: Writable<ProductDiscount>,
148
- { validUntil }: ProductDiscountSetValidUntilAction
149
- ) => {
150
- resource.validUntil = validUntil
151
- },
152
- setValidFromAndUntil: (
153
- context: RepositoryContext,
154
- resource: Writable<ProductDiscount>,
155
- { validFrom, validUntil }: ProductDiscountSetValidFromAndUntilAction
156
- ) => {
157
- resource.validFrom = validFrom
158
- resource.validUntil = validUntil
159
- },
139
+ setKey(
140
+ context: RepositoryContext,
141
+ resource: Writable<ProductDiscount>,
142
+ { key }: ProductDiscountSetKeyAction,
143
+ ) {
144
+ resource.key = key;
145
+ }
146
+
147
+ setValidFrom(
148
+ context: RepositoryContext,
149
+ resource: Writable<ProductDiscount>,
150
+ { validFrom }: ProductDiscountSetValidFromAction,
151
+ ) {
152
+ resource.validFrom = validFrom;
153
+ }
154
+
155
+ setValidFromAndUntil(
156
+ context: RepositoryContext,
157
+ resource: Writable<ProductDiscount>,
158
+ { validFrom, validUntil }: ProductDiscountSetValidFromAndUntilAction,
159
+ ) {
160
+ resource.validFrom = validFrom;
161
+ resource.validUntil = validUntil;
162
+ }
163
+
164
+ setValidUntil(
165
+ context: RepositoryContext,
166
+ resource: Writable<ProductDiscount>,
167
+ { validUntil }: ProductDiscountSetValidUntilAction,
168
+ ) {
169
+ resource.validUntil = validUntil;
160
170
  }
161
171
  }
@@ -3,101 +3,97 @@ import type {
3
3
  ProductDraft,
4
4
  ProductProjection,
5
5
  QueryParam,
6
- } from '@commercetools/platform-sdk'
7
- import { CommercetoolsError } from '../exceptions.js'
8
- import { parseQueryExpression } from '../lib/predicateParser.js'
9
- import { ProductProjectionSearch } from '../product-projection-search.js'
10
- import { type AbstractStorage } from '../storage/index.js'
6
+ } from "@commercetools/platform-sdk";
7
+ import { CommercetoolsError } from "~src/exceptions";
8
+ import { parseQueryExpression } from "../lib/predicateParser";
9
+ import { applyPriceSelector } from "../priceSelector";
10
+ import { ProductProjectionSearch } from "../product-projection-search";
11
+ import { type AbstractStorage } from "../storage";
11
12
  import {
12
13
  AbstractResourceRepository,
13
14
  GetParams,
14
15
  RepositoryContext,
15
- } from './abstract.js'
16
- import { applyPriceSelector } from '../priceSelector.js'
16
+ } from "./abstract";
17
17
 
18
18
  export type ProductProjectionQueryParams = {
19
- staged?: boolean
20
- priceCurrency?: string
21
- priceCountry?: string
22
- priceCustomerGroup?: string
23
- priceChannel?: string
24
- localeProjection?: string
25
- storeProjection?: string
26
- expand?: string | string[]
27
- sort?: string | string[]
28
- limit?: number
29
- offset?: number
30
- withTotal?: boolean
31
- where?: string | string[]
32
- [key: string]: QueryParam
33
- }
19
+ staged?: boolean;
20
+ priceCurrency?: string;
21
+ priceCountry?: string;
22
+ priceCustomerGroup?: string;
23
+ priceChannel?: string;
24
+ localeProjection?: string;
25
+ storeProjection?: string;
26
+ expand?: string | string[];
27
+ sort?: string | string[];
28
+ limit?: number;
29
+ offset?: number;
30
+ withTotal?: boolean;
31
+ where?: string | string[];
32
+ [key: string]: QueryParam;
33
+ };
34
34
 
35
- export class ProductProjectionRepository extends AbstractResourceRepository<'product-projection'> {
36
- protected _searchService: ProductProjectionSearch
35
+ export class ProductProjectionRepository extends AbstractResourceRepository<"product-projection"> {
36
+ protected _searchService: ProductProjectionSearch;
37
37
 
38
38
  constructor(storage: AbstractStorage) {
39
- super(storage)
40
- this._searchService = new ProductProjectionSearch(storage)
41
- }
42
-
43
- getTypeId() {
44
- return 'product-projection' as const
39
+ super("product-projection", storage);
40
+ this._searchService = new ProductProjectionSearch(storage);
45
41
  }
46
42
 
47
43
  create(context: RepositoryContext, draft: ProductDraft): ProductProjection {
48
- throw new Error('No valid action')
44
+ throw new Error("No valid action");
49
45
  }
50
46
 
51
47
  get(
52
48
  context: RepositoryContext,
53
49
  id: string,
54
- params: GetParams = {}
50
+ params: GetParams = {},
55
51
  ): ProductProjection | null {
56
52
  const resource = this._storage.get(
57
53
  context.projectKey,
58
- 'product',
54
+ "product",
59
55
  id,
60
- params
61
- )
56
+ params,
57
+ );
62
58
  if (resource) {
63
- return this._searchService.transform(resource, false)
59
+ return this._searchService.transform(resource, false);
64
60
  }
65
- return null
61
+ return null;
66
62
  }
67
63
 
68
64
  query(context: RepositoryContext, params: ProductProjectionQueryParams = {}) {
69
65
  let resources = this._storage
70
- .all(context.projectKey, 'product')
66
+ .all(context.projectKey, "product")
71
67
  .map((r) => this._searchService.transform(r, params.staged ?? false))
72
68
  .filter((p) => {
73
69
  if (!params.staged ?? false) {
74
- return p.published
70
+ return p.published;
75
71
  }
76
- return true
77
- })
72
+ return true;
73
+ });
78
74
 
79
75
  // Apply predicates
80
76
  if (params.where) {
81
- const variableMap: Record<string, QueryParam> = {}
77
+ const variableMap: Record<string, QueryParam> = {};
82
78
  for (const [k, v] of Object.entries(params)) {
83
- if (k.startsWith('var.')) {
84
- variableMap[k.substring(4)] = v
79
+ if (k.startsWith("var.")) {
80
+ variableMap[k.substring(4)] = v;
85
81
  }
86
82
  }
87
83
 
88
84
  try {
89
- const filterFunc = parseQueryExpression(params.where)
85
+ const filterFunc = parseQueryExpression(params.where);
90
86
  resources = resources.filter((resource) =>
91
- filterFunc(resource, variableMap)
92
- )
87
+ filterFunc(resource, variableMap),
88
+ );
93
89
  } catch (err) {
94
90
  throw new CommercetoolsError<InvalidInputError>(
95
91
  {
96
- code: 'InvalidInput',
92
+ code: "InvalidInput",
97
93
  message: (err as any).message,
98
94
  },
99
- 400
100
- )
95
+ 400,
96
+ );
101
97
  }
102
98
  }
103
99
 
@@ -112,23 +108,23 @@ export class ProductProjectionRepository extends AbstractResourceRepository<'pro
112
108
  customerGroup: params.priceCustomerGroup,
113
109
  currency: params.priceCurrency,
114
110
  },
115
- true
116
- )
111
+ true,
112
+ );
117
113
 
118
114
  // Expand the resources
119
115
  if (params.expand !== undefined) {
120
116
  resources = resources.map((resource) =>
121
- this._storage.expand(context.projectKey, resource, params.expand)
122
- )
117
+ this._storage.expand(context.projectKey, resource, params.expand),
118
+ );
123
119
  }
124
120
 
125
121
  // Create a slice for the pagination. If we were working with large datasets
126
122
  // then we should have done this before transforming. But that isn't the
127
123
  // goal of this library. So lets keep it simple.
128
- const totalResults = resources.length
129
- const offset = params.offset || 0
130
- const limit = params.limit || 20
131
- const results = resources.slice(offset, offset + limit)
124
+ const totalResults = resources.length;
125
+ const offset = params.offset || 0;
126
+ const limit = params.limit || 20;
127
+ const results = resources.slice(offset, offset + limit);
132
128
 
133
129
  return {
134
130
  count: totalResults,
@@ -136,12 +132,10 @@ export class ProductProjectionRepository extends AbstractResourceRepository<'pro
136
132
  offset: offset,
137
133
  limit: limit,
138
134
  results: results,
139
- }
135
+ };
140
136
  }
141
137
 
142
138
  search(context: RepositoryContext, query: ProductProjectionQueryParams) {
143
- return this._searchService.search(context.projectKey, query)
139
+ return this._searchService.search(context.projectKey, query);
144
140
  }
145
-
146
- actions = {}
147
141
  }