@things-factory/biz-base 8.0.0-beta.9 → 8.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/dist-server/tsconfig.tsbuildinfo +1 -1
  2. package/package.json +3 -3
  3. package/server/index.ts +3 -0
  4. package/server/middlewares/index.ts +0 -0
  5. package/server/migrations/index.ts +9 -0
  6. package/server/service/biz-option/biz-option-mutation.ts +65 -0
  7. package/server/service/biz-option/biz-option-query.ts +30 -0
  8. package/server/service/biz-option/biz-option-types.ts +44 -0
  9. package/server/service/biz-option/biz-option.ts +70 -0
  10. package/server/service/biz-option/index.ts +6 -0
  11. package/server/service/biz-option-detail/biz-option-detail-mutation.ts +59 -0
  12. package/server/service/biz-option-detail/biz-option-detail-query.ts +27 -0
  13. package/server/service/biz-option-detail/biz-option-detail-types.ts +38 -0
  14. package/server/service/biz-option-detail/biz-option-detail.ts +62 -0
  15. package/server/service/biz-option-detail/index.ts +6 -0
  16. package/server/service/bizplace/bizplace-mutation.ts +144 -0
  17. package/server/service/bizplace/bizplace-query.ts +236 -0
  18. package/server/service/bizplace/bizplace-types.ts +89 -0
  19. package/server/service/bizplace/bizplace.ts +108 -0
  20. package/server/service/bizplace/index.ts +6 -0
  21. package/server/service/company/company-mutation.ts +122 -0
  22. package/server/service/company/company-query.ts +70 -0
  23. package/server/service/company/company-types.ts +68 -0
  24. package/server/service/company/company.ts +105 -0
  25. package/server/service/company/index.ts +6 -0
  26. package/server/service/contact-point/contact-point-mutation.ts +165 -0
  27. package/server/service/contact-point/contact-point-query.ts +43 -0
  28. package/server/service/contact-point/contact-point-types.ts +152 -0
  29. package/server/service/contact-point/contact-point.ts +132 -0
  30. package/server/service/contact-point/index.ts +6 -0
  31. package/server/service/index.ts +46 -0
  32. package/server/service/worker/index.ts +6 -0
  33. package/server/service/worker/worker-mutation.ts +111 -0
  34. package/server/service/worker/worker-query.ts +37 -0
  35. package/server/service/worker/worker-types.ts +50 -0
  36. package/server/service/worker/worker.ts +58 -0
  37. package/server/utils/get-permitted-bizplaces.ts +342 -0
  38. package/server/utils/index.ts +1 -0
