@or-sdk/idw 1.3.7-beta.1500.0 → 1.3.8-beta.1536.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.
Files changed (78) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/dist/cjs/constants.js +2 -1
  3. package/dist/cjs/constants.js.map +1 -1
  4. package/dist/cjs/idw.js +44 -73
  5. package/dist/cjs/idw.js.map +1 -1
  6. package/dist/cjs/index.js +14 -3
  7. package/dist/cjs/index.js.map +1 -1
  8. package/dist/cjs/migration-base.js +72 -0
  9. package/dist/cjs/migration-base.js.map +1 -0
  10. package/dist/cjs/migration-handler.js +183 -0
  11. package/dist/cjs/migration-handler.js.map +1 -0
  12. package/dist/cjs/migrations/Migration__1.js +108 -0
  13. package/dist/cjs/migrations/Migration__1.js.map +1 -0
  14. package/dist/cjs/migrations/Migration__2.js +155 -0
  15. package/dist/cjs/migrations/Migration__2.js.map +1 -0
  16. package/dist/cjs/migrations/Migration__3.js +108 -0
  17. package/dist/cjs/migrations/Migration__3.js.map +1 -0
  18. package/dist/cjs/migrations/index.js +13 -0
  19. package/dist/cjs/migrations/index.js.map +1 -0
  20. package/dist/esm/constants.js +1 -0
  21. package/dist/esm/constants.js.map +1 -1
  22. package/dist/esm/idw.js +24 -36
  23. package/dist/esm/idw.js.map +1 -1
  24. package/dist/esm/index.js +2 -1
  25. package/dist/esm/index.js.map +1 -1
  26. package/dist/esm/migration-base.js +30 -0
  27. package/dist/esm/migration-base.js.map +1 -0
  28. package/dist/esm/migration-handler.js +86 -0
  29. package/dist/esm/migration-handler.js.map +1 -0
  30. package/dist/esm/migrations/Migration__1.js +40 -0
  31. package/dist/esm/migrations/Migration__1.js.map +1 -0
  32. package/dist/esm/migrations/Migration__2.js +80 -0
  33. package/dist/esm/migrations/Migration__2.js.map +1 -0
  34. package/dist/esm/migrations/Migration__3.js +53 -0
  35. package/dist/esm/migrations/Migration__3.js.map +1 -0
  36. package/dist/esm/migrations/index.js +4 -0
  37. package/dist/esm/migrations/index.js.map +1 -0
  38. package/dist/types/constants.d.ts +1 -0
  39. package/dist/types/constants.d.ts.map +1 -1
  40. package/dist/types/idw.d.ts +5 -3
  41. package/dist/types/idw.d.ts.map +1 -1
  42. package/dist/types/index.d.ts +1 -1
  43. package/dist/types/index.d.ts.map +1 -1
  44. package/dist/types/migration-base.d.ts +24 -0
  45. package/dist/types/migration-base.d.ts.map +1 -0
  46. package/dist/types/migration-handler.d.ts +20 -0
  47. package/dist/types/migration-handler.d.ts.map +1 -0
  48. package/dist/types/migrations/Migration__1.d.ts +6 -0
  49. package/dist/types/migrations/Migration__1.d.ts.map +1 -0
  50. package/dist/types/migrations/Migration__2.d.ts +7 -0
  51. package/dist/types/migrations/Migration__2.d.ts.map +1 -0
  52. package/dist/types/migrations/Migration__3.d.ts +5 -0
  53. package/dist/types/migrations/Migration__3.d.ts.map +1 -0
  54. package/dist/types/migrations/index.d.ts +4 -0
  55. package/dist/types/migrations/index.d.ts.map +1 -0
  56. package/package.json +5 -3
  57. package/src/constants.ts +1 -0
  58. package/src/idw.ts +36 -36
  59. package/src/index.ts +1 -1
  60. package/src/migration-base.ts +35 -0
  61. package/src/migration-handler.ts +94 -0
  62. package/src/migrations/Migration__1.ts +33 -0
  63. package/src/{data/channels.ts → migrations/Migration__2.ts} +33 -1
  64. package/src/migrations/Migration__3.ts +45 -0
  65. package/src/migrations/index.ts +3 -0
  66. package/dist/cjs/data/channels.js +0 -41
  67. package/dist/cjs/data/channels.js.map +0 -1
  68. package/dist/cjs/data/idwSetupData.js +0 -9
  69. package/dist/cjs/data/idwSetupData.js.map +0 -1
  70. package/dist/esm/data/channels.js +0 -38
  71. package/dist/esm/data/channels.js.map +0 -1
  72. package/dist/esm/data/idwSetupData.js +0 -6
  73. package/dist/esm/data/idwSetupData.js.map +0 -1
  74. package/dist/types/data/channels.d.ts +0 -6
  75. package/dist/types/data/channels.d.ts.map +0 -1
  76. package/dist/types/data/idwSetupData.d.ts +0 -6
  77. package/dist/types/data/idwSetupData.d.ts.map +0 -1
  78. package/src/data/idwSetupData.ts +0 -5
