@things-factory/integration-base 7.0.0-alpha.1 → 7.0.0-alpha.18

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 (86) hide show
  1. package/dist-server/controllers/index.js +5 -0
  2. package/dist-server/controllers/index.js.map +1 -0
  3. package/dist-server/controllers/scenario-controller.js +87 -0
  4. package/dist-server/controllers/scenario-controller.js.map +1 -0
  5. package/dist-server/engine/connection-manager.js +37 -6
  6. package/dist-server/engine/connection-manager.js.map +1 -1
  7. package/dist-server/engine/connector/graphql-connector.js +6 -6
  8. package/dist-server/engine/connector/graphql-connector.js.map +1 -1
  9. package/dist-server/engine/connector/operato-connector.js +19 -22
  10. package/dist-server/engine/connector/operato-connector.js.map +1 -1
  11. package/dist-server/engine/connector/oracle-connector.js +113 -17
  12. package/dist-server/engine/connector/oracle-connector.js.map +1 -1
  13. package/dist-server/engine/connector/proxy-connector.js +44 -0
  14. package/dist-server/engine/connector/proxy-connector.js.map +1 -0
  15. package/dist-server/engine/edge-client.js +38 -0
  16. package/dist-server/engine/edge-client.js.map +1 -0
  17. package/dist-server/engine/index.js +1 -0
  18. package/dist-server/engine/index.js.map +1 -1
  19. package/dist-server/engine/task/oracle-procedure.js +1 -15
  20. package/dist-server/engine/task/oracle-procedure.js.map +1 -1
  21. package/dist-server/engine/task/script.js +1 -0
  22. package/dist-server/engine/task/script.js.map +1 -1
  23. package/dist-server/engine/task/utils/headless-pool-for-scenario.js +1 -1
  24. package/dist-server/engine/task/utils/headless-pool-for-scenario.js.map +1 -1
  25. package/dist-server/engine/types.js.map +1 -1
  26. package/dist-server/index.js +1 -0
  27. package/dist-server/index.js.map +1 -1
  28. package/dist-server/restful/unstable/run-scenario.js.map +1 -1
  29. package/dist-server/restful/unstable/start-scenario.js +1 -1
  30. package/dist-server/restful/unstable/start-scenario.js.map +1 -1
  31. package/dist-server/restful/unstable/stop-scenario.js +1 -1
  32. package/dist-server/restful/unstable/stop-scenario.js.map +1 -1
  33. package/dist-server/service/connection/connection-mutation.js +4 -8
  34. package/dist-server/service/connection/connection-mutation.js.map +1 -1
  35. package/dist-server/service/connection/connection-query.js +17 -14
  36. package/dist-server/service/connection/connection-query.js.map +1 -1
  37. package/dist-server/service/connection/connection-subscription.js +2 -2
  38. package/dist-server/service/connection/connection-subscription.js.map +1 -1
  39. package/dist-server/service/connection/connection-type.js +32 -8
  40. package/dist-server/service/connection/connection-type.js.map +1 -1
  41. package/dist-server/service/scenario-instance/scenario-instance-mutation.js +4 -71
  42. package/dist-server/service/scenario-instance/scenario-instance-mutation.js.map +1 -1
  43. package/dist-server/service/scenario-instance/scenario-instance-type.js +18 -5
  44. package/dist-server/service/scenario-instance/scenario-instance-type.js.map +1 -1
  45. package/dist-server/tsconfig.tsbuildinfo +1 -1
  46. package/helps/integration/concept/script-internal-variables.ja.md +21 -1
  47. package/helps/integration/concept/script-internal-variables.ko.md +17 -0
  48. package/helps/integration/concept/script-internal-variables.md +18 -0
  49. package/helps/integration/concept/script-internal-variables.ms.md +19 -1
  50. package/helps/integration/concept/script-internal-variables.zh.md +18 -0
  51. package/helps/integration/connector/oracle-connector.ja.md +90 -0
  52. package/helps/integration/connector/oracle-connector.ko.md +87 -0
  53. package/helps/integration/connector/oracle-connector.md +46 -25
  54. package/helps/integration/connector/oracle-connector.ms.md +87 -0
  55. package/helps/integration/connector/oracle-connector.zh.md +87 -0
  56. package/helps/integration/task/script.ja.md +1 -1
  57. package/helps/integration/task/script.ko.md +1 -1
  58. package/helps/integration/task/script.md +1 -1
  59. package/helps/integration/task/script.ms.md +1 -1
  60. package/helps/integration/task/script.zh.md +1 -1
  61. package/openapi/unstable/scenario.yaml +100 -100
  62. package/openapi/unstable.yaml +11 -11
  63. package/package.json +7 -11
  64. package/server/controllers/index.ts +1 -0
  65. package/server/controllers/scenario-controller.ts +116 -0
  66. package/server/engine/connection-manager.ts +49 -7
  67. package/server/engine/connector/graphql-connector.ts +8 -10
  68. package/server/engine/connector/operato-connector.ts +22 -32
  69. package/server/engine/connector/oracle-connector.ts +132 -22
  70. package/server/engine/connector/proxy-connector.ts +53 -0
  71. package/server/engine/edge-client.ts +45 -0
  72. package/server/engine/index.ts +1 -0
  73. package/server/engine/task/oracle-procedure.ts +1 -16
  74. package/server/engine/task/script.ts +1 -0
  75. package/server/engine/task/utils/headless-pool-for-scenario.ts +1 -1
  76. package/server/engine/types.ts +45 -46
  77. package/server/index.ts +1 -0
  78. package/server/restful/unstable/run-scenario.ts +0 -1
  79. package/server/restful/unstable/start-scenario.ts +1 -1
  80. package/server/restful/unstable/stop-scenario.ts +1 -1
  81. package/server/service/connection/connection-mutation.ts +9 -29
  82. package/server/service/connection/connection-query.ts +13 -12
  83. package/server/service/connection/connection-subscription.ts +2 -2
  84. package/server/service/connection/connection-type.ts +53 -41
  85. package/server/service/scenario-instance/scenario-instance-mutation.ts +10 -121
  86. package/server/service/scenario-instance/scenario-instance-type.ts +24 -13
