@itleanchatbot/shared-models-js-postgres 2.8.17 → 2.8.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@itleanchatbot/shared-models-js-postgres",
3
- "version": "2.8.17",
3
+ "version": "2.8.18",
4
4
  "description": "Shared Models JS Postgres",
5
5
  "main": "index.js",
6
6
  "license": "ISC",
@@ -0,0 +1,321 @@
1
+ 'use strict'
2
+
3
+ module.exports = {
4
+ up: async (queryInterface, Sequelize) => {
5
+
6
+ await queryInterface.sequelize.query(
7
+ `
8
+ CREATE OR REPLACE FUNCTION get_inactive_sessions(first_call boolean, records_per_page int, starter_page int, maximun int, current_page int DEFAULT 1)
9
+ RETURNS TABLE ("id" uuid) AS $$
10
+ declare r RECORD;
11
+ declare last_iterable "BotIterables"%ROWTYPE;
12
+ declare min_diff INT;
13
+ declare counter INT := 0;
14
+ BEGIN
15
+
16
+ IF ((records_per_page * current_page) > maximun) THEN
17
+ RETURN;
18
+ END IF;
19
+
20
+ IF (first_call) THEN
21
+ DROP TABLE IF EXISTS _tmp_get_inactive_sessions;
22
+ CREATE TEMPORARY TABLE _tmp_get_inactive_sessions (
23
+ "id" uuid
24
+ );
25
+ END IF;
26
+
27
+ FOR r IN (
28
+ select distinct
29
+ "Sessions"."id",
30
+ "InactivityTriggers"."min",
31
+ "Sessions"."createdAt"
32
+ from "Sessions"
33
+ inner join "Clients"
34
+ on "Clients"."id" = "Sessions"."ClientId"
35
+ inner join "ConversationSessions"
36
+ on "ConversationSessions"."ClientId" = "Clients"."id"
37
+ and age(current_timestamp, "ConversationSessions"."createdAt") < interval '24 hours'
38
+ inner join "Channels"
39
+ on "Channels"."id" = "Sessions"."ChannelId"
40
+ inner join "InactivityTriggers"
41
+ on "InactivityTriggers"."SkillId" = "Channels"."SkillId"
42
+ and "InactivityTriggers"."order" = "Sessions"."inactivityCounter"
43
+ where "Sessions"."status" = 'open'
44
+ and "Sessions"."tranship" = false
45
+ and "Channels"."deletedAt" is null
46
+ order by "Sessions"."createdAt"
47
+ limit records_per_page
48
+ offset ((starter_page - 1)*records_per_page)
49
+ ) LOOP
50
+
51
+ select * into last_iterable
52
+ from "BotIterables"
53
+ where "BotIterables"."SessionId" = r."id"
54
+ order by "BotIterables"."createdAt" DESC
55
+ limit 1;
56
+
57
+ min_diff := (
58
+ (SELECT DATE_PART('day', current_timestamp - last_iterable."createdAt") * 24 * 60) +
59
+ (SELECT DATE_PART('hour', current_timestamp - last_iterable."createdAt") * 60) +
60
+ (SELECT DATE_PART('minute', current_timestamp - last_iterable."createdAt"))
61
+ );
62
+
63
+ IF (last_iterable."action" = 'bot' and min_diff >= r."min") THEN
64
+
65
+ INSERT INTO _tmp_get_inactive_sessions ("id")
66
+ VALUES (r."id");
67
+
68
+ END IF;
69
+
70
+ counter := counter + 1;
71
+
72
+ END LOOP;
73
+
74
+ IF (counter = records_per_page) THEN
75
+ PERFORM get_inactive_sessions(false, records_per_page, starter_page + 1, maximun, current_page + 1);
76
+ END IF;
77
+
78
+ RETURN QUERY
79
+ SELECT * FROM _tmp_get_inactive_sessions;
80
+
81
+ END;
82
+ $$ LANGUAGE plpgsql;
83
+ `
84
+ )
85
+
86
+ await queryInterface.sequelize.query(
87
+ `
88
+ CREATE OR REPLACE FUNCTION get_inactive_attendances(first_call boolean, records_per_page int, starter_page int, maximun int, current_page int DEFAULT 1)
89
+ RETURNS TABLE ("id" uuid) AS $$
90
+ declare r RECORD;
91
+ declare last_iterable "TranshipConversations"%ROWTYPE;
92
+ declare min_diff INT;
93
+ declare counter INT := 0;
94
+ BEGIN
95
+
96
+ IF ((records_per_page * current_page) > maximun) THEN
97
+ RETURN;
98
+ END IF;
99
+
100
+ IF (first_call) THEN
101
+ DROP TABLE IF EXISTS _tmp_get_inactive_attendances;
102
+ CREATE TEMPORARY TABLE _tmp_get_inactive_attendances (
103
+ "id" uuid
104
+ );
105
+ END IF;
106
+
107
+ FOR r IN (
108
+ select distinct
109
+ "Sessions"."id",
110
+ "Attendances"."id" as "AttendanceId",
111
+ "TranshipInactivityTriggers"."min",
112
+ "Sessions"."createdAt"
113
+ from "Sessions"
114
+ inner join "Clients"
115
+ on "Clients"."id" = "Sessions"."ClientId"
116
+ inner join "ConversationSessions"
117
+ on "ConversationSessions"."ClientId" = "Clients"."id"
118
+ and age(current_timestamp, "ConversationSessions"."createdAt") < interval '24 hours'
119
+ inner join "Attendances"
120
+ on "Attendances"."SessionId" = "Sessions"."id"
121
+ and "Attendances"."status" = 'started'
122
+ inner join "TranshipInactivityTriggers"
123
+ on "Attendances"."LineId" = "TranshipInactivityTriggers"."LineId"
124
+ and "TranshipInactivityTriggers"."order" = "Attendances"."inactivityCounter"
125
+ where "Sessions"."tranship" = true
126
+ and "Sessions"."status" = 'open'
127
+ order by "Sessions"."createdAt"
128
+ limit records_per_page
129
+ offset ((starter_page - 1)*records_per_page)
130
+ ) LOOP
131
+
132
+ select * into last_iterable
133
+ from "TranshipConversations"
134
+ where "TranshipConversations"."AttendanceId" = r."AttendanceId"
135
+ order by "TranshipConversations"."createdAt" DESC
136
+ limit 1;
137
+
138
+ min_diff := (
139
+ (SELECT DATE_PART('day', current_timestamp - last_iterable."createdAt") * 24 * 60) +
140
+ (SELECT DATE_PART('hour', current_timestamp - last_iterable."createdAt") * 60) +
141
+ (SELECT DATE_PART('minute', current_timestamp - last_iterable."createdAt"))
142
+ );
143
+
144
+ IF (last_iterable."action" = 'tranship' and min_diff >= r."min") THEN
145
+
146
+ INSERT INTO _tmp_get_inactive_attendances ("id")
147
+ VALUES (r."id");
148
+
149
+ END IF;
150
+
151
+ counter := counter + 1;
152
+
153
+ END LOOP;
154
+
155
+ IF (counter = records_per_page) THEN
156
+ PERFORM get_inactive_attendances(false, records_per_page, starter_page + 1, maximun, current_page + 1);
157
+ END IF;
158
+
159
+ RETURN QUERY
160
+ SELECT * FROM _tmp_get_inactive_attendances;
161
+
162
+ END;
163
+ $$ LANGUAGE plpgsql;
164
+ `
165
+ )
166
+ },
167
+
168
+ down: async (queryInterface, Sequelize) => {
169
+
170
+ await queryInterface.sequelize.query(
171
+ `
172
+ CREATE OR REPLACE FUNCTION get_inactive_sessions(first_call boolean, records_per_page int, starter_page int, maximun int, current_page int DEFAULT 1)
173
+ RETURNS TABLE ("id" uuid) AS $$
174
+ declare r RECORD;
175
+ declare last_iterable "BotIterables"%ROWTYPE;
176
+ declare min_diff INT;
177
+ declare counter INT := 0;
178
+ BEGIN
179
+
180
+ IF ((records_per_page * current_page) > maximun) THEN
181
+ RETURN;
182
+ END IF;
183
+
184
+ IF (first_call) THEN
185
+ DROP TABLE IF EXISTS _tmp_get_inactive_sessions;
186
+ CREATE TEMPORARY TABLE _tmp_get_inactive_sessions (
187
+ "id" uuid
188
+ );
189
+ END IF;
190
+
191
+ FOR r IN (
192
+ select "Sessions"."id",
193
+ "InactivityTriggers"."min"
194
+ from "Sessions"
195
+ inner join "Clients"
196
+ on "Clients"."id" = "Sessions"."ClientId"
197
+ inner join "Channels"
198
+ on "Channels"."id" = "Sessions"."ChannelId"
199
+ inner join "ChannelTypes"
200
+ on "ChannelTypes"."id" = "Channels"."ChannelTypeId"
201
+ inner join "InactivityTriggers"
202
+ on "InactivityTriggers"."SkillId" = "Channels"."SkillId"
203
+ and "InactivityTriggers"."order" = "Sessions"."inactivityCounter"
204
+ where "Sessions"."status" = 'open'
205
+ and "Sessions"."tranship" = false
206
+ and "Channels"."deletedAt" is null
207
+ order by "Sessions"."createdAt"
208
+ limit records_per_page
209
+ offset ((starter_page - 1)*records_per_page)
210
+ ) LOOP
211
+
212
+ select * into last_iterable
213
+ from "BotIterables"
214
+ where "BotIterables"."SessionId" = r."id"
215
+ order by "BotIterables"."createdAt" DESC
216
+ limit 1;
217
+
218
+ min_diff := (
219
+ (SELECT DATE_PART('day', current_timestamp - last_iterable."createdAt") * 24 * 60) +
220
+ (SELECT DATE_PART('hour', current_timestamp - last_iterable."createdAt") * 60) +
221
+ (SELECT DATE_PART('minute', current_timestamp - last_iterable."createdAt"))
222
+ );
223
+
224
+ IF (last_iterable."action" = 'bot' and min_diff >= r."min") THEN
225
+
226
+ INSERT INTO _tmp_get_inactive_sessions ("id")
227
+ VALUES (r."id");
228
+
229
+ END IF;
230
+
231
+ counter := counter + 1;
232
+
233
+ END LOOP;
234
+
235
+ IF (counter = records_per_page) THEN
236
+ PERFORM get_inactive_sessions(false, records_per_page, starter_page + 1, maximun, current_page + 1);
237
+ END IF;
238
+
239
+ RETURN QUERY
240
+ SELECT * FROM _tmp_get_inactive_sessions;
241
+
242
+ END;
243
+ $$ LANGUAGE plpgsql;
244
+ `
245
+ )
246
+
247
+ await queryInterface.sequelize.query(
248
+ `
249
+ CREATE OR REPLACE FUNCTION get_inactive_attendances(first_call boolean, records_per_page int, starter_page int, maximun int, current_page int DEFAULT 1)
250
+ RETURNS TABLE ("id" uuid) AS $$
251
+ declare r RECORD;
252
+ declare last_iterable "TranshipConversations"%ROWTYPE;
253
+ declare min_diff INT;
254
+ declare counter INT := 0;
255
+ BEGIN
256
+
257
+ IF ((records_per_page * current_page) > maximun) THEN
258
+ RETURN;
259
+ END IF;
260
+
261
+ IF (first_call) THEN
262
+ DROP TABLE IF EXISTS _tmp_get_inactive_attendances;
263
+ CREATE TEMPORARY TABLE _tmp_get_inactive_attendances (
264
+ "id" uuid
265
+ );
266
+ END IF;
267
+
268
+ FOR r IN (
269
+ select "Sessions"."id",
270
+ "Attendances"."id" as "AttendanceId",
271
+ "TranshipInactivityTriggers"."min"
272
+ from "Sessions"
273
+ inner join "Attendances"
274
+ on "Attendances"."SessionId" = "Sessions"."id"
275
+ and "Attendances"."status" = 'started'
276
+ inner join "TranshipInactivityTriggers"
277
+ on "Attendances"."LineId" = "TranshipInactivityTriggers"."LineId"
278
+ and "TranshipInactivityTriggers"."order" = "Attendances"."inactivityCounter"
279
+ where "Sessions"."tranship" = true
280
+ and "Sessions"."status" = 'open'
281
+ order by "Sessions"."createdAt"
282
+ limit records_per_page
283
+ offset ((starter_page - 1)*records_per_page)
284
+ ) LOOP
285
+
286
+ select * into last_iterable
287
+ from "TranshipConversations"
288
+ where "TranshipConversations"."AttendanceId" = r."AttendanceId"
289
+ order by "TranshipConversations"."createdAt" DESC
290
+ limit 1;
291
+
292
+ min_diff := (
293
+ (SELECT DATE_PART('day', current_timestamp - last_iterable."createdAt") * 24 * 60) +
294
+ (SELECT DATE_PART('hour', current_timestamp - last_iterable."createdAt") * 60) +
295
+ (SELECT DATE_PART('minute', current_timestamp - last_iterable."createdAt"))
296
+ );
297
+
298
+ IF (last_iterable."action" = 'tranship' and min_diff >= r."min") THEN
299
+
300
+ INSERT INTO _tmp_get_inactive_attendances ("id")
301
+ VALUES (r."id");
302
+
303
+ END IF;
304
+
305
+ counter := counter + 1;
306
+
307
+ END LOOP;
308
+
309
+ IF (counter = records_per_page) THEN
310
+ PERFORM get_inactive_attendances(false, records_per_page, starter_page + 1, maximun, current_page + 1);
311
+ END IF;
312
+
313
+ RETURN QUERY
314
+ SELECT * FROM _tmp_get_inactive_attendances;
315
+
316
+ END;
317
+ $$ LANGUAGE plpgsql;
318
+ `
319
+ )
320
+ },
321
+ }
@@ -18,6 +18,7 @@ exports.model = (sequelize, DataType) => {
18
18
 
19
19
  Client.associate = (models) => {
20
20
  Client.hasMany(models.Sessions)
21
+ Client.hasMany(models.ConversationSessions)
21
22
  }
22
23
 
23
24
  return Client
@@ -1,43 +1,44 @@
1
- 'use strict';
2
- const { CHANNEL_TYPES } = require('../models/channelTypes')
3
- module.exports = {
4
- up: async (queryInterface) => {
5
- const responses = [
6
- {"action":"text","display":"Texto"},
7
- {"action":"image","display":"Imagem"},
8
- {"action":"video","display":"Vídeo"},
9
- {"action":"audio","display":"Audio"},
10
- {"action":"file","display":"Arquivo"},
11
- {"action":"list","display":"Lista"},
12
- {"action":"receipt","display":"Recibo"},
13
- {"action":"feedback","display":"Feedback"},
14
- {"action":"carousel","display":"Carrossel"},
15
- ]
16
-
17
- const microsoftTeams = {
18
- channelName: CHANNEL_TYPES.FACEBOOK_MESSENGER,
19
- channelLabel: 'Facebook - Messenger',
20
- responsesTypes: JSON.stringify(responses),
21
- createdAt: new Date(),
22
- updatedAt: new Date(),
23
- }
24
-
25
- return queryInterface.bulkInsert('ChannelTypes', [
26
- microsoftTeams
27
- ])
28
- },
29
-
30
- down: async (queryInterface) => {
31
- const ChannelTypeId = await queryInterface.rawSelect(
32
- 'ChannelTypes',
33
- {
34
- where: {
35
- channelName: CHANNEL_TYPES.FACEBOOK_MESSENGER,
36
- },
37
- },
38
- ['id']
39
- )
40
- await queryInterface.bulkDelete('Channels', [{ ChannelTypeId }], {})
41
- return queryInterface.bulkDelete('ChannelTypes', [{ channelName: CHANNEL_TYPES.FACEBOOK_MESSENGER }], {})
42
- }
43
- };
1
+ 'use strict';
2
+ const { CHANNEL_TYPES } = require('../models/channelTypes')
3
+ module.exports = {
4
+ up: async (queryInterface) => {
5
+ const responses = [
6
+ {"action":"text","display":"Texto"},
7
+ {"action":"image","display":"Imagem"},
8
+ {"action":"video","display":"Vídeo"},
9
+ {"action":"audio","display":"Audio"},
10
+ {"action":"file","display":"Arquivo"},
11
+ {"action":"list","display":"Lista"},
12
+ {"action":"quick_replay","display":"Resposta rápida"},
13
+ {"action":"receipt","display":"Recibo"},
14
+ {"action":"feedback","display":"Feedback"},
15
+ {"action":"carousel","display":"Carrossel"},
16
+ ]
17
+
18
+ const microsoftTeams = {
19
+ channelName: CHANNEL_TYPES.FACEBOOK_MESSENGER,
20
+ channelLabel: 'Facebook - Messenger',
21
+ responsesTypes: JSON.stringify(responses),
22
+ createdAt: new Date(),
23
+ updatedAt: new Date(),
24
+ }
25
+
26
+ return queryInterface.bulkInsert('ChannelTypes', [
27
+ microsoftTeams
28
+ ])
29
+ },
30
+
31
+ down: async (queryInterface) => {
32
+ const ChannelTypeId = await queryInterface.rawSelect(
33
+ 'ChannelTypes',
34
+ {
35
+ where: {
36
+ channelName: CHANNEL_TYPES.FACEBOOK_MESSENGER,
37
+ },
38
+ },
39
+ ['id']
40
+ )
41
+ await queryInterface.bulkDelete('Channels', [{ ChannelTypeId }], {})
42
+ return queryInterface.bulkDelete('ChannelTypes', [{ channelName: CHANNEL_TYPES.FACEBOOK_MESSENGER }], {})
43
+ }
44
+ };
@@ -1,62 +1,62 @@
1
- 'use strict';
2
-
3
- const { CHANNEL_TYPES } = require('../models/channelTypes')
4
-
5
- const configurations = [
6
- {
7
- fieldName: 'appId',
8
- fieldType: 'string',
9
- fieldLabel: 'ID do aplicativo',
10
- },
11
- {
12
- fieldName: 'pageId',
13
- fieldType: 'string',
14
- fieldLabel: 'ID da página',
15
- },
16
- {
17
- fieldName: 'accessToken',
18
- fieldType: 'string',
19
- fieldLabel: 'Token de acesso',
20
- },
21
- ]
22
-
23
- module.exports = {
24
- up: async (queryInterface) => {
25
- const ChannelTypeId = await queryInterface.rawSelect(
26
- 'ChannelTypes',
27
- {
28
- where: {
29
- channelName: CHANNEL_TYPES.FACEBOOK_MESSENGER,
30
- },
31
- },
32
- ['id']
33
- )
34
-
35
- const channelConfigurations = configurations.map((channel) => {
36
- return {
37
- ...channel,
38
- ChannelTypeId,
39
- createdAt: new Date(),
40
- updatedAt: new Date()
41
- }
42
- })
43
-
44
- await queryInterface.bulkInsert(
45
- 'ChannelConfigurations',
46
- channelConfigurations
47
- )
48
- },
49
-
50
- down: async (queryInterface) => {
51
- const ChannelTypeId = await queryInterface.rawSelect(
52
- 'ChannelTypes',
53
- {
54
- where: {
55
- channelName: CHANNEL_TYPES.FACEBOOK_MESSENGER,
56
- },
57
- },
58
- ['id']
59
- )
60
- return queryInterface.bulkDelete('ChannelConfigurations', [{ ChannelTypeId }], {})
61
- }
62
- };
1
+ 'use strict';
2
+
3
+ const { CHANNEL_TYPES } = require('../models/channelTypes')
4
+
5
+ const configurations = [
6
+ {
7
+ fieldName: 'appId',
8
+ fieldType: 'string',
9
+ fieldLabel: 'ID do aplicativo',
10
+ },
11
+ {
12
+ fieldName: 'pageId',
13
+ fieldType: 'string',
14
+ fieldLabel: 'ID da página',
15
+ },
16
+ {
17
+ fieldName: 'accessToken',
18
+ fieldType: 'string',
19
+ fieldLabel: 'Token de acesso',
20
+ },
21
+ ]
22
+
23
+ module.exports = {
24
+ up: async (queryInterface) => {
25
+ const ChannelTypeId = await queryInterface.rawSelect(
26
+ 'ChannelTypes',
27
+ {
28
+ where: {
29
+ channelName: CHANNEL_TYPES.FACEBOOK_MESSENGER,
30
+ },
31
+ },
32
+ ['id']
33
+ )
34
+
35
+ const channelConfigurations = configurations.map((channel) => {
36
+ return {
37
+ ...channel,
38
+ ChannelTypeId,
39
+ createdAt: new Date(),
40
+ updatedAt: new Date()
41
+ }
42
+ })
43
+
44
+ await queryInterface.bulkInsert(
45
+ 'ChannelConfigurations',
46
+ channelConfigurations
47
+ )
48
+ },
49
+
50
+ down: async (queryInterface) => {
51
+ const ChannelTypeId = await queryInterface.rawSelect(
52
+ 'ChannelTypes',
53
+ {
54
+ where: {
55
+ channelName: CHANNEL_TYPES.FACEBOOK_MESSENGER,
56
+ },
57
+ },
58
+ ['id']
59
+ )
60
+ return queryInterface.bulkDelete('ChannelConfigurations', [{ ChannelTypeId }], {})
61
+ }
62
+ };