@things-factory/operato-hub 4.3.229 → 4.3.231

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 (47) hide show
  1. package/dist-server/routers/api/restful-apis/v1/marketplace/get-marketplace-order-shipping-list.js +1 -0
  2. package/dist-server/routers/api/restful-apis/v1/marketplace/get-marketplace-order-shipping-list.js.map +1 -1
  3. package/dist-server/routers/internal/apis/create-bizplace.js +40 -0
  4. package/dist-server/routers/internal/apis/create-bizplace.js.map +1 -0
  5. package/dist-server/routers/internal/apis/create-company.js +74 -0
  6. package/dist-server/routers/internal/apis/create-company.js.map +1 -0
  7. package/dist-server/routers/internal/apis/create-partner.js +41 -0
  8. package/dist-server/routers/internal/apis/create-partner.js.map +1 -0
  9. package/dist-server/routers/internal/apis/create-user.js +23 -0
  10. package/dist-server/routers/internal/apis/create-user.js.map +1 -0
  11. package/dist-server/routers/internal/apis/index.js +37 -0
  12. package/dist-server/routers/internal/apis/index.js.map +1 -0
  13. package/dist-server/routers/internal/apis/invite-user.js +32 -0
  14. package/dist-server/routers/internal/apis/invite-user.js.map +1 -0
  15. package/dist-server/routers/internal/apis/terminate-partner.js +38 -0
  16. package/dist-server/routers/internal/apis/terminate-partner.js.map +1 -0
  17. package/dist-server/routers/internal/apis/terminate-user.js +37 -0
  18. package/dist-server/routers/internal/apis/terminate-user.js.map +1 -0
  19. package/dist-server/routers/internal/apis/update-bizplace.js +52 -0
  20. package/dist-server/routers/internal/apis/update-bizplace.js.map +1 -0
  21. package/dist-server/routers/internal/apis/update-company.js +62 -0
  22. package/dist-server/routers/internal/apis/update-company.js.map +1 -0
  23. package/dist-server/routers/internal/apis/update-user.js +33 -0
  24. package/dist-server/routers/internal/apis/update-user.js.map +1 -0
  25. package/dist-server/routers/internal/index.js +1 -0
  26. package/dist-server/routers/internal/index.js.map +1 -1
  27. package/dist-server/routers/internal/powrup-router.js +38 -372
  28. package/dist-server/routers/internal/powrup-router.js.map +1 -1
  29. package/dist-server/routers/internal/utils/params.js +2 -2
  30. package/dist-server/routers/internal/utils/params.js.map +1 -1
  31. package/openapi/v1/marketplace.yaml +4 -0
  32. package/package.json +3 -3
  33. package/server/routers/api/restful-apis/v1/marketplace/get-marketplace-order-shipping-list.ts +1 -0
  34. package/server/routers/internal/apis/create-bizplace.ts +43 -0
  35. package/server/routers/internal/apis/create-company.ts +84 -0
  36. package/server/routers/internal/apis/create-partner.ts +50 -0
  37. package/server/routers/internal/apis/create-user.ts +33 -0
  38. package/server/routers/internal/apis/index.ts +21 -0
  39. package/server/routers/internal/apis/invite-user.ts +38 -0
  40. package/server/routers/internal/apis/terminate-partner.ts +45 -0
  41. package/server/routers/internal/apis/terminate-user.ts +45 -0
  42. package/server/routers/internal/apis/update-bizplace.ts +66 -0
  43. package/server/routers/internal/apis/update-company.ts +79 -0
  44. package/server/routers/internal/apis/update-user.ts +39 -0
  45. package/server/routers/internal/index.ts +1 -0
  46. package/server/routers/internal/powrup-router.ts +15 -483
  47. package/server/routers/internal/utils/params.ts +2 -2