package/server/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './migrations'
2
2
  export * from './engine'
3
+ export * from './controllers'
3
4
  export * from './service'
4
5
 
5
6
  import './routes'
@@ -1,7 +1,6 @@
1
1
  import gql from 'graphql-tag'
2
2
 
3
3
  import { restfulApiRouter as router } from '@things-factory/api'
4
- import { ScenarioInstanceStatus } from '../../service/scenario-instance/scenario-instance-type'
5
4
 
6
5
  const debug = require('debug')('things-factory:integration-base:restful:unstable:run-scenario')
7
6
 
@@ -12,7 +12,7 @@ router.post('/unstable/start-scenario/:scenarioName', async (context, next) => {
12
12
 
13
13
  context.body = await client.mutate({
14
14
  mutation: gql`
15
- mutation($instanceName: String, $scenarioName: String!, $variables: Object) {
15
+ mutation ($instanceName: String, $scenarioName: String!, $variables: Object) {
16
16
  startScenario(instanceName: $instanceName, scenarioName: $scenarioName, variables: $variables) {
17
17
  instanceName
18
18
  scenarioName
@@ -11,7 +11,7 @@ router.post('/unstable/stop-scenario/:instanceName', async (context, next) => {
11
11
 
12
12
  context.body = await client.mutate({
13
13
  mutation: gql`
14
- mutation($instanceName: String!) {
14
+ mutation ($instanceName: String!) {
15
15
  stopScenario(instanceName: $instanceName) {
16
16
  instanceName
17
17
  scenarioName
@@ -10,10 +10,7 @@ import { Connection, ConnectionPatch, ConnectionStatus, NewConnection } from './
10
10
  export class ConnectionMutation {
11
11
  @Directive('@transaction')
12
12
  @Mutation(returns => Connection, { description: 'To create new connection' })
13
- async createConnection(
14
- @Arg('connection') connection: NewConnection,
15
- @Ctx() context: ResolverContext
16
- ): Promise<Connection> {
13
+ async createConnection(@Arg('connection') connection: NewConnection, @Ctx() context: ResolverContext): Promise<Connection> {
17
14
  const { domain, user, tx } = context.state
18
15
 
19
16
  return await tx.getRepository(Connection).save({
@@ -26,11 +23,7 @@ export class ConnectionMutation {
26
23
 
27
24
  @Directive('@transaction')
28
25
  @Mutation(returns => Connection, { description: 'To modify connection information' })
29
- async updateConnection(
30
- @Arg('name') name: string,
31
- @Arg('patch') patch: ConnectionPatch,
32
- @Ctx() context: ResolverContext
33
- ): Promise<Connection> {
26
+ async updateConnection(@Arg('name') name: string, @Arg('patch') patch: ConnectionPatch, @Ctx() context: ResolverContext): Promise<Connection> {
34
27
  const { domain, user, tx } = context.state
35
28
 
36
29
  const repository = tx.getRepository(Connection)
@@ -47,10 +40,7 @@ export class ConnectionMutation {
47
40
 
48
41
  @Directive('@transaction')
49
42
  @Mutation(returns => [Connection], { description: "To modify multiple connections' information" })
50
- async updateMultipleConnection(
51
- @Arg('patches', type => [ConnectionPatch]) patches: ConnectionPatch[],
52
- @Ctx() context: ResolverContext
53
- ): Promise<Connection[]> {
43
+ async updateMultipleConnection(@Arg('patches', type => [ConnectionPatch]) patches: ConnectionPatch[], @Ctx() context: ResolverContext): Promise<Connection[]> {
54
44
  const { domain, user, tx } = context.state
55
45
 
56
46
  let results = []
@@ -102,10 +92,7 @@ export class ConnectionMutation {
102
92
 
103
93
  @Directive('@transaction')
104
94
  @Mutation(returns => Boolean, { description: 'To delete multiple connections' })
105
- async deleteConnections(
106
- @Arg('names', type => [String]) names: string[],
107
- @Ctx() context: ResolverContext
108
- ): Promise<boolean> {
95
+ async deleteConnections(@Arg('names', type => [String]) names: string[], @Ctx() context: ResolverContext): Promise<boolean> {
109
96
  const { domain, tx } = context.state
110
97
 
111
98
  await tx.getRepository(Connection).delete({
@@ -122,7 +109,7 @@ export class ConnectionMutation {
122
109
  var repository = getRepository(Connection)
123
110
  var connection = await repository.findOne({
124
111
  where: { domain: { id: domain.id }, name },
125
- relations: ['domain']
112
+ relations: ['domain', 'edge']
126
113
  })
127
114
 
128
115
  await connection.connect()
@@ -130,9 +117,7 @@ export class ConnectionMutation {
130
117
 
131
118
  return {
132
119
  ...connection,
133
- state: ConnectionManager.getConnectionInstance(connection)
134
- ? ConnectionStatus.CONNECTED
135
- : ConnectionStatus.DISCONNECTED
120
+ state: ConnectionManager.getConnectionInstance(connection) ? ConnectionStatus.CONNECTED : ConnectionStatus.DISCONNECTED
136
121
  } as Connection
137
122
  }
138
123
 
@@ -143,7 +128,7 @@ export class ConnectionMutation {
143
128
  var repository = getRepository(Connection)
144
129
  var connection = await repository.findOne({
145
130
  where: { domain: { id: domain.id }, name },
146
- relations: ['domain']
131
+ relations: ['domain', 'edge']
147
132
  })
148
133
 
149
134
  await connection.disconnect()
@@ -151,19 +136,14 @@ export class ConnectionMutation {
151
136
 
152
137
  return {
153
138
  ...connection,
154
- state: ConnectionManager.getConnectionInstance(connection)
155
- ? ConnectionStatus.CONNECTED
156
- : ConnectionStatus.DISCONNECTED
139
+ state: ConnectionManager.getConnectionInstance(connection) ? ConnectionStatus.CONNECTED : ConnectionStatus.DISCONNECTED
157
140
  }
158
141
  }
159
142
 
160
143
  @Directive('@transaction')
161
144
  // @Directive('@privilege(category: "connection", privilege: "mutation", domainOwnerGranted: true)')
162
145
  @Mutation(returns => Boolean, { description: 'To import multiple connections' })
163
- async importConnections(
164
- @Arg('connections', type => [ConnectionPatch]) connections: Connection[],
165
- @Ctx() context: ResolverContext
166
- ): Promise<boolean> {
146
+ async importConnections(@Arg('connections', type => [ConnectionPatch]) connections: Connection[], @Ctx() context: ResolverContext): Promise<boolean> {
167
147
  const { tx, domain, user } = context.state
168
148
 
169
149
  const repository = tx.getRepository(Connection)
@@ -1,6 +1,6 @@
1
1
  import { Arg, Args, Ctx, FieldResolver, Query, Resolver, Root } from 'type-graphql'
2
2
 
3
- import { User } from '@things-factory/auth-base'
3
+ import { Appliance, User } from '@things-factory/auth-base'
4
4
  import { Domain, getQueryBuilderFromListParams, getRepository, ListParam } from '@things-factory/shell'
5
5
 
6
6
  import { ConnectionManager } from '../../engine/connection-manager'
@@ -19,9 +19,7 @@ export class ConnectionQuery {
19
19
 
20
20
  return {
21
21
  ...connection,
22
- state: ConnectionManager.getConnectionInstance(connection)
23
- ? ConnectionStatus.CONNECTED
24
- : ConnectionStatus.DISCONNECTED
22
+ state: ConnectionManager.getConnectionInstance(connection) ? ConnectionStatus.CONNECTED : ConnectionStatus.DISCONNECTED
25
23
  } as Connection
26
24
  }
27
25
 
@@ -40,9 +38,7 @@ export class ConnectionQuery {
40
38
  const [items, total] = await queryBuilder.leftJoinAndSelect('connection.domain', 'domain').getManyAndCount()
41
39
 
42
40
  items.forEach(connection => {
43
- connection['state'] = ConnectionManager.getConnectionInstance(connection)
44
- ? ConnectionStatus.CONNECTED
45
- : ConnectionStatus.DISCONNECTED
41
+ connection['state'] = ConnectionManager.getConnectionInstance(connection) ? ConnectionStatus.CONNECTED : ConnectionStatus.DISCONNECTED
46
42
  })
47
43
 
48
44
  return { items, total }
@@ -52,26 +48,31 @@ export class ConnectionQuery {
52
48
  fetchConnectionState(@Arg('name') name: string, @Ctx() context: ResolverContext): ConnectionState {
53
49
  const { domain } = context.state
54
50
 
55
- var connection = ConnectionManager.getConnectionInstanceByName(domain, name)
51
+ const connection = ConnectionManager.getConnectionInstanceEntityByName(domain, name)
56
52
 
57
53
  return {
58
- name,
54
+ ...connection,
59
55
  state: connection ? ConnectionStatus.CONNECTED : ConnectionStatus.DISCONNECTED
60
56
  }
61
57
  }
62
58
 
59
+ @FieldResolver(type => Domain)
60
+ async edge(@Root() connection: Connection) {
61
+ return connection.edgeId && (await getRepository(Appliance).findOneBy({ id: connection.edgeId }))
62
+ }
63
+
63
64
  @FieldResolver(type => Domain)
64
65
  async domain(@Root() connection: Connection) {
65
- return connection.domain || (await getRepository(Domain).findOneBy({ id: connection.domainId }))
66
+ return connection.domain || (connection.domainId && (await getRepository(Domain).findOneBy({ id: connection.domainId })))
66
67
  }
67
68
 
68
69
  @FieldResolver(type => User)
69
70
  async updater(@Root() connection: Connection): Promise<User> {
70
- return await getRepository(User).findOneBy({ id: connection.updaterId })
71
+ return connection.updaterId && (await getRepository(User).findOneBy({ id: connection.updaterId }))
71
72
  }
72
73
 
73
74
  @FieldResolver(type => User)
74
75
  async creator(@Root() connection: Connection): Promise<User> {
75
- return await getRepository(User).findOneBy({ id: connection.creatorId })
76
+ return connection.creatorId && (await getRepository(User).findOneBy({ id: connection.creatorId }))
76
77
  }
77
78
  }
@@ -26,14 +26,14 @@ export class ConnectionSubscription {
26
26
  }
27
27
 
28
28
  process.nextTick(async () => {
29
- var where = { domain }
29
+ var where = { domain: { id: domain.id } }
30
30
  if (name) {
31
31
  where['name'] = name
32
32
  }
33
33
 
34
34
  var connections = await getRepository(Connection).find({
35
35
  where,
36
- relations: ['domain']
36
+ relations: ['domain', 'edge']
37
37
  })
38
38
 
39
39
  connections.forEach(connection => {
@@ -1,20 +1,12 @@
1
1
  import { Field, ID, InputType, Int, ObjectType, registerEnumType } from 'type-graphql'
2
- import {
3
- Column,
4
- CreateDateColumn,
5
- Entity,
6
- Index,
7
- ManyToOne,
8
- PrimaryGeneratedColumn,
9
- RelationId,
10
- UpdateDateColumn
11
- } from 'typeorm'
12
-
13
- import { User } from '@things-factory/auth-base'
2
+ import { Column, CreateDateColumn, Entity, Index, ManyToOne, PrimaryGeneratedColumn, RelationId, UpdateDateColumn } from 'typeorm'
3
+
4
+ import { User, Appliance } from '@things-factory/auth-base'
14
5
  import { logger } from '@things-factory/env'
15
- import { Domain } from '@things-factory/shell'
6
+ import { Domain, ObjectRef } from '@things-factory/shell'
16
7
 
17
8
  import { ConnectionManager } from '../../engine'
9
+ import { ProxyConnector } from '../../engine/connector/proxy-connector'
18
10
 
19
11
  export enum ConnectionStatus {
20
12
  CONNECTED = 'CONNECTED',
@@ -30,34 +22,34 @@ registerEnumType(ConnectionStatus, {
30
22
  @Index('ix_connection_0', (connection: Connection) => [connection.domain, connection.name], { unique: true })
31
23
  @ObjectType()
32
24
  export class Connection {
33
- /**
25
+ /**
34
26
  * Unique identifier for the connection, generated in UUID format.
35
27
  */
36
28
  @PrimaryGeneratedColumn('uuid')
37
29
  @Field(type => ID)
38
30
  readonly id: string
39
31
 
40
- /**
32
+ /**
41
33
  * Many-to-One relationship with the Domain entity.
42
34
  */
43
35
  @ManyToOne(type => Domain)
44
36
  @Field({ nullable: true })
45
37
  domain: Domain
46
38
 
47
- /**
39
+ /**
48
40
  * Stores the ID of the associated Domain.
49
41
  */
50
42
  @RelationId((connection: Connection) => connection.domain)
51
43
  domainId: string
52
44
 
53
- /**
45
+ /**
54
46
  * The name of the connection.
55
47
  */
56
48
  @Column()
57
49
  @Field()
58
50
  name: string
59
51
 
60
- /**
52
+ /**
61
53
  * Optional description for the connection.
62
54
  */
63
55
  @Column({
@@ -66,72 +58,81 @@ export class Connection {
66
58
  @Field({ nullable: true })
67
59
  description: string
68
60
 
69
- /**
61
+ /**
70
62
  * The type of the connection.
71
63
  */
72
64
  @Column()
73
65
  @Field({ nullable: true })
74
66
  type: string
75
67
 
76
- /**
68
+ /**
69
+ * Many-to-One relationship with the Appliance entity which delegate the connection. Optional field.
70
+ */
71
+ @ManyToOne(type => Appliance, { nullable: true })
72
+ @Field({ nullable: true })
73
+ edge: Appliance
74
+
75
+ /**
76
+ * Stores the ID of the Appliance who delegate the connection.
77
+ */
78
+ @RelationId((connection: Connection) => connection.edge)
79
+ edgeId: string
80
+
81
+ /**
77
82
  * The endpoint for the connection.
78
83
  */
79
84
  @Column()
80
85
  @Field({ nullable: true })
81
86
  endpoint: string
82
87
 
83
- /**
88
+ /**
84
89
  * Indicates the active status of the connection.
85
90
  */
86
- @Column({
87
- nullable: true
88
- })
91
+ @Column({ nullable: true })
89
92
  @Field({ nullable: true })
90
93
  active: boolean
91
94
 
92
- /**
95
+ /**
93
96
  * The status of the connection, using the ConnectionStatus type.
94
97
  */
95
98
  @Field({ nullable: true })
96
99
  state: ConnectionStatus
97
100
 
98
- /**
101
+ /**
99
102
  * Additional parameters for the connection, stored as a JSON string.
100
103
  */
101
- @Column({
102
- nullable: true
103
- })
104
+ @Column({ nullable: true })
104
105
  @Field({ nullable: true })
105
106
  params: string
106
107
 
107
- /**
108
+ /**
108
109
  * The date and time when the connection was created.
109
110
  */
110
111
  @CreateDateColumn()
111
112
  @Field({ nullable: true })
112
113
  createdAt: Date
113
114
 
114
- /**
115
+ /**
115
116
  * The date and time when the connection was last updated.
116
117
  */
117
118
  @UpdateDateColumn()
118
119
  @Field({ nullable: true })
119
120
  updatedAt: Date
120
121
 
121
- /**
122
+ /**
122
123
  * Many-to-One relationship with the User entity who created the connection. Optional field.
123
124
  */
124
125
  @ManyToOne(type => User, { nullable: true })
125
126
  @Field({ nullable: true })
126
127
  creator: User
127
128
 
128
- /**
129
+ /**
129
130
  * Stores the ID of the User who created the connection.
130
131
  */
131
132
  @RelationId((connection: Connection) => connection.creator)
132
133
  creatorId: string
133
134
 
134
- /**
135
+ /**
135
136
  * Many-to-One relationship with the User entity who last updated the connection.
136
137
  * Optional field.
137
138
  */
@@ -141,17 +142,18 @@ export class Connection {
141
142
 
142
143
  /**
143
144
  * Stores the ID of the User who last updated the connection.
144
- *
145
+ *
145
146
  */
146
147
  @RelationId((connection: Connection) => connection.updater)
147
148
  updaterId: string
148
149
 
149
150
  /**
150
151
  * Asynchronous method to establish the connection.
151
- *
152
+ *
152
153
  */
153
154
  async connect() {
154
- var connector = ConnectionManager.getConnector(this.type)
155
+ const { type, edge } = this
156
+ const connector = edge ? ProxyConnector.instance : ConnectionManager.getConnector(type)
155
157
  var params = {}
156
158
 
157
159
  try {
@@ -168,11 +170,12 @@ export class Connection {
168
170
 
169
171
  /**
170
172
  * @brief Asynchronous method to disconnect the connection.
171
- *
173
+ *
172
174
  */
173
175
  async disconnect() {
174
176
  try {
175
- var connector = ConnectionManager.getConnector(this.type)
177
+ const { type, edge } = this
178
+ const connector = edge ? ProxyConnector.instance : ConnectionManager.getConnector(type)
176
179
  await connector.disconnect(this)
177
180
  } finally {
178
181
  }
@@ -182,10 +185,10 @@ export class Connection {
182
185
  /**
183
186
  * Connection의 params의 원 타입과 사용 시에 타입 불일치로 인해 임시적으로 생성한 타입으로
184
187
  * 추후, 타입 일치를 통해서 제거할 예정임.
185
- *
188
+ *
186
189
  */
187
190
  export interface InputConnection extends Connection {
188
- params: any;
191
+ params: any
189
192
  }
190
193
 
191
194
  @ObjectType()
@@ -205,6 +208,9 @@ export class ConnectionState {
205
208
  @Field({ nullable: true })
206
209
  type?: string
207
210
 
211
+ @Field(type => Appliance, { nullable: true })
212
+ edge?: Appliance
213
+
208
214
  @Field({ nullable: true })
209
215
  state?: ConnectionStatus
210
216
 
@@ -223,6 +229,9 @@ export class NewConnection {
223
229
  @Field({ nullable: true })
224
230
  type?: string
225
231
 
232
+ @Field(type => ObjectRef, { nullable: true })
233
+ edge?: Appliance
234
+
226
235
  @Field({ nullable: true })
227
236
  endpoint?: string
228
237
 
@@ -244,6 +253,9 @@ export class ConnectionPatch {
244
253
  @Field({ nullable: true })
245
254
  type?: string
246
255
 
256
+ @Field(type => ObjectRef, { nullable: true })
257
+ edge?: Appliance
258
+
247
259
  @Field({ nullable: true })
248
260
  endpoint?: string
249
261
 
@@ -1,35 +1,14 @@
1
1
  import { Arg, Ctx, Mutation, Resolver } from 'type-graphql'
2
2
 
3
- import { getRepository, Domain, GraphqlLocalClient, ScalarObject } from '@things-factory/shell'
3
+ import { ScalarObject } from '@things-factory/shell'
4
4
 
5
- import { ScenarioEngine } from '../../engine'
6
- import { Scenario } from '../scenario/scenario'
7
5
  import { ScenarioInstance } from './scenario-instance-type'
8
- import { PrivilegeObject, User, checkPermission } from '@things-factory/auth-base'
9
- import { Step } from '../step/step-type'
10
6
 
11
- const debug = require('debug')('things-factory:integration-base:scenario-instance-mutation')
12
-
13
- async function findScenario(
14
- scenarioName: string,
15
- domain: Domain
16
- ): Promise<{ name: string; steps: Step[]; domain: Domain; privilege?: PrivilegeObject }> {
17
- var repository = getRepository(Scenario)
18
-
19
- var scenario = await repository.findOne({
20
- where: { domain: { id: domain.id }, name: scenarioName },
21
- relations: ['domain', 'steps', 'creator', 'updater']
22
- })
23
-
24
- if (!scenario && domain.parentId) {
25
- scenario = await repository.findOne({
26
- where: { domain: { id: domain.parentId }, name: scenarioName },
27
- relations: ['domain', 'steps', 'creator', 'updater']
28
- })
29
- }
30
-
31
- return scenario as any
32
- }
7
+ import {
8
+ runScenario as controllerRunScenario,
9
+ startScenario as controllerStartScenario,
10
+ stopScenario as controllerStopScenario
11
+ } from '../../controllers/scenario-controller'
33
12
 
34
13
  @Resolver(ScenarioInstance)
35
14
  export class ScenarioInstanceMutation {
@@ -42,42 +21,7 @@ export class ScenarioInstanceMutation {
42
21
  @Arg('variables', type => ScalarObject, { nullable: true }) variables: any,
43
22
  @Ctx() context: ResolverContext
44
23
  ): Promise<ScenarioInstance> {
45
- const { domain, user, lng, unsafeIP, prohibitedPrivileges } = context.state
46
-
47
- debug('runScenario', scenarioName, instanceName, variables)
48
-
49
- var scenario = await findScenario(scenarioName, domain)
50
-
51
- if (!scenario) {
52
- throw new Error(
53
- context.t('error.scenario not found', {
54
- scenario: scenarioName
55
- })
56
- )
57
- }
58
-
59
- if (!(await checkPermission(scenario.privilege, user, domain, unsafeIP, prohibitedPrivileges))) {
60
- const { category, privilege } = scenario.privilege || {}
61
- throw new Error(
62
- `Unauthorized! ${
63
- category && privilege ? category + ':' + privilege + ' privilege' : 'ownership granted'
64
- } required`
65
- )
66
- }
67
-
68
- /* 시나리오 인스턴스를 생성한다. */
69
- instanceName = instanceName || scenarioName + '-' + String(Date.now())
70
- var instance = new ScenarioInstance(instanceName, scenario, {
71
- domain,
72
- user,
73
- lng,
74
- variables,
75
- client: GraphqlLocalClient.client
76
- })
77
-
78
- await instance.run()
79
-
80
- return instance
24
+ return await controllerRunScenario(instanceName, scenarioName, variables, context)
81
25
  }
82
26
 
83
27
  @Mutation(returns => ScenarioInstance, { description: 'To start new scenario instance' })
@@ -87,66 +31,11 @@ export class ScenarioInstanceMutation {
87
31
  @Arg('variables', type => ScalarObject, { nullable: true }) variables: any,
88
32
  @Ctx() context: ResolverContext
89
33
  ): Promise<ScenarioInstance> {
90
- const { domain, user, lng, unsafeIP, prohibitedPrivileges } = context.state
91
-
92
- debug('startScenario', instanceName, scenarioName, variables)
93
-
94
- var scenario = await findScenario(scenarioName, domain)
95
-
96
- if (!scenario) {
97
- throw new Error(
98
- context.t('error.scenario not found', {
99
- scenario: scenarioName
100
- })
101
- )
102
- }
103
-
104
- if (!(await checkPermission(scenario.privilege, user, domain, unsafeIP, prohibitedPrivileges))) {
105
- const { category, privilege } = scenario.privilege || {}
106
- throw new Error(
107
- `Unauthorized! ${
108
- category && privilege ? category + ':' + privilege + ' privilege' : 'ownership granted'
109
- } required`
110
- )
111
- }
112
-
113
- instanceName = instanceName || scenarioName
114
- return await ScenarioEngine.load(instanceName, scenario, { domain, user, lng, variables })
34
+ return await controllerStartScenario(instanceName, scenarioName, variables, context)
115
35
  }
116
36
 
117
37
  @Mutation(returns => ScenarioInstance, { nullable: true, description: 'To start new scenario instance' })
118
- async stopScenario(
119
- @Arg('instanceName', { nullable: true }) instanceName: string,
120
- @Ctx() context: ResolverContext
121
- ): Promise<ScenarioInstance | undefined> {
122
- const { domain, user, unsafeIP, prohibitedPrivileges } = context.state
123
-
124
- debug('stopScenario', instanceName)
125
-
126
- var scenarioInstance = ScenarioEngine.getScenarioInstance(domain, instanceName)
127
-
128
- if (!scenarioInstance) {
129
- debug('stopScenario', `ScenarioInstance(${instanceName}) Not Found.`)
130
- throw new Error(
131
- context.t('error.scenario instance not found', {
132
- instance: instanceName
133
- })
134
- )
135
- }
136
-
137
- var scenario = await findScenario(scenarioInstance.scenarioName, domain)
138
-
139
- if (!(await checkPermission(scenario.privilege, user, domain, unsafeIP, prohibitedPrivileges))) {
140
- const { category, privilege } = scenario.privilege || {}
141
- throw new Error(
142
- `Unauthorized! ${
143
- category && privilege ? category + ':' + privilege + ' privilege' : 'ownership granted'
144
- } required`
145
- )
146
- }
147
-
148
- await ScenarioEngine.unload(domain, instanceName)
149
-
150
- return scenarioInstance
38
+ async stopScenario(@Arg('instanceName', { nullable: true }) instanceName: string, @Ctx() context: ResolverContext): Promise<ScenarioInstance | undefined> {
39
+ return await controllerStopScenario(instanceName, context)
151
40
  }
152
41
  }