@labdigital/commercetools-mock 0.10.1 → 0.11.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@labdigital/commercetools-mock",
3
3
  "author": "Michael van Tellingen",
4
- "version": "0.10.1",
4
+ "version": "0.11.0",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/commercetools--mock.esm.js",
@@ -7,6 +7,8 @@ import {
7
7
  ProductPublishAction,
8
8
  ProductSetAttributeAction,
9
9
  ProductSetDescriptionAction,
10
+ ProductAddExternalImageAction,
11
+ ProductRemoveImageAction,
10
12
  ProductSetKeyAction,
11
13
  ProductTypeReference,
12
14
  ProductUpdateAction,
@@ -183,6 +185,114 @@ export class ProductRepository extends AbstractResourceRepository<'product'> {
183
185
  resource.key = key
184
186
  return resource
185
187
  },
188
+ addExternalImage: (
189
+ context: RepositoryContext,
190
+ resource: Writable<Product>,
191
+ { variantId, sku, image, staged }: ProductAddExternalImageAction
192
+ ) => {
193
+ const addImg = (data: Writable<ProductData>) => {
194
+ const { variant, isMasterVariant, variantIndex } = getVariant(
195
+ data,
196
+ variantId,
197
+ sku
198
+ )
199
+ if (!variant) {
200
+ throw new Error(
201
+ `Variant with id ${variantId} or sku ${sku} not found on product ${resource.id}`
202
+ )
203
+ }
204
+
205
+ if (!variant.images) {
206
+ variant.images = []
207
+ } else {
208
+ const existingImage = variant.images.find((x) => x.url === image.url)
209
+ if (existingImage) {
210
+ throw new Error(
211
+ `Cannot add image '${image.url}' because product '${resource.id}' already has that image.`
212
+ )
213
+ }
214
+ }
215
+
216
+ // Add image
217
+ variant.images.push(image)
218
+
219
+ if (isMasterVariant) {
220
+ data.masterVariant = variant
221
+ } else {
222
+ data.variants[variantIndex] = variant
223
+ }
224
+ }
225
+
226
+ // If true, only the staged Attribute is set. If false, both current and
227
+ // staged Attribute is set. Default is true
228
+ const onlyStaged = staged !== undefined ? staged : true
229
+
230
+ // Write the attribute to the staged data
231
+ addImg(resource.masterData.staged)
232
+
233
+ // Also write to published data is isStaged = false
234
+ // if isStaged is false we set the attribute on both the staged and
235
+ // published data.
236
+ if (!onlyStaged) {
237
+ addImg(resource.masterData.current)
238
+ }
239
+ checkForStagedChanges(resource)
240
+
241
+ return resource
242
+ },
243
+ removeImage: (
244
+ context: RepositoryContext,
245
+ resource: Writable<Product>,
246
+ { variantId, sku, imageUrl, staged }: ProductRemoveImageAction
247
+ ) => {
248
+ const removeImg = (data: Writable<ProductData>) => {
249
+ const { variant, isMasterVariant, variantIndex } = getVariant(
250
+ data,
251
+ variantId,
252
+ sku
253
+ )
254
+ if (!variant) {
255
+ throw new Error(
256
+ `Variant with id ${variantId} or sku ${sku} not found on product ${resource.id}`
257
+ )
258
+ }
259
+
260
+ const variantImages = variant.images ?? []
261
+ const existingImage = variantImages.find((x) => x.url === imageUrl)
262
+ if (!existingImage) {
263
+ throw new Error(
264
+ `Cannot remove image '${imageUrl}' because product '${resource.id}' does not have that image.`
265
+ )
266
+ }
267
+
268
+ // Remove image
269
+ variant.images = variantImages.filter((image) => image.url !== imageUrl)
270
+
271
+ if (isMasterVariant) {
272
+ data.masterVariant = variant
273
+ } else {
274
+ data.variants[variantIndex] = variant
275
+ }
276
+ }
277
+
278
+ // If true, only the staged Attribute is set. If false, both current and
279
+ // staged Attribute is set. Default is true
280
+ const onlyStaged = staged !== undefined ? staged : true
281
+
282
+ // Write the attribute to the staged data
283
+ removeImg(resource.masterData.staged)
284
+
285
+ // Also write to published data is isStaged = false
286
+ // if isStaged is false we set the attribute on both the staged and
287
+ // published data.
288
+ if (!onlyStaged) {
289
+ removeImg(resource.masterData.current)
290
+ }
291
+ checkForStagedChanges(resource)
292
+
293
+ return resource
294
+ },
295
+
186
296
  // 'changeName': () => {},
187
297
  // 'changeSlug': () => {},
188
298
  // 'addVariant': () => {},
@@ -202,9 +312,7 @@ export class ProductRepository extends AbstractResourceRepository<'product'> {
202
312
  // 'setTaxCategory': () => {},
203
313
  // 'setSku': () => {},
204
314
  // 'setProductVariantKey': () => {},
205
- // 'addExternalImage': () => {},
206
315
  // 'moveImageToPosition': () => {},
207
- // 'removeImage': () => {},
208
316
  // 'setImageLabel': () => {},
209
317
  // 'addAsset': () => {},
210
318
  // 'removeAsset': () => {},
@@ -83,6 +83,7 @@ export class ProjectRepository extends AbstractRepository<Project> {
83
83
  { messagesConfiguration }: ProjectChangeMessagesConfigurationAction
84
84
  ) => {
85
85
  resource.messages.enabled = messagesConfiguration.enabled
86
+ resource.messages.deleteDaysAfterCreation = messagesConfiguration.deleteDaysAfterCreation
86
87
  },
87
88
  changeProductSearchIndexingEnabled: (
88
89
  context: RepositoryContext,
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  State,
3
+ StateChangeInitialAction,
3
4
  StateChangeKeyAction,
4
5
  StateDraft,
5
6
  StateReference,
@@ -51,6 +52,13 @@ export class StateRepository extends AbstractResourceRepository<'state'> {
51
52
  ) => {
52
53
  resource.key = key
53
54
  },
55
+ changeInitial: (
56
+ context: RepositoryContext,
57
+ resource: Writable<State>,
58
+ {initial }: StateChangeInitialAction
59
+ ) => {
60
+ resource.initial = initial
61
+ },
54
62
  setDescription: (
55
63
  context: RepositoryContext,
56
64
  resource: Writable<State>,
@@ -1,4 +1,9 @@
1
- import { Product, ProductData, ProductDraft } from '@commercetools/platform-sdk'
1
+ import {
2
+ Image,
3
+ Product,
4
+ ProductData,
5
+ ProductDraft,
6
+ } from '@commercetools/platform-sdk'
2
7
  import assert from 'assert'
3
8
  import supertest from 'supertest'
4
9
  import { CommercetoolsMock } from '../index'
@@ -294,4 +299,60 @@ describe('Product update actions', () => {
294
299
  const attr = response.body.masterData.staged.masterVariant.attributes[0]
295
300
  expect(attr).toEqual({ name: 'test', value: 'foo' })
296
301
  })
302
+
303
+ test('addExternalImage variant', async () => {
304
+ assert(productPublished, 'product not created')
305
+
306
+ const image: Image = {
307
+ url: 'http://example.com/image',
308
+ dimensions: { w: 100, h: 100 },
309
+ }
310
+ const response = await supertest(ctMock.app)
311
+ .post(`/dummy/products/${productPublished.id}`)
312
+ .send({
313
+ version: 1,
314
+ actions: [{ action: 'addExternalImage', sku: '1338', image }],
315
+ })
316
+ expect(response.status).toBe(200)
317
+ expect(response.body.version).toBe(2)
318
+ expect(response.body.masterData.staged.variants[0].images).toHaveLength(1)
319
+ const attr = response.body.masterData.staged.variants[0].images[0]
320
+ expect(attr).toEqual(image)
321
+ })
322
+
323
+ test('removeImage variant', async () => {
324
+ assert(productPublished, 'product not created')
325
+
326
+ const image: Image = {
327
+ url: 'http://example.com/image',
328
+ dimensions: { w: 100, h: 100 },
329
+ }
330
+
331
+ {
332
+ const response = await supertest(ctMock.app)
333
+ .post(`/dummy/products/${productPublished.id}`)
334
+ .send({
335
+ version: 1,
336
+ actions: [{ action: 'addExternalImage', sku: '1338', image }],
337
+ })
338
+ expect(response.status).toBe(200)
339
+ expect(response.body.version).toBe(2)
340
+ }
341
+
342
+ const response = await supertest(ctMock.app)
343
+ .post(`/dummy/products/${productPublished.id}`)
344
+ .send({
345
+ version: 2,
346
+ actions: [
347
+ {
348
+ action: 'removeImage',
349
+ sku: '1338',
350
+ imageUrl: image.url,
351
+ },
352
+ ],
353
+ })
354
+ expect(response.status).toBe(200)
355
+ expect(response.body.version).toBe(3)
356
+ expect(response.body.masterData.staged.variants[0].images).toHaveLength(0)
357
+ })
297
358
  })
@@ -29,13 +29,13 @@ export class ProjectService {
29
29
  return response.status(404).send({})
30
30
  }
31
31
 
32
- this.repository.processUpdateActions(
32
+ const updatedResource = this.repository.processUpdateActions(
33
33
  getRepositoryContext(request),
34
34
  project,
35
35
  updateRequest.version,
36
36
  updateRequest.actions
37
37
  )
38
38
 
39
- return response.status(200).send({})
39
+ return response.status(200).send(updatedResource)
40
40
  }
41
41
  }