@labdigital/commercetools-mock 0.5.12 → 0.5.15

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 (107) hide show
  1. package/README.md +6 -2
  2. package/dist/commercetools-mock.cjs.development.js +3153 -1689
  3. package/dist/commercetools-mock.cjs.development.js.map +1 -1
  4. package/dist/commercetools-mock.cjs.production.min.js +1 -1
  5. package/dist/commercetools-mock.cjs.production.min.js.map +1 -1
  6. package/dist/commercetools-mock.esm.js +3153 -1689
  7. package/dist/commercetools-mock.esm.js.map +1 -1
  8. package/dist/ctMock.d.ts +1 -0
  9. package/dist/lib/masking.d.ts +1 -0
  10. package/dist/repositories/abstract.d.ts +10 -7
  11. package/dist/repositories/cart-discount.d.ts +9 -0
  12. package/dist/repositories/cart.d.ts +16 -3
  13. package/dist/repositories/category.d.ts +18 -0
  14. package/dist/repositories/channel.d.ts +6 -0
  15. package/dist/repositories/custom-object.d.ts +2 -2
  16. package/dist/repositories/customer-group.d.ts +11 -0
  17. package/dist/repositories/customer.d.ts +2 -2
  18. package/dist/repositories/discount-code.d.ts +8 -0
  19. package/dist/repositories/errors.d.ts +2 -2
  20. package/dist/repositories/extension.d.ts +8 -0
  21. package/dist/repositories/inventory-entry.d.ts +2 -2
  22. package/dist/repositories/order.d.ts +2 -2
  23. package/dist/repositories/payment.d.ts +4 -3
  24. package/dist/repositories/product-projection.d.ts +2 -2
  25. package/dist/repositories/product-type.d.ts +5 -4
  26. package/dist/repositories/product.d.ts +2 -2
  27. package/dist/repositories/project.d.ts +8 -0
  28. package/dist/repositories/shipping-method.d.ts +7 -5
  29. package/dist/repositories/shopping-list.d.ts +2 -2
  30. package/dist/repositories/state.d.ts +5 -4
  31. package/dist/repositories/store.d.ts +6 -3
  32. package/dist/repositories/subscription.d.ts +6 -0
  33. package/dist/repositories/tax-category.d.ts +6 -5
  34. package/dist/repositories/type.d.ts +5 -3
  35. package/dist/repositories/zone.d.ts +8 -0
  36. package/dist/services/abstract.d.ts +4 -3
  37. package/dist/services/cart-discount.d.ts +9 -0
  38. package/dist/services/category.d.ts +9 -0
  39. package/dist/services/channel.d.ts +9 -0
  40. package/dist/services/customer-group.d.ts +9 -0
  41. package/dist/services/discount-code.d.ts +9 -0
  42. package/dist/services/extension.d.ts +9 -0
  43. package/dist/services/my-cart.d.ts +11 -0
  44. package/dist/services/project.d.ts +11 -0
  45. package/dist/services/subscription.d.ts +9 -0
  46. package/dist/services/zone.d.ts +9 -0
  47. package/dist/storage.d.ts +12 -1
  48. package/dist/types.d.ts +2 -1
  49. package/package.json +20 -20
  50. package/src/ctMock.ts +44 -17
  51. package/src/lib/masking.ts +22 -0
  52. package/src/oauth/server.ts +3 -1
  53. package/src/repositories/abstract.ts +47 -33
  54. package/src/repositories/cart-discount.ts +140 -0
  55. package/src/repositories/cart.ts +247 -3
  56. package/src/repositories/category.ts +140 -0
  57. package/src/repositories/channel.ts +23 -0
  58. package/src/repositories/custom-object.ts +2 -2
  59. package/src/repositories/customer-group.ts +42 -0
  60. package/src/repositories/customer.ts +2 -2
  61. package/src/repositories/discount-code.ts +143 -0
  62. package/src/repositories/errors.ts +7 -2
  63. package/src/repositories/extension.ts +65 -0
  64. package/src/repositories/inventory-entry.ts +2 -2
  65. package/src/repositories/order.ts +2 -2
  66. package/src/repositories/payment.ts +10 -6
  67. package/src/repositories/product-projection.ts +2 -2
  68. package/src/repositories/product-type.ts +57 -4
  69. package/src/repositories/product.ts +4 -2
  70. package/src/repositories/project.ts +156 -0
  71. package/src/repositories/shipping-method.ts +149 -13
  72. package/src/repositories/shopping-list.ts +2 -2
  73. package/src/repositories/state.ts +48 -4
  74. package/src/repositories/store.ts +69 -4
  75. package/src/repositories/subscription.ts +50 -0
  76. package/src/repositories/tax-category.ts +80 -6
  77. package/src/repositories/type.ts +133 -3
  78. package/src/repositories/zone.ts +77 -0
  79. package/src/server.ts +6 -1
  80. package/src/services/abstract.ts +26 -18
  81. package/src/services/cart-discount.ts +17 -0
  82. package/src/services/cart.test.ts +314 -3
  83. package/src/services/category.test.ts +37 -0
  84. package/src/services/category.ts +17 -0
  85. package/src/services/channel.ts +17 -0
  86. package/src/services/custom-object.test.ts +3 -3
  87. package/src/services/customer-group.ts +17 -0
  88. package/src/services/discount-code.ts +17 -0
  89. package/src/services/extension.ts +17 -0
  90. package/src/services/inventory-entry.test.ts +3 -3
  91. package/src/services/my-cart.test.ts +93 -0
  92. package/src/services/my-cart.ts +44 -0
  93. package/src/services/my-payment.test.ts +2 -2
  94. package/src/services/order.test.ts +4 -4
  95. package/src/services/payment.test.ts +2 -2
  96. package/src/services/product-projection.test.ts +12 -5
  97. package/src/services/product-type.test.ts +2 -2
  98. package/src/services/product.test.ts +6 -2
  99. package/src/services/project.ts +42 -0
  100. package/src/services/shipping-method.test.ts +3 -3
  101. package/src/services/state.test.ts +2 -2
  102. package/src/services/store.test.ts +60 -0
  103. package/src/services/subscription.ts +17 -0
  104. package/src/services/tax-category.test.ts +3 -3
  105. package/src/services/zone.ts +17 -0
  106. package/src/storage.ts +99 -1
  107. package/src/types.ts +2 -1
