@labdigital/commercetools-mock 1.7.0 → 1.8.1

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@labdigital/commercetools-mock",
3
3
  "author": "Michael van Tellingen",
4
- "version": "1.7.0",
4
+ "version": "1.8.1",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.cjs",
7
7
  "module": "dist/index.js",
package/src/helpers.ts CHANGED
@@ -32,17 +32,30 @@ export const nestedLookup = (obj: any, path: string): any => {
32
32
  return val
33
33
  }
34
34
 
35
- export const QueryParamsAsArray = (
35
+ export const queryParamsArray = (
36
36
  input: string | ParsedQs | string[] | ParsedQs[] | undefined
37
- ): string[] => {
37
+ ): string[] | undefined => {
38
38
  if (input == undefined) {
39
- return []
39
+ return undefined
40
40
  }
41
41
 
42
- if (Array.isArray(input)) {
43
- return input as string[]
42
+ const values: string[] = Array.isArray(input)
43
+ ? (input as string[])
44
+ : ([input] as string[])
45
+ if (values.length < 1) {
46
+ return undefined
44
47
  }
45
- return [input] as string[]
48
+ return values
49
+ }
50
+
51
+ export const queryParamsValue = (
52
+ value: string | ParsedQs | string[] | ParsedQs[] | undefined
53
+ ): string | undefined => {
54
+ const values = queryParamsArray(value)
55
+ if (values && values.length > 0) {
56
+ return values[0]
57
+ }
58
+ return undefined
46
59
  }
47
60
 
48
61
  export const cloneObject = <T>(o: T): T => JSON.parse(JSON.stringify(o))
@@ -171,15 +171,25 @@ export class ProductProjectionSearch {
171
171
  products: ProductProjection[]
172
172
  ): FacetResults {
173
173
  if (!params.facet) return {}
174
- const staged = false
175
174
  const result: FacetResults = {}
176
175
 
177
- for (const facet of params.facet) {
176
+ const regexp = new RegExp(/ counting products$/)
177
+ for (let facet of params.facet) {
178
+ let countProducts = false
179
+ if (facet.endsWith(' counting products')) {
180
+ facet = facet.replace(regexp, '')
181
+ countProducts = true
182
+ }
183
+
178
184
  const expression = generateFacetFunc(facet)
179
185
 
180
186
  // Term Facet
181
187
  if (expression.type === 'TermExpression') {
182
- result[facet] = this.termFacet(expression.source, products)
188
+ result[facet] = this.termFacet(
189
+ expression.source,
190
+ products,
191
+ countProducts
192
+ )
183
193
  }
184
194
 
185
195
  // Range Facet
@@ -187,7 +197,8 @@ export class ProductProjectionSearch {
187
197
  result[expression.source] = this.rangeFacet(
188
198
  expression.source,
189
199
  expression.children,
190
- products
200
+ products,
201
+ countProducts
191
202
  )
192
203
  }
193
204
 
@@ -196,7 +207,8 @@ export class ProductProjectionSearch {
196
207
  result[expression.source] = this.filterFacet(
197
208
  expression.source,
198
209
  expression.children,
199
- products
210
+ products,
211
+ countProducts
200
212
  )
201
213
  }
202
214
  }
@@ -209,7 +221,11 @@ export class ProductProjectionSearch {
209
221
  * - counting products
210
222
  * - correct dataType
211
223
  */
212
- termFacet(facet: string, products: ProductProjection[]): TermFacetResult {
224
+ termFacet(
225
+ facet: string,
226
+ products: ProductProjection[],
227
+ countProducts: boolean
228
+ ): TermFacetResult {
213
229
  const result: Writable<TermFacetResult> = {
214
230
  type: 'terms',
215
231
  dataType: 'text',
@@ -254,13 +270,15 @@ export class ProductProjectionSearch {
254
270
  count: terms[term],
255
271
  })
256
272
  }
273
+
257
274
  return result
258
275
  }
259
276
 
260
277
  filterFacet(
261
278
  source: string,
262
279
  filters: FilterExpression[] | undefined,
263
- products: ProductProjection[]
280
+ products: ProductProjection[],
281
+ countProducts: boolean
264
282
  ): FilteredFacetResult {
265
283
  let count = 0
266
284
  if (source.startsWith('variants.')) {
@@ -285,7 +303,8 @@ export class ProductProjectionSearch {
285
303
  rangeFacet(
286
304
  source: string,
287
305
  ranges: RangeExpression[] | undefined,
288
- products: ProductProjection[]
306
+ products: ProductProjection[],
307
+ countProducts: boolean
289
308
  ): RangeFacetResult {
290
309
  const counts =
291
310
  ranges?.map((range) => {
@@ -62,6 +62,13 @@ export class AssociateRoleRepository extends AbstractResourceRepository<'associa
62
62
  ) => {
63
63
  resource.buyerAssignable = buyerAssignable
64
64
  },
65
+ changeBuyerAssignable: (
66
+ context: RepositoryContext,
67
+ resource: Writable<AssociateRole>,
68
+ { buyerAssignable }: AssociateRoleChangeBuyerAssignableAction
69
+ ) => {
70
+ resource.buyerAssignable = buyerAssignable
71
+ },
65
72
  setCustomFields: (
66
73
  context: RepositoryContext,
67
74
  resource: Writable<AssociateRole>,
@@ -4,9 +4,7 @@ import type {
4
4
  ProductProjection,
5
5
  QueryParam,
6
6
  } from '@commercetools/platform-sdk'
7
- import { ParsedQs } from 'qs'
8
7
  import { CommercetoolsError } from '../exceptions.js'
9
- import { QueryParamsAsArray } from '../helpers.js'
10
8
  import { parseQueryExpression } from '../lib/predicateParser.js'
11
9
  import { ProductProjectionSearch } from '../product-projection-search.js'
12
10
  import { type AbstractStorage } from '../storage/index.js'
@@ -16,13 +14,13 @@ import {
16
14
  RepositoryContext,
17
15
  } from './abstract.js'
18
16
 
19
- type ProductProjectionQueryParams = {
17
+ export type ProductProjectionQueryParams = {
20
18
  staged?: boolean
21
19
  priceCurrency?: string
22
20
  priceCountry?: string
23
21
  priceCustomerGroup?: string
24
22
  priceChannel?: string
25
- localeProjection?: string | string[]
23
+ localeProjection?: string
26
24
  storeProjection?: string
27
25
  expand?: string | string[]
28
26
  sort?: string | string[]
@@ -126,18 +124,8 @@ export class ProductProjectionRepository extends AbstractResourceRepository<'pro
126
124
  }
127
125
  }
128
126
 
129
- search(context: RepositoryContext, query: ParsedQs) {
130
- const results = this._searchService.search(context.projectKey, {
131
- filter: QueryParamsAsArray(query.filter),
132
- 'filter.query': QueryParamsAsArray(query['filter.query']),
133
- facet: QueryParamsAsArray(query.facet),
134
- offset: query.offset ? Number(query.offset) : undefined,
135
- limit: query.limit ? Number(query.limit) : undefined,
136
- expand: QueryParamsAsArray(query.expand),
137
- staged: query.staged === 'true',
138
- })
139
-
140
- return results
127
+ search(context: RepositoryContext, query: ProductProjectionQueryParams) {
128
+ return this._searchService.search(context.projectKey, query)
141
129
  }
142
130
 
143
131
  actions = {}
@@ -3,6 +3,7 @@ import { type Request, type Response, Router } from 'express'
3
3
  import { ParsedQs } from 'qs'
4
4
  import { AbstractResourceRepository } from '../repositories/abstract.js'
5
5
  import { getRepositoryContext } from '../repositories/helpers.js'
6
+ import { queryParamsArray } from '../helpers.js'
6
7
 
7
8
  export default abstract class AbstractService {
8
9
  protected abstract getBasePath(): string
@@ -170,12 +171,6 @@ export default abstract class AbstractService {
170
171
  protected _parseParam(
171
172
  value: string | ParsedQs | string[] | ParsedQs[] | undefined
172
173
  ): string[] | undefined {
173
- if (Array.isArray(value)) {
174
- // @ts-ignore
175
- return value
176
- } else if (value !== undefined) {
177
- return [`${value}`]
178
- }
179
- return undefined
174
+ return queryParamsArray(value)
180
175
  }
181
176
  }
@@ -1,7 +1,11 @@
1
1
  import { Request, Response, Router } from 'express'
2
2
  import { getRepositoryContext } from '../repositories/helpers.js'
3
- import { ProductProjectionRepository } from './../repositories/product-projection.js'
3
+ import {
4
+ ProductProjectionQueryParams,
5
+ ProductProjectionRepository,
6
+ } from './../repositories/product-projection.js'
4
7
  import AbstractService from './abstract.js'
8
+ import { queryParamsArray, queryParamsValue } from '../helpers.js'
5
9
 
6
10
  export class ProductProjectionService extends AbstractService {
7
11
  public repository: ProductProjectionRepository
@@ -34,9 +38,25 @@ export class ProductProjectionService extends AbstractService {
34
38
  }
35
39
 
36
40
  search(request: Request, response: Response) {
41
+ const query = request.query
42
+ const searchParams: ProductProjectionQueryParams = {
43
+ filter: queryParamsArray(query.filter),
44
+ 'filter.query': queryParamsArray(query['filter.query']),
45
+ facet: queryParamsArray(query.facet),
46
+ expand: queryParamsArray(query.expand),
47
+ staged: queryParamsValue(query.staged) === 'true',
48
+ localeProjection: queryParamsValue(query.localeProjection),
49
+ storeProjection: queryParamsValue(query.storeProjection),
50
+ priceChannel: queryParamsValue(query.priceChannel),
51
+ priceCountry: queryParamsValue(query.priceCountry),
52
+ priceCurrency: queryParamsValue(query.priceCurrency),
53
+ priceCustomerGroup: queryParamsValue(query.priceCustomerGroup),
54
+ offset: query.offset ? Number(queryParamsValue(query.offset)) : undefined,
55
+ limit: query.limit ? Number(queryParamsValue(query.limit)) : undefined,
56
+ }
37
57
  const resource = this.repository.search(
38
58
  getRepositoryContext(request),
39
- request.query
59
+ searchParams
40
60
  )
41
61
  return response.status(200).send(resource)
42
62
  }