@labdigital/commercetools-mock 0.5.13 → 0.5.14

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 (104) hide show
  1. package/README.md +6 -2
  2. package/dist/commercetools-mock.cjs.development.js +3058 -1645
  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 +3058 -1645
  7. package/dist/commercetools-mock.esm.js.map +1 -1
  8. package/dist/ctMock.d.ts +1 -0
  9. package/dist/repositories/abstract.d.ts +9 -7
  10. package/dist/repositories/cart-discount.d.ts +9 -0
  11. package/dist/repositories/cart.d.ts +16 -3
  12. package/dist/repositories/category.d.ts +18 -0
  13. package/dist/repositories/channel.d.ts +6 -0
  14. package/dist/repositories/custom-object.d.ts +2 -2
  15. package/dist/repositories/customer-group.d.ts +11 -0
  16. package/dist/repositories/customer.d.ts +2 -2
  17. package/dist/repositories/discount-code.d.ts +8 -0
  18. package/dist/repositories/errors.d.ts +2 -2
  19. package/dist/repositories/extension.d.ts +8 -0
  20. package/dist/repositories/inventory-entry.d.ts +2 -2
  21. package/dist/repositories/order.d.ts +2 -2
  22. package/dist/repositories/payment.d.ts +4 -3
  23. package/dist/repositories/product-projection.d.ts +2 -2
  24. package/dist/repositories/product-type.d.ts +5 -4
  25. package/dist/repositories/product.d.ts +2 -2
  26. package/dist/repositories/project.d.ts +8 -0
  27. package/dist/repositories/shipping-method.d.ts +7 -5
  28. package/dist/repositories/shopping-list.d.ts +2 -2
  29. package/dist/repositories/state.d.ts +5 -4
  30. package/dist/repositories/store.d.ts +6 -3
  31. package/dist/repositories/subscription.d.ts +6 -0
  32. package/dist/repositories/tax-category.d.ts +6 -5
  33. package/dist/repositories/type.d.ts +5 -3
  34. package/dist/repositories/zone.d.ts +8 -0
  35. package/dist/services/abstract.d.ts +4 -3
  36. package/dist/services/cart-discount.d.ts +9 -0
  37. package/dist/services/category.d.ts +9 -0
  38. package/dist/services/channel.d.ts +9 -0
  39. package/dist/services/customer-group.d.ts +9 -0
  40. package/dist/services/discount-code.d.ts +9 -0
  41. package/dist/services/extension.d.ts +9 -0
  42. package/dist/services/my-cart.d.ts +11 -0
  43. package/dist/services/project.d.ts +11 -0
  44. package/dist/services/subscription.d.ts +9 -0
  45. package/dist/services/zone.d.ts +9 -0
  46. package/dist/storage.d.ts +10 -1
  47. package/dist/types.d.ts +2 -1
  48. package/package.json +20 -20
  49. package/src/ctMock.ts +44 -17
  50. package/src/oauth/server.ts +3 -1
  51. package/src/repositories/abstract.ts +39 -33
  52. package/src/repositories/cart-discount.ts +140 -0
  53. package/src/repositories/cart.ts +247 -3
  54. package/src/repositories/category.ts +140 -0
  55. package/src/repositories/channel.ts +23 -0
  56. package/src/repositories/custom-object.ts +2 -2
  57. package/src/repositories/customer-group.ts +42 -0
  58. package/src/repositories/customer.ts +2 -2
  59. package/src/repositories/discount-code.ts +143 -0
  60. package/src/repositories/errors.ts +7 -2
  61. package/src/repositories/extension.ts +65 -0
  62. package/src/repositories/inventory-entry.ts +2 -2
  63. package/src/repositories/order.ts +2 -2
  64. package/src/repositories/payment.ts +10 -6
  65. package/src/repositories/product-projection.ts +2 -2
  66. package/src/repositories/product-type.ts +57 -4
  67. package/src/repositories/product.ts +4 -2
  68. package/src/repositories/project.ts +150 -0
  69. package/src/repositories/shipping-method.ts +149 -13
  70. package/src/repositories/shopping-list.ts +2 -2
  71. package/src/repositories/state.ts +48 -4
  72. package/src/repositories/store.ts +69 -4
  73. package/src/repositories/subscription.ts +50 -0
  74. package/src/repositories/tax-category.ts +80 -6
  75. package/src/repositories/type.ts +133 -3
  76. package/src/repositories/zone.ts +77 -0
  77. package/src/server.ts +6 -1
  78. package/src/services/abstract.ts +16 -15
  79. package/src/services/cart-discount.ts +17 -0
  80. package/src/services/cart.test.ts +314 -3
  81. package/src/services/category.test.ts +37 -0
  82. package/src/services/category.ts +17 -0
  83. package/src/services/channel.ts +17 -0
  84. package/src/services/custom-object.test.ts +3 -3
  85. package/src/services/customer-group.ts +17 -0
  86. package/src/services/discount-code.ts +17 -0
  87. package/src/services/extension.ts +17 -0
  88. package/src/services/inventory-entry.test.ts +3 -3
  89. package/src/services/my-cart.test.ts +93 -0
  90. package/src/services/my-cart.ts +44 -0
  91. package/src/services/my-payment.test.ts +2 -2
  92. package/src/services/order.test.ts +4 -4
  93. package/src/services/payment.test.ts +2 -2
  94. package/src/services/product-projection.test.ts +1 -5
  95. package/src/services/product-type.test.ts +2 -2
  96. package/src/services/product.test.ts +6 -2
  97. package/src/services/project.ts +42 -0
  98. package/src/services/shipping-method.test.ts +3 -3
  99. package/src/services/state.test.ts +2 -2
  100. package/src/services/subscription.ts +17 -0
  101. package/src/services/tax-category.test.ts +3 -3
  102. package/src/services/zone.ts +17 -0
  103. package/src/storage.ts +69 -1
  104. package/src/types.ts +2 -1
