@or-sdk/idw 3.1.1 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/dist/cjs/index.js +1 -17
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +0 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/types/api/channelApi.d.ts +3 -1
- package/dist/types/api/channelApi.d.ts.map +1 -1
- package/dist/types/api/memoryApi.d.ts +1 -1
- package/dist/types/api/memoryApi.d.ts.map +1 -1
- package/dist/types/index.d.ts +0 -3
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types/config.d.ts +0 -7
- package/dist/types/types/config.d.ts.map +1 -1
- package/package.json +3 -9
- package/src/api/channelApi.ts +1 -1
- package/src/api/memoryApi.ts +1 -1
- package/src/index.ts +0 -3
- package/src/types/config.ts +0 -28
- package/dist/cjs/idw.js +0 -323
- package/dist/cjs/idw.js.map +0 -1
- package/dist/cjs/migration-base.js +0 -72
- package/dist/cjs/migration-base.js.map +0 -1
- package/dist/cjs/migration-handler.js +0 -183
- package/dist/cjs/migration-handler.js.map +0 -1
- package/dist/cjs/migrations/Migration__1.js +0 -108
- package/dist/cjs/migrations/Migration__1.js.map +0 -1
- package/dist/cjs/migrations/Migration__2.js +0 -155
- package/dist/cjs/migrations/Migration__2.js.map +0 -1
- package/dist/cjs/migrations/Migration__3.js +0 -108
- package/dist/cjs/migrations/Migration__3.js.map +0 -1
- package/dist/cjs/migrations/index.js +0 -13
- package/dist/cjs/migrations/index.js.map +0 -1
- package/dist/esm/idw.js +0 -252
- package/dist/esm/idw.js.map +0 -1
- package/dist/esm/migration-base.js +0 -30
- package/dist/esm/migration-base.js.map +0 -1
- package/dist/esm/migration-handler.js +0 -86
- package/dist/esm/migration-handler.js.map +0 -1
- package/dist/esm/migrations/Migration__1.js +0 -40
- package/dist/esm/migrations/Migration__1.js.map +0 -1
- package/dist/esm/migrations/Migration__2.js +0 -80
- package/dist/esm/migrations/Migration__2.js.map +0 -1
- package/dist/esm/migrations/Migration__3.js +0 -53
- package/dist/esm/migrations/Migration__3.js.map +0 -1
- package/dist/esm/migrations/index.js +0 -4
- package/dist/esm/migrations/index.js.map +0 -1
- package/dist/types/idw.d.ts +0 -23
- package/dist/types/idw.d.ts.map +0 -1
- package/dist/types/migration-base.d.ts +0 -24
- package/dist/types/migration-base.d.ts.map +0 -1
- package/dist/types/migration-handler.d.ts +0 -20
- package/dist/types/migration-handler.d.ts.map +0 -1
- package/dist/types/migrations/Migration__1.d.ts +0 -6
- package/dist/types/migrations/Migration__1.d.ts.map +0 -1
- package/dist/types/migrations/Migration__2.d.ts +0 -7
- package/dist/types/migrations/Migration__2.d.ts.map +0 -1
- package/dist/types/migrations/Migration__3.d.ts +0 -5
- package/dist/types/migrations/Migration__3.d.ts.map +0 -1
- package/dist/types/migrations/index.d.ts +0 -4
- package/dist/types/migrations/index.d.ts.map +0 -1
- package/src/idw.ts +0 -291
- package/src/migration-base.ts +0 -35
- package/src/migration-handler.ts +0 -94
- package/src/migrations/Migration__1.ts +0 -33
- package/src/migrations/Migration__2.ts +0 -69
- package/src/migrations/Migration__3.ts +0 -45
- package/src/migrations/index.ts +0 -3
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { Accounts } from '@or-sdk/accounts';
|
|
2
|
-
import { Graphs } from '@or-sdk/graph';
|
|
3
|
-
import { Users } from '@or-sdk/users';
|
|
4
|
-
import { Settings } from '@or-sdk/settings';
|
|
5
|
-
import { Flows } from '@or-sdk/flows';
|
|
6
|
-
export default class MigrationHandler {
|
|
7
|
-
private graphApi;
|
|
8
|
-
private users;
|
|
9
|
-
private accounts;
|
|
10
|
-
private settings;
|
|
11
|
-
private accountId;
|
|
12
|
-
private flows;
|
|
13
|
-
private migrations;
|
|
14
|
-
constructor(graphApi: Graphs, users: Users, accounts: Accounts, settings: Settings, accountId: string | undefined, flows: Flows);
|
|
15
|
-
applyMigrations(): Promise<void>;
|
|
16
|
-
saveVersion(currentLatestVersion: number): Promise<void>;
|
|
17
|
-
deleteMigrationVersion(): Promise<void>;
|
|
18
|
-
findAccountsMigrationVersion(): Promise<any>;
|
|
19
|
-
}
|
|
20
|
-
//# sourceMappingURL=migration-handler.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"migration-handler.d.ts","sourceRoot":"","sources":["../../src/migration-handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AACvC,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAA6B,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAIvE,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAItC,MAAM,CAAC,OAAO,OAAO,gBAAgB;IAIjC,OAAO,CAAC,QAAQ;IAChB,OAAO,CAAC,KAAK;IACb,OAAO,CAAC,QAAQ;IAChB,OAAO,CAAC,QAAQ;IAChB,OAAO,CAAC,SAAS;IACjB,OAAO,CAAC,KAAK;IARf,OAAO,CAAC,UAAU,CAA6B;gBAGrC,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,KAAK,EACZ,QAAQ,EAAE,QAAQ,EAClB,QAAQ,EAAE,QAAQ,EAClB,SAAS,EAAE,MAAM,GAAG,SAAS,EAC7B,KAAK,EAAE,KAAK;IAkBhB,eAAe;IAwBf,WAAW,CAAC,oBAAoB,EAAE,MAAM;IAaxC,sBAAsB;IAOtB,4BAA4B;CAWnC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Migration__1.d.ts","sourceRoot":"","sources":["../../../src/migrations/Migration__1.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AASlD,MAAM,CAAC,OAAO,OAAO,YAAa,SAAQ,aAAa;IAC/C,EAAE;IAIF,mBAAmB,IAAI,OAAO,CAAC,IAAI,CAAC;CAkB3C"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Migration__2.d.ts","sourceRoot":"","sources":["../../../src/migrations/Migration__2.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAyClD,MAAM,CAAC,OAAO,OAAO,YAAa,SAAQ,aAAa;IAC/C,EAAE;YAOM,cAAc;YAUd,oBAAoB;CAQnC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Migration__3.d.ts","sourceRoot":"","sources":["../../../src/migrations/Migration__3.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAIlD,MAAM,CAAC,OAAO,OAAO,YAAa,SAAQ,aAAa;IAC/C,EAAE;CAuCT"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/migrations/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,gBAAgB,CAAC"}
|
package/src/idw.ts
DELETED
|
@@ -1,291 +0,0 @@
|
|
|
1
|
-
import { Settings } from '@or-sdk/settings';
|
|
2
|
-
import { Base } from '@or-sdk/base';
|
|
3
|
-
import { Graphs } from '@or-sdk/graph';
|
|
4
|
-
import { Users } from '@or-sdk/users';
|
|
5
|
-
import { Accounts } from '@or-sdk/accounts';
|
|
6
|
-
import { GRAPH_NAME, SERVICE_KEY } from './constants';
|
|
7
|
-
import { IDWConfig } from './types';
|
|
8
|
-
import MigrationHandler from './migration-handler';
|
|
9
|
-
import { Flow } from '@or-sdk/deployer';
|
|
10
|
-
import { Flows } from '@or-sdk/flows';
|
|
11
|
-
/**
|
|
12
|
-
* OneReach IDW service client
|
|
13
|
-
* ## Installation:
|
|
14
|
-
* ```
|
|
15
|
-
* $ npm i @or-sdk/idw
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
export class IDW extends Base {
|
|
19
|
-
private readonly graphApi: Graphs;
|
|
20
|
-
private readonly users: Users;
|
|
21
|
-
private readonly accounts: Accounts;
|
|
22
|
-
private readonly settings: Settings;
|
|
23
|
-
private readonly flows: Flows;
|
|
24
|
-
private accountId: string | undefined;
|
|
25
|
-
private migrationHandler: MigrationHandler;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* ```typescript
|
|
29
|
-
* import { IDW } from '@or-sdk/idw'
|
|
30
|
-
* import config from '../config';
|
|
31
|
-
*
|
|
32
|
-
* const idw = new IDW({
|
|
33
|
-
* graphUrl: config.GRAPHS_API_URL,
|
|
34
|
-
* token: 'account-token-string'
|
|
35
|
-
* })
|
|
36
|
-
* ```
|
|
37
|
-
*/
|
|
38
|
-
constructor(params: IDWConfig) {
|
|
39
|
-
const {
|
|
40
|
-
accountId,
|
|
41
|
-
discoveryUrl,
|
|
42
|
-
graphUrl,
|
|
43
|
-
sdkApiUrl,
|
|
44
|
-
token,
|
|
45
|
-
} = params;
|
|
46
|
-
|
|
47
|
-
super({
|
|
48
|
-
accountId,
|
|
49
|
-
discoveryUrl,
|
|
50
|
-
serviceKey: SERVICE_KEY,
|
|
51
|
-
token,
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
this.graphApi = new Graphs({
|
|
55
|
-
accountId,
|
|
56
|
-
discoveryUrl,
|
|
57
|
-
graphUrl,
|
|
58
|
-
token,
|
|
59
|
-
});
|
|
60
|
-
this.users = new Users({
|
|
61
|
-
accountId,
|
|
62
|
-
token,
|
|
63
|
-
discoveryUrl,
|
|
64
|
-
sdkUrl: sdkApiUrl,
|
|
65
|
-
});
|
|
66
|
-
this.accounts = new Accounts({
|
|
67
|
-
token,
|
|
68
|
-
discoveryUrl,
|
|
69
|
-
sdkUrl: sdkApiUrl,
|
|
70
|
-
});
|
|
71
|
-
this.settings = new Settings({
|
|
72
|
-
token,
|
|
73
|
-
sdkUrl: sdkApiUrl,
|
|
74
|
-
});
|
|
75
|
-
this.flows = new Flows({
|
|
76
|
-
token,
|
|
77
|
-
discoveryUrl,
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
this.accountId = accountId;
|
|
81
|
-
|
|
82
|
-
this.migrationHandler = new MigrationHandler(
|
|
83
|
-
this.graphApi,
|
|
84
|
-
this.users,
|
|
85
|
-
this.accounts,
|
|
86
|
-
this.settings,
|
|
87
|
-
this.accountId,
|
|
88
|
-
this.flows
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
async runMigrations() {
|
|
93
|
-
await this.migrationHandler.applyMigrations();
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
async checkOrCreateDb() {
|
|
97
|
-
try {
|
|
98
|
-
await this.graphApi.getGraphInfo(GRAPH_NAME);
|
|
99
|
-
} catch (e) {
|
|
100
|
-
this.createDb();
|
|
101
|
-
// TODO: This function removes account setting storing the migration version
|
|
102
|
-
// to apply all migrations after creating db.
|
|
103
|
-
// Will be moved to the backend.
|
|
104
|
-
this.migrationHandler.deleteMigrationVersion();
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
private async createDb() {
|
|
109
|
-
await this.graphApi.createGraph({ name: GRAPH_NAME });
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
async createOrUpdateFlow(flow: Flow) {
|
|
113
|
-
const id = flow.id;
|
|
114
|
-
if (id === undefined) {
|
|
115
|
-
throw new Error('Only flows with id can be added to the IDW graph');
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (flow.isDeleted) {
|
|
119
|
-
return await this.deleteFlow(id);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const { username, userId } = await this.users.getCurrentUser();
|
|
123
|
-
|
|
124
|
-
await this.graphQuery(`
|
|
125
|
-
MERGE (flow:FLOW { id: $id })
|
|
126
|
-
ON MATCH
|
|
127
|
-
SET flow.createdAt = $createdAt,
|
|
128
|
-
flow.createdBy = $userId,
|
|
129
|
-
flow.description = $description,
|
|
130
|
-
flow.groupId = $groupId,
|
|
131
|
-
flow.name = $name,
|
|
132
|
-
flow.updatedAt = $updatedAt,
|
|
133
|
-
flow.updatedBy = $username,
|
|
134
|
-
flow.steps = $steps
|
|
135
|
-
ON CREATE
|
|
136
|
-
SET flow.createdAt = $createdAt,
|
|
137
|
-
flow.createdBy = $userId,
|
|
138
|
-
flow.description = $description,
|
|
139
|
-
flow.groupId = $groupId,
|
|
140
|
-
flow.isActive = false,
|
|
141
|
-
flow.name = $name,
|
|
142
|
-
flow.updatedAt = $updatedAt,
|
|
143
|
-
flow.updatedBy = $username,
|
|
144
|
-
flow.steps = $steps
|
|
145
|
-
RETURN flow
|
|
146
|
-
`, {
|
|
147
|
-
createdAt: flow.dateCreated,
|
|
148
|
-
description: flow.data.description,
|
|
149
|
-
groupId: flow.botId,
|
|
150
|
-
id,
|
|
151
|
-
name: flow.data.label,
|
|
152
|
-
updatedAt: flow.dateModified,
|
|
153
|
-
username,
|
|
154
|
-
userId,
|
|
155
|
-
steps: flow.data.trees?.main?.steps?.length ?? 0,
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
await this.graphQuery(`
|
|
159
|
-
MATCH (flow:FLOW { id: $id })
|
|
160
|
-
OPTIONAL MATCH (flow)-[channelRelations:IMPLEMENTS { isActive: false }]->(:CHANNEL)
|
|
161
|
-
OPTIONAL MATCH (flow)-[skillRelations:IMPLEMENTS { isActive: false }]->(:SKILL)
|
|
162
|
-
DELETE channelRelations, skillRelations
|
|
163
|
-
`, { id });
|
|
164
|
-
|
|
165
|
-
const channel = flow.data.meta.idw?.channel;
|
|
166
|
-
const skills: string[] | undefined = flow.data.meta.idw?.skills;
|
|
167
|
-
|
|
168
|
-
if (channel !== undefined && channel.length !== 0) {
|
|
169
|
-
await this.graphQuery(`
|
|
170
|
-
MATCH (flow:FLOW { id: $id }),
|
|
171
|
-
(channel:CHANNEL { id: $channel })
|
|
172
|
-
MERGE (flow)-[relationship:IMPLEMENTS]->(channel)
|
|
173
|
-
ON CREATE
|
|
174
|
-
SET relationship.isActive = false
|
|
175
|
-
`, {
|
|
176
|
-
channel,
|
|
177
|
-
id,
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
if (skills?.length) {
|
|
182
|
-
const preparedIds = skills.map(id => `'${id}'`);
|
|
183
|
-
await this.graphQuery(`
|
|
184
|
-
MATCH (flow:FLOW { id: $id }), (skill:SKILL)
|
|
185
|
-
WHERE skill.id in [${preparedIds}]
|
|
186
|
-
MERGE (flow)-[relationship:IMPLEMENTS]->(skill)
|
|
187
|
-
ON CREATE
|
|
188
|
-
SET relationship.isActive = false
|
|
189
|
-
`, {
|
|
190
|
-
id,
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
async deleteFlow(id: string) {
|
|
196
|
-
await this.graphQuery(`
|
|
197
|
-
MATCH (flow:FLOW { id: $id })
|
|
198
|
-
DETACH DELETE flow
|
|
199
|
-
`, { id });
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
async deleteFlows(ids: string[]) {
|
|
203
|
-
if (ids.length) {
|
|
204
|
-
await this.graphQuery(`
|
|
205
|
-
MATCH (flow:FLOW)
|
|
206
|
-
where flow.id in [${ids.map(id => `'${id}'`)}]
|
|
207
|
-
DETACH DELETE flow
|
|
208
|
-
`);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
async activateFlow(flow: Flow) {
|
|
213
|
-
const id = flow.id;
|
|
214
|
-
if (id === undefined) {
|
|
215
|
-
throw new Error('Only flows with id can be added to the IDW graph');
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
await this.graphQuery(`
|
|
219
|
-
MATCH (flow:FLOW { id: $id })
|
|
220
|
-
OPTIONAL MATCH (flow)-[channelRelations:IMPLEMENTS]->(:CHANNEL)
|
|
221
|
-
OPTIONAL MATCH (flow)-[skillRelations:IMPLEMENTS]->(:SKILL)
|
|
222
|
-
DELETE channelRelations, skillRelations
|
|
223
|
-
SET flow.activatedAt = $activatedAt,
|
|
224
|
-
flow.isActive = true
|
|
225
|
-
`, {
|
|
226
|
-
activatedAt: Date.now(),
|
|
227
|
-
id,
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
const channel = flow.data.meta.idw?.channel;
|
|
231
|
-
const skills: string[] | undefined = flow.data.meta.idw?.skills;
|
|
232
|
-
|
|
233
|
-
if (channel !== undefined && channel.length !== 0) {
|
|
234
|
-
await this.graphQuery(`
|
|
235
|
-
MATCH (flow:FLOW { id: $id }),
|
|
236
|
-
(channel:CHANNEL { id: $channel })
|
|
237
|
-
MERGE (flow)-[relationship:IMPLEMENTS]->(channel)
|
|
238
|
-
SET relationship.isActive = true
|
|
239
|
-
`, {
|
|
240
|
-
channel,
|
|
241
|
-
id,
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
if (skills?.length) {
|
|
246
|
-
const preparedIds = skills.map(id => `'${id}'`);
|
|
247
|
-
await this.graphQuery(`
|
|
248
|
-
MATCH (flow:FLOW { id: $id }), (skill:SKILL)
|
|
249
|
-
WHERE skill.id in [${preparedIds}]
|
|
250
|
-
MERGE (flow)-[relationship:IMPLEMENTS]->(skill)
|
|
251
|
-
SET relationship.isActive = true
|
|
252
|
-
`, {
|
|
253
|
-
id,
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
async deactivateFlow(id: string) {
|
|
259
|
-
await this.graphQuery(`
|
|
260
|
-
MATCH (flow:FLOW { id: $id })
|
|
261
|
-
OPTIONAL MATCH (flow)-[channelRelations:IMPLEMENTS]->(:CHANNEL)
|
|
262
|
-
OPTIONAL MATCH (flow)-[skillRelations:IMPLEMENTS]->(:SKILL)
|
|
263
|
-
SET flow.isActive = false,
|
|
264
|
-
channelRelations.isActive = false,
|
|
265
|
-
skillRelations.isActive = false
|
|
266
|
-
REMOVE flow.activatedAt
|
|
267
|
-
RETURN flow
|
|
268
|
-
`, { id });
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
private async graphQuery(query: string, params?: Record<string, null | number | string | boolean>) {
|
|
272
|
-
//create graph if not exists
|
|
273
|
-
try {
|
|
274
|
-
await this.checkOrCreateDb();
|
|
275
|
-
} catch (e) {
|
|
276
|
-
console.warn(e);
|
|
277
|
-
// TODO: handle errors 🤔
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
try {
|
|
281
|
-
return await this.graphApi.query({
|
|
282
|
-
graph: GRAPH_NAME,
|
|
283
|
-
params,
|
|
284
|
-
query,
|
|
285
|
-
});
|
|
286
|
-
} catch (e) {
|
|
287
|
-
console.warn(e);
|
|
288
|
-
// TODO: handle errors 🤔
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
}
|
package/src/migration-base.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { Accounts } from '@or-sdk/accounts';
|
|
2
|
-
import { Graphs } from '@or-sdk/graph';
|
|
3
|
-
import { Settings } from '@or-sdk/settings';
|
|
4
|
-
import { Users } from '@or-sdk/users';
|
|
5
|
-
import { Flows } from '@or-sdk/flows';
|
|
6
|
-
|
|
7
|
-
export interface MigrationsParams {
|
|
8
|
-
graphApi: Graphs;
|
|
9
|
-
users: Users;
|
|
10
|
-
accounts: Accounts;
|
|
11
|
-
settings: Settings;
|
|
12
|
-
accountId: string | undefined;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export abstract class MigrationBase {
|
|
16
|
-
constructor(
|
|
17
|
-
protected graphApi: Graphs,
|
|
18
|
-
protected users: Users,
|
|
19
|
-
protected accounts: Accounts,
|
|
20
|
-
protected settings: Settings,
|
|
21
|
-
protected accountId: string | undefined,
|
|
22
|
-
protected flows: Flows
|
|
23
|
-
) {
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
abstract up(): Promise<void>;
|
|
27
|
-
|
|
28
|
-
protected async resolveAccountId(): Promise<string> {
|
|
29
|
-
return this.accountId ?? (async () => {
|
|
30
|
-
const { accountId } = await this.users.getCurrentUser();
|
|
31
|
-
this.accountId = accountId;
|
|
32
|
-
return accountId;
|
|
33
|
-
})();
|
|
34
|
-
}
|
|
35
|
-
}
|
package/src/migration-handler.ts
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import { Accounts } from '@or-sdk/accounts';
|
|
2
|
-
import { Graphs } from '@or-sdk/graph';
|
|
3
|
-
import { Users } from '@or-sdk/users';
|
|
4
|
-
import { AccountRouteDeletePayload, Settings } from '@or-sdk/settings';
|
|
5
|
-
import { MigrationBase } from './migration-base';
|
|
6
|
-
import { SERVICE_KEY, VERSION_PROP } from './constants';
|
|
7
|
-
import * as migrations from './migrations';
|
|
8
|
-
import { Flows } from '@or-sdk/flows';
|
|
9
|
-
|
|
10
|
-
const PREFIX = 'Migration__';
|
|
11
|
-
|
|
12
|
-
export default class MigrationHandler {
|
|
13
|
-
private migrations: Map<string, MigrationBase>;
|
|
14
|
-
|
|
15
|
-
constructor(
|
|
16
|
-
private graphApi: Graphs,
|
|
17
|
-
private users: Users,
|
|
18
|
-
private accounts: Accounts,
|
|
19
|
-
private settings: Settings,
|
|
20
|
-
private accountId: string | undefined,
|
|
21
|
-
private flows: Flows
|
|
22
|
-
) {
|
|
23
|
-
|
|
24
|
-
this.migrations = new Map(
|
|
25
|
-
Object.values(migrations)
|
|
26
|
-
.map(ctor => [
|
|
27
|
-
ctor.name, new ctor(
|
|
28
|
-
this.graphApi,
|
|
29
|
-
this.users,
|
|
30
|
-
this.accounts,
|
|
31
|
-
this.settings,
|
|
32
|
-
this.accountId,
|
|
33
|
-
this.flows
|
|
34
|
-
),
|
|
35
|
-
])
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
async applyMigrations() {
|
|
40
|
-
const accountsLatestVersion = (await this.findAccountsMigrationVersion()) ?? 0;
|
|
41
|
-
|
|
42
|
-
const migrationVersions = Array.from(this.migrations.keys()).map(migrationName => {
|
|
43
|
-
const [, version] = migrationName.split(PREFIX);
|
|
44
|
-
return Number(version);
|
|
45
|
-
}).sort();
|
|
46
|
-
|
|
47
|
-
for (let i = 0;i < migrationVersions.length;i++) {
|
|
48
|
-
if (migrationVersions[i] > accountsLatestVersion) {
|
|
49
|
-
const migration = this.migrations.get(PREFIX + migrationVersions[i]);
|
|
50
|
-
if (migration) {
|
|
51
|
-
try {
|
|
52
|
-
await migration.up();
|
|
53
|
-
await this.saveVersion(migrationVersions[i]);
|
|
54
|
-
|
|
55
|
-
} catch (e) {
|
|
56
|
-
throw new Error(`Migration ${migration.constructor.name} is not applied`);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
async saveVersion(currentLatestVersion: number) {
|
|
64
|
-
try {
|
|
65
|
-
const params = {
|
|
66
|
-
key: `${SERVICE_KEY}.${VERSION_PROP}`,
|
|
67
|
-
value: currentLatestVersion,
|
|
68
|
-
};
|
|
69
|
-
await this.settings.setAccountSettings(params);
|
|
70
|
-
} catch (e) {
|
|
71
|
-
// eslint-disable-next-line no-console
|
|
72
|
-
throw new Error(`Error while saving migration "${PREFIX + currentLatestVersion}"`);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
async deleteMigrationVersion() {
|
|
77
|
-
const params = {
|
|
78
|
-
key: `${SERVICE_KEY}.${VERSION_PROP}`,
|
|
79
|
-
} as AccountRouteDeletePayload;
|
|
80
|
-
await this.settings.deleteAccountSettings(params);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
async findAccountsMigrationVersion() {
|
|
84
|
-
try {
|
|
85
|
-
const params = {
|
|
86
|
-
key: `${SERVICE_KEY}.${VERSION_PROP}`,
|
|
87
|
-
};
|
|
88
|
-
return await this.settings.getAccountSettings(params);
|
|
89
|
-
} catch (e) {
|
|
90
|
-
// eslint-disable-next-line no-console
|
|
91
|
-
throw new Error('Error while getting account settings');
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { MigrationBase } from '../migration-base';
|
|
2
|
-
|
|
3
|
-
const idwSetupData = {
|
|
4
|
-
description: 'Add a fancy description to your IDW',
|
|
5
|
-
role: 'You can set your IDW\'s role/s just by chatting with the embedded RWC',
|
|
6
|
-
avatar: 'https://files.qa.api.onereach.ai/public/03db4abe-0477-47c0-ae4e-4c68f1047d59/IDW/IDWs-media-data/Img.png',
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
10
|
-
export default class Migration__1 extends MigrationBase {
|
|
11
|
-
async up() {
|
|
12
|
-
await this.checkOrSetupIDWInfo();
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
async checkOrSetupIDWInfo(): Promise<void> {
|
|
16
|
-
const accountId = await this.resolveAccountId();
|
|
17
|
-
const idwData = await this.accounts.getIdwData(accountId);
|
|
18
|
-
const hasIdwInfo = Boolean(idwData && idwData.name && idwData.role);
|
|
19
|
-
|
|
20
|
-
if (!hasIdwInfo) {
|
|
21
|
-
const { name, role, description, avatar } = idwData;
|
|
22
|
-
await this.accounts.updateIdwData(accountId, {
|
|
23
|
-
name,
|
|
24
|
-
// using || instead of ?? to replace empty strings
|
|
25
|
-
role: role?.trim() || idwSetupData.role,
|
|
26
|
-
description: description?.trim() || idwSetupData.description,
|
|
27
|
-
avatar: avatar?.trim() || idwSetupData.avatar,
|
|
28
|
-
});
|
|
29
|
-
//TODO it's necessary bc redis cache.
|
|
30
|
-
return new Promise(res => setTimeout(res, 60000));
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { GRAPH_NAME } from '../constants';
|
|
2
|
-
import { MigrationBase } from '../migration-base';
|
|
3
|
-
|
|
4
|
-
const startUpChannels = [
|
|
5
|
-
{
|
|
6
|
-
id: 'facebook-messenger',
|
|
7
|
-
name: 'Facebook Messenger',
|
|
8
|
-
icon: 'https://files.qa.api.onereach.ai/public/9c1b43df-0f7c-43fe-819f-be9df1f39759/channel-icons/messenger.png',
|
|
9
|
-
},
|
|
10
|
-
{
|
|
11
|
-
id: 'sms',
|
|
12
|
-
name: 'SMS',
|
|
13
|
-
icon: 'https://files.qa.api.onereach.ai/public/9c1b43df-0f7c-43fe-819f-be9df1f39759/channel-icons/sms.png',
|
|
14
|
-
},
|
|
15
|
-
{
|
|
16
|
-
id: 'telegram',
|
|
17
|
-
name: 'Telegram',
|
|
18
|
-
icon: 'https://files.qa.api.onereach.ai/public/9c1b43df-0f7c-43fe-819f-be9df1f39759/channel-icons/telegram.png',
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
id: 'twitter',
|
|
22
|
-
name: 'Twitter',
|
|
23
|
-
icon: 'https://files.qa.api.onereach.ai/public/9c1b43df-0f7c-43fe-819f-be9df1f39759/channel-icons/twitter.png',
|
|
24
|
-
},
|
|
25
|
-
{
|
|
26
|
-
id: 'viber',
|
|
27
|
-
name: 'Viber',
|
|
28
|
-
icon: 'https://files.qa.api.onereach.ai/public/9c1b43df-0f7c-43fe-819f-be9df1f39759/channel-icons/viber.png',
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
id: 'viber-business',
|
|
32
|
-
name: 'Viber Business',
|
|
33
|
-
icon: 'https://files.qa.api.onereach.ai/public/9c1b43df-0f7c-43fe-819f-be9df1f39759/channel-icons/viber.png',
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
id: 'whatsapp',
|
|
37
|
-
name: 'WhatsApp',
|
|
38
|
-
icon: 'https://files.qa.api.onereach.ai/public/9c1b43df-0f7c-43fe-819f-be9df1f39759/channel-icons/whatsapp.png',
|
|
39
|
-
},
|
|
40
|
-
];
|
|
41
|
-
|
|
42
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
43
|
-
export default class Migration__2 extends MigrationBase {
|
|
44
|
-
async up() {
|
|
45
|
-
const channelsExist = await this.checkIfChannelsExist();
|
|
46
|
-
if (!channelsExist) {
|
|
47
|
-
await this.createChannels();
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
private async createChannels() {
|
|
52
|
-
await this.graphApi.query({
|
|
53
|
-
graph: GRAPH_NAME,
|
|
54
|
-
query: `CREATE ${startUpChannels.map(({ id, name, icon }) => {
|
|
55
|
-
return `(:CHANNEL {id: '${id}', name: '${name}', icon: '${icon}'})`;
|
|
56
|
-
}).join(', ')}`,
|
|
57
|
-
params: {},
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
private async checkIfChannelsExist() {
|
|
62
|
-
const [data] = await this.graphApi.query<{ channels: any[]; }>({
|
|
63
|
-
graph: GRAPH_NAME,
|
|
64
|
-
query: 'MATCH (channels: CHANNEL) RETURN collect(DISTINCT channels) as channels',
|
|
65
|
-
params: {},
|
|
66
|
-
});
|
|
67
|
-
return data.channels.length > 0;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { MigrationBase } from '../migration-base';
|
|
2
|
-
import { GRAPH_NAME } from '../constants';
|
|
3
|
-
|
|
4
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
5
|
-
export default class Migration__3 extends MigrationBase {
|
|
6
|
-
async up() {
|
|
7
|
-
const flowsIds = await this.graphApi.query<{id: string;}>({
|
|
8
|
-
graph: GRAPH_NAME,
|
|
9
|
-
query: `match (flow: FLOW)
|
|
10
|
-
where flow.steps is NULL
|
|
11
|
-
return flow.id as id
|
|
12
|
-
`,
|
|
13
|
-
params: {},
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
if (flowsIds.length) {
|
|
17
|
-
const preparedIds = flowsIds.map(f => f.id);
|
|
18
|
-
const flows = await this.flows.listFlows({
|
|
19
|
-
query: {
|
|
20
|
-
id: preparedIds,
|
|
21
|
-
},
|
|
22
|
-
projection: ['data.trees.main.steps'],
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
const pairs = flows.items.map(flow => (
|
|
26
|
-
{
|
|
27
|
-
steps: flow.data.trees?.main.steps?.length ?? 0,
|
|
28
|
-
id: flow.id,
|
|
29
|
-
})
|
|
30
|
-
);
|
|
31
|
-
|
|
32
|
-
await this.graphApi.query({
|
|
33
|
-
graph: GRAPH_NAME,
|
|
34
|
-
query: `
|
|
35
|
-
unwind $list as pairs
|
|
36
|
-
MERGE (flow:FLOW { id: pairs.id })
|
|
37
|
-
SET flow.steps = pairs.steps
|
|
38
|
-
`,
|
|
39
|
-
params: {
|
|
40
|
-
list: pairs as any,
|
|
41
|
-
},
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
package/src/migrations/index.ts
DELETED