@@ -1,9 +1,19 @@
1
1
  import { getBaseResourceProperties } from '../helpers'
2
2
  import { getReferenceFromResourceIdentifier } from './helpers'
3
- import { ReferenceTypeId, State, StateDraft } from '@commercetools/platform-sdk'
4
- import AbstractRepository from './abstract'
3
+ import {
4
+ ReferenceTypeId,
5
+ State,
6
+ StateChangeKeyAction,
7
+ StateDraft,
8
+ StateSetDescriptionAction,
9
+ StateSetNameAction,
10
+ StateSetRolesAction,
11
+ StateUpdateAction,
12
+ } from '@commercetools/platform-sdk'
13
+ import { AbstractResourceRepository } from './abstract'
14
+ import { Writable } from 'types'
5
15
 
6
- export class StateRepository extends AbstractRepository {
16
+ export class StateRepository extends AbstractResourceRepository {
7
17
  getTypeId(): ReferenceTypeId {
8
18
  return 'state'
9
19
  }
@@ -23,5 +33,39 @@ export class StateRepository extends AbstractRepository {
23
33
  return resource
24
34
  }
25
35
 
26
- actions = {}
36
+ actions: Partial<
37
+ Record<
38
+ StateUpdateAction['action'],
39
+ (projectKey: string, resource: Writable<State>, action: any) => void
40
+ >
41
+ > = {
42
+ changeKey: (
43
+ projectKey: string,
44
+ resource: Writable<State>,
45
+ { key }: StateChangeKeyAction
46
+ ) => {
47
+ resource.key = key
48
+ },
49
+ setDescription: (
50
+ projectKey: string,
51
+ resource: Writable<State>,
52
+ { description }: StateSetDescriptionAction
53
+ ) => {
54
+ resource.description = description
55
+ },
56
+ setName: (
57
+ projectKey: string,
58
+ resource: Writable<State>,
59
+ { name }: StateSetNameAction
60
+ ) => {
61
+ resource.name = name
62
+ },
63
+ setRoles: (
64
+ projectKey: string,
65
+ resource: Writable<State>,
66
+ { roles }: StateSetRolesAction
67
+ ) => {
68
+ resource.roles = roles
69
+ },
70
+ }
27
71
  }
@@ -1,8 +1,20 @@
1
- import { Store, StoreDraft, ReferenceTypeId } from '@commercetools/platform-sdk'
1
+ import {
2
+ Store,
3
+ StoreDraft,
4
+ ReferenceTypeId,
5
+ StoreUpdateAction,
6
+ StoreSetNameAction,
7
+ ChannelReference,
8
+ StoreSetDistributionChannelsAction,
9
+ ChannelResourceIdentifier,
10
+ StoreSetLanguagesAction,
11
+ } from '@commercetools/platform-sdk'
12
+ import { Writable } from 'types'
2
13
  import { getBaseResourceProperties } from '../helpers'
3
- import AbstractRepository from './abstract'
14
+ import { AbstractResourceRepository } from './abstract'
15
+ import { getReferenceFromResourceIdentifier } from './helpers'
4
16
 
5
- export class StoreRepository extends AbstractRepository {
17
+ export class StoreRepository extends AbstractResourceRepository {
6
18
  getTypeId(): ReferenceTypeId {
7
19
  return 'store'
8
20
  }
@@ -11,9 +23,62 @@ export class StoreRepository extends AbstractRepository {
11
23
  const resource: Store = {
12
24
  ...getBaseResourceProperties(),
13
25
  key: draft.key,
14
- distributionChannels: [],
26
+ name: draft.name,
27
+ languages: draft.languages,
28
+ distributionChannels: this.transformChannels(
29
+ projectKey,
30
+ draft.distributionChannels
31
+ ),
32
+ supplyChannels: this.transformChannels(projectKey, draft.supplyChannels),
15
33
  }
16
34
  this.save(projectKey, resource)
17
35
  return resource
18
36
  }
37
+
38
+ private transformChannels(
39
+ projectKey: string,
40
+ channels?: ChannelResourceIdentifier[]
41
+ ) {
42
+ if (!channels) return []
43
+
44
+ return channels.map(ref =>
45
+ getReferenceFromResourceIdentifier<ChannelReference>(
46
+ ref,
47
+ projectKey,
48
+ this._storage
49
+ )
50
+ )
51
+ }
52
+
53
+ actions: Partial<
54
+ Record<
55
+ StoreUpdateAction['action'],
56
+ (projectKey: string, resource: Writable<Store>, action: any) => void
57
+ >
58
+ > = {
59
+ setName: (
60
+ projectKey: string,
61
+ resource: Writable<Store>,
62
+ { name }: StoreSetNameAction
63
+ ) => {
64
+ resource.name = name
65
+ },
66
+ setDistributionChannels: (
67
+ projectKey: string,
68
+ resource: Writable<Store>,
69
+ { distributionChannels }: StoreSetDistributionChannelsAction
70
+ ) => {
71
+ resource.distributionChannels = this.transformChannels(
72
+ projectKey,
73
+ distributionChannels
74
+ )
75
+ },
76
+ setLanguages: (
77
+ projectKey: string,
78
+ resource: Writable<Store>,
79
+ { languages }: StoreSetLanguagesAction
80
+ ) => {
81
+ resource.languages = languages
82
+ },
83
+ }
19
84
  }
@@ -0,0 +1,50 @@
1
+ import {
2
+ InvalidInputError,
3
+ ReferenceTypeId,
4
+ Subscription,
5
+ SubscriptionDraft,
6
+ } from '@commercetools/platform-sdk'
7
+ import { CommercetoolsError } from '../exceptions'
8
+ import { getBaseResourceProperties } from '../helpers'
9
+ import { AbstractResourceRepository } from './abstract'
10
+
11
+ export class SubscriptionRepository extends AbstractResourceRepository {
12
+ getTypeId(): ReferenceTypeId {
13
+ return 'subscription'
14
+ }
15
+ create(projectKey: string, draft: SubscriptionDraft): Subscription {
16
+ // TODO: We could actually test this here by using the aws sdk. For now
17
+ // hardcode a failed check when account id is 0000000000
18
+ if (draft.destination.type == 'SQS') {
19
+ const queueURL = new URL(draft.destination.queueUrl)
20
+ const accountId = queueURL.pathname.split('/')[1]
21
+ if (accountId == '0000000000') {
22
+ const dest = draft.destination
23
+ throw new CommercetoolsError<InvalidInputError>(
24
+ {
25
+ code: 'InvalidInput',
26
+ message:
27
+ 'A test message could not be delivered to this destination: ' +
28
+ `SQS ${dest.queueUrl} in ${dest.region} for ${dest.accessKey}. ` +
29
+ 'Please make sure your destination is correctly configured.',
30
+ },
31
+ 400
32
+ )
33
+ }
34
+ }
35
+
36
+ const resource: Subscription = {
37
+ ...getBaseResourceProperties(),
38
+ changes: draft.changes || [],
39
+ destination: draft.destination,
40
+ format: draft.format || {
41
+ type: 'Platform',
42
+ },
43
+ key: draft.key,
44
+ messages: draft.messages || [],
45
+ status: 'Healthy',
46
+ }
47
+ this.save(projectKey, resource)
48
+ return resource
49
+ }
50
+ }
@@ -1,15 +1,23 @@
1
1
  import {
2
2
  ReferenceTypeId,
3
3
  TaxCategory,
4
+ TaxCategoryAddTaxRateAction,
5
+ TaxCategoryChangeNameAction,
4
6
  TaxCategoryDraft,
7
+ TaxCategoryRemoveTaxRateAction,
8
+ TaxCategoryReplaceTaxRateAction,
9
+ TaxCategorySetDescriptionAction,
10
+ TaxCategorySetKeyAction,
11
+ TaxCategoryUpdateAction,
5
12
  TaxRate,
6
13
  TaxRateDraft,
7
14
  } from '@commercetools/platform-sdk'
8
15
  import { getBaseResourceProperties } from '../helpers'
9
- import AbstractRepository from './abstract'
16
+ import { AbstractResourceRepository } from './abstract'
10
17
  import { v4 as uuidv4 } from 'uuid'
18
+ import { Writable } from 'types'
11
19
 
12
- export class TaxCategoryRepository extends AbstractRepository {
20
+ export class TaxCategoryRepository extends AbstractResourceRepository {
13
21
  getTypeId(): ReferenceTypeId {
14
22
  return 'tax-category'
15
23
  }
@@ -18,13 +26,13 @@ export class TaxCategoryRepository extends AbstractRepository {
18
26
  const resource: TaxCategory = {
19
27
  ...getBaseResourceProperties(),
20
28
  ...draft,
21
- rates: draft.rates.map(this.taxRateFromTaxRateDraft),
29
+ rates: draft.rates?.map(this.taxRateFromTaxRateDraft) || [],
22
30
  }
23
31
  this.save(projectKey, resource)
24
32
  return resource
25
33
  }
26
34
 
27
- taxRateFromTaxRateDraft = (draft: TaxRateDraft): TaxRate => ({
35
+ private taxRateFromTaxRateDraft = (draft: TaxRateDraft): TaxRate => ({
28
36
  ...draft,
29
37
  id: uuidv4(),
30
38
  amount: draft.amount || 0,
@@ -40,11 +48,77 @@ export class TaxCategoryRepository extends AbstractRepository {
40
48
 
41
49
  // Catch this for now, should be checked when creating/updating
42
50
  if (result.count > 1) {
43
- throw new Error('Duplicate tax categorie key')
51
+ throw new Error('Duplicate tax category key')
44
52
  }
45
53
 
46
54
  return
47
55
  }
48
56
 
49
- actions = {}
57
+ actions: Partial<
58
+ Record<
59
+ TaxCategoryUpdateAction['action'],
60
+ (projectKey: string, resource: Writable<TaxCategory>, action: any) => void
61
+ >
62
+ > = {
63
+ addTaxRate: (
64
+ projectKey: string,
65
+ resource: Writable<TaxCategory>,
66
+ { taxRate }: TaxCategoryAddTaxRateAction
67
+ ) => {
68
+ if (resource.rates === undefined) {
69
+ resource.rates = []
70
+ }
71
+ resource.rates.push(this.taxRateFromTaxRateDraft(taxRate))
72
+ },
73
+ removeTaxRate: (
74
+ projectKey: string,
75
+ resource: Writable<TaxCategory>,
76
+ { taxRateId }: TaxCategoryRemoveTaxRateAction
77
+ ) => {
78
+ if (resource.rates === undefined) {
79
+ resource.rates = []
80
+ }
81
+ resource.rates = resource.rates.filter(taxRate => {
82
+ return taxRate.id !== taxRateId
83
+ })
84
+ },
85
+ replaceTaxRate: (
86
+ projectKey: string,
87
+ resource: Writable<TaxCategory>,
88
+ { taxRateId, taxRate }: TaxCategoryReplaceTaxRateAction
89
+ ) => {
90
+ if (resource.rates === undefined) {
91
+ resource.rates = []
92
+ }
93
+
94
+ const taxRateObj = this.taxRateFromTaxRateDraft(taxRate)
95
+ for (let i = 0; i < resource.rates.length; i++) {
96
+ const rate = resource.rates[i]
97
+ if (rate.id == taxRateId) {
98
+ resource.rates[i] = taxRateObj
99
+ }
100
+ }
101
+ },
102
+ setDescription: (
103
+ projectKey: string,
104
+ resource: Writable<TaxCategory>,
105
+ { description }: TaxCategorySetDescriptionAction
106
+ ) => {
107
+ resource.description = description
108
+ },
109
+ setKey: (
110
+ projectKey: string,
111
+ resource: Writable<TaxCategory>,
112
+ { key }: TaxCategorySetKeyAction
113
+ ) => {
114
+ resource.key = key
115
+ },
116
+ changeName: (
117
+ projectKey: string,
118
+ resource: Writable<TaxCategory>,
119
+ { name }: TaxCategoryChangeNameAction
120
+ ) => {
121
+ resource.name = name
122
+ },
123
+ }
50
124
  }
@@ -1,8 +1,22 @@
1
- import { Type, TypeDraft, ReferenceTypeId } from '@commercetools/platform-sdk'
1
+ import {
2
+ Type,
3
+ TypeDraft,
4
+ ReferenceTypeId,
5
+ TypeUpdateAction,
6
+ FieldDefinition,
7
+ TypeSetDescriptionAction,
8
+ TypeChangeNameAction,
9
+ TypeAddFieldDefinitionAction,
10
+ TypeChangeEnumValueLabelAction,
11
+ TypeAddEnumValueAction,
12
+ TypeChangeFieldDefinitionOrderAction,
13
+ TypeRemoveFieldDefinitionAction,
14
+ } from '@commercetools/platform-sdk'
15
+ import { Writable } from 'types'
2
16
  import { getBaseResourceProperties } from '../helpers'
3
- import AbstractRepository from './abstract'
17
+ import { AbstractResourceRepository } from './abstract'
4
18
 
5
- export class TypeRepository extends AbstractRepository {
19
+ export class TypeRepository extends AbstractResourceRepository {
6
20
  getTypeId(): ReferenceTypeId {
7
21
  return 'type'
8
22
  }
@@ -19,4 +33,120 @@ export class TypeRepository extends AbstractRepository {
19
33
  this.save(projectKey, resource)
20
34
  return resource
21
35
  }
36
+ actions: Partial<
37
+ Record<
38
+ TypeUpdateAction['action'],
39
+ (projectKey: string, resource: Writable<Type>, action: any) => void
40
+ >
41
+ > = {
42
+ addFieldDefinition: (
43
+ projectKey: string,
44
+ resource: Writable<Type>,
45
+ { fieldDefinition }: TypeAddFieldDefinitionAction
46
+ ) => {
47
+ resource.fieldDefinitions.push(fieldDefinition)
48
+ },
49
+ removeFieldDefinition: (
50
+ projectKey: string,
51
+ resource: Writable<Type>,
52
+ { fieldName }: TypeRemoveFieldDefinitionAction
53
+ ) => {
54
+ resource.fieldDefinitions = resource.fieldDefinitions.filter(f => {
55
+ return f.name !== fieldName
56
+ })
57
+ },
58
+ setDescription: (
59
+ projectKey: string,
60
+ resource: Writable<Type>,
61
+ { description }: TypeSetDescriptionAction
62
+ ) => {
63
+ resource.description = description
64
+ },
65
+ changeName: (
66
+ projectKey: string,
67
+ resource: Writable<Type>,
68
+ { name }: TypeChangeNameAction
69
+ ) => {
70
+ resource.name = name
71
+ },
72
+ changeFieldDefinitionOrder: (
73
+ projectKey: string,
74
+ resource: Writable<Type>,
75
+ { fieldNames }: TypeChangeFieldDefinitionOrderAction
76
+ ) => {
77
+ const fields = new Map(
78
+ resource.fieldDefinitions.map(item => [item.name, item])
79
+ )
80
+ const result: FieldDefinition[] = []
81
+ let current = resource.fieldDefinitions
82
+
83
+ fieldNames.forEach(fieldName => {
84
+ const field = fields.get(fieldName)
85
+ if (field === undefined) {
86
+ throw new Error('New field')
87
+ }
88
+ result.push(field)
89
+
90
+ // Remove from current items
91
+ current = current.filter(f => {
92
+ return f.name !== fieldName
93
+ })
94
+ })
95
+
96
+ resource.fieldDefinitions = result
97
+ // Add fields which were not specified in the order as last items. Not
98
+ // sure if this follows commercetools
99
+ resource.fieldDefinitions.push(...current)
100
+ },
101
+ addEnumValue: (
102
+ projectKey: string,
103
+ resource: Writable<Type>,
104
+ { fieldName, value }: TypeAddEnumValueAction
105
+ ) => {
106
+ resource.fieldDefinitions.forEach(field => {
107
+ if (field.name == fieldName) {
108
+ // TODO, should be done better i suppose
109
+ if (field.type.name == 'Enum') {
110
+ field.type.values.push(value)
111
+ } else if (
112
+ field.type.name == 'Set' &&
113
+ field.type.elementType.name == 'Enum'
114
+ ) {
115
+ field.type.elementType.values.push(value)
116
+ } else {
117
+ throw new Error('Type is not a Enum (or Set of Enum)')
118
+ }
119
+ }
120
+ })
121
+ },
122
+ changeEnumValueLabel: (
123
+ projectKey: string,
124
+ resource: Writable<Type>,
125
+ { fieldName, value }: TypeChangeEnumValueLabelAction
126
+ ) => {
127
+ resource.fieldDefinitions.forEach(field => {
128
+ if (field.name == fieldName) {
129
+ // TODO, should be done better i suppose
130
+ if (field.type.name == 'Enum') {
131
+ field.type.values.forEach(v => {
132
+ if (v.key == value.key) {
133
+ v.label = value.label
134
+ }
135
+ })
136
+ } else if (
137
+ field.type.name == 'Set' &&
138
+ field.type.elementType.name == 'Enum'
139
+ ) {
140
+ field.type.elementType.values.forEach(v => {
141
+ if (v.key == value.key) {
142
+ v.label = value.label
143
+ }
144
+ })
145
+ } else {
146
+ throw new Error('Type is not a Enum (or Set of Enum)')
147
+ }
148
+ }
149
+ })
150
+ },
151
+ }
22
152
  }
@@ -0,0 +1,77 @@
1
+ import {
2
+ ReferenceTypeId,
3
+ Zone,
4
+ ZoneAddLocationAction,
5
+ ZoneChangeNameAction,
6
+ ZoneDraft,
7
+ ZoneRemoveLocationAction,
8
+ ZoneSetDescriptionAction,
9
+ ZoneSetKeyAction,
10
+ ZoneUpdateAction,
11
+ } from '@commercetools/platform-sdk'
12
+ import { Writable } from 'types'
13
+ import { getBaseResourceProperties } from '../helpers'
14
+ import { AbstractResourceRepository } from './abstract'
15
+
16
+ export class ZoneRepository extends AbstractResourceRepository {
17
+ getTypeId(): ReferenceTypeId {
18
+ return 'zone'
19
+ }
20
+
21
+ create(projectKey: string, draft: ZoneDraft): Zone {
22
+ const resource: Zone = {
23
+ ...getBaseResourceProperties(),
24
+ key: draft.key,
25
+ locations: draft.locations || [],
26
+ name: draft.name,
27
+ description: draft.description,
28
+ }
29
+ this.save(projectKey, resource)
30
+ return resource
31
+ }
32
+
33
+ actions: Partial<
34
+ Record<
35
+ ZoneUpdateAction['action'],
36
+ (projectKey: string, resource: Writable<Zone>, action: any) => void
37
+ >
38
+ > = {
39
+ addLocation: (
40
+ projectKey: string,
41
+ resource: Writable<Zone>,
42
+ { location }: ZoneAddLocationAction
43
+ ) => {
44
+ resource.locations.push(location)
45
+ },
46
+ removeLocation: (
47
+ projectKey: string,
48
+ resource: Writable<Zone>,
49
+ { location }: ZoneRemoveLocationAction
50
+ ) => {
51
+ resource.locations = resource.locations.filter(loc => {
52
+ return !(loc.country == location.country && loc.state == location.state)
53
+ })
54
+ },
55
+ changeName: (
56
+ projectKey: string,
57
+ resource: Writable<Zone>,
58
+ { name }: ZoneChangeNameAction
59
+ ) => {
60
+ resource.name = name
61
+ },
62
+ setDescription: (
63
+ projectKey: string,
64
+ resource: Writable<Zone>,
65
+ { description }: ZoneSetDescriptionAction
66
+ ) => {
67
+ resource.description = description
68
+ },
69
+ setKey: (
70
+ projectKey: string,
71
+ resource: Writable<Zone>,
72
+ { key }: ZoneSetKeyAction
73
+ ) => {
74
+ resource.key = key
75
+ },
76
+ }
77
+ }
package/src/server.ts CHANGED
@@ -1,4 +1,9 @@
1
1
  import { CommercetoolsMock } from './index'
2
2
 
3
3
  const instance = new CommercetoolsMock()
4
- instance.runServer(3000)
4
+
5
+ let port = 3000
6
+
7
+ if (process.env.HTTP_SERVER_PORT) port = parseInt(process.env.HTTP_SERVER_PORT)
8
+
9
+ instance.runServer(port)
@@ -1,11 +1,13 @@
1
1
  import { Update } from '@commercetools/platform-sdk'
2
2
  import { ParsedQs } from 'qs'
3
3
  import { Request, Response, Router } from 'express'
4
- import AbstractRepository from '../repositories/abstract'
4
+ import { AbstractResourceRepository } from '../repositories/abstract'
5
5
 
6
6
  export default abstract class AbstractService {
7
7
  protected abstract getBasePath(): string
8
- public abstract repository: AbstractRepository
8
+ public abstract repository: AbstractResourceRepository
9
+
10
+ createStatusCode = 201
9
11
 
10
12
  constructor(parent: Router) {
11
13
  this.registerRoutes(parent)
@@ -21,15 +23,15 @@ export default abstract class AbstractService {
21
23
  this.extraRoutes(router)
22
24
 
23
25
  router.get('/', this.get.bind(this))
26
+ router.get('/key=:key', this.getWithKey.bind(this)) // same thing goes for the key routes
24
27
  router.get('/:id', this.getWithId.bind(this))
25
- router.get('/key=:key', this.getWithKey.bind(this))
26
28
 
27
- router.delete('/:id', this.deletewithId.bind(this))
28
29
  router.delete('/key=:key', this.deletewithKey.bind(this))
30
+ router.delete('/:id', this.deletewithId.bind(this))
29
31
 
30
32
  router.post('/', this.post.bind(this))
31
- router.post('/:id', this.postWithId.bind(this))
32
33
  router.post('/key=:key', this.postWithKey.bind(this))
34
+ router.post('/:id', this.postWithId.bind(this))
33
35
 
34
36
  parent.use(`/${basePath}`, router)
35
37
  }
@@ -43,11 +45,22 @@ export default abstract class AbstractService {
43
45
  }
44
46
 
45
47
  getWithId(request: Request, response: Response) {
46
- return this._expandWithId(request, response, request.params['id'])
48
+ const result = this._expandWithId(request, request.params['id'])
49
+ if (!result) {
50
+ return response.status(404).send()
51
+ }
52
+ console.log(JSON.stringify(result, null, 4))
53
+ return response.status(200).send(result)
47
54
  }
48
55
 
49
56
  getWithKey(request: Request, response: Response) {
50
- return response.status(500).send('Not implemented')
57
+ const result = this.repository.getByKey(
58
+ request.params.projectKey,
59
+ request.params['key'],
60
+ { expand: this._parseParam(request.query.expand) }
61
+ )
62
+ if (!result) return response.status(404).send()
63
+ return response.status(200).send(result)
51
64
  }
52
65
 
53
66
  deletewithId(request: Request, response: Response) {
@@ -71,7 +84,8 @@ export default abstract class AbstractService {
71
84
  post(request: Request, response: Response) {
72
85
  const draft = request.body
73
86
  const resource = this.repository.create(request.params.projectKey, draft)
74
- return this._expandWithId(request, response, resource.id)
87
+ const result = this._expandWithId(request, resource.id)
88
+ return response.status(this.createStatusCode).send(result)
75
89
  }
76
90
 
77
91
  postWithId(request: Request, response: Response) {
@@ -94,25 +108,19 @@ export default abstract class AbstractService {
94
108
  updateRequest.actions
95
109
  )
96
110
 
97
- return this._expandWithId(request, response, updatedResource.id)
111
+ const result = this._expandWithId(request, updatedResource.id)
112
+ return response.status(200).send(result)
98
113
  }
99
114
 
100
115
  postWithKey(request: Request, response: Response) {
101
116
  return response.status(500).send('Not implemented')
102
117
  }
103
118
 
104
- protected _expandWithId(
105
- request: Request,
106
- response: Response,
107
- resourceId: string
108
- ) {
119
+ protected _expandWithId(request: Request, resourceId: string) {
109
120
  const result = this.repository.get(request.params.projectKey, resourceId, {
110
121
  expand: this._parseParam(request.query.expand),
111
122
  })
112
- if (!result) {
113
- return response.status(404).send('Not found')
114
- }
115
- return response.status(200).send(result)
123
+ return result
116
124
  }
117
125
 
118
126
  // No idea what i'm doing
@@ -0,0 +1,17 @@
1
+ import { Router } from 'express'
2
+ import AbstractService from './abstract'
3
+ import { AbstractStorage } from '../storage'
4
+ import { CartDiscountRepository } from '../repositories/cart-discount'
5
+
6
+ export class CartDiscountService extends AbstractService {
7
+ public repository: CartDiscountRepository
8
+
9
+ constructor(parent: Router, storage: AbstractStorage) {
10
+ super(parent)
11
+ this.repository = new CartDiscountRepository(storage)
12
+ }
13
+
14
+ getBasePath() {
15
+ return 'cart-discounts'
16
+ }
17
+ }