@labdigital/commercetools-mock 0.10.0 → 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/dist/index.d.ts +353 -188
- package/dist/index.global.js +1007 -876
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +348 -217
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +348 -217
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -2
- package/src/ctMock.ts +15 -9
- package/src/helpers.ts +1 -1
- package/src/lib/projectionSearchFilter.ts +3 -2
- package/src/priceSelector.test.ts +1 -3
- package/src/product-projection-search.ts +1 -3
- package/src/projectAPI.test.ts +7 -0
- package/src/projectAPI.ts +17 -22
- package/src/repositories/abstract.ts +31 -24
- package/src/repositories/cart-discount.ts +3 -4
- package/src/repositories/cart.ts +20 -15
- package/src/repositories/category.ts +6 -7
- package/src/repositories/channel.ts +4 -5
- package/src/repositories/custom-object.ts +9 -13
- package/src/repositories/customer-group.ts +3 -4
- package/src/repositories/customer.ts +4 -5
- package/src/repositories/discount-code.ts +4 -5
- package/src/repositories/errors.ts +1 -3
- package/src/repositories/extension.ts +7 -8
- package/src/repositories/helpers.ts +2 -2
- package/src/repositories/index.ts +19 -3
- package/src/repositories/inventory-entry.ts +4 -5
- package/src/repositories/my-order.ts +2 -2
- package/src/repositories/order-edit.ts +39 -0
- package/src/repositories/order.test.ts +16 -11
- package/src/repositories/order.ts +15 -8
- package/src/repositories/payment.ts +6 -7
- package/src/repositories/product-discount.ts +4 -24
- package/src/repositories/product-projection.ts +11 -5
- package/src/repositories/product-selection.ts +40 -0
- package/src/repositories/product-type.ts +11 -28
- package/src/repositories/product.ts +117 -10
- package/src/repositories/project.ts +9 -8
- package/src/repositories/quote-request.ts +28 -0
- package/src/repositories/quote.ts +28 -0
- package/src/repositories/review.ts +34 -0
- package/src/repositories/shipping-method.ts +10 -11
- package/src/repositories/shopping-list.ts +4 -4
- package/src/repositories/staged-quote.ts +29 -0
- package/src/repositories/standalone-price.ts +36 -0
- package/src/repositories/state.ts +15 -8
- package/src/repositories/store.ts +11 -27
- package/src/repositories/subscription.ts +3 -4
- package/src/repositories/tax-category.ts +5 -22
- package/src/repositories/type.ts +12 -13
- package/src/repositories/zone.ts +4 -5
- package/src/server.ts +4 -4
- package/src/services/abstract.ts +3 -5
- package/src/services/cart-discount.ts +1 -1
- package/src/services/cart.test.ts +1 -1
- package/src/services/cart.ts +40 -33
- package/src/services/category.ts +1 -1
- package/src/services/channel.ts +1 -1
- package/src/services/custom-object.test.ts +1 -1
- package/src/services/custom-object.ts +2 -2
- package/src/services/customer-group.ts +1 -1
- package/src/services/customer.test.ts +1 -1
- package/src/services/customer.ts +3 -3
- package/src/services/discount-code.ts +1 -1
- package/src/services/extension.ts +1 -1
- package/src/services/inventory-entry.test.ts +1 -1
- package/src/services/inventory-entry.ts +1 -1
- package/src/services/my-cart.test.ts +2 -0
- package/src/services/my-cart.ts +1 -1
- package/src/services/my-customer.ts +1 -1
- package/src/services/my-order.ts +1 -1
- package/src/services/my-payment.ts +1 -1
- package/src/services/order.test.ts +24 -20
- package/src/services/order.ts +2 -2
- package/src/services/payment.ts +1 -1
- package/src/services/product-discount.ts +1 -17
- package/src/services/product-projection.test.ts +1 -1
- package/src/services/product-projection.ts +2 -2
- package/src/services/product-type.ts +1 -17
- package/src/services/product.test.ts +63 -2
- package/src/services/product.ts +1 -1
- package/src/services/project.ts +4 -5
- package/src/services/shipping-method.ts +1 -1
- package/src/services/shopping-list.ts +1 -1
- package/src/services/state.ts +1 -1
- package/src/services/store.ts +2 -18
- package/src/services/subscription.ts +1 -1
- package/src/services/tax-category.ts +1 -17
- package/src/services/type.ts +1 -1
- package/src/services/zone.ts +1 -1
- package/src/storage/abstract.ts +82 -0
- package/src/{storage.ts → storage/in-memory.ts} +57 -118
- package/src/storage/index.ts +2 -0
- package/src/types.ts +48 -119
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.
|
|
4
|
+
"version": "0.11.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"module": "dist/commercetools--mock.esm.js",
|
|
@@ -39,12 +39,13 @@
|
|
|
39
39
|
"perplex": "^0.11.0",
|
|
40
40
|
"pratt": "^0.7.0",
|
|
41
41
|
"supertest": "^6.1.6",
|
|
42
|
+
"type-fest": "^3.1.0",
|
|
42
43
|
"uuid": "^8.3.2"
|
|
43
44
|
},
|
|
44
45
|
"devDependencies": {
|
|
45
46
|
"@babel/preset-env": "^7.18.9",
|
|
46
47
|
"@babel/preset-typescript": "^7.18.6",
|
|
47
|
-
"@commercetools/platform-sdk": "
|
|
48
|
+
"@commercetools/platform-sdk": "4.0.0",
|
|
48
49
|
"@types/basic-auth": "^1.1.3",
|
|
49
50
|
"@types/body-parser": "^1.19.2",
|
|
50
51
|
"@types/deep-equal": "^1.0.1",
|
package/src/ctMock.ts
CHANGED
|
@@ -3,7 +3,7 @@ import express, { NextFunction, Request, Response } from 'express'
|
|
|
3
3
|
import supertest from 'supertest'
|
|
4
4
|
import morgan from 'morgan'
|
|
5
5
|
import { AbstractStorage, InMemoryStorage } from './storage'
|
|
6
|
-
import {
|
|
6
|
+
import { Services } from './types'
|
|
7
7
|
import { CommercetoolsError } from './exceptions'
|
|
8
8
|
import { OAuth2Server } from './oauth/server'
|
|
9
9
|
import { ProjectAPI } from './projectAPI'
|
|
@@ -12,7 +12,7 @@ import { DEFAULT_API_HOSTNAME, DEFAULT_AUTH_HOSTNAME } from './constants'
|
|
|
12
12
|
|
|
13
13
|
// Services
|
|
14
14
|
import { ProjectService } from './services/project'
|
|
15
|
-
import { createRepositories } from './repositories'
|
|
15
|
+
import { createRepositories, RepositoryMap } from './repositories'
|
|
16
16
|
import { createServices } from './services'
|
|
17
17
|
import { ProjectRepository } from 'repositories/project'
|
|
18
18
|
|
|
@@ -46,14 +46,14 @@ export class CommercetoolsMock {
|
|
|
46
46
|
auth: nock.Scope | undefined
|
|
47
47
|
api: nock.Scope | undefined
|
|
48
48
|
} = { auth: undefined, api: undefined }
|
|
49
|
-
private _services: Services
|
|
50
|
-
private _repositories:
|
|
49
|
+
private _services: Services | null
|
|
50
|
+
private _repositories: RepositoryMap | null
|
|
51
51
|
private _projectService?: ProjectService
|
|
52
52
|
|
|
53
53
|
constructor(options: Partial<CommercetoolsMockOptions> = {}) {
|
|
54
54
|
this.options = { ...DEFAULT_OPTIONS, ...options }
|
|
55
|
-
this._services =
|
|
56
|
-
this._repositories =
|
|
55
|
+
this._services = null
|
|
56
|
+
this._repositories = null
|
|
57
57
|
this._projectService = undefined
|
|
58
58
|
|
|
59
59
|
this._storage = new InMemoryStorage()
|
|
@@ -88,9 +88,13 @@ export class CommercetoolsMock {
|
|
|
88
88
|
throw new Error('No projectKey passed and no default set')
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
+
if (this._repositories === null) {
|
|
92
|
+
throw new Error('repositories not initialized yet')
|
|
93
|
+
}
|
|
94
|
+
|
|
91
95
|
return new ProjectAPI(
|
|
92
96
|
projectKey || this.options.defaultProjectKey!,
|
|
93
|
-
this.
|
|
97
|
+
this._repositories,
|
|
94
98
|
this._storage
|
|
95
99
|
)
|
|
96
100
|
}
|
|
@@ -103,6 +107,8 @@ export class CommercetoolsMock {
|
|
|
103
107
|
}
|
|
104
108
|
|
|
105
109
|
private createApp(options?: AppOptions): express.Express {
|
|
110
|
+
this._repositories = createRepositories(this._storage)
|
|
111
|
+
|
|
106
112
|
const app = express()
|
|
107
113
|
|
|
108
114
|
const projectRouter = express.Router({ mergeParams: true })
|
|
@@ -126,12 +132,12 @@ export class CommercetoolsMock {
|
|
|
126
132
|
app.use('/:projectKey/in-store/key=:storeKey', projectRouter)
|
|
127
133
|
}
|
|
128
134
|
|
|
129
|
-
|
|
135
|
+
// Register the rest api services in the router
|
|
136
|
+
this._services = createServices(projectRouter, this._repositories)
|
|
130
137
|
this._projectService = new ProjectService(
|
|
131
138
|
projectRouter,
|
|
132
139
|
this._repositories.project as ProjectRepository
|
|
133
140
|
)
|
|
134
|
-
this._services = createServices(projectRouter, this._repositories)
|
|
135
141
|
|
|
136
142
|
app.use((err: Error, req: Request, resp: Response, next: NextFunction) => {
|
|
137
143
|
if (err instanceof CommercetoolsError) {
|
package/src/helpers.ts
CHANGED
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
import { ProductProjection, ProductVariant } from '@commercetools/platform-sdk'
|
|
6
6
|
import perplex from 'perplex'
|
|
7
7
|
import Parser from 'pratt'
|
|
8
|
-
import { nestedLookup } from '../helpers'
|
|
9
8
|
import { Writable } from '../types'
|
|
9
|
+
import { nestedLookup } from '../helpers'
|
|
10
10
|
|
|
11
11
|
type MatchFunc = (target: any) => boolean
|
|
12
12
|
|
|
@@ -99,7 +99,8 @@ const parseFilter = (filter: string): ExpressionSet => {
|
|
|
99
99
|
.nud('IDENTIFIER', 100, (t) => t.token.match)
|
|
100
100
|
.led(':', 100, ({ left, bp }) => {
|
|
101
101
|
const parsed: any = parser.parse({ terminals: [bp - 1] })
|
|
102
|
-
const expressions: RangeExpression[] | FilterExpression[] | TypeSymbol[] =
|
|
102
|
+
const expressions: RangeExpression[] | FilterExpression[] | TypeSymbol[] =
|
|
103
|
+
!Array.isArray(parsed) ? [parsed] : parsed
|
|
103
104
|
|
|
104
105
|
// Make sure we only have one type of expression (cannot mix)
|
|
105
106
|
const unique = new Set(expressions.map((expr) => expr.type))
|
|
@@ -60,7 +60,6 @@ export class ProductProjectionSearch {
|
|
|
60
60
|
let resources = this._storage
|
|
61
61
|
.all(projectKey, 'product')
|
|
62
62
|
.map((r) => this.transform(r, params.staged ?? false))
|
|
63
|
-
.filter((p): p is ProductProjection => p !== null)
|
|
64
63
|
.filter((p) => {
|
|
65
64
|
if (!params.staged ?? false) {
|
|
66
65
|
return p.published
|
|
@@ -145,9 +144,8 @@ export class ProductProjectionSearch {
|
|
|
145
144
|
}
|
|
146
145
|
}
|
|
147
146
|
|
|
148
|
-
transform(product: Product, staged: boolean): ProductProjection
|
|
147
|
+
transform(product: Product, staged: boolean): ProductProjection {
|
|
149
148
|
const obj = !staged ? product.masterData.current : product.masterData.staged
|
|
150
|
-
if (!obj) return null
|
|
151
149
|
|
|
152
150
|
return {
|
|
153
151
|
id: product.id,
|
package/src/projectAPI.ts
CHANGED
|
@@ -1,34 +1,31 @@
|
|
|
1
|
-
import { ReferenceTypeId } from '@commercetools/platform-sdk'
|
|
2
1
|
import { GetParams } from 'repositories/abstract'
|
|
3
2
|
import { getBaseResourceProperties } from './helpers'
|
|
4
3
|
import { AbstractStorage } from './storage'
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
RepositoryTypes,
|
|
8
|
-
ResourceMap,
|
|
9
|
-
Services,
|
|
10
|
-
ServiceTypes,
|
|
11
|
-
} from './types'
|
|
4
|
+
import { RepositoryMap } from './repositories'
|
|
5
|
+
import { ResourceMap, ResourceType } from './types'
|
|
12
6
|
|
|
13
7
|
export class ProjectAPI {
|
|
14
8
|
private projectKey: string
|
|
15
9
|
private _storage: AbstractStorage
|
|
16
|
-
private
|
|
10
|
+
private _repositories: RepositoryMap
|
|
17
11
|
|
|
18
12
|
constructor(
|
|
19
13
|
projectKey: string,
|
|
20
|
-
|
|
14
|
+
repositories: RepositoryMap,
|
|
21
15
|
storage: AbstractStorage
|
|
22
16
|
) {
|
|
23
17
|
this.projectKey = projectKey
|
|
24
18
|
this._storage = storage
|
|
25
|
-
this.
|
|
19
|
+
this._repositories = repositories
|
|
26
20
|
}
|
|
27
21
|
|
|
28
|
-
add
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
22
|
+
add<T extends keyof RepositoryMap & keyof ResourceMap>(
|
|
23
|
+
typeId: T,
|
|
24
|
+
resource: ResourceMap[T]
|
|
25
|
+
) {
|
|
26
|
+
const repository = this._repositories[typeId]
|
|
27
|
+
if (repository) {
|
|
28
|
+
this._storage.add(this.projectKey, typeId, {
|
|
32
29
|
...getBaseResourceProperties(),
|
|
33
30
|
...resource,
|
|
34
31
|
})
|
|
@@ -37,7 +34,7 @@ export class ProjectAPI {
|
|
|
37
34
|
}
|
|
38
35
|
}
|
|
39
36
|
|
|
40
|
-
get<RT extends
|
|
37
|
+
get<RT extends ResourceType>(
|
|
41
38
|
typeId: RT,
|
|
42
39
|
id: string,
|
|
43
40
|
params?: GetParams
|
|
@@ -51,12 +48,10 @@ export class ProjectAPI {
|
|
|
51
48
|
}
|
|
52
49
|
|
|
53
50
|
// TODO: Not sure if we want to expose this...
|
|
54
|
-
getRepository<RT extends keyof RepositoryMap>(
|
|
55
|
-
typeId
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if (service !== undefined) {
|
|
59
|
-
return service.repository as RepositoryMap[RT]
|
|
51
|
+
getRepository<RT extends keyof RepositoryMap>(typeId: RT): RepositoryMap[RT] {
|
|
52
|
+
const repository = this._repositories[typeId]
|
|
53
|
+
if (repository !== undefined) {
|
|
54
|
+
return repository as RepositoryMap[RT]
|
|
60
55
|
}
|
|
61
56
|
throw new Error('No such repository')
|
|
62
57
|
}
|
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
import { RepositoryTypes, Resource, Writable } from './../types'
|
|
2
|
-
import deepEqual from 'deep-equal'
|
|
3
|
-
|
|
4
1
|
import {
|
|
5
2
|
BaseResource,
|
|
6
3
|
Project,
|
|
7
4
|
ResourceNotFoundError,
|
|
8
5
|
UpdateAction,
|
|
9
6
|
} from '@commercetools/platform-sdk'
|
|
10
|
-
import
|
|
11
|
-
import { checkConcurrentModification } from './errors'
|
|
7
|
+
import deepEqual from 'deep-equal'
|
|
12
8
|
import { CommercetoolsError } from '../exceptions'
|
|
13
9
|
import { cloneObject } from '../helpers'
|
|
10
|
+
import { AbstractStorage } from '../storage'
|
|
11
|
+
import { ResourceMap, ResourceType, ShallowWritable } from './../types'
|
|
12
|
+
import { checkConcurrentModification } from './errors'
|
|
14
13
|
|
|
15
14
|
export type QueryParams = {
|
|
16
15
|
expand?: string[]
|
|
@@ -27,7 +26,8 @@ export type RepositoryContext = {
|
|
|
27
26
|
projectKey: string
|
|
28
27
|
storeKey?: string
|
|
29
28
|
}
|
|
30
|
-
|
|
29
|
+
|
|
30
|
+
export abstract class AbstractRepository<R extends BaseResource | Project> {
|
|
31
31
|
protected _storage: AbstractStorage
|
|
32
32
|
protected actions: Partial<
|
|
33
33
|
Record<
|
|
@@ -40,22 +40,22 @@ export abstract class AbstractRepository {
|
|
|
40
40
|
this._storage = storage
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
abstract saveNew({ projectKey }: RepositoryContext, resource:
|
|
43
|
+
abstract saveNew({ projectKey }: RepositoryContext, resource: R): void
|
|
44
44
|
|
|
45
45
|
abstract saveUpdate(
|
|
46
46
|
{ projectKey }: RepositoryContext,
|
|
47
47
|
version: number,
|
|
48
|
-
resource:
|
|
48
|
+
resource: R
|
|
49
49
|
): void
|
|
50
50
|
|
|
51
|
-
processUpdateActions
|
|
51
|
+
processUpdateActions(
|
|
52
52
|
context: RepositoryContext,
|
|
53
|
-
resource:
|
|
53
|
+
resource: R,
|
|
54
54
|
version: number,
|
|
55
55
|
actions: UpdateAction[]
|
|
56
|
-
):
|
|
56
|
+
): R {
|
|
57
57
|
// Deep-copy
|
|
58
|
-
const updatedResource = cloneObject(resource) as
|
|
58
|
+
const updatedResource = cloneObject(resource) as ShallowWritable<R>
|
|
59
59
|
const identifier = (resource as BaseResource).id
|
|
60
60
|
? (resource as BaseResource).id
|
|
61
61
|
: (resource as Project).key
|
|
@@ -96,22 +96,26 @@ export abstract class AbstractRepository {
|
|
|
96
96
|
if (!result) {
|
|
97
97
|
throw new Error('invalid post process action')
|
|
98
98
|
}
|
|
99
|
-
return result
|
|
99
|
+
return result
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
postProcessResource
|
|
103
|
-
return resource
|
|
104
|
-
}
|
|
102
|
+
abstract postProcessResource(resource: any): any
|
|
105
103
|
}
|
|
106
104
|
|
|
107
|
-
export abstract class AbstractResourceRepository
|
|
108
|
-
|
|
109
|
-
|
|
105
|
+
export abstract class AbstractResourceRepository<
|
|
106
|
+
T extends ResourceType
|
|
107
|
+
> extends AbstractRepository<ResourceMap[T]> {
|
|
108
|
+
abstract create(context: RepositoryContext, draft: any): ResourceMap[T]
|
|
109
|
+
abstract getTypeId(): T
|
|
110
110
|
|
|
111
111
|
constructor(storage: AbstractStorage) {
|
|
112
112
|
super(storage)
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
+
postProcessResource(resource: ResourceMap[T]): ResourceMap[T] {
|
|
116
|
+
return resource
|
|
117
|
+
}
|
|
118
|
+
|
|
115
119
|
query(context: RepositoryContext, params: QueryParams = {}) {
|
|
116
120
|
const result = this._storage.query(context.projectKey, this.getTypeId(), {
|
|
117
121
|
expand: params.expand,
|
|
@@ -129,7 +133,7 @@ export abstract class AbstractResourceRepository extends AbstractRepository {
|
|
|
129
133
|
context: RepositoryContext,
|
|
130
134
|
id: string,
|
|
131
135
|
params: GetParams = {}
|
|
132
|
-
):
|
|
136
|
+
): ResourceMap[T] | null {
|
|
133
137
|
const resource = this._storage.get(
|
|
134
138
|
context.projectKey,
|
|
135
139
|
this.getTypeId(),
|
|
@@ -143,7 +147,7 @@ export abstract class AbstractResourceRepository extends AbstractRepository {
|
|
|
143
147
|
context: RepositoryContext,
|
|
144
148
|
key: string,
|
|
145
149
|
params: GetParams = {}
|
|
146
|
-
):
|
|
150
|
+
): ResourceMap[T] | null {
|
|
147
151
|
const resource = this._storage.getByKey(
|
|
148
152
|
context.projectKey,
|
|
149
153
|
this.getTypeId(),
|
|
@@ -157,7 +161,7 @@ export abstract class AbstractResourceRepository extends AbstractRepository {
|
|
|
157
161
|
context: RepositoryContext,
|
|
158
162
|
id: string,
|
|
159
163
|
params: GetParams = {}
|
|
160
|
-
):
|
|
164
|
+
): ResourceMap[T] | null {
|
|
161
165
|
const resource = this._storage.delete(
|
|
162
166
|
context.projectKey,
|
|
163
167
|
this.getTypeId(),
|
|
@@ -167,7 +171,10 @@ export abstract class AbstractResourceRepository extends AbstractRepository {
|
|
|
167
171
|
return resource ? this.postProcessResource(resource) : null
|
|
168
172
|
}
|
|
169
173
|
|
|
170
|
-
saveNew(
|
|
174
|
+
saveNew(
|
|
175
|
+
context: RepositoryContext,
|
|
176
|
+
resource: ShallowWritable<ResourceMap[T]>
|
|
177
|
+
) {
|
|
171
178
|
resource.version = 1
|
|
172
179
|
this._storage.add(context.projectKey, this.getTypeId(), resource as any)
|
|
173
180
|
}
|
|
@@ -175,7 +182,7 @@ export abstract class AbstractResourceRepository extends AbstractRepository {
|
|
|
175
182
|
saveUpdate(
|
|
176
183
|
context: RepositoryContext,
|
|
177
184
|
version: number,
|
|
178
|
-
resource:
|
|
185
|
+
resource: ShallowWritable<ResourceMap[T]>
|
|
179
186
|
) {
|
|
180
187
|
// Check if the resource still exists.
|
|
181
188
|
const current = this._storage.get(
|
|
@@ -14,16 +14,15 @@ import {
|
|
|
14
14
|
CartDiscountValueFixed,
|
|
15
15
|
CartDiscountValueGiftLineItem,
|
|
16
16
|
CartDiscountValueRelative,
|
|
17
|
-
ReferenceTypeId,
|
|
18
17
|
} from '@commercetools/platform-sdk'
|
|
19
18
|
import { Writable } from 'types'
|
|
20
19
|
import { getBaseResourceProperties } from '../helpers'
|
|
21
20
|
import { AbstractResourceRepository, RepositoryContext } from './abstract'
|
|
22
21
|
import { createTypedMoney } from './helpers'
|
|
23
22
|
|
|
24
|
-
export class CartDiscountRepository extends AbstractResourceRepository {
|
|
25
|
-
getTypeId()
|
|
26
|
-
return 'cart-discount'
|
|
23
|
+
export class CartDiscountRepository extends AbstractResourceRepository<'cart-discount'> {
|
|
24
|
+
getTypeId() {
|
|
25
|
+
return 'cart-discount' as const
|
|
27
26
|
}
|
|
28
27
|
|
|
29
28
|
create(context: RepositoryContext, draft: CartDiscountDraft): CartDiscount {
|
package/src/repositories/cart.ts
CHANGED
|
@@ -18,18 +18,17 @@ import {
|
|
|
18
18
|
Product,
|
|
19
19
|
ProductPagedQueryResponse,
|
|
20
20
|
ProductVariant,
|
|
21
|
-
ReferenceTypeId,
|
|
22
21
|
} from '@commercetools/platform-sdk'
|
|
23
22
|
import { v4 as uuidv4 } from 'uuid'
|
|
23
|
+
import { CommercetoolsError } from '../exceptions'
|
|
24
24
|
import { getBaseResourceProperties } from '../helpers'
|
|
25
|
+
import { Writable } from '../types'
|
|
25
26
|
import { AbstractResourceRepository, RepositoryContext } from './abstract'
|
|
26
27
|
import { createCustomFields } from './helpers'
|
|
27
|
-
import { Writable } from '../types'
|
|
28
|
-
import { CommercetoolsError } from '../exceptions'
|
|
29
28
|
|
|
30
|
-
export class CartRepository extends AbstractResourceRepository {
|
|
31
|
-
getTypeId()
|
|
32
|
-
return 'cart'
|
|
29
|
+
export class CartRepository extends AbstractResourceRepository<'cart'> {
|
|
30
|
+
getTypeId() {
|
|
31
|
+
return 'cart' as const
|
|
33
32
|
}
|
|
34
33
|
|
|
35
34
|
create(context: RepositoryContext, draft: CartDraft): Cart {
|
|
@@ -46,21 +45,23 @@ export class CartRepository extends AbstractResourceRepository {
|
|
|
46
45
|
const resource: Writable<Cart> = {
|
|
47
46
|
...getBaseResourceProperties(),
|
|
48
47
|
cartState: 'Active',
|
|
49
|
-
|
|
48
|
+
country: draft.country,
|
|
50
49
|
customLineItems: [],
|
|
50
|
+
lineItems,
|
|
51
|
+
locale: draft.locale,
|
|
52
|
+
taxCalculationMode: draft.taxCalculationMode ?? 'LineItemLevel',
|
|
53
|
+
taxMode: draft.taxMode ?? 'Platform',
|
|
54
|
+
taxRoundingMode: draft.taxRoundingMode ?? 'HalfEven',
|
|
51
55
|
totalPrice: {
|
|
52
56
|
type: 'centPrecision',
|
|
53
57
|
centAmount: 0,
|
|
54
58
|
currencyCode: draft.currency,
|
|
55
59
|
fractionDigits: 0,
|
|
56
60
|
},
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
taxCalculationMode: draft.taxCalculationMode ?? 'LineItemLevel',
|
|
60
|
-
refusedGifts: [],
|
|
61
|
-
locale: draft.locale,
|
|
62
|
-
country: draft.country,
|
|
61
|
+
shippingMode: 'Single',
|
|
62
|
+
shipping: [],
|
|
63
63
|
origin: draft.origin ?? 'Customer',
|
|
64
|
+
refusedGifts: [],
|
|
64
65
|
custom: createCustomFields(
|
|
65
66
|
draft.custom,
|
|
66
67
|
context.projectKey,
|
|
@@ -181,11 +182,13 @@ export class CartRepository extends AbstractResourceRepository {
|
|
|
181
182
|
id: uuidv4(),
|
|
182
183
|
productId: product.id,
|
|
183
184
|
productKey: product.key,
|
|
184
|
-
name: product.masterData.current.name,
|
|
185
185
|
productSlug: product.masterData.current.slug,
|
|
186
186
|
productType: product.productType,
|
|
187
|
+
name: product.masterData.current.name,
|
|
187
188
|
variant,
|
|
188
189
|
price: price,
|
|
190
|
+
taxedPricePortions: [],
|
|
191
|
+
perMethodTaxRate: [],
|
|
189
192
|
totalPrice: {
|
|
190
193
|
...price.value,
|
|
191
194
|
centAmount: price.value.centAmount * quantity,
|
|
@@ -403,15 +406,17 @@ export class CartRepository extends AbstractResourceRepository {
|
|
|
403
406
|
id: uuidv4(),
|
|
404
407
|
productId: product.id,
|
|
405
408
|
productKey: product.key,
|
|
406
|
-
name: product.masterData.current.name,
|
|
407
409
|
productSlug: product.masterData.current.slug,
|
|
408
410
|
productType: product.productType,
|
|
411
|
+
name: product.masterData.current.name,
|
|
409
412
|
variant,
|
|
410
413
|
price: price,
|
|
411
414
|
totalPrice: {
|
|
412
415
|
...price.value,
|
|
413
416
|
centAmount: price.value.centAmount * quant,
|
|
414
417
|
},
|
|
418
|
+
taxedPricePortions: [],
|
|
419
|
+
perMethodTaxRate: [],
|
|
415
420
|
quantity: quant,
|
|
416
421
|
discountedPricePerQuantity: [],
|
|
417
422
|
lineItemMode: 'Standard',
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { v4 as uuidv4 } from 'uuid'
|
|
2
1
|
import {
|
|
3
2
|
Category,
|
|
4
3
|
CategoryChangeAssetNameAction,
|
|
@@ -13,16 +12,16 @@ import {
|
|
|
13
12
|
CategorySetMetaDescriptionAction,
|
|
14
13
|
CategorySetMetaKeywordsAction,
|
|
15
14
|
CategorySetMetaTitleAction,
|
|
16
|
-
ReferenceTypeId,
|
|
17
15
|
} from '@commercetools/platform-sdk'
|
|
18
|
-
import {
|
|
16
|
+
import { v4 as uuidv4 } from 'uuid'
|
|
19
17
|
import { getBaseResourceProperties } from '../helpers'
|
|
20
|
-
import {
|
|
18
|
+
import { Writable } from '../types'
|
|
21
19
|
import { AbstractResourceRepository, RepositoryContext } from './abstract'
|
|
20
|
+
import { createCustomFields } from './helpers'
|
|
22
21
|
|
|
23
|
-
export class CategoryRepository extends AbstractResourceRepository {
|
|
24
|
-
getTypeId()
|
|
25
|
-
return 'category'
|
|
22
|
+
export class CategoryRepository extends AbstractResourceRepository<'category'> {
|
|
23
|
+
getTypeId() {
|
|
24
|
+
return 'category' as const
|
|
26
25
|
}
|
|
27
26
|
|
|
28
27
|
create(context: RepositoryContext, draft: CategoryDraft): Category {
|
|
@@ -9,16 +9,15 @@ import {
|
|
|
9
9
|
ChannelSetCustomTypeAction,
|
|
10
10
|
ChannelSetGeoLocationAction,
|
|
11
11
|
ChannelUpdateAction,
|
|
12
|
-
ReferenceTypeId,
|
|
13
12
|
} from '@commercetools/platform-sdk'
|
|
14
|
-
import { Writable } from 'types'
|
|
15
13
|
import { getBaseResourceProperties } from '../helpers'
|
|
14
|
+
import { Writable } from '../types'
|
|
16
15
|
import { AbstractResourceRepository, RepositoryContext } from './abstract'
|
|
17
16
|
import { createAddress, createCustomFields } from './helpers'
|
|
18
17
|
|
|
19
|
-
export class ChannelRepository extends AbstractResourceRepository {
|
|
20
|
-
getTypeId()
|
|
21
|
-
return 'channel'
|
|
18
|
+
export class ChannelRepository extends AbstractResourceRepository<'channel'> {
|
|
19
|
+
getTypeId() {
|
|
20
|
+
return 'channel' as const
|
|
22
21
|
}
|
|
23
22
|
|
|
24
23
|
create(context: RepositoryContext, draft: ChannelDraft): Channel {
|
|
@@ -2,17 +2,16 @@ import {
|
|
|
2
2
|
CustomObject,
|
|
3
3
|
CustomObjectDraft,
|
|
4
4
|
InvalidOperationError,
|
|
5
|
-
ReferenceTypeId,
|
|
6
5
|
} from '@commercetools/platform-sdk'
|
|
7
|
-
import { checkConcurrentModification } from './errors'
|
|
8
|
-
import { AbstractResourceRepository, RepositoryContext } from './abstract'
|
|
9
|
-
import { Writable } from '../types'
|
|
10
|
-
import { cloneObject, getBaseResourceProperties } from '../helpers'
|
|
11
6
|
import { CommercetoolsError } from '../exceptions'
|
|
7
|
+
import { cloneObject, getBaseResourceProperties } from '../helpers'
|
|
8
|
+
import { Writable } from '../types'
|
|
9
|
+
import { AbstractResourceRepository, RepositoryContext } from './abstract'
|
|
10
|
+
import { checkConcurrentModification } from './errors'
|
|
12
11
|
|
|
13
|
-
export class CustomObjectRepository extends AbstractResourceRepository {
|
|
14
|
-
getTypeId()
|
|
15
|
-
return 'key-value-document'
|
|
12
|
+
export class CustomObjectRepository extends AbstractResourceRepository<'key-value-document'> {
|
|
13
|
+
getTypeId() {
|
|
14
|
+
return 'key-value-document' as const
|
|
16
15
|
}
|
|
17
16
|
|
|
18
17
|
create(
|
|
@@ -34,7 +33,7 @@ export class CustomObjectRepository extends AbstractResourceRepository {
|
|
|
34
33
|
}
|
|
35
34
|
|
|
36
35
|
if (draft.value !== current.value) {
|
|
37
|
-
const updated = cloneObject(current)
|
|
36
|
+
const updated = cloneObject(current) as Writable<CustomObject>
|
|
38
37
|
updated.value = draft.value
|
|
39
38
|
updated.version += 1
|
|
40
39
|
this.saveUpdate(context, draft.version, updated)
|
|
@@ -70,10 +69,7 @@ export class CustomObjectRepository extends AbstractResourceRepository {
|
|
|
70
69
|
container: string,
|
|
71
70
|
key: string
|
|
72
71
|
) {
|
|
73
|
-
const items = this._storage.all(
|
|
74
|
-
context.projectKey,
|
|
75
|
-
this.getTypeId()
|
|
76
|
-
) as Array<CustomObject>
|
|
72
|
+
const items = this._storage.all(context.projectKey, this.getTypeId())
|
|
77
73
|
return items.find(
|
|
78
74
|
(item) => item.container === container && item.key === key
|
|
79
75
|
)
|
|
@@ -5,16 +5,15 @@ import {
|
|
|
5
5
|
CustomerGroupSetCustomFieldAction,
|
|
6
6
|
CustomerGroupSetCustomTypeAction,
|
|
7
7
|
CustomerGroupSetKeyAction,
|
|
8
|
-
ReferenceTypeId,
|
|
9
8
|
} from '@commercetools/platform-sdk'
|
|
10
9
|
import { Writable } from 'types'
|
|
11
10
|
import { getBaseResourceProperties } from '../helpers'
|
|
12
11
|
import { AbstractResourceRepository, RepositoryContext } from './abstract'
|
|
13
12
|
import { createCustomFields } from './helpers'
|
|
14
13
|
|
|
15
|
-
export class CustomerGroupRepository extends AbstractResourceRepository {
|
|
16
|
-
getTypeId()
|
|
17
|
-
return 'customer'
|
|
14
|
+
export class CustomerGroupRepository extends AbstractResourceRepository<'customer-group'> {
|
|
15
|
+
getTypeId() {
|
|
16
|
+
return 'customer-group' as const
|
|
18
17
|
}
|
|
19
18
|
create(context: RepositoryContext, draft: CustomerGroupDraft): CustomerGroup {
|
|
20
19
|
const resource: CustomerGroup = {
|
|
@@ -5,16 +5,15 @@ import {
|
|
|
5
5
|
CustomerSetAuthenticationModeAction,
|
|
6
6
|
InvalidInputError,
|
|
7
7
|
InvalidJsonInputError,
|
|
8
|
-
ReferenceTypeId,
|
|
9
8
|
} from '@commercetools/platform-sdk'
|
|
10
9
|
import { Writable } from 'types'
|
|
10
|
+
import { CommercetoolsError } from '../exceptions'
|
|
11
11
|
import { getBaseResourceProperties } from '../helpers'
|
|
12
12
|
import { AbstractResourceRepository, RepositoryContext } from './abstract'
|
|
13
|
-
import { CommercetoolsError } from '../exceptions'
|
|
14
13
|
|
|
15
|
-
export class CustomerRepository extends AbstractResourceRepository {
|
|
16
|
-
getTypeId()
|
|
17
|
-
return 'customer'
|
|
14
|
+
export class CustomerRepository extends AbstractResourceRepository<'customer'> {
|
|
15
|
+
getTypeId() {
|
|
16
|
+
return 'customer' as const
|
|
18
17
|
}
|
|
19
18
|
|
|
20
19
|
create(context: RepositoryContext, draft: CustomerDraft): Customer {
|
|
@@ -15,16 +15,15 @@ import {
|
|
|
15
15
|
DiscountCodeSetValidFromAndUntilAction,
|
|
16
16
|
DiscountCodeSetValidUntilAction,
|
|
17
17
|
DiscountCodeUpdateAction,
|
|
18
|
-
ReferenceTypeId,
|
|
19
18
|
} from '@commercetools/platform-sdk'
|
|
20
|
-
import { Writable } from 'types'
|
|
21
19
|
import { getBaseResourceProperties } from '../helpers'
|
|
20
|
+
import { Writable } from '../types'
|
|
22
21
|
import { AbstractResourceRepository, RepositoryContext } from './abstract'
|
|
23
22
|
import { createCustomFields } from './helpers'
|
|
24
23
|
|
|
25
|
-
export class DiscountCodeRepository extends AbstractResourceRepository {
|
|
26
|
-
getTypeId()
|
|
27
|
-
return '
|
|
24
|
+
export class DiscountCodeRepository extends AbstractResourceRepository<'discount-code'> {
|
|
25
|
+
getTypeId() {
|
|
26
|
+
return 'discount-code' as const
|
|
28
27
|
}
|
|
29
28
|
|
|
30
29
|
create(context: RepositoryContext, draft: DiscountCodeDraft): DiscountCode {
|