@labdigital/commercetools-mock 1.6.2 → 1.7.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.cjs +63 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +10 -2
- package/dist/index.d.ts +10 -2
- package/dist/index.js +63 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/repositories/associate-role.ts +94 -3
- package/src/services/associate-roles.test.ts +42 -0
- package/src/services/associate-roles.ts +17 -0
- package/src/services/index.ts +2 -0
package/package.json
CHANGED
|
@@ -1,14 +1,105 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
AssociateRole,
|
|
3
|
+
AssociateRoleAddPermissionAction,
|
|
4
|
+
AssociateRoleChangeBuyerAssignableAction,
|
|
5
|
+
AssociateRoleDraft,
|
|
6
|
+
AssociateRoleRemovePermissionAction,
|
|
7
|
+
AssociateRoleSetCustomFieldAction,
|
|
8
|
+
AssociateRoleSetNameAction,
|
|
9
|
+
AssociateRoleSetPermissionsAction,
|
|
10
|
+
} from '@commercetools/platform-sdk'
|
|
2
11
|
import {
|
|
3
12
|
AbstractResourceRepository,
|
|
4
13
|
type RepositoryContext,
|
|
5
14
|
} from './abstract.js'
|
|
15
|
+
import { getBaseResourceProperties } from '../helpers.js'
|
|
16
|
+
import { createCustomFields } from './helpers.js'
|
|
17
|
+
import { Writable } from '../types.js'
|
|
6
18
|
|
|
7
19
|
export class AssociateRoleRepository extends AbstractResourceRepository<'associate-role'> {
|
|
8
20
|
getTypeId() {
|
|
9
21
|
return 'associate-role' as const
|
|
10
22
|
}
|
|
11
|
-
|
|
12
|
-
|
|
23
|
+
|
|
24
|
+
create(context: RepositoryContext, draft: AssociateRoleDraft): AssociateRole {
|
|
25
|
+
const resource: AssociateRole = {
|
|
26
|
+
...getBaseResourceProperties(),
|
|
27
|
+
key: draft.key,
|
|
28
|
+
name: draft.name,
|
|
29
|
+
buyerAssignable: draft.buyerAssignable || false,
|
|
30
|
+
permissions: draft.permissions || [],
|
|
31
|
+
custom: createCustomFields(
|
|
32
|
+
draft.custom,
|
|
33
|
+
context.projectKey,
|
|
34
|
+
this._storage
|
|
35
|
+
),
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
this.saveNew(context, resource)
|
|
39
|
+
|
|
40
|
+
return resource
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
actions = {
|
|
44
|
+
setName: (
|
|
45
|
+
context: RepositoryContext,
|
|
46
|
+
resource: Writable<AssociateRole>,
|
|
47
|
+
{ name }: AssociateRoleSetNameAction
|
|
48
|
+
) => {
|
|
49
|
+
resource.name = name
|
|
50
|
+
},
|
|
51
|
+
setPermissions: (
|
|
52
|
+
context: RepositoryContext,
|
|
53
|
+
resource: Writable<AssociateRole>,
|
|
54
|
+
{ permissions }: AssociateRoleSetPermissionsAction
|
|
55
|
+
) => {
|
|
56
|
+
resource.permissions = permissions || []
|
|
57
|
+
},
|
|
58
|
+
setBuyerAssignable: (
|
|
59
|
+
context: RepositoryContext,
|
|
60
|
+
resource: Writable<AssociateRole>,
|
|
61
|
+
{ buyerAssignable }: AssociateRoleChangeBuyerAssignableAction
|
|
62
|
+
) => {
|
|
63
|
+
resource.buyerAssignable = buyerAssignable
|
|
64
|
+
},
|
|
65
|
+
setCustomFields: (
|
|
66
|
+
context: RepositoryContext,
|
|
67
|
+
resource: Writable<AssociateRole>,
|
|
68
|
+
{ name, value }: AssociateRoleSetCustomFieldAction
|
|
69
|
+
) => {
|
|
70
|
+
if (!resource.custom) {
|
|
71
|
+
return
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (value === null) {
|
|
75
|
+
delete resource.custom.fields[name]
|
|
76
|
+
} else {
|
|
77
|
+
resource.custom.fields[name] = value
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
addPermission: (
|
|
81
|
+
context: RepositoryContext,
|
|
82
|
+
resource: Writable<AssociateRole>,
|
|
83
|
+
{ permission }: AssociateRoleAddPermissionAction
|
|
84
|
+
) => {
|
|
85
|
+
if (!resource.permissions) {
|
|
86
|
+
resource.permissions = [permission]
|
|
87
|
+
} else {
|
|
88
|
+
resource.permissions.push(permission)
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
removePermission: (
|
|
92
|
+
context: RepositoryContext,
|
|
93
|
+
resource: Writable<AssociateRole>,
|
|
94
|
+
{ permission }: AssociateRoleRemovePermissionAction
|
|
95
|
+
) => {
|
|
96
|
+
if (!resource.permissions) {
|
|
97
|
+
return
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
resource.permissions = resource.permissions.filter((p) => {
|
|
101
|
+
p !== permission
|
|
102
|
+
})
|
|
103
|
+
},
|
|
13
104
|
}
|
|
14
105
|
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { CommercetoolsMock } from '../ctMock.js'
|
|
2
|
+
import { AssociateRole } from '@commercetools/platform-sdk'
|
|
3
|
+
import supertest from 'supertest'
|
|
4
|
+
import { beforeEach, afterEach, describe, expect, test } from 'vitest'
|
|
5
|
+
|
|
6
|
+
describe('Associate roles query', () => {
|
|
7
|
+
const ctMock = new CommercetoolsMock()
|
|
8
|
+
let associateRole: AssociateRole | undefined
|
|
9
|
+
|
|
10
|
+
beforeEach(async () => {
|
|
11
|
+
const response = await supertest(ctMock.app)
|
|
12
|
+
.post('/dummy/associate-roles')
|
|
13
|
+
.send({
|
|
14
|
+
name: 'example-role',
|
|
15
|
+
buyerAssignable: false,
|
|
16
|
+
key: 'example-role-associate-role',
|
|
17
|
+
permissions: ['ViewMyQuotes', 'ViewMyOrders', 'ViewMyCarts'],
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
expect(response.status).toBe(201)
|
|
21
|
+
|
|
22
|
+
associateRole = response.body as AssociateRole
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
afterEach(() => {
|
|
26
|
+
ctMock.clear()
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
test('no filter', async () => {
|
|
30
|
+
const response = await supertest(ctMock.app)
|
|
31
|
+
.get('/dummy/associate-roles')
|
|
32
|
+
.query('{}')
|
|
33
|
+
.send()
|
|
34
|
+
|
|
35
|
+
expect(response.status).toBe(200)
|
|
36
|
+
expect(response.body.count).toBe(1)
|
|
37
|
+
|
|
38
|
+
const associateRole = response.body.results[0] as AssociateRole
|
|
39
|
+
|
|
40
|
+
expect(associateRole.key).toBe('example-role-associate-role')
|
|
41
|
+
})
|
|
42
|
+
})
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Router } from 'express'
|
|
2
|
+
import { AssociateRoleRepository } from '../repositories/associate-role.js'
|
|
3
|
+
import AbstractService from './abstract.js'
|
|
4
|
+
|
|
5
|
+
export class AssociateRoleServices extends AbstractService {
|
|
6
|
+
public repository: AssociateRoleRepository
|
|
7
|
+
|
|
8
|
+
constructor(parent: Router, repository: AssociateRoleRepository) {
|
|
9
|
+
super(parent)
|
|
10
|
+
|
|
11
|
+
this.repository = repository
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
protected getBasePath(): string {
|
|
15
|
+
return 'associate-roles'
|
|
16
|
+
}
|
|
17
|
+
}
|
package/src/services/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AssociateRoleServices } from './associate-roles'
|
|
1
2
|
import { CartService } from './cart.js'
|
|
2
3
|
import { CartDiscountService } from './cart-discount.js'
|
|
3
4
|
import { CategoryServices } from './category.js'
|
|
@@ -30,6 +31,7 @@ import { ZoneService } from './zone.js'
|
|
|
30
31
|
import { AttributeGroupService } from './attribute-group.js'
|
|
31
32
|
|
|
32
33
|
export const createServices = (router: any, repos: any) => ({
|
|
34
|
+
'associate-role': new AssociateRoleServices(router, repos['associate-role']),
|
|
33
35
|
category: new CategoryServices(router, repos['category']),
|
|
34
36
|
cart: new CartService(router, repos['cart'], repos['order']),
|
|
35
37
|
'cart-discount': new CartDiscountService(router, repos['cart-discount']),
|