@@ -0,0 +1,111 @@
1
+ import { Arg, Ctx, Directive, Mutation, Resolver } from 'type-graphql'
2
+ import { EntityManager, In, Repository } from 'typeorm'
3
+
4
+ import { Domain, getRepository } from '@things-factory/shell'
5
+
6
+ import { getMyBizplace } from '../../utils'
7
+ import { Worker } from './worker'
8
+ import { NewWorker, WorkerPatch } from './worker-types'
9
+
10
+ @Resolver(Worker)
11
+ export class WorkerMutation {
12
+ @Directive('@privilege(category: "worker", privilege: "mutation")')
13
+ @Mutation(returns => Worker)
14
+ async createWorker(@Arg('worker') worker: NewWorker, @Ctx() context: ResolverContext): Promise<Worker> {
15
+ return await createWorker(worker, context.state.domain, context.state.user)
16
+ }
17
+
18
+ @Directive('@privilege(category: "worker", privilege: "mutation")')
19
+ @Mutation(returns => Worker)
20
+ async updateWorker(
21
+ @Arg('id') id: string,
22
+ @Arg('patch', type => WorkerPatch) patch: WorkerPatch,
23
+ @Ctx() context: ResolverContext
24
+ ): Promise<Worker> {
25
+ return await updateWorker(id, patch, context.state.domain, context.state.user)
26
+ }
27
+
28
+ @Directive('@privilege(category: "worker", privilege: "mutation")')
29
+ @Directive('@transaction')
30
+ @Mutation(returns => [Worker])
31
+ async updateMultipleWorker(
32
+ @Arg('patches', type => [WorkerPatch]) patches: WorkerPatch[],
33
+ @Ctx() context: ResolverContext
34
+ ): Promise<Worker[]> {
35
+ const { tx } = context.state
36
+
37
+ let results = []
38
+ const _createRecords = patches.filter((patch: any) => patch.cuFlag.toUpperCase() === '+')
39
+ const _updateRecords = patches.filter((patch: any) => patch.cuFlag.toUpperCase() === 'M')
40
+
41
+ if (_createRecords.length > 0) {
42
+ for (let i = 0; i < _createRecords.length; i++) {
43
+ const patch: WorkerPatch = _createRecords[i]
44
+ const result = await createWorker(patch as NewWorker, context.state.domain, context.state.user, tx)
45
+ results.push({ ...result, cuFlag: '+' })
46
+ }
47
+ }
48
+
49
+ if (_updateRecords.length > 0) {
50
+ for (let i = 0; i < _updateRecords.length; i++) {
51
+ const patch: WorkerPatch = _updateRecords[i]
52
+ const result = await updateWorker(patch.id, patch, context.state.domain, context.state.user, tx)
53
+ results.push({ ...result, cuFlag: 'M' })
54
+ }
55
+ }
56
+
57
+ return results
58
+ }
59
+
60
+ @Directive('@privilege(category: "worker", privilege: "mutation")')
61
+ @Mutation(returns => Boolean)
62
+ async deleteWorker(@Arg('id') id: string) {
63
+ return deleteWorker(id)
64
+ }
65
+
66
+ @Directive('@privilege(category: "worker", privilege: "mutation")')
67
+ @Mutation(returns => Boolean)
68
+ async deleteWorkers(@Arg('ids', type => [String]) ids: string[], @Ctx() context: ResolverContext): Promise<Boolean> {
69
+ return deleteWorkers({ ids }, context)
70
+ }
71
+ }
72
+
73
+ export async function createWorker(worker: NewWorker, domain: Domain, user: any, trxMgr?: EntityManager) {
74
+ const repository: Repository<Worker> = trxMgr ? trxMgr.getRepository(Worker) : getRepository(Worker)
75
+
76
+ return await repository.save({
77
+ ...worker,
78
+ domain,
79
+ bizplace: await getMyBizplace(domain, user),
80
+ creator: user,
81
+ updater: user
82
+ })
83
+ }
84
+
85
+ export async function updateWorker(id: string, patch: WorkerPatch, domain: Domain, user: any, trxMgr?: EntityManager) {
86
+ const repository: Repository<Worker> = trxMgr ? trxMgr.getRepository(Worker) : getRepository(Worker)
87
+ const worker = await repository.findOneBy({
88
+ domain: { id: domain.id },
89
+ id
90
+ })
91
+
92
+ return repository.save({
93
+ ...worker,
94
+ ...patch,
95
+ updater: user
96
+ })
97
+ }
98
+
99
+ export async function deleteWorker(id: string, trxMgr?: EntityManager) {
100
+ const repository: Repository<Worker> = trxMgr ? trxMgr.getRepository(Worker) : getRepository(Worker)
101
+ return await repository.delete(id)
102
+ }
103
+
104
+ export async function deleteWorkers({ ids }, _context: any) {
105
+ const { domain } = _context.state
106
+ await getRepository(Worker).delete({
107
+ domain: { id: domain.id },
108
+ id: In(ids)
109
+ })
110
+ return true
111
+ }
@@ -0,0 +1,37 @@
1
+ import { Arg, Args, Ctx, Directive, Query, Resolver } from 'type-graphql'
2
+ import { In } from 'typeorm'
3
+
4
+ import { convertListParams, getRepository, ListParam } from '@things-factory/shell'
5
+
6
+ import { getPermittedBizplaceIds } from '../../utils'
7
+ import { Worker } from './worker'
8
+ import { WorkerList } from './worker-types'
9
+
10
+ @Resolver(Worker)
11
+ export class WorkerQuery {
12
+ @Directive('@privilege(category: "worker", privilege: "query", domainOwnerGranted: true)')
13
+ @Query(returns => Worker)
14
+ async worker(@Arg('name') name: string, @Ctx() context: ResolverContext): Promise<Worker> {
15
+ return await getRepository(Worker).findOne({
16
+ where: {
17
+ domain: { id: context.state.domain.id },
18
+ name,
19
+ bizplace: In(await getPermittedBizplaceIds(context.state.domain, context.state.user))
20
+ },
21
+ relations: ['domain', 'bizplace', 'creator', 'updater']
22
+ })
23
+ }
24
+
25
+ @Directive('@privilege(category: "worker", privilege: "query", domainOwnerGranted: true)')
26
+ @Query(returns => WorkerList)
27
+ async workers(@Args(type => ListParam) params: ListParam, @Ctx() context: ResolverContext): Promise<WorkerList> {
28
+ const convertedParams = convertListParams(params)
29
+ convertedParams.where.bizplace = In(await getPermittedBizplaceIds(context.state.domain, context.state.user))
30
+ const [items, total] = await getRepository(Worker).findAndCount({
31
+ ...convertedParams,
32
+ relations: ['domain', 'bizplace', 'creator', 'updater']
33
+ })
34
+
35
+ return { items, total }
36
+ }
37
+ }
@@ -0,0 +1,50 @@
1
+ import { Field, InputType, Int, ObjectType } from 'type-graphql'
2
+
3
+ import { ObjectRef } from '@things-factory/shell'
4
+
5
+ import { Worker } from './worker'
6
+
7
+ @ObjectType()
8
+ export class WorkerList {
9
+ @Field(type => [Worker], { nullable: true })
10
+ items?: Worker[]
11
+
12
+ @Field(type => Int, { nullable: true })
13
+ total?: number
14
+ }
15
+
16
+ @InputType()
17
+ export class NewWorker {
18
+ @Field(type => ObjectRef)
19
+ bizplace: ObjectRef
20
+
21
+ @Field()
22
+ name: string
23
+
24
+ @Field()
25
+ type: string
26
+
27
+ @Field({ nullable: true })
28
+ description?: string
29
+ }
30
+
31
+ @InputType()
32
+ export class WorkerPatch {
33
+ @Field({ nullable: true })
34
+ id?: string
35
+
36
+ @Field(type => ObjectRef, { nullable: true })
37
+ bizplace?: ObjectRef
38
+
39
+ @Field({ nullable: true })
40
+ name?: string
41
+
42
+ @Field({ nullable: true })
43
+ type?: string
44
+
45
+ @Field({ nullable: true })
46
+ description?: string
47
+
48
+ @Field({ nullable: true })
49
+ cuFlag?: string
50
+ }
@@ -0,0 +1,58 @@
1
+ import { Field, ID, ObjectType } from 'type-graphql'
2
+ import { Column, CreateDateColumn, Entity, Index, ManyToOne, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm'
3
+
4
+ import { User } from '@things-factory/auth-base'
5
+ import { Domain } from '@things-factory/shell'
6
+
7
+ import { Bizplace } from '../bizplace/bizplace'
8
+
9
+ @Entity()
10
+ @Index('ix_worker_0', (worker: Worker) => [worker.domain, worker.name], { unique: true })
11
+ @ObjectType()
12
+ export class Worker {
13
+ @PrimaryGeneratedColumn('uuid')
14
+ @Field(type => ID)
15
+ id: string
16
+
17
+ @ManyToOne(type => Domain)
18
+ @Field(type => Domain)
19
+ domain?: Domain
20
+
21
+ @ManyToOne(type => Bizplace)
22
+ @Field(type => Bizplace)
23
+ bizplace: Bizplace
24
+
25
+ @Column()
26
+ @Field()
27
+ name: string
28
+
29
+ @Column()
30
+ @Field()
31
+ type: string
32
+
33
+ @Column({
34
+ nullable: true
35
+ })
36
+ @Field({ nullable: true })
37
+ description: string
38
+
39
+ @ManyToOne(type => User, {
40
+ nullable: true
41
+ })
42
+ @Field({ nullable: true })
43
+ creator: User
44
+
45
+ @ManyToOne(type => User, {
46
+ nullable: true
47
+ })
48
+ @Field({ nullable: true })
49
+ updater: User
50
+
51
+ @CreateDateColumn()
52
+ @Field({ nullable: true })
53
+ createdAt: Date
54
+
55
+ @UpdateDateColumn()
56
+ @Field({ nullable: true })
57
+ updatedAt: Date
58
+ }
@@ -0,0 +1,342 @@
1
+ import { EntityManager, In, SelectQueryBuilder } from 'typeorm'
2
+
3
+ import { checkUserBelongsDomain, Partner, User } from '@things-factory/auth-base'
4
+ import { Domain, getRepository } from '@things-factory/shell'
5
+
6
+ import { Bizplace } from '../service/bizplace/bizplace'
7
+
8
+ /**
9
+ * @description 도메인과 사용자의 관계를 판단하여
10
+ * 접속한 도메인을 통해 접근 할 수 있는 대상 bizplaces를 찾아 return
11
+ *
12
+ * @param domain
13
+ * @param user
14
+ */
15
+ export async function getPermittedBizplaces(domain: Domain, user: User, tx?: EntityManager): Promise<Bizplace[]> {
16
+ const bizplaceRepo = tx ? tx.getRepository(Bizplace) : getRepository(Bizplace)
17
+
18
+ // 1. Check whether `user` belongs to `domain` or not
19
+ let permittedBizplaces: Bizplace[] = []
20
+ if (await checkUserBelongsDomain(domain, user)) {
21
+ // If `user` belongs to `domain`
22
+ // return set of company bizplace, bizplace of passed domain, bizplaces of customers
23
+
24
+ if (domain.extType !== 'company') permittedBizplaces.push(await getCompanyBizplace(domain, user, null, tx))
25
+ permittedBizplaces.push(await bizplaceRepo.findOne({ where: { domain: { id: domain.id } } }))
26
+ permittedBizplaces = permittedBizplaces.concat(await getCustomerBizplaces(domain, tx))
27
+ } else {
28
+ // If `user` doesn't belong to `domain`
29
+ // return set of company bizplace, bizplaces which `user` can access
30
+ permittedBizplaces = await getUserBizplaces(user)
31
+ }
32
+
33
+ return permittedBizplaces
34
+ }
35
+
36
+ export async function getPermittedBizplaceIds(domain: Domain, user: User): Promise<string[] | any[]> {
37
+ const bizplaces: Bizplace[] = await getPermittedBizplaces(domain, user)
38
+ return bizplaces.map((bizplace: Bizplace) => bizplace.id || null)
39
+ }
40
+
41
+ export async function getMyBizplace(domain: Domain, user: User, tx?: EntityManager): Promise<Bizplace> {
42
+ const bizplaceRepo = tx ? tx.getRepository(Bizplace) : getRepository(Bizplace)
43
+
44
+ // Case 1. 도메인에 소속된 사용자
45
+ // 1. 현재 도메인을 릴레이션으로 가지고 있는 비즈플레이스를 리턴
46
+
47
+ if (await checkUserBelongsDomain(domain, user)) {
48
+ return await bizplaceRepo.findOne({ where: { domain: { id: domain.id } } })
49
+ } else {
50
+ const customerBizplaces: Bizplace[] = await getCustomerBizplaces(domain, tx)
51
+ const userBizplaces: Bizplace[] = await getUserBizplaces(user, tx)
52
+
53
+ const customerBizplaceIdSet: Set<string> = new Set(customerBizplaces.map((pb: Bizplace) => pb.id))
54
+ const userBizplaceIdSet: Set<string> = new Set(userBizplaces.map((ub: Bizplace) => ub.id))
55
+
56
+ const myBizplaceIds: string[] = Array.from(customerBizplaceIdSet).filter((pbId: string) =>
57
+ userBizplaceIdSet.has(pbId)
58
+ )
59
+ if (myBizplaceIds.length > 1) console.warn(`User has multiple bizplaces ${myBizplaceIds}`)
60
+
61
+ // TODO: 복수의 비즈플레이스를 가지고 있는 사용자는 조회 결과의 첫번째 비즈플레이스만 리턴 받도록 되어 있음
62
+ // 이런 케이스에 처리 방식을 결정하고 수정해야함
63
+ return await bizplaceRepo.findOne({
64
+ where: { id: In(myBizplaceIds) }
65
+ })
66
+ }
67
+ }
68
+
69
+ export async function getOutletBizplace(outletId: string, tx?: EntityManager) {
70
+ const bizplaceRepo = tx ? tx.getRepository(Bizplace) : getRepository(Bizplace)
71
+
72
+ const bizplace: Bizplace = await bizplaceRepo.findOne({
73
+ where: { id: outletId }
74
+ })
75
+ return bizplace
76
+ }
77
+
78
+ export async function getCompanyBizplace(
79
+ domain: Domain,
80
+ user: User,
81
+ customerBizplaceId?: string,
82
+ tx?: EntityManager
83
+ ): Promise<Bizplace> {
84
+ const bizplaceRepo = tx ? tx.getRepository(Bizplace) : getRepository(Bizplace)
85
+
86
+ let companyDomain: Domain
87
+
88
+ if (user) {
89
+ let myBizplace: Bizplace = await getMyBizplace(domain, user, tx)
90
+ if (!myBizplace.company?.id) {
91
+ myBizplace = await bizplaceRepo.findOne({
92
+ where: { id: myBizplace.id },
93
+ relations: ['company', 'company.domain']
94
+ })
95
+ companyDomain = myBizplace.company.domain
96
+ }
97
+ }
98
+
99
+ if (customerBizplaceId) {
100
+ const customerBizplace: Bizplace = await bizplaceRepo.findOne({
101
+ where: { id: customerBizplaceId },
102
+ relations: ['company', 'company.domain']
103
+ })
104
+
105
+ companyDomain = customerBizplace.company?.domain
106
+ }
107
+ return await bizplaceRepo.findOne({ where: { domain: { id: companyDomain.id } }, relations: ['domain'] })
108
+ }
109
+
110
+ export async function getCompaniesBizplaces(
111
+ domains: Domain[],
112
+ warehouseDomain: Domain,
113
+ tx?: EntityManager
114
+ ): Promise<Bizplace[]> {
115
+ const bizplaceRepo = tx ? tx.getRepository(Bizplace) : getRepository(Bizplace)
116
+
117
+ const partnersBizplaces = await bizplaceRepo
118
+ .createQueryBuilder('b')
119
+ .select('b.id', 'id')
120
+ .addSelect('b.name', 'name')
121
+ .addSelect('b.description', 'description')
122
+ .innerJoin('companies', 'c', 'c.domain_id = b.domain_id')
123
+ .innerJoin('bizplaces', 'b2', 'b2.company_id = c.id')
124
+ .innerJoin('domains', 'd', 'd.id = b2.domain_id')
125
+ .innerJoin('partners', 'p', 'p.partner_domain_id = d.id')
126
+ .where('p.domain_id = :domainId', { domainId: warehouseDomain.id })
127
+ .andWhere('b2.domain_id IN (:...domainIds)', { domainIds: domains.map(domain => domain.id) })
128
+ .groupBy('b.id')
129
+ .addGroupBy('b.name')
130
+ .addGroupBy('b.description')
131
+ .getRawMany()
132
+
133
+ return partnersBizplaces
134
+ }
135
+
136
+ export async function getPartnerBizplaceIds(domain: Domain, user: User, tx?: EntityManager): Promise<string[] | any[]> {
137
+ const bizplaceRepo = tx ? tx.getRepository(Bizplace) : getRepository(Bizplace)
138
+ const partnerRepo = tx ? tx.getRepository(Partner) : getRepository(Partner)
139
+
140
+ let partnerBizplaces: Bizplace[] = []
141
+
142
+ const domainBizplace: Bizplace = await bizplaceRepo.findOne({ where: { domain: { id: domain.id } } })
143
+ if (!domainBizplace) {
144
+ } else {
145
+ const partners: Partner[] = await partnerRepo.find({
146
+ where: { domain: { id: domainBizplace.id } } /* CONFIRMME regarding TYPEORM - compare original logic */,
147
+ relations: ['partnerBizplace', 'partnerBizplace.company', 'partnerBizplace.company.domain']
148
+ })
149
+
150
+ for (let partner of partners) {
151
+ partnerBizplaces.push((partner as any).partnerBizplace)
152
+ }
153
+ }
154
+ return partnerBizplaces.map((bizplace: Bizplace) => bizplace.id || null)
155
+ }
156
+
157
+ export async function getPartnersBizplaces(domain: Domain, user: User, tx?: EntityManager): Promise<Bizplace[]> {
158
+ const userRepo = tx ? tx.getRepository(User) : getRepository(User)
159
+ const bizplaceRepo = tx ? tx.getRepository(Bizplace) : getRepository(Bizplace)
160
+ const partnerRepo = tx ? tx.getRepository(Partner) : getRepository(Partner)
161
+
162
+ let partnersBizplaces: Bizplace[] = []
163
+
164
+ const qb: SelectQueryBuilder<User> = userRepo
165
+ .createQueryBuilder('user')
166
+ .select('user.id', 'userId')
167
+ .addSelect('role.id', 'roleId')
168
+ .addSelect('role.name', 'roleName')
169
+ .addSelect('domain.id', 'domainId')
170
+ .addSelect('domain.name', 'domainName')
171
+ .innerJoin('users_roles', 'ur', 'user.id = ur.users_id')
172
+ .innerJoin('roles', 'role', 'ur.roles_id = role.id')
173
+ .innerJoin('domains', 'domain', 'role.domain_id = domain.id')
174
+ .where('user.id = :userId', { userId: user.id })
175
+ .andWhere('domain.id = :domainId', { domainId: domain.id })
176
+
177
+ let userRoles = await qb.getRawMany()
178
+
179
+ // detect if user is Customer
180
+ let foundCustomerRole = userRoles.find(role => role.roleName === 'Customer')
181
+
182
+ // get all partners
183
+ let partnersDomains: Partner[] = await partnerRepo.find({
184
+ where: { domain: { id: domain.id } },
185
+ relations: ['partnerDomain']
186
+ })
187
+
188
+ if (foundCustomerRole) {
189
+ let userDomains = user.domains
190
+ for (let partDomain of partnersDomains) {
191
+ for (let userDomain of userDomains) {
192
+ if (userDomain.id === partDomain.partnerDomain.id) {
193
+ let partnerBizplace: Bizplace = await bizplaceRepo.findOne({
194
+ where: { domain: { id: partDomain.partnerDomain.id } },
195
+ relations: ['company', 'company.domain']
196
+ })
197
+
198
+ partnersBizplaces.push(partnerBizplace)
199
+ }
200
+ }
201
+ }
202
+ } else {
203
+ for (let partDomain of partnersDomains) {
204
+ let partnerBizplace: Bizplace = await bizplaceRepo.findOne({
205
+ where: { domain: { id: partDomain.partnerDomain.id } },
206
+ relations: ['company', 'company.domain']
207
+ })
208
+
209
+ partnersBizplaces.push(partnerBizplace)
210
+ }
211
+ }
212
+
213
+ return partnersBizplaces
214
+ }
215
+
216
+ export async function getPartnersCompanyBizplaces(
217
+ domain: Domain,
218
+ user: User,
219
+ strictAccess: Boolean = false,
220
+ tx?: EntityManager
221
+ ): Promise<Bizplace[]> {
222
+ const userRepo = tx ? tx.getRepository(User) : getRepository(User)
223
+ const bizplaceRepo = tx ? tx.getRepository(Bizplace) : getRepository(Bizplace)
224
+
225
+ let partnersBizplaces: Bizplace[] = []
226
+
227
+ let userRoles: any = await userRepo
228
+ .createQueryBuilder('user')
229
+ .select('user.id', 'userId')
230
+ .addSelect('role.id', 'roleId')
231
+ .addSelect('role.name', 'roleName')
232
+ .addSelect('domain.id', 'domainId')
233
+ .addSelect('domain.name', 'domainName')
234
+ .innerJoin('users_roles', 'ur', 'user.id = ur.users_id')
235
+ .innerJoin('roles', 'role', 'ur.roles_id = role.id')
236
+ .innerJoin('domains', 'domain', 'role.domain_id = domain.id')
237
+ .where('user.id = :userId', { userId: user.id })
238
+ .andWhere('domain.id = :domainId', { domainId: domain.id })
239
+ .getRawMany()
240
+
241
+ if (userRoles.find(role => role.roleName !== 'Customer')) {
242
+ let qbPartnersBizplaces = bizplaceRepo
243
+ .createQueryBuilder('b')
244
+ .select('b.id', 'id')
245
+ .addSelect('b.name', 'name')
246
+ .addSelect('b.description', 'description')
247
+ .innerJoin('companies', 'c', 'c.domain_id = b.domain_id')
248
+ .innerJoin('bizplaces', 'b2', 'b2.company_id = c.id')
249
+ .innerJoin('domains', 'd', 'd.id = b2.domain_id')
250
+ .innerJoin('partners', 'p', 'p.partner_domain_id = d.id')
251
+ .where('p.domain_id = :domainId', { domainId: domain.id })
252
+ .groupBy('b.id')
253
+ .addGroupBy('b.name')
254
+ .addGroupBy('b.description')
255
+
256
+ if (strictAccess) {
257
+ const userRepo = tx ? tx.getRepository(User) : getRepository(User)
258
+ qbPartnersBizplaces.andWhere(
259
+ `exists (
260
+ ${userRepo
261
+ .createQueryBuilder('u2')
262
+ .innerJoin('users_roles', 'ur', 'ur.users_id = u2.id')
263
+ .innerJoin('roles', 'r', 'r.id = ur.roles_id')
264
+ .where('r.domain_id = c.domain_id')
265
+ .andWhere(`r.name IN ('BUSINESS-OWNER','BUSINESS-MANAGER')`)
266
+ .andWhere(`u2.id = '${user.id}'`)
267
+ .getQuery()})`
268
+ )
269
+ }
270
+
271
+ partnersBizplaces = await qbPartnersBizplaces.getRawMany()
272
+ }
273
+
274
+ return partnersBizplaces
275
+ }
276
+
277
+ export async function getCustomerBizplaces(domain: Domain, tx?: EntityManager): Promise<Bizplace[]> {
278
+ const partnerRepo = tx ? tx.getRepository(Partner) : getRepository(Partner)
279
+ const bizplaceRepo = tx ? tx.getRepository(Bizplace) : getRepository(Bizplace)
280
+
281
+ const customers: Partner[] = await partnerRepo.find({
282
+ where: { domain: { id: domain.id } },
283
+ relations: ['partnerDomain']
284
+ })
285
+ const customerDomains: Domain[] = customers.map((customer: Partner) => customer.partnerDomain)
286
+ return await bizplaceRepo.find({
287
+ where: { domain: In(customerDomains.map((d: Domain) => d.id)) },
288
+ relations: ['company', 'company.domain', 'domain']
289
+ })
290
+ }
291
+
292
+ export async function getSubdomainBizplace(subdomain: string, tx?: EntityManager): Promise<Bizplace> {
293
+ const domainRepo = tx ? tx.getRepository(Domain) : getRepository(Domain)
294
+
295
+ const domain: Domain = await domainRepo.findOne({
296
+ where: { subdomain }
297
+ })
298
+
299
+ return await getRepository(Bizplace).findOne({ where: { domain: { id: domain.id } } })
300
+ }
301
+
302
+ export async function getUserBizplaces(user: User, tx?: EntityManager): Promise<Bizplace[]> {
303
+ const userRepo = tx ? tx.getRepository(User) : getRepository(User)
304
+ let userBizplaces: Bizplace[] = []
305
+
306
+ if (!user.domains?.length) {
307
+ user = await userRepo.findOne({
308
+ where: { id: user.id },
309
+ relations: ['domains']
310
+ })
311
+ }
312
+
313
+ const userDomains: Domain[] = user.domains
314
+ if (userDomains.every((userDomain: Domain) => userDomain.extType !== 'company')) {
315
+ userBizplaces.push(await getCompanyBizplace(userDomains[0], user, null, tx))
316
+ }
317
+
318
+ return userBizplaces.concat(
319
+ await getRepository(Bizplace).find({ where: { domain: In(userDomains.map((d: Domain) => d.id)) } })
320
+ )
321
+ }
322
+
323
+ export async function getDomainUsers(bizplace: Bizplace, tx?: EntityManager): Promise<any[]> {
324
+ const userDomainRepo = tx ? tx.getRepository('users_domains') : getRepository('users_domains')
325
+
326
+ const users: any[] = await userDomainRepo
327
+ .createQueryBuilder('ud')
328
+ .select('ud.users_id', 'id')
329
+ .distinct(true)
330
+ .where(qb => {
331
+ const subquery = qb
332
+ .subQuery()
333
+ .select('bizplace.domain_id')
334
+ .from(Bizplace, 'bizplace')
335
+ .where('bizplace.id = :customerBizplaceId', { customerBizplaceId: bizplace?.id })
336
+ .getQuery()
337
+ return 'ud.domains_id = ' + subquery
338
+ })
339
+ .getRawMany()
340
+
341
+ return users
342
+ }
@@ -0,0 +1 @@
1
+ export * from './get-permitted-bizplaces'