@things-factory/biz-base 8.0.0-beta.9 → 8.0.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-server/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/server/index.ts +3 -0
- package/server/middlewares/index.ts +0 -0
- package/server/migrations/index.ts +9 -0
- package/server/service/biz-option/biz-option-mutation.ts +65 -0
- package/server/service/biz-option/biz-option-query.ts +30 -0
- package/server/service/biz-option/biz-option-types.ts +44 -0
- package/server/service/biz-option/biz-option.ts +70 -0
- package/server/service/biz-option/index.ts +6 -0
- package/server/service/biz-option-detail/biz-option-detail-mutation.ts +59 -0
- package/server/service/biz-option-detail/biz-option-detail-query.ts +27 -0
- package/server/service/biz-option-detail/biz-option-detail-types.ts +38 -0
- package/server/service/biz-option-detail/biz-option-detail.ts +62 -0
- package/server/service/biz-option-detail/index.ts +6 -0
- package/server/service/bizplace/bizplace-mutation.ts +144 -0
- package/server/service/bizplace/bizplace-query.ts +236 -0
- package/server/service/bizplace/bizplace-types.ts +89 -0
- package/server/service/bizplace/bizplace.ts +108 -0
- package/server/service/bizplace/index.ts +6 -0
- package/server/service/company/company-mutation.ts +122 -0
- package/server/service/company/company-query.ts +70 -0
- package/server/service/company/company-types.ts +68 -0
- package/server/service/company/company.ts +105 -0
- package/server/service/company/index.ts +6 -0
- package/server/service/contact-point/contact-point-mutation.ts +165 -0
- package/server/service/contact-point/contact-point-query.ts +43 -0
- package/server/service/contact-point/contact-point-types.ts +152 -0
- package/server/service/contact-point/contact-point.ts +132 -0
- package/server/service/contact-point/index.ts +6 -0
- package/server/service/index.ts +46 -0
- package/server/service/worker/index.ts +6 -0
- package/server/service/worker/worker-mutation.ts +111 -0
- package/server/service/worker/worker-query.ts +37 -0
- package/server/service/worker/worker-types.ts +50 -0
- package/server/service/worker/worker.ts +58 -0
- package/server/utils/get-permitted-bizplaces.ts +342 -0
- package/server/utils/index.ts +1 -0
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { Arg, Args, Ctx, Directive, Query, Resolver } from 'type-graphql'
|
|
2
|
+
import { EntityManager, FindManyOptions, In, Not, Repository } from 'typeorm'
|
|
3
|
+
|
|
4
|
+
import { User, UserList } from '@things-factory/auth-base'
|
|
5
|
+
import { convertListParams, getRepository, ListParam } from '@things-factory/shell'
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
getMyBizplace,
|
|
9
|
+
getPartnerBizplaceIds,
|
|
10
|
+
getPartnersBizplaces,
|
|
11
|
+
getPartnersCompanyBizplaces,
|
|
12
|
+
getPermittedBizplaceIds,
|
|
13
|
+
getUserBizplaces
|
|
14
|
+
} from '../../utils'
|
|
15
|
+
import { Bizplace } from './bizplace'
|
|
16
|
+
import { BizplaceList } from './bizplace-types'
|
|
17
|
+
|
|
18
|
+
@Resolver(Bizplace)
|
|
19
|
+
export class BizplaceQuery {
|
|
20
|
+
@Directive('@privilege(category: "bizplace", privilege: "query")')
|
|
21
|
+
@Query(returns => [Bizplace])
|
|
22
|
+
async customerBizplaces(@Ctx() context: ResolverContext): Promise<Bizplace[]> {
|
|
23
|
+
const { domain, user } = context.state
|
|
24
|
+
return await getRepository(Bizplace).find({
|
|
25
|
+
where: {
|
|
26
|
+
domain: { id: domain.id },
|
|
27
|
+
id: Not(In([await getMyBizplace(domain, user)]))
|
|
28
|
+
},
|
|
29
|
+
relations: ['domain', 'creator', 'updater']
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
@Directive('@privilege(category: "bizplace", privilege: "query", domainOwnerGranted: true)')
|
|
34
|
+
@Query(returns => Bizplace)
|
|
35
|
+
async businessBizplace(@Ctx() context: ResolverContext): Promise<Bizplace> {
|
|
36
|
+
const { domain, user } = context.state
|
|
37
|
+
const myBizplace = await getMyBizplace(domain, user)
|
|
38
|
+
return await getRepository(Bizplace).findOne({
|
|
39
|
+
where: { id: myBizplace.id },
|
|
40
|
+
relations: ['company', 'domain', 'users', 'creator', 'updater']
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/* TODO make it work */
|
|
45
|
+
@Directive('@privilege(category: "bizplace", privilege: "query", domainOwnerGranted: true)')
|
|
46
|
+
@Query(returns => [Bizplace])
|
|
47
|
+
async userBizplaces(@Arg('email') email: string, @Ctx() context: ResolverContext): Promise<Bizplace[]> {
|
|
48
|
+
const { domain } = context.state
|
|
49
|
+
|
|
50
|
+
const user: User = await getRepository(User).findOneBy({
|
|
51
|
+
email: email != '' ? email : context.state.user.email
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
return await getUserBizplaces(user)
|
|
55
|
+
|
|
56
|
+
// const bizplaces = await getRepository(Bizplace).find({ domain })
|
|
57
|
+
// // const userBizplaces = user.bizplaces
|
|
58
|
+
// return bizplaces.map((bizplace: Bizplace) => {
|
|
59
|
+
// return {
|
|
60
|
+
// id: bizplace.id,
|
|
61
|
+
// name: bizplace.name,
|
|
62
|
+
// description: bizplace.description,
|
|
63
|
+
// userBizplaceId: userBizplaces
|
|
64
|
+
// .map(userBiz => userBiz.bizplace.id)
|
|
65
|
+
// .slice(0, 1)
|
|
66
|
+
// .shift(),
|
|
67
|
+
// assigned: userBizplaces.filter((userBizplace: Bizplace) => userBizplace.id === bizplace.id).length > 0
|
|
68
|
+
// }
|
|
69
|
+
// })
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
@Directive('@privilege(category: "bizplace", privilege: "query", domainOwnerGranted: true)')
|
|
73
|
+
@Query(returns => BizplaceList)
|
|
74
|
+
async bizplaces(@Args(type => ListParam) params: ListParam, @Ctx() context: ResolverContext): Promise<BizplaceList> {
|
|
75
|
+
let bizplace_ids = []
|
|
76
|
+
let bizplaceIndex: any = params.filters.findIndex(x => x.name === 'bizplace')
|
|
77
|
+
if (bizplaceIndex > -1) {
|
|
78
|
+
bizplace_ids = params.filters
|
|
79
|
+
.filter(x => x.name === 'bizplace')
|
|
80
|
+
.map(item => {
|
|
81
|
+
return item.value
|
|
82
|
+
})
|
|
83
|
+
params.filters.splice(bizplaceIndex, 1)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const convertedParams = convertListParams(params)
|
|
87
|
+
|
|
88
|
+
let domainIndex: any = params.filters.findIndex(x => x.name === 'domain')
|
|
89
|
+
if (domainIndex > -1) {
|
|
90
|
+
convertedParams.where.id = In(await getPartnerBizplaceIds(context.state.domain, context.state.user))
|
|
91
|
+
} else {
|
|
92
|
+
if (!(bizplace_ids.length > 0))
|
|
93
|
+
convertedParams.where.id = In(await getPermittedBizplaceIds(context.state.domain, context.state.user))
|
|
94
|
+
else convertedParams.where.id = In(bizplace_ids)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const [items, total] = await getRepository(Bizplace).findAndCount({
|
|
98
|
+
...convertedParams,
|
|
99
|
+
relations: ['domain', 'company', 'users', 'creator', 'updater']
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
return { items, total }
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
@Directive('@privilege(category: "bizplace", privilege: "query", domainOwnerGranted: true)')
|
|
106
|
+
@Query(returns => Bizplace)
|
|
107
|
+
async bizplace(@Arg('id') id: string): Promise<Bizplace> {
|
|
108
|
+
return await getRepository(Bizplace).findOne({
|
|
109
|
+
where: { id },
|
|
110
|
+
relations: ['company', 'domain', 'users', 'users.roles', 'creator', 'updater']
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
@Query(returns => UserList)
|
|
115
|
+
async bizplaceUsers(@Args(type => ListParam) params: ListParam, @Ctx() context: ResolverContext): Promise<UserList> {
|
|
116
|
+
const { domain, user } = context.state
|
|
117
|
+
const myBizplace: Bizplace = await getMyBizplace(domain, user)
|
|
118
|
+
return await bizplaceUsers(myBizplace, {
|
|
119
|
+
...(await convertListParams(params)),
|
|
120
|
+
relations: ['domain', 'domains', 'roles', 'roles.privileges', 'roles.users']
|
|
121
|
+
})
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
@Query(returns => BizplaceList)
|
|
125
|
+
async partnersBizplaces(
|
|
126
|
+
@Args(type => ListParam) params: ListParam,
|
|
127
|
+
@Ctx() context: ResolverContext
|
|
128
|
+
): Promise<BizplaceList> {
|
|
129
|
+
let partnersBizplaceIds = []
|
|
130
|
+
let bizplaceIndex: any = params.filters.findIndex(x => x.name === 'bizplace')
|
|
131
|
+
if (bizplaceIndex > -1) {
|
|
132
|
+
partnersBizplaceIds = params.filters
|
|
133
|
+
.filter(x => x.name === 'bizplace')
|
|
134
|
+
.map(item => {
|
|
135
|
+
return item.value
|
|
136
|
+
})
|
|
137
|
+
params.filters.splice(bizplaceIndex, 1)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (!(partnersBizplaceIds.length > 0)) {
|
|
141
|
+
const partnersBizplaces: Bizplace[] = await getPartnersBizplaces(context.state.domain, context.state.user)
|
|
142
|
+
partnersBizplaceIds = partnersBizplaces.map((bizplace: Bizplace) => bizplace.id || null)
|
|
143
|
+
params.filters.push({
|
|
144
|
+
name: 'id',
|
|
145
|
+
operator: 'in',
|
|
146
|
+
value: partnersBizplaceIds
|
|
147
|
+
})
|
|
148
|
+
} else {
|
|
149
|
+
params.filters.push({
|
|
150
|
+
name: 'id',
|
|
151
|
+
operator: 'in',
|
|
152
|
+
value: partnersBizplaceIds
|
|
153
|
+
})
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const convertedParams = convertListParams(params, {
|
|
157
|
+
searchables: ['name', 'description']
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
const [items, total] = await getRepository(Bizplace).findAndCount({
|
|
161
|
+
...convertedParams,
|
|
162
|
+
relations: ['domain', 'company', 'users', 'creator', 'updater']
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
return { items, total }
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
@Query(returns => BizplaceList)
|
|
169
|
+
async permittedPartnersCompanyBizplaces(
|
|
170
|
+
@Args(type => ListParam) params: ListParam,
|
|
171
|
+
@Ctx() context: ResolverContext
|
|
172
|
+
): Promise<BizplaceList> {
|
|
173
|
+
let partnersBizplaceIds = []
|
|
174
|
+
let bizplaceIndex: any = params.filters.findIndex(x => x.name === 'bizplace')
|
|
175
|
+
if (bizplaceIndex > -1) {
|
|
176
|
+
partnersBizplaceIds = params.filters
|
|
177
|
+
.filter(x => x.name === 'bizplace')
|
|
178
|
+
.map(item => {
|
|
179
|
+
return item.value
|
|
180
|
+
})
|
|
181
|
+
params.filters.splice(bizplaceIndex, 1)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const convertedParams = convertListParams(params)
|
|
185
|
+
|
|
186
|
+
if (!(partnersBizplaceIds.length > 0)) {
|
|
187
|
+
const partnersBizplaces: Bizplace[] = await getPartnersCompanyBizplaces(
|
|
188
|
+
context.state.domain,
|
|
189
|
+
context.state.user,
|
|
190
|
+
true
|
|
191
|
+
)
|
|
192
|
+
partnersBizplaceIds = partnersBizplaces.map((bizplace: Bizplace) => bizplace.id || null)
|
|
193
|
+
convertedParams.where.id = In(partnersBizplaceIds)
|
|
194
|
+
} else {
|
|
195
|
+
convertedParams.where.id = In(partnersBizplaceIds)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const [items, total] = await getRepository(Bizplace).findAndCount({
|
|
199
|
+
...convertedParams,
|
|
200
|
+
relations: ['domain', 'company', 'users', 'creator', 'updater']
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
return { items, total }
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export async function bizplaceUsers(
|
|
208
|
+
bizplace: Bizplace,
|
|
209
|
+
params: FindManyOptions<User>,
|
|
210
|
+
trxMgr?: EntityManager
|
|
211
|
+
): Promise<{ items: User[]; total: number }> {
|
|
212
|
+
const bizplaceRepo: Repository<Bizplace> = trxMgr?.getRepository(Bizplace) || getRepository(Bizplace)
|
|
213
|
+
const userRepo: Repository<User> = trxMgr?.getRepository(User) || getRepository(User)
|
|
214
|
+
|
|
215
|
+
if (!bizplace?.users?.length) {
|
|
216
|
+
bizplace = await bizplaceRepo.findOne({
|
|
217
|
+
where: { id: bizplace.id },
|
|
218
|
+
relations: ['users']
|
|
219
|
+
})
|
|
220
|
+
}
|
|
221
|
+
const users: User[] = bizplace.users
|
|
222
|
+
const userIds: string[] = users.map((user: User) => user.id)
|
|
223
|
+
|
|
224
|
+
const [items, total] = await userRepo.findAndCount({
|
|
225
|
+
...params,
|
|
226
|
+
where: {
|
|
227
|
+
...(params.where as any),
|
|
228
|
+
id: In(userIds)
|
|
229
|
+
}
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
return {
|
|
233
|
+
items,
|
|
234
|
+
total
|
|
235
|
+
}
|
|
236
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { Field, InputType, Int, ObjectType } from 'type-graphql'
|
|
2
|
+
|
|
3
|
+
import { ObjectRef } from '@things-factory/shell'
|
|
4
|
+
|
|
5
|
+
import { Bizplace } from './bizplace'
|
|
6
|
+
|
|
7
|
+
@ObjectType()
|
|
8
|
+
export class BizplaceList {
|
|
9
|
+
@Field(type => [Bizplace], { nullable: true })
|
|
10
|
+
items?: Bizplace[]
|
|
11
|
+
|
|
12
|
+
@Field(type => Int, { nullable: true })
|
|
13
|
+
total?: number
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
@ObjectType()
|
|
17
|
+
export class UserBizplace {
|
|
18
|
+
@Field({ nullable: true })
|
|
19
|
+
id?: string
|
|
20
|
+
|
|
21
|
+
@Field({ nullable: true })
|
|
22
|
+
name?: string
|
|
23
|
+
|
|
24
|
+
@Field({ nullable: true })
|
|
25
|
+
description?: string
|
|
26
|
+
|
|
27
|
+
@Field({ nullable: true })
|
|
28
|
+
assigned?: boolean
|
|
29
|
+
|
|
30
|
+
@Field({ nullable: true })
|
|
31
|
+
userBizplaceId?: string
|
|
32
|
+
|
|
33
|
+
@Field({ nullable: true })
|
|
34
|
+
mainBizplace?: boolean
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
@InputType()
|
|
38
|
+
export class BizplacePatch {
|
|
39
|
+
@Field({ nullable: true })
|
|
40
|
+
id?: string
|
|
41
|
+
|
|
42
|
+
@Field({ nullable: true })
|
|
43
|
+
name?: string
|
|
44
|
+
|
|
45
|
+
@Field({ nullable: true })
|
|
46
|
+
company?: ObjectRef
|
|
47
|
+
|
|
48
|
+
@Field({ nullable: true })
|
|
49
|
+
description?: string
|
|
50
|
+
|
|
51
|
+
@Field({ nullable: true })
|
|
52
|
+
address?: string
|
|
53
|
+
|
|
54
|
+
@Field({ nullable: true })
|
|
55
|
+
postalCode?: string
|
|
56
|
+
|
|
57
|
+
@Field({ nullable: true })
|
|
58
|
+
latlng?: string
|
|
59
|
+
|
|
60
|
+
@Field({ nullable: true })
|
|
61
|
+
status?: string
|
|
62
|
+
|
|
63
|
+
@Field({ nullable: true })
|
|
64
|
+
cuFlag?: string
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
@InputType()
|
|
68
|
+
export class NewBizplace {
|
|
69
|
+
@Field()
|
|
70
|
+
name: string
|
|
71
|
+
|
|
72
|
+
@Field()
|
|
73
|
+
company: ObjectRef
|
|
74
|
+
|
|
75
|
+
@Field({ nullable: true })
|
|
76
|
+
description?: string
|
|
77
|
+
|
|
78
|
+
@Field()
|
|
79
|
+
address: string
|
|
80
|
+
|
|
81
|
+
@Field()
|
|
82
|
+
postalCode: string
|
|
83
|
+
|
|
84
|
+
@Field()
|
|
85
|
+
latlng: string
|
|
86
|
+
|
|
87
|
+
@Field({ nullable: true })
|
|
88
|
+
status?: string
|
|
89
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { Field, ID, ObjectType } from 'type-graphql'
|
|
2
|
+
import {
|
|
3
|
+
Column,
|
|
4
|
+
CreateDateColumn,
|
|
5
|
+
Entity,
|
|
6
|
+
Index,
|
|
7
|
+
JoinTable,
|
|
8
|
+
ManyToMany,
|
|
9
|
+
ManyToOne,
|
|
10
|
+
OneToMany,
|
|
11
|
+
PrimaryGeneratedColumn,
|
|
12
|
+
UpdateDateColumn
|
|
13
|
+
} from 'typeorm'
|
|
14
|
+
|
|
15
|
+
import { User } from '@things-factory/auth-base'
|
|
16
|
+
import { Domain } from '@things-factory/shell'
|
|
17
|
+
|
|
18
|
+
import { BizOption } from '../biz-option/biz-option'
|
|
19
|
+
import { Company } from '../company/company'
|
|
20
|
+
|
|
21
|
+
@Entity()
|
|
22
|
+
@Index('ix_bizplaces_0', (bizplace: Bizplace) => [bizplace.name], { unique: true })
|
|
23
|
+
@Index('ix_bizplaces_1', (company: Company) => [company.domain], { unique: false })
|
|
24
|
+
@ObjectType()
|
|
25
|
+
export class Bizplace {
|
|
26
|
+
@PrimaryGeneratedColumn('uuid')
|
|
27
|
+
@Field(type => ID)
|
|
28
|
+
readonly id: string
|
|
29
|
+
|
|
30
|
+
@ManyToOne(type => Domain)
|
|
31
|
+
@Field(type => Domain)
|
|
32
|
+
domain?: Domain
|
|
33
|
+
|
|
34
|
+
@OneToMany(type => BizOption, bizOption => bizOption.bizplace)
|
|
35
|
+
@Field(type => [BizOption], { nullable: true })
|
|
36
|
+
bizOptions: BizOption[]
|
|
37
|
+
|
|
38
|
+
@ManyToMany(type => User)
|
|
39
|
+
@JoinTable({ name: 'bizplaces_users' })
|
|
40
|
+
@Field(type => [User])
|
|
41
|
+
users: User[]
|
|
42
|
+
|
|
43
|
+
@ManyToOne(type => Company)
|
|
44
|
+
@Field(type => Company)
|
|
45
|
+
company: Company
|
|
46
|
+
|
|
47
|
+
@Column()
|
|
48
|
+
@Field()
|
|
49
|
+
name: string
|
|
50
|
+
|
|
51
|
+
@Column({
|
|
52
|
+
nullable: true
|
|
53
|
+
})
|
|
54
|
+
@Field({ nullable: true })
|
|
55
|
+
description: string
|
|
56
|
+
|
|
57
|
+
@Column()
|
|
58
|
+
@Field()
|
|
59
|
+
address: string
|
|
60
|
+
|
|
61
|
+
@Column()
|
|
62
|
+
@Field()
|
|
63
|
+
postalCode: string
|
|
64
|
+
|
|
65
|
+
@Column()
|
|
66
|
+
@Field()
|
|
67
|
+
latlng: string
|
|
68
|
+
|
|
69
|
+
@Column({
|
|
70
|
+
nullable: true
|
|
71
|
+
})
|
|
72
|
+
@Field({ nullable: true })
|
|
73
|
+
status: string
|
|
74
|
+
|
|
75
|
+
@ManyToOne(type => User, {
|
|
76
|
+
nullable: true
|
|
77
|
+
})
|
|
78
|
+
@Field({ nullable: true })
|
|
79
|
+
creator: User
|
|
80
|
+
|
|
81
|
+
@ManyToOne(type => User, {
|
|
82
|
+
nullable: true
|
|
83
|
+
})
|
|
84
|
+
@Field({ nullable: true })
|
|
85
|
+
updater: User
|
|
86
|
+
|
|
87
|
+
@CreateDateColumn()
|
|
88
|
+
@Field({ nullable: true })
|
|
89
|
+
createdAt: Date
|
|
90
|
+
|
|
91
|
+
@UpdateDateColumn()
|
|
92
|
+
@Field({ nullable: true })
|
|
93
|
+
updatedAt: Date
|
|
94
|
+
|
|
95
|
+
constructor(obj?) {
|
|
96
|
+
if (obj) {
|
|
97
|
+
this.id = obj.Bizplace_id
|
|
98
|
+
this.name = obj.Bizplace_name
|
|
99
|
+
this.description = obj.Bizplace_description
|
|
100
|
+
this.address = obj.Bizplace_address
|
|
101
|
+
this.postalCode = obj.Bizplace_postal_code
|
|
102
|
+
this.latlng = obj.Bizplace_latlng
|
|
103
|
+
this.status = obj.Bizplace_status
|
|
104
|
+
this.createdAt = obj.Bizplace_created_at
|
|
105
|
+
this.updatedAt = obj.Bizplace_updated_at
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { Arg, Ctx, Directive, Mutation, Resolver } from 'type-graphql'
|
|
2
|
+
import { EntityManager, Repository } from 'typeorm'
|
|
3
|
+
|
|
4
|
+
import { User } from '@things-factory/auth-base'
|
|
5
|
+
import { Domain, getRepository } from '@things-factory/shell'
|
|
6
|
+
import { slugger } from '@things-factory/utils'
|
|
7
|
+
|
|
8
|
+
import { Company } from './company'
|
|
9
|
+
import { CompanyPatch, NewCompany } from './company-types'
|
|
10
|
+
|
|
11
|
+
@Resolver(Company)
|
|
12
|
+
export class CompanyMutation {
|
|
13
|
+
@Directive('@transaction')
|
|
14
|
+
@Mutation(returns => Company)
|
|
15
|
+
async createCompany(@Arg('company') company: NewCompany, @Ctx() context: ResolverContext): Promise<Company> {
|
|
16
|
+
const { domain, user, tx } = context.state
|
|
17
|
+
return await createCompany(company, domain, user, tx)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
@Directive('@privilege(category: "company", privilege: "mutation")')
|
|
21
|
+
@Directive('@transaction')
|
|
22
|
+
@Mutation(returns => Company)
|
|
23
|
+
async updateCompany(
|
|
24
|
+
@Arg('name') name: string,
|
|
25
|
+
@Arg('patch') patch: CompanyPatch,
|
|
26
|
+
@Ctx() context: ResolverContext
|
|
27
|
+
): Promise<Company> {
|
|
28
|
+
const { domain, user, tx } = context.state
|
|
29
|
+
return await updateCompany(name, patch, domain, user, tx)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
@Directive('@privilege(category: "company", privilege: "mutation")')
|
|
33
|
+
@Directive('@transaction')
|
|
34
|
+
@Mutation(returns => [Company])
|
|
35
|
+
async updateMultipleCompany(
|
|
36
|
+
@Arg('patches', type => [CompanyPatch]) patches: CompanyPatch[],
|
|
37
|
+
@Ctx() context: ResolverContext
|
|
38
|
+
) {
|
|
39
|
+
let results = []
|
|
40
|
+
const _createRecords = patches.filter((patch: any) => patch.cuFlag.toUpperCase() === '+')
|
|
41
|
+
const _updateRecords = patches.filter((patch: any) => patch.cuFlag.toUpperCase() === 'M')
|
|
42
|
+
|
|
43
|
+
if (_createRecords.length > 0) {
|
|
44
|
+
for (let i = 0; i < _createRecords.length; i++) {
|
|
45
|
+
const patch: CompanyPatch = _createRecords[i]
|
|
46
|
+
const result = await this.createCompany(patch as NewCompany, context)
|
|
47
|
+
results.push({ ...result, cuFlag: '+' })
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (_updateRecords.length > 0) {
|
|
52
|
+
for (let i = 0; i < _updateRecords.length; i++) {
|
|
53
|
+
const patch: CompanyPatch = _updateRecords[i]
|
|
54
|
+
const result = await this.updateCompany(patch.name, patch, context)
|
|
55
|
+
results.push({ ...result, cuFlag: 'M' })
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return results
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
@Directive('@transaction')
|
|
63
|
+
@Mutation(returns => Boolean)
|
|
64
|
+
async deleteCompany(@Arg('id') id: string, @Ctx() context: ResolverContext): Promise<Boolean> {
|
|
65
|
+
await deleteCompany(id, context.state.tx)
|
|
66
|
+
|
|
67
|
+
return true
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
@Directive('@transaction')
|
|
71
|
+
@Directive('@privilege(category: "company", privilege: "mutation")')
|
|
72
|
+
@Mutation(returns => Boolean)
|
|
73
|
+
async deleteCompanies(
|
|
74
|
+
@Arg('ids', type => [String]) ids: string[],
|
|
75
|
+
@Ctx() context: ResolverContext
|
|
76
|
+
): Promise<Boolean> {
|
|
77
|
+
await deleteCompanies(ids, context.state.tx)
|
|
78
|
+
|
|
79
|
+
return true
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export async function createCompany(company: NewCompany, domain: Domain, user: User, tx?: EntityManager) {
|
|
84
|
+
const companyRepository: Repository<Company> = tx ? tx.getRepository(Company) : getRepository(Company)
|
|
85
|
+
const domainRepository: Repository<Domain> = tx ? tx.getRepository(Domain) : getRepository(Domain)
|
|
86
|
+
|
|
87
|
+
const companyDomain: Domain = await domainRepository.save({
|
|
88
|
+
subdomain: slugger(company.name),
|
|
89
|
+
name: company.name
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
return await companyRepository.save({
|
|
93
|
+
...company,
|
|
94
|
+
domain: companyDomain,
|
|
95
|
+
creator: user,
|
|
96
|
+
updater: user
|
|
97
|
+
})
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export async function updateCompany(name: string, patch: CompanyPatch, domain: Domain, user: User, tx?: EntityManager) {
|
|
101
|
+
const repository: Repository<Company> = tx ? tx.getRepository(Company) : getRepository(Company)
|
|
102
|
+
const company = await repository.findOneBy({
|
|
103
|
+
domain: { id: domain.id },
|
|
104
|
+
name
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
return repository.save({
|
|
108
|
+
...company,
|
|
109
|
+
...patch,
|
|
110
|
+
updater: user
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export async function deleteCompanies(ids: string[], tx?: EntityManager) {
|
|
115
|
+
const repository = tx ? tx.getRepository(Company) : getRepository(Company)
|
|
116
|
+
return await repository.delete(ids)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export async function deleteCompany(id: string, tx?: EntityManager) {
|
|
120
|
+
const repository: Repository<Company> = tx ? tx.getRepository(Company) : getRepository(Company)
|
|
121
|
+
return await repository.delete(id)
|
|
122
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { Args, Ctx, Directive, Query, Resolver } from 'type-graphql'
|
|
2
|
+
import { In } from 'typeorm'
|
|
3
|
+
|
|
4
|
+
import { getDomainUsers, getUserDomains, User, UserList } from '@things-factory/auth-base'
|
|
5
|
+
import { convertListParams, Domain, getRepository, ListParam } from '@things-factory/shell'
|
|
6
|
+
|
|
7
|
+
import { Bizplace } from '../bizplace/bizplace'
|
|
8
|
+
import { BizplaceList } from '../bizplace/bizplace-types'
|
|
9
|
+
import { Company } from './company'
|
|
10
|
+
import { CompanyList } from './company-types'
|
|
11
|
+
|
|
12
|
+
@Resolver(Company)
|
|
13
|
+
export class CompanyQuery {
|
|
14
|
+
@Directive('@privilege(category: "company", privilege: "query", domainOwnerGranted: true)')
|
|
15
|
+
@Query(returns => Company)
|
|
16
|
+
async company(@Ctx() context: ResolverContext): Promise<Company> {
|
|
17
|
+
const { domain } = context.state
|
|
18
|
+
|
|
19
|
+
const foundCompany: Company | null = await getRepository(Company).findOneBy({
|
|
20
|
+
domain: { id: domain.id }
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
if (!foundCompany) throw new Error(context.t('error.no x found', { x: context.t('field.company') }))
|
|
24
|
+
return foundCompany
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
@Directive('@privilege(category: "company", privilege: "query")')
|
|
28
|
+
@Query(returns => CompanyList)
|
|
29
|
+
async companies(@Args(type => ListParam) params: ListParam, @Ctx() context: ResolverContext): Promise<CompanyList> {
|
|
30
|
+
const { domain } = context.state
|
|
31
|
+
const convertedParams = convertListParams(params, { domain })
|
|
32
|
+
|
|
33
|
+
const [items, total] = await getRepository(Company).findAndCount({
|
|
34
|
+
...convertedParams,
|
|
35
|
+
relations: ['domain', 'creator', 'updater']
|
|
36
|
+
})
|
|
37
|
+
return { items, total }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
@Directive('@privilege(category: "company", privilege: "query", domainOwnerGranted: true)')
|
|
41
|
+
@Query(returns => BizplaceList)
|
|
42
|
+
async companyBizplaces(@Ctx() context: ResolverContext): Promise<BizplaceList> {
|
|
43
|
+
const { domain, user } = context.state
|
|
44
|
+
|
|
45
|
+
const userDomains: Partial<Domain>[] = await getUserDomains(user)
|
|
46
|
+
const company: Company = await getRepository(Company).findOneBy({
|
|
47
|
+
domain: { id: domain.id }
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
const [items, total] = await getRepository(Bizplace).findAndCount({
|
|
51
|
+
where: { company: { id: company.id }, domain: { id: In(userDomains.map(({ id }: Domain) => id)) } },
|
|
52
|
+
relations: ['domain']
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
return { items, total }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
@Directive('@privilege(category: "company", privilege: "query", domainOwnerGranted: true)')
|
|
59
|
+
@Query(returns => UserList)
|
|
60
|
+
async companyUsers(@Ctx() context: ResolverContext): Promise<UserList> {
|
|
61
|
+
const { domain } = context.state
|
|
62
|
+
|
|
63
|
+
const items: User[] = await getDomainUsers(domain)
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
items,
|
|
67
|
+
total: items.length
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { ObjectType, InputType, Field, Int } from 'type-graphql'
|
|
2
|
+
import { Company } from './company'
|
|
3
|
+
|
|
4
|
+
@ObjectType()
|
|
5
|
+
export class CompanyList {
|
|
6
|
+
@Field(type => [Company], { nullable: true })
|
|
7
|
+
items?: Company[]
|
|
8
|
+
|
|
9
|
+
@Field(type => Int, { nullable: true })
|
|
10
|
+
total?: number
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
@InputType()
|
|
14
|
+
export class CompanyPatch {
|
|
15
|
+
@Field({ nullable: true })
|
|
16
|
+
id?: string
|
|
17
|
+
|
|
18
|
+
@Field({ nullable: true })
|
|
19
|
+
name?: string
|
|
20
|
+
|
|
21
|
+
@Field({ nullable: true })
|
|
22
|
+
description?: string
|
|
23
|
+
|
|
24
|
+
@Field({ nullable: true })
|
|
25
|
+
countryCode?: string
|
|
26
|
+
|
|
27
|
+
@Field({ nullable: true })
|
|
28
|
+
brn?: string
|
|
29
|
+
|
|
30
|
+
@Field({ nullable: true })
|
|
31
|
+
address?: string
|
|
32
|
+
|
|
33
|
+
@Field({ nullable: true })
|
|
34
|
+
postalCode?: string
|
|
35
|
+
|
|
36
|
+
@Field({ nullable: true })
|
|
37
|
+
status?: string
|
|
38
|
+
|
|
39
|
+
@Field({ nullable: true })
|
|
40
|
+
cuFlag?: string
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
@InputType()
|
|
44
|
+
export class NewCompany {
|
|
45
|
+
@Field()
|
|
46
|
+
name: string
|
|
47
|
+
|
|
48
|
+
@Field({ nullable: true })
|
|
49
|
+
description?: string
|
|
50
|
+
|
|
51
|
+
@Field()
|
|
52
|
+
countryCode: string
|
|
53
|
+
|
|
54
|
+
@Field()
|
|
55
|
+
brn: string
|
|
56
|
+
|
|
57
|
+
@Field()
|
|
58
|
+
address: string
|
|
59
|
+
|
|
60
|
+
@Field({ nullable: true })
|
|
61
|
+
postalCode: string
|
|
62
|
+
|
|
63
|
+
@Field({ nullable: true })
|
|
64
|
+
type?: string
|
|
65
|
+
|
|
66
|
+
@Field()
|
|
67
|
+
status: string
|
|
68
|
+
}
|