@labdigital/commercetools-mock 0.5.11 → 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.
- package/README.md +30 -12
- package/dist/commercetools-mock.cjs.development.js +3190 -1574
- package/dist/commercetools-mock.cjs.development.js.map +1 -1
- package/dist/commercetools-mock.cjs.production.min.js +1 -1
- package/dist/commercetools-mock.cjs.production.min.js.map +1 -1
- package/dist/commercetools-mock.esm.js +3190 -1574
- package/dist/commercetools-mock.esm.js.map +1 -1
- package/dist/ctMock.d.ts +1 -0
- package/dist/lib/filterParser.d.ts +1 -0
- package/dist/repositories/abstract.d.ts +11 -7
- package/dist/repositories/cart-discount.d.ts +9 -0
- package/dist/repositories/cart.d.ts +16 -3
- package/dist/repositories/category.d.ts +18 -0
- package/dist/repositories/channel.d.ts +6 -0
- package/dist/repositories/custom-object.d.ts +2 -2
- package/dist/repositories/customer-group.d.ts +11 -0
- package/dist/repositories/customer.d.ts +2 -2
- package/dist/repositories/discount-code.d.ts +8 -0
- package/dist/repositories/errors.d.ts +2 -2
- package/dist/repositories/extension.d.ts +8 -0
- package/dist/repositories/inventory-entry.d.ts +2 -2
- package/dist/repositories/order.d.ts +2 -2
- package/dist/repositories/payment.d.ts +4 -3
- package/dist/repositories/product-projection.d.ts +10 -0
- package/dist/repositories/product-type.d.ts +5 -4
- package/dist/repositories/product.d.ts +2 -2
- package/dist/repositories/project.d.ts +8 -0
- package/dist/repositories/shipping-method.d.ts +7 -5
- package/dist/repositories/shopping-list.d.ts +6 -0
- package/dist/repositories/state.d.ts +5 -4
- package/dist/repositories/store.d.ts +6 -3
- package/dist/repositories/subscription.d.ts +6 -0
- package/dist/repositories/tax-category.d.ts +6 -5
- package/dist/repositories/type.d.ts +5 -3
- package/dist/repositories/zone.d.ts +8 -0
- package/dist/services/abstract.d.ts +4 -3
- package/dist/services/cart-discount.d.ts +9 -0
- package/dist/services/category.d.ts +9 -0
- package/dist/services/channel.d.ts +9 -0
- package/dist/services/custom-object.d.ts +2 -0
- package/dist/services/customer-group.d.ts +9 -0
- package/dist/services/discount-code.d.ts +9 -0
- package/dist/services/extension.d.ts +9 -0
- package/dist/services/my-cart.d.ts +11 -0
- package/dist/services/product-projection.d.ts +11 -0
- package/dist/services/project.d.ts +11 -0
- package/dist/services/shopping-list.d.ts +9 -0
- package/dist/services/subscription.d.ts +9 -0
- package/dist/services/zone.d.ts +9 -0
- package/dist/storage.d.ts +27 -16
- package/dist/types.d.ts +9 -4
- package/package.json +20 -20
- package/src/ctMock.ts +49 -15
- package/src/lib/filterParser.test.ts +15 -0
- package/src/lib/filterParser.ts +17 -0
- package/src/oauth/server.ts +3 -1
- package/src/repositories/abstract.ts +49 -34
- package/src/repositories/cart-discount.ts +140 -0
- package/src/repositories/cart.ts +247 -3
- package/src/repositories/category.ts +140 -0
- package/src/repositories/channel.ts +23 -0
- package/src/repositories/custom-object.ts +5 -2
- package/src/repositories/customer-group.ts +42 -0
- package/src/repositories/customer.ts +2 -2
- package/src/repositories/discount-code.ts +143 -0
- package/src/repositories/errors.ts +7 -2
- package/src/repositories/extension.ts +65 -0
- package/src/repositories/inventory-entry.ts +2 -2
- package/src/repositories/order.ts +2 -2
- package/src/repositories/payment.ts +10 -6
- package/src/repositories/product-projection.ts +74 -0
- package/src/repositories/product-type.ts +57 -4
- package/src/repositories/product.ts +4 -2
- package/src/repositories/project.ts +150 -0
- package/src/repositories/shipping-method.ts +149 -13
- package/src/repositories/shopping-list.ts +50 -0
- package/src/repositories/state.ts +48 -4
- package/src/repositories/store.ts +69 -4
- package/src/repositories/subscription.ts +50 -0
- package/src/repositories/tax-category.ts +80 -6
- package/src/repositories/type.ts +133 -3
- package/src/repositories/zone.ts +77 -0
- package/src/server.ts +6 -1
- package/src/services/abstract.ts +26 -18
- package/src/services/cart-discount.ts +17 -0
- package/src/services/cart.test.ts +314 -3
- package/src/services/category.test.ts +37 -0
- package/src/services/category.ts +17 -0
- package/src/services/channel.ts +17 -0
- package/src/services/custom-object.test.ts +53 -3
- package/src/services/custom-object.ts +30 -0
- package/src/services/customer-group.ts +17 -0
- package/src/services/discount-code.ts +17 -0
- package/src/services/extension.ts +17 -0
- package/src/services/inventory-entry.test.ts +3 -3
- package/src/services/my-cart.test.ts +93 -0
- package/src/services/my-cart.ts +44 -0
- package/src/services/my-payment.test.ts +2 -2
- package/src/services/my-payment.ts +1 -1
- package/src/services/order.test.ts +4 -4
- package/src/services/payment.test.ts +2 -2
- package/src/services/product-projection.test.ts +105 -0
- package/src/services/product-projection.ts +29 -0
- package/src/services/product-type.test.ts +2 -2
- package/src/services/product.test.ts +6 -2
- package/src/services/project.ts +42 -0
- package/src/services/shipping-method.test.ts +3 -3
- package/src/services/shopping-list.ts +17 -0
- package/src/services/state.test.ts +2 -2
- package/src/services/store.test.ts +60 -0
- package/src/services/subscription.ts +17 -0
- package/src/services/tax-category.test.ts +3 -3
- package/src/services/zone.ts +17 -0
- package/src/storage.ts +127 -31
- package/src/types.ts +9 -3
package/dist/types.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ProductProjectionRepository } from './repositories/product-projection';
|
|
2
|
+
import { ShoppingListRepository } from './repositories/shopping-list';
|
|
1
3
|
import * as ctp from '@commercetools/platform-sdk';
|
|
2
4
|
import { ReferenceTypeId } from '@commercetools/platform-sdk';
|
|
3
5
|
import AbstractService from './services/abstract';
|
|
@@ -15,9 +17,10 @@ import { TaxCategoryRepository } from './repositories/tax-category';
|
|
|
15
17
|
export declare type Writable<T> = {
|
|
16
18
|
-readonly [P in keyof T]: Writable<T[P]>;
|
|
17
19
|
};
|
|
18
|
-
declare type
|
|
20
|
+
export declare type RepositoryTypes = ReferenceTypeId | 'product-projection';
|
|
21
|
+
export declare type ServiceTypes = RepositoryTypes | 'my-cart' | 'my-payment';
|
|
19
22
|
export declare type Services = Partial<{
|
|
20
|
-
[index in
|
|
23
|
+
[index in ServiceTypes]: AbstractService;
|
|
21
24
|
}>;
|
|
22
25
|
export declare type ResourceMap = {
|
|
23
26
|
cart: ctp.Cart;
|
|
@@ -35,6 +38,8 @@ export declare type ResourceMap = {
|
|
|
35
38
|
payment: ctp.Payment;
|
|
36
39
|
product: ctp.Product;
|
|
37
40
|
'product-discount': ctp.ProductDiscount;
|
|
41
|
+
'product-projection': ctp.ProductProjection;
|
|
42
|
+
'product-selection': ctp.ProductSelection;
|
|
38
43
|
'product-type': ctp.ProductType;
|
|
39
44
|
review: ctp.Review;
|
|
40
45
|
'shipping-method': ctp.ShippingMethod;
|
|
@@ -69,11 +74,12 @@ export declare type RepositoryMap = {
|
|
|
69
74
|
'order-edit': never;
|
|
70
75
|
payment: PaymentRepository;
|
|
71
76
|
product: ProductRepository;
|
|
77
|
+
'product-projection': ProductProjectionRepository;
|
|
72
78
|
'product-discount': never;
|
|
73
79
|
'product-type': ProductTypeRepository;
|
|
74
80
|
review: never;
|
|
75
81
|
'shipping-method': ShippingMethodRepository;
|
|
76
|
-
'shopping-list':
|
|
82
|
+
'shopping-list': ShoppingListRepository;
|
|
77
83
|
state: StateRepository;
|
|
78
84
|
store: never;
|
|
79
85
|
subscription: never;
|
|
@@ -81,4 +87,3 @@ export declare type RepositoryMap = {
|
|
|
81
87
|
type: ctp.Type;
|
|
82
88
|
zone: ctp.Zone;
|
|
83
89
|
};
|
|
84
|
-
export {};
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.5.
|
|
2
|
+
"version": "0.5.14",
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"typings": "dist/index.d.ts",
|
|
@@ -41,35 +41,35 @@
|
|
|
41
41
|
}
|
|
42
42
|
],
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@babel/preset-typescript": "^7.
|
|
45
|
-
"@size-limit/preset-small-lib": "^
|
|
44
|
+
"@babel/preset-typescript": "^7.16.7",
|
|
45
|
+
"@size-limit/preset-small-lib": "^7.0.5",
|
|
46
46
|
"@types/basic-auth": "^1.1.3",
|
|
47
47
|
"@types/deep-equal": "^1.0.1",
|
|
48
|
-
"@types/express": "^4.17.
|
|
49
|
-
"@types/morgan": "^1.9.
|
|
48
|
+
"@types/express": "^4.17.13",
|
|
49
|
+
"@types/morgan": "^1.9.3",
|
|
50
50
|
"@types/supertest": "^2.0.11",
|
|
51
|
-
"@types/uuid": "^8.3.
|
|
52
|
-
"esbuild-register": "^
|
|
53
|
-
"got": "^11.8.
|
|
54
|
-
"husky": "^
|
|
55
|
-
"nodemon": "^2.0.
|
|
56
|
-
"size-limit": "^
|
|
57
|
-
"ts-node": "^
|
|
51
|
+
"@types/uuid": "^8.3.4",
|
|
52
|
+
"esbuild-register": "^3.3.1",
|
|
53
|
+
"got": "^11.8.3",
|
|
54
|
+
"husky": "^7.0.4",
|
|
55
|
+
"nodemon": "^2.0.15",
|
|
56
|
+
"size-limit": "^7.0.5",
|
|
57
|
+
"ts-node": "^10.4.0",
|
|
58
58
|
"tsdx": "^0.14.1",
|
|
59
|
-
"tslib": "^2.
|
|
60
|
-
"typescript": "^4.
|
|
59
|
+
"tslib": "^2.3.1",
|
|
60
|
+
"typescript": "^4.5.4"
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
|
-
"@commercetools/platform-sdk": "
|
|
64
|
-
"ajv": "^8.
|
|
65
|
-
"ajv-formats": "^2.
|
|
63
|
+
"@commercetools/platform-sdk": "2.4.1",
|
|
64
|
+
"ajv": "^8.8.2",
|
|
65
|
+
"ajv-formats": "^2.1.1",
|
|
66
66
|
"basic-auth": "^2.0.1",
|
|
67
67
|
"deep-equal": "^2.0.5",
|
|
68
|
-
"express": "^4.17.
|
|
68
|
+
"express": "^4.17.2",
|
|
69
69
|
"morgan": "^1.10.0",
|
|
70
|
-
"nock": "^13.
|
|
70
|
+
"nock": "^13.2.1",
|
|
71
71
|
"perplex": "^0.11.0",
|
|
72
72
|
"pratt": "^0.7.0",
|
|
73
|
-
"supertest": "^6.1.
|
|
73
|
+
"supertest": "^6.1.6"
|
|
74
74
|
}
|
|
75
75
|
}
|
package/src/ctMock.ts
CHANGED
|
@@ -1,28 +1,42 @@
|
|
|
1
|
-
import { ProductTypeService } from './services/product-type'
|
|
2
|
-
import { ShippingMethodService } from './services/shipping-method'
|
|
3
|
-
import { StateService } from './services/state'
|
|
4
|
-
import { TaxCategoryService } from './services/tax-category'
|
|
5
|
-
import { PaymentService } from './services/payment'
|
|
6
1
|
import nock from 'nock'
|
|
7
2
|
import express, { NextFunction, Request, Response } from 'express'
|
|
8
3
|
import supertest from 'supertest'
|
|
9
4
|
import morgan from 'morgan'
|
|
10
5
|
import { AbstractStorage, InMemoryStorage } from './storage'
|
|
11
|
-
import { TypeService } from './services/type'
|
|
12
|
-
import { CustomObjectService } from './services/custom-object'
|
|
13
|
-
import { CustomerService } from './services/customer'
|
|
14
|
-
import { CartService } from './services/cart'
|
|
15
|
-
import { InventoryEntryService } from './services/inventory-entry'
|
|
16
|
-
import { OrderService } from './services/order'
|
|
17
6
|
import { Services } from './types'
|
|
18
|
-
import { StoreService } from './services/store'
|
|
19
7
|
import { CommercetoolsError } from './exceptions'
|
|
20
8
|
import { OAuth2Server } from './oauth/server'
|
|
21
|
-
import { DEFAULT_API_HOSTNAME, DEFAULT_AUTH_HOSTNAME } from './constants'
|
|
22
9
|
import { ProjectAPI } from './projectAPI'
|
|
23
10
|
import { copyHeaders } from './lib/proxy'
|
|
24
|
-
import {
|
|
11
|
+
import { DEFAULT_API_HOSTNAME, DEFAULT_AUTH_HOSTNAME } from './constants'
|
|
12
|
+
|
|
13
|
+
// Services
|
|
14
|
+
import { CartDiscountService } from './services/cart-discount'
|
|
15
|
+
import { CartService } from './services/cart'
|
|
16
|
+
import { CategoryServices } from './services/category'
|
|
17
|
+
import { ChannelService } from './services/channel'
|
|
18
|
+
import { CustomerGroupService } from './services/customer-group'
|
|
19
|
+
import { CustomerService } from './services/customer'
|
|
20
|
+
import { CustomObjectService } from './services/custom-object'
|
|
21
|
+
import { DiscountCodeService } from './services/discount-code'
|
|
22
|
+
import { ExtensionServices } from './services/extension'
|
|
23
|
+
import { InventoryEntryService } from './services/inventory-entry'
|
|
24
|
+
import { MyCartService } from './services/my-cart'
|
|
25
25
|
import { MyPaymentService } from './services/my-payment'
|
|
26
|
+
import { OrderService } from './services/order'
|
|
27
|
+
import { PaymentService } from './services/payment'
|
|
28
|
+
import { ProductProjectionService } from './services/product-projection'
|
|
29
|
+
import { ProductService } from './services/product'
|
|
30
|
+
import { ProductTypeService } from './services/product-type'
|
|
31
|
+
import { ProjectService } from './services/project'
|
|
32
|
+
import { ShippingMethodService } from './services/shipping-method'
|
|
33
|
+
import { ShoppingListService } from './services/shopping-list'
|
|
34
|
+
import { StateService } from './services/state'
|
|
35
|
+
import { StoreService } from './services/store'
|
|
36
|
+
import { SubscriptionService } from './services/subscription'
|
|
37
|
+
import { TaxCategoryService } from './services/tax-category'
|
|
38
|
+
import { TypeService } from './services/type'
|
|
39
|
+
import { ZoneService } from './services/zone'
|
|
26
40
|
|
|
27
41
|
export type CommercetoolsMockOptions = {
|
|
28
42
|
validateCredentials: boolean
|
|
@@ -55,10 +69,12 @@ export class CommercetoolsMock {
|
|
|
55
69
|
api: nock.Scope | undefined
|
|
56
70
|
} = { auth: undefined, api: undefined }
|
|
57
71
|
private _services: Services
|
|
72
|
+
private _projectService?: ProjectService
|
|
58
73
|
|
|
59
74
|
constructor(options: Partial<CommercetoolsMockOptions> = {}) {
|
|
60
75
|
this.options = { ...DEFAULT_OPTIONS, ...options }
|
|
61
76
|
this._services = {}
|
|
77
|
+
this._projectService = undefined
|
|
62
78
|
|
|
63
79
|
this._storage = new InMemoryStorage()
|
|
64
80
|
this._oauth2 = new OAuth2Server({
|
|
@@ -101,9 +117,10 @@ export class CommercetoolsMock {
|
|
|
101
117
|
|
|
102
118
|
runServer(port: number = 3000, options?: AppOptions) {
|
|
103
119
|
const app = this.createApp(options)
|
|
104
|
-
app.listen(port, () => {
|
|
120
|
+
const server = app.listen(port, () => {
|
|
105
121
|
console.log(`Mock server listening at http://localhost:${port}`)
|
|
106
122
|
})
|
|
123
|
+
server.keepAliveTimeout = 60 * 1000
|
|
107
124
|
}
|
|
108
125
|
|
|
109
126
|
private createApp(options?: AppOptions): express.Express {
|
|
@@ -124,9 +141,17 @@ export class CommercetoolsMock {
|
|
|
124
141
|
app.use('/:projectKey', projectRouter)
|
|
125
142
|
}
|
|
126
143
|
|
|
144
|
+
this._projectService = new ProjectService(projectRouter, this._storage)
|
|
145
|
+
|
|
127
146
|
this._services = {
|
|
147
|
+
category: new CategoryServices(projectRouter, this._storage),
|
|
128
148
|
cart: new CartService(projectRouter, this._storage),
|
|
149
|
+
'cart-discount': new CartDiscountService(projectRouter, this._storage),
|
|
129
150
|
customer: new CustomerService(projectRouter, this._storage),
|
|
151
|
+
channel: new ChannelService(projectRouter, this._storage),
|
|
152
|
+
'customer-group': new CustomerGroupService(projectRouter, this._storage),
|
|
153
|
+
'discount-code': new DiscountCodeService(projectRouter, this._storage),
|
|
154
|
+
extension: new ExtensionServices(projectRouter, this._storage),
|
|
130
155
|
'inventory-entry': new InventoryEntryService(
|
|
131
156
|
projectRouter,
|
|
132
157
|
this._storage
|
|
@@ -137,6 +162,7 @@ export class CommercetoolsMock {
|
|
|
137
162
|
),
|
|
138
163
|
order: new OrderService(projectRouter, this._storage),
|
|
139
164
|
payment: new PaymentService(projectRouter, this._storage),
|
|
165
|
+
'my-cart': new MyCartService(projectRouter, this._storage),
|
|
140
166
|
'my-payment': new MyPaymentService(projectRouter, this._storage),
|
|
141
167
|
'shipping-method': new ShippingMethodService(
|
|
142
168
|
projectRouter,
|
|
@@ -144,10 +170,17 @@ export class CommercetoolsMock {
|
|
|
144
170
|
),
|
|
145
171
|
'product-type': new ProductTypeService(projectRouter, this._storage),
|
|
146
172
|
product: new ProductService(projectRouter, this._storage),
|
|
173
|
+
'product-projection': new ProductProjectionService(
|
|
174
|
+
projectRouter,
|
|
175
|
+
this._storage
|
|
176
|
+
),
|
|
177
|
+
'shopping-list': new ShoppingListService(projectRouter, this._storage),
|
|
147
178
|
state: new StateService(projectRouter, this._storage),
|
|
148
179
|
store: new StoreService(projectRouter, this._storage),
|
|
180
|
+
subscription: new SubscriptionService(projectRouter, this._storage),
|
|
149
181
|
'tax-category': new TaxCategoryService(projectRouter, this._storage),
|
|
150
182
|
type: new TypeService(projectRouter, this._storage),
|
|
183
|
+
zone: new ZoneService(projectRouter, this._storage),
|
|
151
184
|
}
|
|
152
185
|
|
|
153
186
|
app.use((err: Error, req: Request, resp: Response, next: NextFunction) => {
|
|
@@ -158,6 +191,7 @@ export class CommercetoolsMock {
|
|
|
158
191
|
errors: [err.info],
|
|
159
192
|
})
|
|
160
193
|
} else {
|
|
194
|
+
console.error(err)
|
|
161
195
|
return resp.status(500).send({
|
|
162
196
|
error: err.message,
|
|
163
197
|
})
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { parseFilterExpression } from './filterParser'
|
|
2
|
+
|
|
3
|
+
describe('filter parser', () => {
|
|
4
|
+
test('parses array', () => {
|
|
5
|
+
expect(
|
|
6
|
+
parseFilterExpression(['variant.sku:"123"', `variant.version="312"`])
|
|
7
|
+
).toEqual('variant(sku="123") and variant(version="312")')
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
test('parses string', () => {
|
|
11
|
+
expect(parseFilterExpression('variant.something.sku:"123"')).toEqual(
|
|
12
|
+
'variant(something(sku="123"))'
|
|
13
|
+
)
|
|
14
|
+
})
|
|
15
|
+
})
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export const parseFilterExpression = (filter: string | string[]) => {
|
|
2
|
+
if (typeof filter === 'object') {
|
|
3
|
+
const filters = filter.map(parse)
|
|
4
|
+
return filters.join(' and ')
|
|
5
|
+
}
|
|
6
|
+
return parse(filter)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const parse = (filter: string) => {
|
|
10
|
+
let parsed = filter.replace(/:/g, '=')
|
|
11
|
+
|
|
12
|
+
do {
|
|
13
|
+
parsed = parsed.replace(/(.*)\.(.*)=(.*)$/g, '$1($2=$3)')
|
|
14
|
+
} while (parsed.includes('.'))
|
|
15
|
+
|
|
16
|
+
return parsed
|
|
17
|
+
}
|
package/src/oauth/server.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import auth from 'basic-auth'
|
|
2
|
+
import bodyParser from 'body-parser'
|
|
2
3
|
import express, { NextFunction, Request, Response } from 'express'
|
|
3
4
|
import {
|
|
4
5
|
AccessDeniedError,
|
|
@@ -18,6 +19,7 @@ export class OAuth2Server {
|
|
|
18
19
|
|
|
19
20
|
createRouter() {
|
|
20
21
|
const router = express.Router()
|
|
22
|
+
router.use(bodyParser.urlencoded({ extended: true }))
|
|
21
23
|
router.post('/token', this.tokenHandler.bind(this))
|
|
22
24
|
return router
|
|
23
25
|
}
|
|
@@ -81,7 +83,7 @@ export class OAuth2Server {
|
|
|
81
83
|
)
|
|
82
84
|
}
|
|
83
85
|
|
|
84
|
-
const grantType = request.query.grant_type
|
|
86
|
+
const grantType = request.query.grant_type || request.body.grant_type
|
|
85
87
|
if (!grantType) {
|
|
86
88
|
return next(
|
|
87
89
|
new CommercetoolsError<InvalidRequestError>(
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import { RepositoryTypes } from './../types'
|
|
1
2
|
import deepEqual from 'deep-equal'
|
|
2
3
|
|
|
3
4
|
import {
|
|
4
5
|
BaseResource,
|
|
5
6
|
InvalidOperationError,
|
|
6
|
-
|
|
7
|
+
Project,
|
|
7
8
|
UpdateAction,
|
|
8
9
|
} from '@commercetools/platform-sdk'
|
|
9
10
|
import { AbstractStorage } from '../storage'
|
|
@@ -19,20 +20,50 @@ export type GetParams = {
|
|
|
19
20
|
expand?: string[]
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
export
|
|
23
|
+
export abstract class AbstractRepository {
|
|
23
24
|
protected _storage: AbstractStorage
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
} = {}
|
|
25
|
+
protected actions: Partial<
|
|
26
|
+
Record<any, (projectKey: string, resource: any, action: any) => void>
|
|
27
|
+
> = {}
|
|
28
28
|
|
|
29
29
|
constructor(storage: AbstractStorage) {
|
|
30
30
|
this._storage = storage
|
|
31
|
-
this._storage.assertStorage(this.getTypeId())
|
|
32
31
|
}
|
|
33
32
|
|
|
34
|
-
abstract
|
|
33
|
+
abstract save(projectKey: string, resource: BaseResource | Project): void
|
|
34
|
+
|
|
35
|
+
processUpdateActions(
|
|
36
|
+
projectKey: string,
|
|
37
|
+
resource: BaseResource | Project,
|
|
38
|
+
actions: UpdateAction[]
|
|
39
|
+
): BaseResource {
|
|
40
|
+
// Deep-copy
|
|
41
|
+
const modifiedResource = JSON.parse(JSON.stringify(resource))
|
|
42
|
+
|
|
43
|
+
actions.forEach(action => {
|
|
44
|
+
const updateFunc = this.actions[action.action]
|
|
45
|
+
if (!updateFunc) {
|
|
46
|
+
console.error(`No mock implemented for update action ${action.action}`)
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
updateFunc(projectKey, modifiedResource, action)
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
if (!deepEqual(modifiedResource, resource)) {
|
|
53
|
+
this.save(projectKey, modifiedResource)
|
|
54
|
+
}
|
|
55
|
+
return modifiedResource
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export abstract class AbstractResourceRepository extends AbstractRepository {
|
|
35
60
|
abstract create(projectKey: string, draft: any): BaseResource
|
|
61
|
+
abstract getTypeId(): RepositoryTypes
|
|
62
|
+
|
|
63
|
+
constructor(storage: AbstractStorage) {
|
|
64
|
+
super(storage)
|
|
65
|
+
this._storage.assertStorage(this.getTypeId())
|
|
66
|
+
}
|
|
36
67
|
|
|
37
68
|
query(projectKey: string, params: QueryParams = {}) {
|
|
38
69
|
return this._storage.query(projectKey, this.getTypeId(), {
|
|
@@ -49,6 +80,14 @@ export default abstract class AbstractRepository {
|
|
|
49
80
|
return this._storage.get(projectKey, this.getTypeId(), id, params)
|
|
50
81
|
}
|
|
51
82
|
|
|
83
|
+
getByKey(
|
|
84
|
+
projectKey: string,
|
|
85
|
+
key: string,
|
|
86
|
+
params: GetParams = {}
|
|
87
|
+
): BaseResource | null {
|
|
88
|
+
return this._storage.getByKey(projectKey, this.getTypeId(), key, params)
|
|
89
|
+
}
|
|
90
|
+
|
|
52
91
|
delete(
|
|
53
92
|
projectKey: string,
|
|
54
93
|
id: string,
|
|
@@ -58,9 +97,8 @@ export default abstract class AbstractRepository {
|
|
|
58
97
|
}
|
|
59
98
|
|
|
60
99
|
save(projectKey: string, resource: BaseResource) {
|
|
61
|
-
const typeId = this.getTypeId()
|
|
62
|
-
|
|
63
100
|
const current = this.get(projectKey, resource.id)
|
|
101
|
+
|
|
64
102
|
if (current) {
|
|
65
103
|
checkConcurrentModification(current, resource.version)
|
|
66
104
|
} else {
|
|
@@ -77,29 +115,6 @@ export default abstract class AbstractRepository {
|
|
|
77
115
|
|
|
78
116
|
// @ts-ignore
|
|
79
117
|
resource.version += 1
|
|
80
|
-
this._storage.add(projectKey,
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
processUpdateActions(
|
|
84
|
-
projectKey: string,
|
|
85
|
-
resource: BaseResource,
|
|
86
|
-
actions: UpdateAction[]
|
|
87
|
-
): BaseResource {
|
|
88
|
-
// Deep-copy
|
|
89
|
-
const modifiedResource = JSON.parse(JSON.stringify(resource))
|
|
90
|
-
|
|
91
|
-
actions.forEach(action => {
|
|
92
|
-
const updateFunc = this.actions[action.action]
|
|
93
|
-
if (!updateFunc) {
|
|
94
|
-
console.error(`No mock implemented for update action ${action.action}`)
|
|
95
|
-
return
|
|
96
|
-
}
|
|
97
|
-
updateFunc(projectKey, modifiedResource, action)
|
|
98
|
-
})
|
|
99
|
-
|
|
100
|
-
if (!deepEqual(modifiedResource, resource)) {
|
|
101
|
-
this.save(projectKey, modifiedResource)
|
|
102
|
-
}
|
|
103
|
-
return modifiedResource
|
|
118
|
+
this._storage.add(projectKey, this.getTypeId(), resource as any)
|
|
104
119
|
}
|
|
105
120
|
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CartDiscount,
|
|
3
|
+
CartDiscountChangeIsActiveAction,
|
|
4
|
+
CartDiscountChangeSortOrderAction,
|
|
5
|
+
CartDiscountDraft,
|
|
6
|
+
CartDiscountSetDescriptionAction,
|
|
7
|
+
CartDiscountSetKeyAction,
|
|
8
|
+
CartDiscountSetValidFromAction,
|
|
9
|
+
CartDiscountSetValidFromAndUntilAction,
|
|
10
|
+
CartDiscountSetValidUntilAction,
|
|
11
|
+
CartDiscountUpdateAction,
|
|
12
|
+
CartDiscountValueAbsolute,
|
|
13
|
+
CartDiscountValueDraft,
|
|
14
|
+
CartDiscountValueFixed,
|
|
15
|
+
CartDiscountValueGiftLineItem,
|
|
16
|
+
CartDiscountValueRelative,
|
|
17
|
+
ReferenceTypeId,
|
|
18
|
+
} from '@commercetools/platform-sdk'
|
|
19
|
+
import { Writable } from 'types'
|
|
20
|
+
import { getBaseResourceProperties } from '../helpers'
|
|
21
|
+
import { AbstractResourceRepository } from './abstract'
|
|
22
|
+
import { createTypedMoney } from './helpers'
|
|
23
|
+
|
|
24
|
+
export class CartDiscountRepository extends AbstractResourceRepository {
|
|
25
|
+
getTypeId(): ReferenceTypeId {
|
|
26
|
+
return 'cart-discount'
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
create(projectKey: string, draft: CartDiscountDraft): CartDiscount {
|
|
30
|
+
const resource: CartDiscount = {
|
|
31
|
+
...getBaseResourceProperties(),
|
|
32
|
+
key: draft.key,
|
|
33
|
+
description: draft.description,
|
|
34
|
+
cartPredicate: draft.cartPredicate,
|
|
35
|
+
isActive: draft.isActive || false,
|
|
36
|
+
name: draft.name,
|
|
37
|
+
references: [],
|
|
38
|
+
target: draft.target,
|
|
39
|
+
requiresDiscountCode: draft.requiresDiscountCode || false,
|
|
40
|
+
sortOrder: draft.sortOrder,
|
|
41
|
+
stackingMode: draft.stackingMode || 'Stacking',
|
|
42
|
+
validFrom: draft.validFrom,
|
|
43
|
+
validUntil: draft.validUntil,
|
|
44
|
+
value: this.transformValueDraft(draft.value),
|
|
45
|
+
}
|
|
46
|
+
this.save(projectKey, resource)
|
|
47
|
+
return resource
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
private transformValueDraft(value: CartDiscountValueDraft) {
|
|
51
|
+
switch (value.type) {
|
|
52
|
+
case 'absolute': {
|
|
53
|
+
return {
|
|
54
|
+
type: 'absolute',
|
|
55
|
+
money: value.money.map(createTypedMoney),
|
|
56
|
+
} as CartDiscountValueAbsolute
|
|
57
|
+
}
|
|
58
|
+
case 'fixed': {
|
|
59
|
+
return {
|
|
60
|
+
type: 'fixed',
|
|
61
|
+
money: value.money.map(createTypedMoney),
|
|
62
|
+
} as CartDiscountValueFixed
|
|
63
|
+
}
|
|
64
|
+
case 'giftLineItem': {
|
|
65
|
+
return {
|
|
66
|
+
...value,
|
|
67
|
+
} as CartDiscountValueGiftLineItem
|
|
68
|
+
}
|
|
69
|
+
case 'relative': {
|
|
70
|
+
return {
|
|
71
|
+
...value,
|
|
72
|
+
} as CartDiscountValueRelative
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return value
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
actions: Partial<
|
|
80
|
+
Record<
|
|
81
|
+
CartDiscountUpdateAction['action'],
|
|
82
|
+
(
|
|
83
|
+
projectKey: string,
|
|
84
|
+
resource: Writable<CartDiscount>,
|
|
85
|
+
action: any
|
|
86
|
+
) => void
|
|
87
|
+
>
|
|
88
|
+
> = {
|
|
89
|
+
setKey: (
|
|
90
|
+
projectKey: string,
|
|
91
|
+
resource: Writable<CartDiscount>,
|
|
92
|
+
{ key }: CartDiscountSetKeyAction
|
|
93
|
+
) => {
|
|
94
|
+
resource.key = key
|
|
95
|
+
},
|
|
96
|
+
setDescription: (
|
|
97
|
+
projectKey: string,
|
|
98
|
+
resource: Writable<CartDiscount>,
|
|
99
|
+
{ description }: CartDiscountSetDescriptionAction
|
|
100
|
+
) => {
|
|
101
|
+
resource.description = description
|
|
102
|
+
},
|
|
103
|
+
setValidFrom: (
|
|
104
|
+
projectKey: string,
|
|
105
|
+
resource: Writable<CartDiscount>,
|
|
106
|
+
{ validFrom }: CartDiscountSetValidFromAction
|
|
107
|
+
) => {
|
|
108
|
+
resource.validFrom = validFrom
|
|
109
|
+
},
|
|
110
|
+
setValidUntil: (
|
|
111
|
+
projectKey: string,
|
|
112
|
+
resource: Writable<CartDiscount>,
|
|
113
|
+
{ validUntil }: CartDiscountSetValidUntilAction
|
|
114
|
+
) => {
|
|
115
|
+
resource.validUntil = validUntil
|
|
116
|
+
},
|
|
117
|
+
setValidFromAndUntil: (
|
|
118
|
+
projectKey: string,
|
|
119
|
+
resource: Writable<CartDiscount>,
|
|
120
|
+
{ validFrom, validUntil }: CartDiscountSetValidFromAndUntilAction
|
|
121
|
+
) => {
|
|
122
|
+
resource.validFrom = validFrom
|
|
123
|
+
resource.validUntil = validUntil
|
|
124
|
+
},
|
|
125
|
+
changeSortOrder: (
|
|
126
|
+
projectKey: string,
|
|
127
|
+
resource: Writable<CartDiscount>,
|
|
128
|
+
{ sortOrder }: CartDiscountChangeSortOrderAction
|
|
129
|
+
) => {
|
|
130
|
+
resource.sortOrder = sortOrder
|
|
131
|
+
},
|
|
132
|
+
changeIsActive: (
|
|
133
|
+
projectKey: string,
|
|
134
|
+
resource: Writable<CartDiscount>,
|
|
135
|
+
{ isActive }: CartDiscountChangeIsActiveAction
|
|
136
|
+
) => {
|
|
137
|
+
resource.isActive = isActive
|
|
138
|
+
},
|
|
139
|
+
}
|
|
140
|
+
}
|