@things-factory/operato-hub 4.3.222 → 4.3.227
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/routers/internal/powrup-router.js +433 -54
- package/dist-server/routers/internal/powrup-router.js.map +1 -1
- package/dist-server/routers/internal/utils/params.js +134 -2
- package/dist-server/routers/internal/utils/params.js.map +1 -1
- package/package.json +56 -56
- package/server/routers/internal/powrup-router.ts +523 -55
- package/server/routers/internal/utils/params.ts +134 -2
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import Router from 'koa-router'
|
|
2
|
-
import { EntityManager, getConnection
|
|
2
|
+
import { EntityManager, getConnection } from 'typeorm'
|
|
3
3
|
|
|
4
|
-
import { NewUser, User } from '@things-factory/auth-base'
|
|
4
|
+
import { NewUser, Partner, Role, terminateGrantedRoles, User } from '@things-factory/auth-base'
|
|
5
5
|
import { Bizplace, Company } from '@things-factory/biz-base'
|
|
6
6
|
import { Integration } from '@things-factory/integration-base'
|
|
7
7
|
import { Domain } from '@things-factory/shell'
|
|
8
8
|
import { slugger } from '@things-factory/utils'
|
|
9
9
|
|
|
10
|
-
import { generateBizplaceDomain } from '../../controllers/bizplace-initializer
|
|
10
|
+
import { generateBizplaceDomain } from '../../controllers/bizplace-initializer'
|
|
11
|
+
import { generateBusinessRoles } from '../../controllers/company-initializer'
|
|
11
12
|
import { validationMiddleware } from './middlewares/validation-middleware'
|
|
12
13
|
import { ApiError, ApiErrorHandler, throwInternalServerError } from './utils/error-util'
|
|
13
14
|
|
|
@@ -26,7 +27,7 @@ powrupRouter.post('/internal/account/create-user', validationMiddleware, async (
|
|
|
26
27
|
const { email } = user
|
|
27
28
|
user.email = email.trim()
|
|
28
29
|
|
|
29
|
-
const oldUser: User = await tx.getRepository(User).findOne({ where: {
|
|
30
|
+
const oldUser: User = await tx.getRepository(User).findOne({ where: { refUserId: user.refUserId } })
|
|
30
31
|
if (oldUser) {
|
|
31
32
|
throw new ApiError(
|
|
32
33
|
'E04',
|
|
@@ -38,6 +39,7 @@ powrupRouter.post('/internal/account/create-user', validationMiddleware, async (
|
|
|
38
39
|
|
|
39
40
|
let createdUser: User = await tx.getRepository(User).save({
|
|
40
41
|
...user,
|
|
42
|
+
refUserId: user?.refUserId,
|
|
41
43
|
userType: user?.userType || 'user',
|
|
42
44
|
status: user?.status || 'inactive',
|
|
43
45
|
salt,
|
|
@@ -45,8 +47,71 @@ powrupRouter.post('/internal/account/create-user', validationMiddleware, async (
|
|
|
45
47
|
password: User.encode(user.password, salt)
|
|
46
48
|
})
|
|
47
49
|
|
|
50
|
+
let data = {
|
|
51
|
+
id: createdUser.id,
|
|
52
|
+
name: createdUser.name,
|
|
53
|
+
description: createdUser.description,
|
|
54
|
+
email: createdUser.email,
|
|
55
|
+
userType: createdUser.userType,
|
|
56
|
+
status: createdUser.status,
|
|
57
|
+
refUserId: createdUser.refUserId
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
context.body = {
|
|
61
|
+
result: { data }
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
} catch (e) {
|
|
65
|
+
if (e instanceof ApiError) ApiErrorHandler(context, e)
|
|
66
|
+
else throwInternalServerError(context, e)
|
|
67
|
+
}
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
powrupRouter.post('/internal/account/update-user', validationMiddleware, async (context, next) => {
|
|
71
|
+
try {
|
|
72
|
+
await getConnection().transaction(async (tx: EntityManager) => {
|
|
73
|
+
const { body } = context.request
|
|
74
|
+
|
|
75
|
+
await getIntegration(context, tx)
|
|
76
|
+
|
|
77
|
+
let user: NewUser = body?.data || {}
|
|
78
|
+
|
|
79
|
+
const existingUser: User = await tx.getRepository(User).findOne({ where: { id: user.id } })
|
|
80
|
+
if (!existingUser) {
|
|
81
|
+
throw new ApiError(
|
|
82
|
+
'E04',
|
|
83
|
+
context.t('error.x does not exists in y', { x: context.t('field.user'), y: 'operato' })
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const salt = User.generateSalt()
|
|
88
|
+
|
|
89
|
+
await tx.getRepository(User).update(
|
|
90
|
+
{ id: existingUser.id },
|
|
91
|
+
{
|
|
92
|
+
name: user?.name || existingUser.name,
|
|
93
|
+
status: user?.status || existingUser.status,
|
|
94
|
+
userType: user?.userType || existingUser.userType,
|
|
95
|
+
salt: user?.password ? salt : existingUser.salt,
|
|
96
|
+
password: user?.password ? User.encode(user.password, salt) : existingUser.password,
|
|
97
|
+
passwordUpdatedAt: user?.password ? new Date() : existingUser.passwordUpdatedAt
|
|
98
|
+
}
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
let updatedUser: User = await tx.getRepository(User).findOne(existingUser.id)
|
|
102
|
+
|
|
103
|
+
let data = {
|
|
104
|
+
id: updatedUser.id,
|
|
105
|
+
name: updatedUser.name,
|
|
106
|
+
description: updatedUser.description,
|
|
107
|
+
email: updatedUser.email,
|
|
108
|
+
userType: updatedUser.userType,
|
|
109
|
+
status: updatedUser.status,
|
|
110
|
+
refUserId: updatedUser.refUserId
|
|
111
|
+
}
|
|
112
|
+
|
|
48
113
|
context.body = {
|
|
49
|
-
|
|
114
|
+
result: { data }
|
|
50
115
|
}
|
|
51
116
|
})
|
|
52
117
|
} catch (e) {
|
|
@@ -62,14 +127,18 @@ powrupRouter.post('/internal/account/create-company', validationMiddleware, asyn
|
|
|
62
127
|
|
|
63
128
|
await getIntegration(context, tx)
|
|
64
129
|
|
|
65
|
-
const { name, description, brn, address, postalCode, type, status } = body.data
|
|
130
|
+
const { name, description, brn, address, postalCode, countryCode, type, status, userId, refCompanyId } = body.data
|
|
131
|
+
|
|
132
|
+
let foundUser: User = await tx
|
|
133
|
+
.getRepository(User)
|
|
134
|
+
.findOne({ where: { id: userId }, relations: ['domains', 'roles'] })
|
|
66
135
|
|
|
67
136
|
const subdomain = slugger(name)
|
|
68
137
|
|
|
69
138
|
//check for existing company
|
|
70
139
|
const oldCompany = await tx.getRepository(Company).findOne({
|
|
71
140
|
where: {
|
|
72
|
-
|
|
141
|
+
refCompanyId
|
|
73
142
|
}
|
|
74
143
|
})
|
|
75
144
|
|
|
@@ -91,7 +160,9 @@ powrupRouter.post('/internal/account/create-company', validationMiddleware, asyn
|
|
|
91
160
|
const domain: Domain = await tx.getRepository(Domain).save({
|
|
92
161
|
subdomain,
|
|
93
162
|
name,
|
|
94
|
-
extType: 'company'
|
|
163
|
+
extType: 'company',
|
|
164
|
+
creator: foundUser,
|
|
165
|
+
updater: foundUser
|
|
95
166
|
})
|
|
96
167
|
|
|
97
168
|
const company: Company = await tx.getRepository(Company).save({
|
|
@@ -100,13 +171,140 @@ powrupRouter.post('/internal/account/create-company', validationMiddleware, asyn
|
|
|
100
171
|
brn,
|
|
101
172
|
address,
|
|
102
173
|
postalCode,
|
|
174
|
+
countryCode: countryCode || 'my',
|
|
103
175
|
type,
|
|
104
176
|
status,
|
|
105
|
-
domain
|
|
177
|
+
domain,
|
|
178
|
+
refCompanyId: refCompanyId || null,
|
|
179
|
+
creator: foundUser,
|
|
180
|
+
updater: foundUser
|
|
106
181
|
})
|
|
107
182
|
|
|
183
|
+
const userHavingRoles: Role[] = foundUser.roles
|
|
184
|
+
const domainRoles: Role[] = await generateBusinessRoles(domain, tx)
|
|
185
|
+
|
|
186
|
+
foundUser.domains = [...foundUser.domains, domain]
|
|
187
|
+
foundUser.roles = [...userHavingRoles, domainRoles.find(r => r.name === 'BUSINESS-OWNER')]
|
|
188
|
+
|
|
189
|
+
await tx.getRepository(User).save(foundUser)
|
|
190
|
+
await tx.getRepository(Bizplace).save({
|
|
191
|
+
name: company.name,
|
|
192
|
+
description: company.description,
|
|
193
|
+
address: company.address,
|
|
194
|
+
postalCode: company.postalCode || '',
|
|
195
|
+
latlng: '',
|
|
196
|
+
domain,
|
|
197
|
+
company
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
let data = {
|
|
201
|
+
id: company.id,
|
|
202
|
+
name: company.name,
|
|
203
|
+
description: company.description,
|
|
204
|
+
address: company.address,
|
|
205
|
+
brn: company.brn,
|
|
206
|
+
postalCode: company.postalCode,
|
|
207
|
+
countryCode: company.countryCode,
|
|
208
|
+
type: company.type,
|
|
209
|
+
status: company.status,
|
|
210
|
+
refCompanyId: company.refCompanyId
|
|
211
|
+
}
|
|
212
|
+
|
|
108
213
|
context.body = {
|
|
109
|
-
|
|
214
|
+
result: { data }
|
|
215
|
+
}
|
|
216
|
+
})
|
|
217
|
+
} catch (e) {
|
|
218
|
+
if (e instanceof ApiError) ApiErrorHandler(context, e)
|
|
219
|
+
else throwInternalServerError(context, e)
|
|
220
|
+
}
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
powrupRouter.post('/internal/account/update-company', validationMiddleware, async (context, next) => {
|
|
224
|
+
try {
|
|
225
|
+
await getConnection().transaction(async (tx: EntityManager) => {
|
|
226
|
+
const { body } = context.request
|
|
227
|
+
const { id, name, description, brn, address, postalCode, countryCode, type, status, userId } = body.data
|
|
228
|
+
|
|
229
|
+
let foundUser: User = await tx
|
|
230
|
+
.getRepository(User)
|
|
231
|
+
.findOne({ where: { id: userId }, relations: ['domains', 'roles'] })
|
|
232
|
+
|
|
233
|
+
const subdomain = slugger(name)
|
|
234
|
+
|
|
235
|
+
//check for existing company
|
|
236
|
+
const existingCompany: Company = await tx.getRepository(Company).findOne({
|
|
237
|
+
where: { id }
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
if (!existingCompany) {
|
|
241
|
+
throw new ApiError('E04', `${name} does not exist`)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const existingDomain: Domain = await tx.getRepository(Domain).findOne({
|
|
245
|
+
where: {
|
|
246
|
+
subdomain
|
|
247
|
+
},
|
|
248
|
+
relations: ['bizplace']
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
if (!existingDomain) {
|
|
252
|
+
throw new ApiError('E04', `${subdomain} does not exist`)
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// update company
|
|
256
|
+
await tx.getRepository(Domain).update(
|
|
257
|
+
{ id: existingDomain.id },
|
|
258
|
+
{
|
|
259
|
+
subdomain: name ? subdomain : existingDomain.subdomain,
|
|
260
|
+
name: name ? name : existingDomain.name,
|
|
261
|
+
updater: foundUser
|
|
262
|
+
}
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
await tx.getRepository(Company).update(
|
|
266
|
+
{ id: existingCompany.id },
|
|
267
|
+
{
|
|
268
|
+
name: name || existingCompany.name,
|
|
269
|
+
description: description || existingCompany.description,
|
|
270
|
+
brn: brn || existingCompany.brn,
|
|
271
|
+
address: address || existingCompany.address,
|
|
272
|
+
countryCode: countryCode || existingCompany.countryCode,
|
|
273
|
+
postalCode: postalCode || existingCompany.postalCode || '',
|
|
274
|
+
updater: foundUser
|
|
275
|
+
}
|
|
276
|
+
)
|
|
277
|
+
await tx.getRepository(Bizplace).update(
|
|
278
|
+
{ id: existingDomain.bizplace.id },
|
|
279
|
+
{
|
|
280
|
+
name: name || existingDomain.bizplace.name,
|
|
281
|
+
description: description || existingDomain.bizplace.description,
|
|
282
|
+
address: address || existingDomain.bizplace.address,
|
|
283
|
+
postalCode: postalCode || existingDomain.bizplace.postalCode || '',
|
|
284
|
+
latlng: '',
|
|
285
|
+
updater: foundUser
|
|
286
|
+
}
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
const updatedCompany: Company = await tx.getRepository(Company).findOne({
|
|
290
|
+
where: { id }
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
let data = {
|
|
294
|
+
id: updatedCompany.id,
|
|
295
|
+
name: updatedCompany.name,
|
|
296
|
+
description: updatedCompany.description,
|
|
297
|
+
address: updatedCompany.address,
|
|
298
|
+
brn: updatedCompany.brn,
|
|
299
|
+
postalCode: updatedCompany.postalCode,
|
|
300
|
+
countryCode: updatedCompany.countryCode,
|
|
301
|
+
type: updatedCompany.type,
|
|
302
|
+
status: updatedCompany.status,
|
|
303
|
+
refCompanyId: updatedCompany.refCompanyId
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
context.body = {
|
|
307
|
+
result: { data }
|
|
110
308
|
}
|
|
111
309
|
})
|
|
112
310
|
} catch (e) {
|
|
@@ -123,33 +321,127 @@ powrupRouter.post('/internal/account/create-bizplace', validationMiddleware, asy
|
|
|
123
321
|
await getIntegration(context, tx)
|
|
124
322
|
|
|
125
323
|
const { data: bizplace } = body
|
|
126
|
-
const company: Company = await tx.getRepository(Company).findOne({ where: { id: bizplace.
|
|
324
|
+
const company: Company = await tx.getRepository(Company).findOne({ where: { id: bizplace.companyId } })
|
|
127
325
|
|
|
128
326
|
const oldBizplace = await tx.getRepository(Bizplace).findOne({
|
|
129
327
|
where: {
|
|
130
|
-
|
|
328
|
+
refBizplaceId: bizplace.refBizplaceId
|
|
131
329
|
}
|
|
132
330
|
})
|
|
133
331
|
if (oldBizplace) {
|
|
134
332
|
throw new ApiError('E04', `${bizplace.name} is duplicated`)
|
|
135
333
|
}
|
|
136
334
|
|
|
335
|
+
let foundUser: User = await tx.getRepository(User).findOne({ where: { id: bizplace.userId } })
|
|
336
|
+
|
|
137
337
|
let createdBizplace: Bizplace = await tx.getRepository(Bizplace).save({
|
|
138
338
|
name: bizplace.name,
|
|
139
|
-
description: bizplace
|
|
140
|
-
address: bizplace
|
|
141
|
-
postalCode: bizplace
|
|
142
|
-
latlng: bizplace
|
|
143
|
-
status: bizplace
|
|
339
|
+
description: bizplace?.description || null,
|
|
340
|
+
address: bizplace?.address || '',
|
|
341
|
+
postalCode: bizplace?.postalCode || '',
|
|
342
|
+
latlng: bizplace?.latlng || '',
|
|
343
|
+
status: bizplace?.status || null,
|
|
344
|
+
refBizplaceId: bizplace?.refBizplaceId || null,
|
|
144
345
|
company,
|
|
145
|
-
updater:
|
|
146
|
-
creator:
|
|
346
|
+
updater: foundUser,
|
|
347
|
+
creator: foundUser
|
|
147
348
|
})
|
|
148
349
|
|
|
149
|
-
await generateBizplaceDomain(
|
|
350
|
+
await generateBizplaceDomain(createdBizplace.id, foundUser, tx)
|
|
351
|
+
|
|
352
|
+
let data = {
|
|
353
|
+
id: createdBizplace.id,
|
|
354
|
+
name: createdBizplace.name,
|
|
355
|
+
description: createdBizplace.description,
|
|
356
|
+
address: createdBizplace.address,
|
|
357
|
+
postalCode: createdBizplace.postalCode,
|
|
358
|
+
latlng: createdBizplace.latlng,
|
|
359
|
+
status: createdBizplace.status,
|
|
360
|
+
company: createdBizplace.company
|
|
361
|
+
}
|
|
150
362
|
|
|
151
363
|
context.body = {
|
|
152
|
-
|
|
364
|
+
result: { data }
|
|
365
|
+
}
|
|
366
|
+
})
|
|
367
|
+
} catch (e) {
|
|
368
|
+
if (e instanceof ApiError) ApiErrorHandler(context, e)
|
|
369
|
+
else throwInternalServerError(context, e)
|
|
370
|
+
}
|
|
371
|
+
})
|
|
372
|
+
|
|
373
|
+
powrupRouter.post('/internal/account/update-bizplace', validationMiddleware, async (context, next) => {
|
|
374
|
+
try {
|
|
375
|
+
await getConnection().transaction(async (tx: EntityManager) => {
|
|
376
|
+
const { body } = context.request
|
|
377
|
+
|
|
378
|
+
await getIntegration(context, tx)
|
|
379
|
+
|
|
380
|
+
const { data: bizplace } = body
|
|
381
|
+
|
|
382
|
+
const existingBizplace: Bizplace = await tx.getRepository(Bizplace).findOne({
|
|
383
|
+
where: {
|
|
384
|
+
id: bizplace.id
|
|
385
|
+
},
|
|
386
|
+
relations: ['company', 'domain']
|
|
387
|
+
})
|
|
388
|
+
if (!existingBizplace) {
|
|
389
|
+
throw new ApiError('E04', `${bizplace.id} does not exist`)
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const company: Company = existingBizplace.company
|
|
393
|
+
const domain: Domain = existingBizplace.domain
|
|
394
|
+
|
|
395
|
+
await tx.getRepository(Bizplace).update(
|
|
396
|
+
{ id: existingBizplace.id },
|
|
397
|
+
{
|
|
398
|
+
name: bizplace?.name || existingBizplace.name,
|
|
399
|
+
description: bizplace?.description || existingBizplace.description,
|
|
400
|
+
address: bizplace?.address || existingBizplace.address,
|
|
401
|
+
postalCode: bizplace?.postalCode || existingBizplace.postalCode,
|
|
402
|
+
latlng: bizplace?.latlng || existingBizplace.latlng,
|
|
403
|
+
status: bizplace?.status || existingBizplace.status,
|
|
404
|
+
updatedAt: new Date()
|
|
405
|
+
}
|
|
406
|
+
)
|
|
407
|
+
|
|
408
|
+
if (existingBizplace.domain.extType == 'company') {
|
|
409
|
+
await tx.getRepository(Company).update(
|
|
410
|
+
{ id: company.id },
|
|
411
|
+
{
|
|
412
|
+
name: bizplace?.name || company.name,
|
|
413
|
+
description: bizplace?.description || company.description,
|
|
414
|
+
address: bizplace?.address || company.address,
|
|
415
|
+
postalCode: bizplace?.postalCode || company.postalCode || '',
|
|
416
|
+
updatedAt: new Date()
|
|
417
|
+
}
|
|
418
|
+
)
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
await tx.getRepository(Domain).update(
|
|
422
|
+
{ id: domain.id },
|
|
423
|
+
{
|
|
424
|
+
name: bizplace?.name ? bizplace.name : domain.name,
|
|
425
|
+
subdomain: bizplace?.name ? slugger(bizplace.name) : domain.subdomain,
|
|
426
|
+
updatedAt: new Date()
|
|
427
|
+
}
|
|
428
|
+
)
|
|
429
|
+
|
|
430
|
+
let updatedBizplace: Bizplace = await tx.getRepository(Bizplace).findOne({ where: { id: existingBizplace.id } })
|
|
431
|
+
|
|
432
|
+
let data = {
|
|
433
|
+
id: updatedBizplace.id,
|
|
434
|
+
name: updatedBizplace.name,
|
|
435
|
+
description: updatedBizplace.description,
|
|
436
|
+
address: updatedBizplace.address,
|
|
437
|
+
postalCode: updatedBizplace.postalCode,
|
|
438
|
+
latlng: updatedBizplace.latlng,
|
|
439
|
+
status: updatedBizplace.status,
|
|
440
|
+
company: updatedBizplace.company
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
context.body = {
|
|
444
|
+
result: { data }
|
|
153
445
|
}
|
|
154
446
|
})
|
|
155
447
|
} catch (e) {
|
|
@@ -159,40 +451,216 @@ powrupRouter.post('/internal/account/create-bizplace', validationMiddleware, asy
|
|
|
159
451
|
})
|
|
160
452
|
|
|
161
453
|
powrupRouter.post('/internal/account/create-partner', validationMiddleware, async (context, next) => {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
454
|
+
try {
|
|
455
|
+
await getConnection().transaction(async (tx: EntityManager) => {
|
|
456
|
+
const { body } = context.request
|
|
457
|
+
await getIntegration(context, tx)
|
|
458
|
+
|
|
459
|
+
const { bizplaceId, partnerBizplaceId, requesterId } = body.data
|
|
460
|
+
|
|
461
|
+
const bizplace: Bizplace = await tx
|
|
462
|
+
.getRepository(Bizplace)
|
|
463
|
+
.findOne({ where: { id: bizplaceId }, relations: ['domain'] })
|
|
464
|
+
|
|
465
|
+
if (!bizplace) {
|
|
466
|
+
throw new ApiError('E04', `${bizplaceId} does not exist`)
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
const partnerBizplace: Bizplace = await tx
|
|
470
|
+
.getRepository(Bizplace)
|
|
471
|
+
.findOne({ where: { id: partnerBizplaceId }, relations: ['domain'] })
|
|
472
|
+
|
|
473
|
+
if (!partnerBizplace) {
|
|
474
|
+
throw new ApiError('E04', `${partnerBizplaceId} does not exist`)
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
const isExistingCustomer: boolean = Boolean(
|
|
478
|
+
await tx.getRepository(Partner).count({
|
|
479
|
+
where: { domain: bizplace.domain, partnerDomain: partnerBizplace.domain }
|
|
480
|
+
})
|
|
481
|
+
)
|
|
482
|
+
|
|
483
|
+
if (isExistingCustomer) {
|
|
484
|
+
throw new ApiError('E04', `${partnerBizplaceId} already registered as partner`)
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
let user: User = await tx.getRepository(User).findOne({ where: { id: requesterId } })
|
|
488
|
+
|
|
489
|
+
await tx.getRepository(Partner).save({
|
|
490
|
+
domain: bizplace.domain,
|
|
491
|
+
partnerDomain: partnerBizplace.domain,
|
|
492
|
+
requester: user || null,
|
|
493
|
+
approver: user || null
|
|
494
|
+
})
|
|
495
|
+
|
|
496
|
+
context.body = {
|
|
497
|
+
result: {
|
|
498
|
+
data: {
|
|
499
|
+
bizplace,
|
|
500
|
+
partnerBizplaceId
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
})
|
|
505
|
+
} catch (e) {
|
|
506
|
+
if (e instanceof ApiError) ApiErrorHandler(context, e)
|
|
507
|
+
else throwInternalServerError(context, e)
|
|
508
|
+
}
|
|
509
|
+
})
|
|
510
|
+
|
|
511
|
+
powrupRouter.post('/internal/account/terminate-partner', validationMiddleware, async (context, next) => {
|
|
512
|
+
try {
|
|
513
|
+
await getConnection().transaction(async (tx: EntityManager) => {
|
|
514
|
+
const { body } = context.request
|
|
515
|
+
await getIntegration(context, tx)
|
|
516
|
+
|
|
517
|
+
const { bizplaceId, partnerBizplaceId } = body.data
|
|
518
|
+
|
|
519
|
+
const bizplace: Bizplace = await tx
|
|
520
|
+
.getRepository(Bizplace)
|
|
521
|
+
.findOne({ where: { id: bizplaceId }, relations: ['domain'] })
|
|
522
|
+
|
|
523
|
+
if (!bizplace) {
|
|
524
|
+
throw new ApiError('E04', `${bizplaceId} does not exist`)
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
const partnerBizplace: Bizplace = await tx
|
|
528
|
+
.getRepository(Bizplace)
|
|
529
|
+
.findOne({ where: { id: partnerBizplaceId }, relations: ['domain'] })
|
|
530
|
+
|
|
531
|
+
if (!partnerBizplace) {
|
|
532
|
+
throw new ApiError('E04', `${partnerBizplaceId} does not exist`)
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
const partner: Partner = await tx.getRepository(Partner).findOne({
|
|
536
|
+
where: { domain: bizplace.domain, partnerDomain: partnerBizplace.domain }
|
|
537
|
+
})
|
|
538
|
+
|
|
539
|
+
if (!partner) {
|
|
540
|
+
throw new ApiError('E04', `partner does not exist`)
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// Remove partner
|
|
544
|
+
await tx.getRepository(Partner).delete(partner.id)
|
|
545
|
+
|
|
546
|
+
// Remove granted roles
|
|
547
|
+
await terminateGrantedRoles(bizplace.domain, partnerBizplace.domain, tx)
|
|
548
|
+
|
|
549
|
+
context.body = {
|
|
550
|
+
result: {
|
|
551
|
+
data: {
|
|
552
|
+
bizplace,
|
|
553
|
+
partnerBizplaceId
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
})
|
|
558
|
+
} catch (e) {
|
|
559
|
+
if (e instanceof ApiError) ApiErrorHandler(context, e)
|
|
560
|
+
else throwInternalServerError(context, e)
|
|
561
|
+
}
|
|
562
|
+
})
|
|
563
|
+
|
|
564
|
+
powrupRouter.post('/internal/account/invite-user', validationMiddleware, async (context, next) => {
|
|
565
|
+
try {
|
|
566
|
+
await getConnection().transaction(async (tx: EntityManager) => {
|
|
567
|
+
const { body } = context.request
|
|
568
|
+
await getIntegration(context, tx)
|
|
569
|
+
|
|
570
|
+
const { userId, bizplaceId } = body.data
|
|
571
|
+
|
|
572
|
+
const user: User = await tx.getRepository(User).findOne({ where: { id: userId }, relations: ['domains'] })
|
|
573
|
+
|
|
574
|
+
if (!user) {
|
|
575
|
+
throw new ApiError('E04', `${userId} does not exist`)
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
const bizplace: Bizplace = await tx
|
|
579
|
+
.getRepository(Bizplace)
|
|
580
|
+
.findOne({ where: { id: bizplaceId }, relations: ['domain'] })
|
|
581
|
+
|
|
582
|
+
if (!bizplace) {
|
|
583
|
+
throw new ApiError('E04', `${bizplaceId} does not exist`)
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
const existingDomains: Domain[] = user.domains
|
|
587
|
+
if (existingDomains.find((d: Domain) => d.id === bizplace.domain.id)) {
|
|
588
|
+
throw new ApiError('E04', `user already exists in ${bizplace.name}`)
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
user.domains = [...existingDomains, bizplace.domain]
|
|
592
|
+
let updatedUser: User = await tx.getRepository(User).save(user)
|
|
593
|
+
|
|
594
|
+
context.body = {
|
|
595
|
+
result: {
|
|
596
|
+
data: {
|
|
597
|
+
id: updatedUser.id,
|
|
598
|
+
name: updatedUser.name,
|
|
599
|
+
email: updatedUser.email,
|
|
600
|
+
domains: updatedUser.domains
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
})
|
|
605
|
+
} catch (e) {
|
|
606
|
+
if (e instanceof ApiError) ApiErrorHandler(context, e)
|
|
607
|
+
else throwInternalServerError(context, e)
|
|
608
|
+
}
|
|
609
|
+
})
|
|
610
|
+
|
|
611
|
+
powrupRouter.post('/internal/account/terminate-user', validationMiddleware, async (context, next) => {
|
|
612
|
+
try {
|
|
613
|
+
await getConnection().transaction(async (tx: EntityManager) => {
|
|
614
|
+
const { body } = context.request
|
|
615
|
+
await getIntegration(context, tx)
|
|
616
|
+
|
|
617
|
+
const { userId, bizplaceId } = body.data
|
|
618
|
+
|
|
619
|
+
const user: User = await tx
|
|
620
|
+
.getRepository(User)
|
|
621
|
+
.findOne({ where: { id: userId }, relations: ['domains', 'roles', 'roles.domain'] })
|
|
622
|
+
|
|
623
|
+
if (!user) {
|
|
624
|
+
throw new ApiError('E04', `${userId} does not exist`)
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
const bizplace: Bizplace = await tx
|
|
628
|
+
.getRepository(Bizplace)
|
|
629
|
+
.findOne({ where: { id: bizplaceId }, relations: ['domain'] })
|
|
630
|
+
|
|
631
|
+
if (!bizplace) {
|
|
632
|
+
throw new ApiError('E04', `${bizplaceId} does not exist`)
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
const targetDomainIdx: number = user.domains.findIndex(
|
|
636
|
+
(userDomain: Domain) => userDomain.id === bizplace.domain.id
|
|
637
|
+
)
|
|
638
|
+
if (targetDomainIdx < 0) {
|
|
639
|
+
throw new ApiError('E04', `${user.name} is not a member of ${bizplace.name}`)
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
// Remove domain relation with user
|
|
643
|
+
user.domains.splice(targetDomainIdx, 1)
|
|
644
|
+
|
|
645
|
+
// Remove domain's roles that user has
|
|
646
|
+
user.roles = user.roles.filter((role: Role) => role.domain.id !== bizplace.domain.id)
|
|
647
|
+
|
|
648
|
+
let updatedUser: User = await tx.getRepository(User).save(user)
|
|
649
|
+
|
|
650
|
+
context.body = {
|
|
651
|
+
result: {
|
|
652
|
+
data: {
|
|
653
|
+
id: updatedUser.id,
|
|
654
|
+
name: updatedUser.name,
|
|
655
|
+
email: updatedUser.email
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
})
|
|
660
|
+
} catch (e) {
|
|
661
|
+
if (e instanceof ApiError) ApiErrorHandler(context, e)
|
|
662
|
+
else throwInternalServerError(context, e)
|
|
663
|
+
}
|
|
196
664
|
})
|
|
197
665
|
|
|
198
666
|
async function getIntegration(context, tx) {
|