@@ -0,0 +1,150 @@
1
+ import {
2
+ Project,
3
+ ProjectChangeCartsConfigurationAction,
4
+ ProjectChangeCountriesAction,
5
+ ProjectChangeCountryTaxRateFallbackEnabledAction,
6
+ ProjectChangeCurrenciesAction,
7
+ ProjectChangeLanguagesAction,
8
+ ProjectChangeMessagesEnabledAction,
9
+ ProjectChangeNameAction,
10
+ ProjectChangeOrderSearchStatusAction,
11
+ ProjectChangeProductSearchIndexingEnabledAction,
12
+ ProjectSetExternalOAuthAction,
13
+ ProjectSetShippingRateInputTypeAction,
14
+ ProjectUpdateAction,
15
+ } from '@commercetools/platform-sdk'
16
+ import { InvalidOperationError } from '@commercetools/platform-sdk'
17
+ import { Writable } from 'types'
18
+ import { checkConcurrentModification } from './errors'
19
+ import { CommercetoolsError } from '../exceptions'
20
+ import { AbstractRepository } from './abstract'
21
+
22
+ export class ProjectRepository extends AbstractRepository {
23
+ get(projectKey: string): Project | null {
24
+ const data = this._storage.getProject(projectKey)
25
+ return this._storage.getProject(projectKey)
26
+ }
27
+
28
+ save(projectKey: string, resource: Project) {
29
+ const current = this.get(projectKey)
30
+
31
+ if (current) {
32
+ checkConcurrentModification(current, resource.version)
33
+ } else {
34
+ if (resource.version !== 0) {
35
+ throw new CommercetoolsError<InvalidOperationError>(
36
+ {
37
+ code: 'InvalidOperation',
38
+ message: 'version on create must be 0',
39
+ },
40
+ 400
41
+ )
42
+ }
43
+ }
44
+
45
+ // @ts-ignore
46
+ resource.version += 1
47
+ this._storage.saveProject(resource)
48
+ }
49
+
50
+ actions: Partial<
51
+ Record<
52
+ ProjectUpdateAction['action'],
53
+ (projectKey: string, resource: Writable<Project>, action: any) => void
54
+ >
55
+ > = {
56
+ changeName: (
57
+ projectKey: string,
58
+ resource: Writable<Project>,
59
+ { name }: ProjectChangeNameAction
60
+ ) => {
61
+ resource.name = name
62
+ },
63
+ changeCurrencies: (
64
+ projectKey: string,
65
+ resource: Writable<Project>,
66
+ { currencies }: ProjectChangeCurrenciesAction
67
+ ) => {
68
+ resource.currencies = currencies
69
+ },
70
+ changeCountries: (
71
+ projectKey: string,
72
+ resource: Writable<Project>,
73
+ { countries }: ProjectChangeCountriesAction
74
+ ) => {
75
+ resource.countries = countries
76
+ },
77
+ changeLanguages: (
78
+ projectKey: string,
79
+ resource: Writable<Project>,
80
+ { languages }: ProjectChangeLanguagesAction
81
+ ) => {
82
+ resource.languages = languages
83
+ },
84
+ changeMessagesEnabled: (
85
+ projectKey: string,
86
+ resource: Writable<Project>,
87
+ { messagesEnabled }: ProjectChangeMessagesEnabledAction
88
+ ) => {
89
+ resource.messages.enabled = messagesEnabled
90
+ },
91
+ changeProductSearchIndexingEnabled: (
92
+ projectKey: string,
93
+ resource: Writable<Project>,
94
+ { enabled }: ProjectChangeProductSearchIndexingEnabledAction
95
+ ) => {
96
+ if (!resource.searchIndexing?.products) {
97
+ throw new Error('Invalid project state')
98
+ }
99
+ resource.searchIndexing.products.status = enabled
100
+ ? 'Activated'
101
+ : 'Deactivated'
102
+ resource.searchIndexing.products.lastModifiedAt = new Date().toISOString()
103
+ },
104
+ changeOrderSearchStatus: (
105
+ projectKey: string,
106
+ resource: Writable<Project>,
107
+ { status }: ProjectChangeOrderSearchStatusAction
108
+ ) => {
109
+ if (!resource.searchIndexing?.orders) {
110
+ throw new Error('Invalid project state')
111
+ }
112
+ resource.searchIndexing.orders.status = status
113
+ resource.searchIndexing.orders.lastModifiedAt = new Date().toISOString()
114
+ },
115
+ setShippingRateInputType: (
116
+ projectKey: string,
117
+ resource: Writable<Project>,
118
+ { shippingRateInputType }: ProjectSetShippingRateInputTypeAction
119
+ ) => {
120
+ resource.shippingRateInputType = shippingRateInputType
121
+ },
122
+ setExternalOAuth: (
123
+ projectKey: string,
124
+ resource: Writable<Project>,
125
+ { externalOAuth }: ProjectSetExternalOAuthAction
126
+ ) => {
127
+ resource.externalOAuth = externalOAuth
128
+ },
129
+ changeCountryTaxRateFallbackEnabled: (
130
+ projectKey: string,
131
+ resource: Writable<Project>,
132
+ {
133
+ countryTaxRateFallbackEnabled,
134
+ }: ProjectChangeCountryTaxRateFallbackEnabledAction
135
+ ) => {
136
+ resource.carts.countryTaxRateFallbackEnabled = countryTaxRateFallbackEnabled
137
+ },
138
+ changeCartsConfiguration: (
139
+ projectKey: string,
140
+ resource: Writable<Project>,
141
+ { cartsConfiguration }: ProjectChangeCartsConfigurationAction
142
+ ) => {
143
+ console.log(cartsConfiguration)
144
+ resource.carts = cartsConfiguration || {
145
+ countryTaxRateFallbackEnabled: false,
146
+ deleteDaysAfterLastModification: 90,
147
+ }
148
+ },
149
+ }
150
+ }
@@ -6,14 +6,30 @@ import {
6
6
  import {
7
7
  ReferenceTypeId,
8
8
  ShippingMethod,
9
+ ShippingMethodAddShippingRateAction,
10
+ ShippingMethodAddZoneAction,
11
+ ShippingMethodChangeIsDefaultAction,
12
+ ShippingMethodChangeNameAction,
9
13
  ShippingMethodDraft,
14
+ ShippingMethodRemoveZoneAction,
15
+ ShippingMethodSetDescriptionAction,
16
+ ShippingMethodSetKeyAction,
17
+ ShippingMethodSetLocalizedDescriptionAction,
18
+ ShippingMethodSetPredicateAction,
19
+ ShippingMethodUpdateAction,
20
+ ShippingRate,
21
+ ShippingRateDraft,
10
22
  ZoneRate,
11
23
  ZoneRateDraft,
24
+ ZoneReference,
12
25
  } from '@commercetools/platform-sdk'
13
26
  import { getBaseResourceProperties } from '../helpers'
14
- import AbstractRepository from './abstract'
27
+ import { AbstractResourceRepository } from './abstract'
28
+ import { Writable } from 'types'
29
+ import { _ } from 'ajv'
30
+ import deepEqual from 'deep-equal'
15
31
 
16
- export class ShippingMethodRepository extends AbstractRepository {
32
+ export class ShippingMethodRepository extends AbstractResourceRepository {
17
33
  getTypeId(): ReferenceTypeId {
18
34
  return 'shipping-method'
19
35
  }
@@ -27,8 +43,8 @@ export class ShippingMethodRepository extends AbstractRepository {
27
43
  projectKey,
28
44
  this._storage
29
45
  ),
30
- zoneRates: draft.zoneRates.map(z =>
31
- this.zoneRateFromZoneRateDraft(projectKey, z)
46
+ zoneRates: draft.zoneRates?.map(z =>
47
+ this._transformZoneRateDraft(projectKey, z)
32
48
  ),
33
49
  custom: createCustomFields(draft.custom, projectKey, this._storage),
34
50
  }
@@ -36,23 +52,143 @@ export class ShippingMethodRepository extends AbstractRepository {
36
52
  return resource
37
53
  }
38
54
 
39
- zoneRateFromZoneRateDraft = (
55
+ private _transformZoneRateDraft = (
40
56
  projectKey: string,
41
57
  draft: ZoneRateDraft
42
58
  ): ZoneRate => ({
43
59
  ...draft,
44
- zone: getReferenceFromResourceIdentifier(
60
+ zone: getReferenceFromResourceIdentifier<ZoneReference>(
45
61
  draft.zone,
46
62
  projectKey,
47
63
  this._storage
48
64
  ),
49
- shippingRates: draft.shippingRates.map(s => ({
50
- ...s,
51
- price: createTypedMoney(s.price),
52
- freeAbove: s.freeAbove && createTypedMoney(s.freeAbove),
53
- tiers: s.tiers || [],
54
- })),
65
+ shippingRates: draft.shippingRates?.map(this._transformShippingRate),
55
66
  })
56
67
 
57
- actions = {}
68
+ private _transformShippingRate = (rate: ShippingRateDraft): ShippingRate => {
69
+ return {
70
+ price: createTypedMoney(rate.price),
71
+ freeAbove: rate.freeAbove && createTypedMoney(rate.freeAbove),
72
+ tiers: rate.tiers || [],
73
+ }
74
+ }
75
+
76
+ actions: Partial<
77
+ Record<
78
+ ShippingMethodUpdateAction['action'],
79
+ (
80
+ projectKey: string,
81
+ resource: Writable<ShippingMethod>,
82
+ action: any
83
+ ) => void
84
+ >
85
+ > = {
86
+ addShippingRate: (
87
+ projectKey: string,
88
+ resource: Writable<ShippingMethod>,
89
+ { shippingRate, zone }: ShippingMethodAddShippingRateAction
90
+ ) => {
91
+ const rate = this._transformShippingRate(shippingRate)
92
+
93
+ resource.zoneRates.forEach(zoneRate => {
94
+ if (zoneRate.zone.id == zone.id) {
95
+ zoneRate.shippingRates.push(rate)
96
+ return
97
+ }
98
+ })
99
+ resource.zoneRates.push({
100
+ zone: {
101
+ typeId: 'zone',
102
+ id: zone.id!,
103
+ },
104
+ shippingRates: [rate],
105
+ })
106
+ },
107
+ removeShippingRate: (
108
+ projectKey: string,
109
+ resource: Writable<ShippingMethod>,
110
+ { shippingRate, zone }: ShippingMethodAddShippingRateAction
111
+ ) => {
112
+ const rate = this._transformShippingRate(shippingRate)
113
+
114
+ resource.zoneRates.forEach(zoneRate => {
115
+ if (zoneRate.zone.id == zone.id) {
116
+ zoneRate.shippingRates = zoneRate.shippingRates.filter(otherRate => {
117
+ return !deepEqual(rate, otherRate)
118
+ })
119
+ }
120
+ })
121
+ },
122
+ addZone: (
123
+ projectKey: string,
124
+ resource: Writable<ShippingMethod>,
125
+ { zone }: ShippingMethodAddZoneAction
126
+ ) => {
127
+ const zoneReference = getReferenceFromResourceIdentifier<ZoneReference>(
128
+ zone,
129
+ projectKey,
130
+ this._storage
131
+ )
132
+
133
+ if (resource.zoneRates === undefined) {
134
+ resource.zoneRates = []
135
+ }
136
+
137
+ resource.zoneRates.push({
138
+ zone: zoneReference,
139
+ shippingRates: [],
140
+ })
141
+ },
142
+ removeZone: (
143
+ projectKey: string,
144
+ resource: Writable<ShippingMethod>,
145
+ { zone }: ShippingMethodRemoveZoneAction
146
+ ) => {
147
+ resource.zoneRates = resource.zoneRates.filter(zoneRate => {
148
+ return zoneRate.zone.id !== zone.id
149
+ })
150
+ },
151
+ setKey: (
152
+ projectKey: string,
153
+ resource: Writable<ShippingMethod>,
154
+ { key }: ShippingMethodSetKeyAction
155
+ ) => {
156
+ resource.key = key
157
+ },
158
+ setDescription: (
159
+ projectKey: string,
160
+ resource: Writable<ShippingMethod>,
161
+ { description }: ShippingMethodSetDescriptionAction
162
+ ) => {
163
+ resource.description = description
164
+ },
165
+ setLocalizedDescription: (
166
+ projectKey: string,
167
+ resource: Writable<ShippingMethod>,
168
+ { localizedDescription }: ShippingMethodSetLocalizedDescriptionAction
169
+ ) => {
170
+ resource.localizedDescription = localizedDescription
171
+ },
172
+ setPredicate: (
173
+ projectKey: string,
174
+ resource: Writable<ShippingMethod>,
175
+ { predicate }: ShippingMethodSetPredicateAction
176
+ ) => {
177
+ resource.predicate = predicate
178
+ },
179
+ changeIsDefault: (
180
+ projectKey: string,
181
+ resource: Writable<ShippingMethod>,
182
+ { isDefault }: ShippingMethodChangeIsDefaultAction
183
+ ) => {
184
+ resource.isDefault = isDefault
185
+ },
186
+ changeName: (
187
+ projectKey: string,
188
+ resource: Writable<ShippingMethod>,
189
+ { name }: ShippingMethodChangeNameAction
190
+ ) => {
191
+ resource.name = name
192
+ },
193
+ }
58
194
  }
@@ -5,13 +5,13 @@ import {
5
5
  ShoppingListDraft,
6
6
  } from '@commercetools/platform-sdk'
7
7
  import { getBaseResourceProperties } from '../helpers'
8
- import AbstractRepository from './abstract'
8
+ import { AbstractResourceRepository } from './abstract'
9
9
  import {
10
10
  createCustomFields,
11
11
  getReferenceFromResourceIdentifier,
12
12
  } from './helpers'
13
13
 
14
- export class ShoppingListRepository extends AbstractRepository {
14
+ export class ShoppingListRepository extends AbstractResourceRepository {
15
15
  getTypeId(): ReferenceTypeId {
16
16
  return 'shopping-list'
17
17
  }
@@ -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
  }