@@ -0,0 +1,43 @@
1
+ import { User } from '@things-factory/auth-base'
2
+ import { Bizplace, Company } from '@things-factory/biz-base'
3
+
4
+ import { generateBizplaceDomain } from '../../../controllers/bizplace-initializer'
5
+ import { ApiError } from '../utils/error-util'
6
+
7
+ export async function createBizplace(request, context, tx) {
8
+ const { data: bizplace } = request
9
+ const company: Company = await tx.getRepository(Company).findOne({ where: { id: bizplace.companyId } })
10
+
11
+ const oldBizplace = await tx.getRepository(Bizplace).findOne({
12
+ where: {
13
+ refBizplaceId: bizplace.refBizplaceId
14
+ }
15
+ })
16
+ if (oldBizplace) {
17
+ throw new ApiError('E04', `${bizplace.name} is duplicated`)
18
+ }
19
+
20
+ let foundUser: User = await tx.getRepository(User).findOne({ where: { id: bizplace.userId } })
21
+
22
+ let createdBizplace: Bizplace = await tx.getRepository(Bizplace).save({
23
+ name: bizplace.name,
24
+ description: bizplace?.description || null,
25
+ address: bizplace?.address || '',
26
+ postalCode: bizplace?.postalCode || '',
27
+ latlng: bizplace?.latlng || '',
28
+ status: bizplace?.status || null,
29
+ refBizplaceId: bizplace?.refBizplaceId || null,
30
+ company,
31
+ updater: foundUser,
32
+ creator: foundUser
33
+ })
34
+
35
+ await generateBizplaceDomain(createdBizplace.id, foundUser, tx)
36
+
37
+ let data = {
38
+ id: createdBizplace.id,
39
+ name: createdBizplace.name
40
+ }
41
+
42
+ return data
43
+ }
@@ -0,0 +1,84 @@
1
+ import { Role, User } from '@things-factory/auth-base'
2
+ import { Bizplace, Company } from '@things-factory/biz-base'
3
+ import { Domain } from '@things-factory/shell'
4
+ import { slugger } from '@things-factory/utils'
5
+
6
+ import { generateBusinessRoles } from '../../../controllers/company-initializer'
7
+ import { ApiError } from '../utils/error-util'
8
+
9
+ export async function createCompany(request, context, tx) {
10
+ const { name, description, brn, address, postalCode, countryCode, type, status, userId, refCompanyId } = request.data
11
+
12
+ let foundUser: User = await tx.getRepository(User).findOne({ where: { id: userId }, relations: ['domains', 'roles'] })
13
+
14
+ const subdomain = slugger(name)
15
+
16
+ //check for existing company
17
+ const oldCompany = await tx.getRepository(Company).findOne({
18
+ where: {
19
+ refCompanyId
20
+ }
21
+ })
22
+
23
+ if (oldCompany) {
24
+ throw new ApiError('E04', `${name} is duplicated`)
25
+ }
26
+
27
+ const oldDomain = await tx.getRepository(Domain).findOne({
28
+ where: {
29
+ subdomain
30
+ }
31
+ })
32
+
33
+ if (oldDomain) {
34
+ throw new ApiError('E04', `${subdomain} is duplicated`)
35
+ }
36
+
37
+ // create company
38
+ const domain: Domain = await tx.getRepository(Domain).save({
39
+ subdomain,
40
+ name,
41
+ extType: 'company',
42
+ creator: foundUser,
43
+ updater: foundUser
44
+ })
45
+
46
+ const company: Company = await tx.getRepository(Company).save({
47
+ name,
48
+ description,
49
+ brn,
50
+ address,
51
+ postalCode,
52
+ countryCode: countryCode || 'my',
53
+ type,
54
+ status,
55
+ domain,
56
+ refCompanyId: refCompanyId || null,
57
+ creator: foundUser,
58
+ updater: foundUser
59
+ })
60
+
61
+ const userHavingRoles: Role[] = foundUser.roles
62
+ const domainRoles: Role[] = await generateBusinessRoles(domain, tx)
63
+
64
+ foundUser.domains = [...foundUser.domains, domain]
65
+ foundUser.roles = [...userHavingRoles, domainRoles.find(r => r.name === 'BUSINESS-OWNER')]
66
+
67
+ await tx.getRepository(User).save(foundUser)
68
+ await tx.getRepository(Bizplace).save({
69
+ name: company.name,
70
+ description: company.description,
71
+ address: company.address,
72
+ postalCode: company.postalCode || '',
73
+ latlng: '',
74
+ domain,
75
+ company
76
+ })
77
+
78
+ let data = {
79
+ id: company.id,
80
+ name: company.name
81
+ }
82
+
83
+ return data
84
+ }
@@ -0,0 +1,50 @@
1
+ import { Partner, User } from '@things-factory/auth-base'
2
+ import { Bizplace } from '@things-factory/biz-base'
3
+
4
+ import { ApiError } from '../utils/error-util'
5
+
6
+ export async function createPartner(request, context, tx) {
7
+ const { bizplaceId, partnerBizplaceId, requesterId } = request.data
8
+
9
+ const bizplace: Bizplace = await tx
10
+ .getRepository(Bizplace)
11
+ .findOne({ where: { id: bizplaceId }, relations: ['domain'] })
12
+
13
+ if (!bizplace) {
14
+ throw new ApiError('E04', `${bizplaceId} does not exist`)
15
+ }
16
+
17
+ const partnerBizplace: Bizplace = await tx
18
+ .getRepository(Bizplace)
19
+ .findOne({ where: { id: partnerBizplaceId }, relations: ['domain'] })
20
+
21
+ if (!partnerBizplace) {
22
+ throw new ApiError('E04', `${partnerBizplaceId} does not exist`)
23
+ }
24
+
25
+ const isExistingCustomer: boolean = Boolean(
26
+ await tx.getRepository(Partner).count({
27
+ where: { domain: bizplace.domain, partnerDomain: partnerBizplace.domain }
28
+ })
29
+ )
30
+
31
+ if (isExistingCustomer) {
32
+ throw new ApiError('E04', `${partnerBizplaceId} already registered as partner`)
33
+ }
34
+
35
+ let user: User = await tx.getRepository(User).findOne({ where: { id: requesterId } })
36
+
37
+ await tx.getRepository(Partner).save({
38
+ domain: bizplace.domain,
39
+ partnerDomain: partnerBizplace.domain,
40
+ requester: user || null,
41
+ approver: user || null
42
+ })
43
+
44
+ let data = {
45
+ bizplaceId,
46
+ partnerBizplaceId
47
+ }
48
+
49
+ return data
50
+ }
@@ -0,0 +1,33 @@
1
+ import { NewUser, User } from '@things-factory/auth-base'
2
+
3
+ import { ApiError } from '../utils/error-util'
4
+
5
+ export async function createUser(request, context, tx) {
6
+ let user: NewUser = request?.data || {}
7
+ const { email } = user
8
+ user.email = email.trim()
9
+
10
+ const oldUser: User = await tx.getRepository(User).findOne({ where: { refUserId: user.refUserId } })
11
+ if (oldUser) {
12
+ throw new ApiError('E04', context.t('error.x already exists in y', { x: context.t('field.user'), y: 'operato' }))
13
+ }
14
+
15
+ const salt = User.generateSalt()
16
+
17
+ let createdUser: User = await tx.getRepository(User).save({
18
+ ...user,
19
+ refUserId: user?.refUserId,
20
+ userType: user?.userType || 'user',
21
+ status: user?.status || 'inactive',
22
+ salt: user?.salt || salt,
23
+ passwordUpdatedAt: new Date(),
24
+ password: user?.password || User.encode(user.password, salt)
25
+ })
26
+
27
+ let data = {
28
+ id: createdUser.id,
29
+ email: createdUser.email
30
+ }
31
+
32
+ return data
33
+ }
@@ -0,0 +1,21 @@
1
+ import './create-user'
2
+ import './update-user'
3
+ import './create-company'
4
+ import './update-company'
5
+ import './create-bizplace'
6
+ import './update-bizplace'
7
+ import './create-partner'
8
+ import './terminate-partner'
9
+ import './invite-user'
10
+ import './terminate-user'
11
+
12
+ export * from './create-user'
13
+ export * from './update-user'
14
+ export * from './create-company'
15
+ export * from './update-company'
16
+ export * from './create-bizplace'
17
+ export * from './update-bizplace'
18
+ export * from './create-partner'
19
+ export * from './terminate-partner'
20
+ export * from './invite-user'
21
+ export * from './terminate-user'
@@ -0,0 +1,38 @@
1
+ import { User } from '@things-factory/auth-base'
2
+ import { Bizplace } from '@things-factory/biz-base'
3
+ import { Domain } from '@things-factory/shell'
4
+
5
+ import { ApiError } from '../utils/error-util'
6
+
7
+ export async function inviteUser(request, context, tx) {
8
+ const { userId, bizplaceId } = request.data
9
+
10
+ const user: User = await tx.getRepository(User).findOne({ where: { id: userId }, relations: ['domains'] })
11
+
12
+ if (!user) {
13
+ throw new ApiError('E04', `${userId} does not exist`)
14
+ }
15
+
16
+ const bizplace: Bizplace = await tx
17
+ .getRepository(Bizplace)
18
+ .findOne({ where: { id: bizplaceId }, relations: ['domain'] })
19
+
20
+ if (!bizplace) {
21
+ throw new ApiError('E04', `${bizplaceId} does not exist`)
22
+ }
23
+
24
+ const existingDomains: Domain[] = user.domains
25
+ if (existingDomains.find((d: Domain) => d.id === bizplace.domain.id)) {
26
+ throw new ApiError('E04', `user already exists in ${bizplace.name}`)
27
+ }
28
+
29
+ user.domains = [...existingDomains, bizplace.domain]
30
+ await tx.getRepository(User).save(user)
31
+
32
+ let data = {
33
+ bizplaceId,
34
+ userId
35
+ }
36
+
37
+ return data
38
+ }
@@ -0,0 +1,45 @@
1
+ import { Partner, terminateGrantedRoles } from '@things-factory/auth-base'
2
+ import { Bizplace } from '@things-factory/biz-base'
3
+
4
+ import { ApiError } from '../utils/error-util'
5
+
6
+ export async function terminatePartner(request, context, tx) {
7
+ const { bizplaceId, partnerBizplaceId } = request.data
8
+
9
+ const bizplace: Bizplace = await tx
10
+ .getRepository(Bizplace)
11
+ .findOne({ where: { id: bizplaceId }, relations: ['domain'] })
12
+
13
+ if (!bizplace) {
14
+ throw new ApiError('E04', `${bizplaceId} does not exist`)
15
+ }
16
+
17
+ const partnerBizplace: Bizplace = await tx
18
+ .getRepository(Bizplace)
19
+ .findOne({ where: { id: partnerBizplaceId }, relations: ['domain'] })
20
+
21
+ if (!partnerBizplace) {
22
+ throw new ApiError('E04', `${partnerBizplaceId} does not exist`)
23
+ }
24
+
25
+ const partner: Partner = await tx.getRepository(Partner).findOne({
26
+ where: { domain: bizplace.domain, partnerDomain: partnerBizplace.domain }
27
+ })
28
+
29
+ if (!partner) {
30
+ throw new ApiError('E04', `partner does not exist`)
31
+ }
32
+
33
+ // Remove partner
34
+ await tx.getRepository(Partner).delete(partner.id)
35
+
36
+ // Remove granted roles
37
+ await terminateGrantedRoles(bizplace.domain, partnerBizplace.domain, tx)
38
+
39
+ let data = {
40
+ bizplaceId,
41
+ partnerBizplaceId
42
+ }
43
+
44
+ return data
45
+ }
@@ -0,0 +1,45 @@
1
+ import { Role, User } from '@things-factory/auth-base'
2
+ import { Bizplace } from '@things-factory/biz-base'
3
+ import { Domain } from '@things-factory/shell'
4
+
5
+ import { ApiError } from '../utils/error-util'
6
+
7
+ export async function terminateUser(request, context, tx) {
8
+ const { userId, bizplaceId } = request.data
9
+
10
+ const user: User = await tx
11
+ .getRepository(User)
12
+ .findOne({ where: { id: userId }, relations: ['domains', 'roles', 'roles.domain'] })
13
+
14
+ if (!user) {
15
+ throw new ApiError('E04', `${userId} does not exist`)
16
+ }
17
+
18
+ const bizplace: Bizplace = await tx
19
+ .getRepository(Bizplace)
20
+ .findOne({ where: { id: bizplaceId }, relations: ['domain'] })
21
+
22
+ if (!bizplace) {
23
+ throw new ApiError('E04', `${bizplaceId} does not exist`)
24
+ }
25
+
26
+ const targetDomainIdx: number = user.domains.findIndex((userDomain: Domain) => userDomain.id === bizplace.domain.id)
27
+ if (targetDomainIdx < 0) {
28
+ throw new ApiError('E04', `${user.name} is not a member of ${bizplace.name}`)
29
+ }
30
+
31
+ // Remove domain relation with user
32
+ user.domains.splice(targetDomainIdx, 1)
33
+
34
+ // Remove domain's roles that user has
35
+ user.roles = user.roles.filter((role: Role) => role.domain.id !== bizplace.domain.id)
36
+
37
+ await tx.getRepository(User).save(user)
38
+
39
+ let data = {
40
+ bizplaceId,
41
+ userId
42
+ }
43
+
44
+ return data
45
+ }
@@ -0,0 +1,66 @@
1
+ import { Bizplace, Company } from '@things-factory/biz-base'
2
+ import { Domain } from '@things-factory/shell'
3
+ import { slugger } from '@things-factory/utils'
4
+
5
+ import { ApiError } from '../utils/error-util'
6
+
7
+ export async function updateBizplace(request, context, tx) {
8
+ const { data: bizplace } = request
9
+
10
+ const existingBizplace: Bizplace = await tx.getRepository(Bizplace).findOne({
11
+ where: {
12
+ id: bizplace.id
13
+ },
14
+ relations: ['company', 'domain']
15
+ })
16
+ if (!existingBizplace) {
17
+ throw new ApiError('E04', `${bizplace.id} does not exist`)
18
+ }
19
+
20
+ const company: Company = existingBizplace.company
21
+ const domain: Domain = existingBizplace.domain
22
+
23
+ await tx.getRepository(Bizplace).update(
24
+ { id: existingBizplace.id },
25
+ {
26
+ name: bizplace?.name || existingBizplace.name,
27
+ description: bizplace?.description || existingBizplace.description,
28
+ address: bizplace?.address || existingBizplace.address,
29
+ postalCode: bizplace?.postalCode || existingBizplace.postalCode,
30
+ latlng: bizplace?.latlng || existingBizplace.latlng,
31
+ status: bizplace?.status || existingBizplace.status,
32
+ updatedAt: new Date()
33
+ }
34
+ )
35
+
36
+ if (existingBizplace.domain.extType == 'company') {
37
+ await tx.getRepository(Company).update(
38
+ { id: company.id },
39
+ {
40
+ name: bizplace?.name || company.name,
41
+ description: bizplace?.description || company.description,
42
+ address: bizplace?.address || company.address,
43
+ postalCode: bizplace?.postalCode || company.postalCode || '',
44
+ updatedAt: new Date()
45
+ }
46
+ )
47
+ }
48
+
49
+ await tx.getRepository(Domain).update(
50
+ { id: domain.id },
51
+ {
52
+ name: bizplace?.name ? bizplace.name : domain.name,
53
+ subdomain: bizplace?.name ? slugger(bizplace.name) : domain.subdomain,
54
+ updatedAt: new Date()
55
+ }
56
+ )
57
+
58
+ let updatedBizplace: Bizplace = await tx.getRepository(Bizplace).findOne({ where: { id: existingBizplace.id } })
59
+
60
+ let data = {
61
+ id: updatedBizplace.id,
62
+ name: updatedBizplace.name
63
+ }
64
+
65
+ return data
66
+ }
@@ -0,0 +1,79 @@
1
+ import { User } from '@things-factory/auth-base'
2
+ import { Bizplace, Company } from '@things-factory/biz-base'
3
+ import { Domain } from '@things-factory/shell'
4
+ import { slugger } from '@things-factory/utils'
5
+
6
+ import { ApiError } from '../utils/error-util'
7
+
8
+ export async function updateCompany(request, context, tx) {
9
+ const { id, name, description, brn, address, postalCode, countryCode, type, status, userId } = request.data
10
+
11
+ let foundUser: User = await tx.getRepository(User).findOne({ where: { id: userId }, relations: ['domains', 'roles'] })
12
+
13
+ const subdomain = slugger(name)
14
+
15
+ //check for existing company
16
+ const existingCompany: Company = await tx.getRepository(Company).findOne({
17
+ where: { id }
18
+ })
19
+
20
+ if (!existingCompany) {
21
+ throw new ApiError('E04', `${name} does not exist`)
22
+ }
23
+
24
+ const existingDomain: Domain = await tx.getRepository(Domain).findOne({
25
+ where: {
26
+ subdomain
27
+ },
28
+ relations: ['bizplace']
29
+ })
30
+
31
+ if (!existingDomain) {
32
+ throw new ApiError('E04', `${subdomain} does not exist`)
33
+ }
34
+
35
+ // update company
36
+ await tx.getRepository(Domain).update(
37
+ { id: existingDomain.id },
38
+ {
39
+ subdomain: name ? subdomain : existingDomain.subdomain,
40
+ name: name ? name : existingDomain.name,
41
+ updater: foundUser
42
+ }
43
+ )
44
+
45
+ await tx.getRepository(Company).update(
46
+ { id: existingCompany.id },
47
+ {
48
+ name: name || existingCompany.name,
49
+ description: description || existingCompany.description,
50
+ brn: brn || existingCompany.brn,
51
+ address: address || existingCompany.address,
52
+ countryCode: countryCode || existingCompany.countryCode,
53
+ postalCode: postalCode || existingCompany.postalCode || '',
54
+ updater: foundUser
55
+ }
56
+ )
57
+ await tx.getRepository(Bizplace).update(
58
+ { id: existingDomain.bizplace.id },
59
+ {
60
+ name: name || existingDomain.bizplace.name,
61
+ description: description || existingDomain.bizplace.description,
62
+ address: address || existingDomain.bizplace.address,
63
+ postalCode: postalCode || existingDomain.bizplace.postalCode || '',
64
+ latlng: '',
65
+ updater: foundUser
66
+ }
67
+ )
68
+
69
+ const updatedCompany: Company = await tx.getRepository(Company).findOne({
70
+ where: { id }
71
+ })
72
+
73
+ let data = {
74
+ id: updatedCompany.id,
75
+ name: updatedCompany.name
76
+ }
77
+
78
+ return data
79
+ }
@@ -0,0 +1,39 @@
1
+ import { NewUser, User } from '@things-factory/auth-base'
2
+
3
+ import { ApiError } from '../utils/error-util'
4
+
5
+ export async function updateUser(request, context, tx) {
6
+ let user: NewUser = request?.data || {}
7
+
8
+ const existingUser: User = await tx.getRepository(User).findOne({ where: { id: user.id } })
9
+ if (!existingUser) {
10
+ throw new ApiError('E04', context.t('error.x does not exists in y', { x: context.t('field.user'), y: 'operato' }))
11
+ }
12
+
13
+ const salt = User.generateSalt()
14
+
15
+ await tx.getRepository(User).update(
16
+ { id: existingUser.id },
17
+ {
18
+ name: user?.name || existingUser.name,
19
+ status: user?.status || existingUser.status,
20
+ userType: user?.userType || existingUser.userType,
21
+ salt: user?.password ? (user?.salt ? user.salt : salt) : existingUser.salt,
22
+ password: user?.password
23
+ ? user?.salt
24
+ ? user.password
25
+ : User.encode(user.password, salt)
26
+ : existingUser.password,
27
+ passwordUpdatedAt: user?.password ? new Date() : existingUser.passwordUpdatedAt
28
+ }
29
+ )
30
+
31
+ let updatedUser: User = await tx.getRepository(User).findOne(existingUser.id)
32
+
33
+ let data = {
34
+ id: updatedUser.id,
35
+ email: updatedUser.email
36
+ }
37
+
38
+ return data
39
+ }
@@ -1,3 +1,4 @@
1
1
  import './powrup-router'
2
2
  import './middlewares'
3
3
  import './utils'
4
+ import './apis'