@@ -0,0 +1,35 @@
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
+ }
@@ -0,0 +1,94 @@
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
+ }
@@ -0,0 +1,33 @@
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,4 +1,7 @@
1
- export const startUpChannels = [
1
+ import { GRAPH_NAME } from '../constants';
2
+ import { MigrationBase } from '../migration-base';
3
+
4
+ const startUpChannels = [
2
5
  {
3
6
  id: 'facebook-messenger',
4
7
  name: 'Facebook Messenger',
@@ -35,3 +38,32 @@ export const startUpChannels = [
35
38
  icon: 'https://files.qa.api.onereach.ai/public/9c1b43df-0f7c-43fe-819f-be9df1f39759/channel-icons/whatsapp.png',
36
39
  },
37
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
+ }
@@ -0,0 +1,45 @@
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
+ }
@@ -0,0 +1,3 @@
1
+ export { default as Migration__1 } from './Migration__1';
2
+ export { default as Migration__2 } from './Migration__2';
3
+ export { default as Migration__3 } from './Migration__3';
@@ -1,41 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.startUpChannels = void 0;
4
- exports.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
- //# sourceMappingURL=channels.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"channels.js","sourceRoot":"","sources":["../../../src/data/channels.ts"],"names":[],"mappings":";;;AAAa,QAAA,eAAe,GAAG;IAC7B;QACE,EAAE,EAAE,oBAAoB;QACxB,IAAI,EAAE,oBAAoB;QAC1B,IAAI,EAAE,0GAA0G;KACjH;IACD;QACE,EAAE,EAAE,KAAK;QACT,IAAI,EAAE,KAAK;QACX,IAAI,EAAE,oGAAoG;KAC3G;IACD;QACE,EAAE,EAAE,UAAU;QACd,IAAI,EAAE,UAAU;QAChB,IAAI,EAAE,yGAAyG;KAChH;IACD;QACE,EAAE,EAAE,SAAS;QACb,IAAI,EAAE,SAAS;QACf,IAAI,EAAE,wGAAwG;KAC/G;IACD;QACE,EAAE,EAAE,OAAO;QACX,IAAI,EAAE,OAAO;QACb,IAAI,EAAE,sGAAsG;KAC7G;IACD;QACE,EAAE,EAAE,gBAAgB;QACpB,IAAI,EAAE,gBAAgB;QACtB,IAAI,EAAE,sGAAsG;KAC7G;IACD;QACE,EAAE,EAAE,UAAU;QACd,IAAI,EAAE,UAAU;QAChB,IAAI,EAAE,yGAAyG;KAChH;CACF,CAAC"}
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.idwSetupData = void 0;
4
- exports.idwSetupData = {
5
- description: 'Add a fancy description to your IDW',
6
- role: 'You can set your IDW\'s role/s just by chatting with the embedded RWC',
7
- avatar: 'https://files.qa.api.onereach.ai/public/03db4abe-0477-47c0-ae4e-4c68f1047d59/IDW/IDWs-media-data/Img.png',
8
- };
9
- //# sourceMappingURL=idwSetupData.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"idwSetupData.js","sourceRoot":"","sources":["../../../src/data/idwSetupData.ts"],"names":[],"mappings":";;;AAAa,QAAA,YAAY,GAAG;IAC1B,WAAW,EAAE,qCAAqC;IAClD,IAAI,EAAE,uEAAuE;IAC7E,MAAM,EAAE,0GAA0G;CACnH,CAAC"}
@@ -1,38 +0,0 @@
1
- export const startUpChannels = [
2
- {
3
- id: 'facebook-messenger',
4
- name: 'Facebook Messenger',
5
- icon: 'https://files.qa.api.onereach.ai/public/9c1b43df-0f7c-43fe-819f-be9df1f39759/channel-icons/messenger.png',
6
- },
7
- {
8
- id: 'sms',
9
- name: 'SMS',
10
- icon: 'https://files.qa.api.onereach.ai/public/9c1b43df-0f7c-43fe-819f-be9df1f39759/channel-icons/sms.png',
11
- },
12
- {
13
- id: 'telegram',
14
- name: 'Telegram',
15
- icon: 'https://files.qa.api.onereach.ai/public/9c1b43df-0f7c-43fe-819f-be9df1f39759/channel-icons/telegram.png',
16
- },
17
- {
18
- id: 'twitter',
19
- name: 'Twitter',
20
- icon: 'https://files.qa.api.onereach.ai/public/9c1b43df-0f7c-43fe-819f-be9df1f39759/channel-icons/twitter.png',
21
- },
22
- {
23
- id: 'viber',
24
- name: 'Viber',
25
- icon: 'https://files.qa.api.onereach.ai/public/9c1b43df-0f7c-43fe-819f-be9df1f39759/channel-icons/viber.png',
26
- },
27
- {
28
- id: 'viber-business',
29
- name: 'Viber Business',
30
- icon: 'https://files.qa.api.onereach.ai/public/9c1b43df-0f7c-43fe-819f-be9df1f39759/channel-icons/viber.png',
31
- },
32
- {
33
- id: 'whatsapp',
34
- name: 'WhatsApp',
35
- icon: 'https://files.qa.api.onereach.ai/public/9c1b43df-0f7c-43fe-819f-be9df1f39759/channel-icons/whatsapp.png',
36
- },
37
- ];
38
- //# sourceMappingURL=channels.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"channels.js","sourceRoot":"","sources":["../../../src/data/channels.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,eAAe,GAAG;IAC7B;QACE,EAAE,EAAE,oBAAoB;QACxB,IAAI,EAAE,oBAAoB;QAC1B,IAAI,EAAE,0GAA0G;KACjH;IACD;QACE,EAAE,EAAE,KAAK;QACT,IAAI,EAAE,KAAK;QACX,IAAI,EAAE,oGAAoG;KAC3G;IACD;QACE,EAAE,EAAE,UAAU;QACd,IAAI,EAAE,UAAU;QAChB,IAAI,EAAE,yGAAyG;KAChH;IACD;QACE,EAAE,EAAE,SAAS;QACb,IAAI,EAAE,SAAS;QACf,IAAI,EAAE,wGAAwG;KAC/G;IACD;QACE,EAAE,EAAE,OAAO;QACX,IAAI,EAAE,OAAO;QACb,IAAI,EAAE,sGAAsG;KAC7G;IACD;QACE,EAAE,EAAE,gBAAgB;QACpB,IAAI,EAAE,gBAAgB;QACtB,IAAI,EAAE,sGAAsG;KAC7G;IACD;QACE,EAAE,EAAE,UAAU;QACd,IAAI,EAAE,UAAU;QAChB,IAAI,EAAE,yGAAyG;KAChH;CACF,CAAC"}
@@ -1,6 +0,0 @@
1
- export const idwSetupData = {
2
- description: 'Add a fancy description to your IDW',
3
- role: 'You can set your IDW\'s role/s just by chatting with the embedded RWC',
4
- avatar: 'https://files.qa.api.onereach.ai/public/03db4abe-0477-47c0-ae4e-4c68f1047d59/IDW/IDWs-media-data/Img.png',
5
- };
6
- //# sourceMappingURL=idwSetupData.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"idwSetupData.js","sourceRoot":"","sources":["../../../src/data/idwSetupData.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,YAAY,GAAG;IAC1B,WAAW,EAAE,qCAAqC;IAClD,IAAI,EAAE,uEAAuE;IAC7E,MAAM,EAAE,0GAA0G;CACnH,CAAC"}
@@ -1,6 +0,0 @@
1
- export declare const startUpChannels: {
2
- id: string;
3
- name: string;
4
- icon: string;
5
- }[];
6
- //# sourceMappingURL=channels.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"channels.d.ts","sourceRoot":"","sources":["../../../src/data/channels.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,eAAe;;;;GAoC3B,CAAC"}
@@ -1,6 +0,0 @@
1
- export declare const idwSetupData: {
2
- description: string;
3
- role: string;
4
- avatar: string;
5
- };
6
- //# sourceMappingURL=idwSetupData.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"idwSetupData.d.ts","sourceRoot":"","sources":["../../../src/data/idwSetupData.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY;;;;CAIxB,CAAC"}
@@ -1,5 +0,0 @@
1
- export const idwSetupData = {
2
- description: 'Add a fancy description to your IDW',
3
- role: 'You can set your IDW\'s role/s just by chatting with the embedded RWC',
4
- avatar: 'https://files.qa.api.onereach.ai/public/03db4abe-0477-47c0-ae4e-4c68f1047d59/IDW/IDWs-media-data/Img.png',
5
- };