@itleanchatbot/shared-models-js-postgres 3.1.0 → 3.1.3
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 +1 -1
- package/src/migrations/20250715190352-create-transcriptions-table.js +41 -0
- package/src/migrations/20250819-create-view-channel_message_type_daily.js +43 -0
- package/src/migrations/20250820141518-update-view-channel_message_type_daily.js +61 -0
- package/src/models/Transcription.js +34 -0
- package/src/models/channelMessageTypeDaily.js +20 -0
- package/src/models/skill.js +5 -0
- package/_leanbot_enterprises_map.json +0 -7
package/package.json
CHANGED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
async up(queryInterface, Sequelize) {
|
|
5
|
+
await queryInterface.createTable('Transcriptions', {
|
|
6
|
+
id: {
|
|
7
|
+
type: Sequelize.UUID,
|
|
8
|
+
allowNull: false,
|
|
9
|
+
primaryKey: true,
|
|
10
|
+
defaultValue: Sequelize.UUIDV4
|
|
11
|
+
},
|
|
12
|
+
SkillId: {
|
|
13
|
+
type: Sequelize.UUID,
|
|
14
|
+
allowNull: false
|
|
15
|
+
},
|
|
16
|
+
TTSSST: {
|
|
17
|
+
type: Sequelize.BOOLEAN
|
|
18
|
+
},
|
|
19
|
+
CloudProvider: {
|
|
20
|
+
type: Sequelize.STRING
|
|
21
|
+
},
|
|
22
|
+
TTSSSTConfig: {
|
|
23
|
+
type: Sequelize.TEXT
|
|
24
|
+
},
|
|
25
|
+
createdAt: {
|
|
26
|
+
allowNull: false,
|
|
27
|
+
type: Sequelize.DATE,
|
|
28
|
+
defaultValue: Sequelize.fn('NOW')
|
|
29
|
+
},
|
|
30
|
+
updatedAt: {
|
|
31
|
+
allowNull: false,
|
|
32
|
+
type: Sequelize.DATE,
|
|
33
|
+
defaultValue: Sequelize.fn('NOW')
|
|
34
|
+
}
|
|
35
|
+
})
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
async down(queryInterface, Sequelize) {
|
|
39
|
+
await queryInterface.dropTable('Transcriptions')
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** 20250819-create-view-channel_message_type_daily */
|
|
2
|
+
module.exports = {
|
|
3
|
+
up: async queryInterface => {
|
|
4
|
+
await queryInterface.sequelize.query(`
|
|
5
|
+
DROP VIEW IF EXISTS channel_message_type_daily;
|
|
6
|
+
CREATE VIEW channel_message_type_daily AS
|
|
7
|
+
SELECT
|
|
8
|
+
(bi."createdAt")::date AS day,
|
|
9
|
+
bi."EnterpriseId" AS "EnterpriseId",
|
|
10
|
+
s."ChannelId" AS "ChannelId",
|
|
11
|
+
ch.name AS "channelName",
|
|
12
|
+
CASE
|
|
13
|
+
WHEN
|
|
14
|
+
-- transcrição presente no inbound_message => áudio
|
|
15
|
+
((bi.context::jsonb #> '{inbound_message,payload,payload}') ? 'transcription') OR
|
|
16
|
+
((bi.context::jsonb #> '{inboundMessage,payload,payload}') ? 'transcription') OR
|
|
17
|
+
-- tipo audio no inbound
|
|
18
|
+
((bi.context::jsonb #>> '{inbound_message,payload,payload,type}') = 'audio') OR
|
|
19
|
+
((bi.context::jsonb #>> '{inboundMessage,payload,payload,type}') = 'audio') OR
|
|
20
|
+
-- contentType de áudio em qualquer um dos lugares usuais
|
|
21
|
+
((bi.context::jsonb #>> '{inbound_message,payload,payload,contentType}') ILIKE 'audio/%') OR
|
|
22
|
+
((bi.context::jsonb #>> '{inboundMessage,payload,payload,contentType}') ILIKE 'audio/%') OR
|
|
23
|
+
((coalesce(bi.payload, '{}'::jsonb) ->> 'mimetype') ILIKE 'audio/%') OR
|
|
24
|
+
-- fallback mínimo: nossa flag explícita
|
|
25
|
+
(bi.message = 'whatsapp_gupshup_audio')
|
|
26
|
+
THEN 'audio'
|
|
27
|
+
ELSE 'text'
|
|
28
|
+
END AS "messageType",
|
|
29
|
+
COUNT(*) AS total
|
|
30
|
+
FROM "BotIterables" bi
|
|
31
|
+
LEFT JOIN "Sessions" s ON s.id = bi."SessionId"
|
|
32
|
+
LEFT JOIN "Channels" ch ON ch.id = s."ChannelId"
|
|
33
|
+
WHERE bi.action = 'client'
|
|
34
|
+
AND bi.type = 'message'
|
|
35
|
+
GROUP BY day, bi."EnterpriseId", s."ChannelId", ch.name, "messageType";
|
|
36
|
+
`)
|
|
37
|
+
},
|
|
38
|
+
down: async queryInterface => {
|
|
39
|
+
await queryInterface.sequelize.query(`
|
|
40
|
+
DROP VIEW IF EXISTS channel_message_type_daily;
|
|
41
|
+
`)
|
|
42
|
+
},
|
|
43
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
/** @type {import('sequelize-cli').Migration} */
|
|
3
|
+
module.exports = {
|
|
4
|
+
async up(queryInterface) {
|
|
5
|
+
await queryInterface.sequelize.query(`
|
|
6
|
+
DROP VIEW IF EXISTS channel_message_type_daily;
|
|
7
|
+
CREATE VIEW channel_message_type_daily AS
|
|
8
|
+
WITH base AS (
|
|
9
|
+
SELECT
|
|
10
|
+
/* Ajuste de fuso antes do ::date */
|
|
11
|
+
((bi."createdAt" AT TIME ZONE 'UTC') AT TIME ZONE 'America/Sao_Paulo')::date AS day,
|
|
12
|
+
bi."EnterpriseId" AS "EnterpriseId",
|
|
13
|
+
s."ChannelId" AS "ChannelId",
|
|
14
|
+
ch.name AS "channelName",
|
|
15
|
+
/* Um id “estável” do inbound para evitar duplicatas (quando existir) */
|
|
16
|
+
COALESCE(
|
|
17
|
+
bi.context::jsonb #>> '{inbound_message,payload,id}',
|
|
18
|
+
bi.context::jsonb #>> '{inboundMessage,payload,id}',
|
|
19
|
+
bi.context::jsonb #>> '{inbound_message,id}',
|
|
20
|
+
bi.context::jsonb #>> '{inboundMessage,id}',
|
|
21
|
+
bi.id::text
|
|
22
|
+
) AS inbound_id,
|
|
23
|
+
/* Regra de áudio bem ampla, sempre em jsonb */
|
|
24
|
+
CASE WHEN
|
|
25
|
+
/* type='audio' em variações do payload */
|
|
26
|
+
((bi.context::jsonb #>> '{inbound_message,payload,payload,type}') = 'audio') OR
|
|
27
|
+
((bi.context::jsonb #>> '{inboundMessage,payload,payload,type}') = 'audio') OR
|
|
28
|
+
/* contentType indicando áudio */
|
|
29
|
+
((bi.context::jsonb #>> '{inbound_message,payload,payload,contentType}') ILIKE 'audio/%') OR
|
|
30
|
+
((bi.context::jsonb #>> '{inboundMessage,payload,payload,contentType}') ILIKE 'audio/%') OR
|
|
31
|
+
/* mimetype salvo em payload do BotIterable (ex.: webchat/whats) */
|
|
32
|
+
((bi.payload::jsonb ->> 'mimetype') ILIKE 'audio/%')
|
|
33
|
+
THEN 'audio'
|
|
34
|
+
ELSE 'text'
|
|
35
|
+
END AS "messageType"
|
|
36
|
+
FROM "BotIterables" bi
|
|
37
|
+
LEFT JOIN "Sessions" s ON s.id = bi."SessionId"
|
|
38
|
+
LEFT JOIN "Channels" ch ON ch.id = s."ChannelId"
|
|
39
|
+
WHERE bi.action = 'client'
|
|
40
|
+
AND bi.type = 'message'
|
|
41
|
+
/* Garante que é mensagem de entrada “real”
|
|
42
|
+
(descarta os BotIterables sintéticos do audio-status) */
|
|
43
|
+
AND (
|
|
44
|
+
(bi.context::jsonb ? 'inbound_message')
|
|
45
|
+
OR
|
|
46
|
+
(bi.context::jsonb ? 'inboundMessage')
|
|
47
|
+
)
|
|
48
|
+
)
|
|
49
|
+
SELECT
|
|
50
|
+
day, "EnterpriseId", "ChannelId", "channelName", "messageType",
|
|
51
|
+
COUNT(DISTINCT inbound_id) AS total
|
|
52
|
+
FROM base
|
|
53
|
+
GROUP BY day, "EnterpriseId", "ChannelId", "channelName", "messageType";
|
|
54
|
+
`)
|
|
55
|
+
},
|
|
56
|
+
async down(queryInterface) {
|
|
57
|
+
await queryInterface.sequelize.query(
|
|
58
|
+
`DROP VIEW IF EXISTS channel_message_type_daily;`,
|
|
59
|
+
)
|
|
60
|
+
},
|
|
61
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
module.exports.model = (sequelize, DataTypes) => {
|
|
2
|
+
const Transcription = sequelize.define('Transcription', {
|
|
3
|
+
id: {
|
|
4
|
+
type: DataTypes.UUID,
|
|
5
|
+
primaryKey: true,
|
|
6
|
+
defaultValue: DataTypes.UUIDV4,
|
|
7
|
+
allowNull: false,
|
|
8
|
+
},
|
|
9
|
+
SkillId: {
|
|
10
|
+
type: DataTypes.UUID,
|
|
11
|
+
allowNull: false,
|
|
12
|
+
},
|
|
13
|
+
TTSSST: {
|
|
14
|
+
type: DataTypes.BOOLEAN,
|
|
15
|
+
},
|
|
16
|
+
CloudProvider: {
|
|
17
|
+
type: DataTypes.STRING,
|
|
18
|
+
},
|
|
19
|
+
TTSSSTConfig: {
|
|
20
|
+
type: DataTypes.TEXT,
|
|
21
|
+
},
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
Transcription.associate = (models) => {
|
|
25
|
+
Transcription.belongsTo(models.Skill, {
|
|
26
|
+
foreignKey: 'SkillId',
|
|
27
|
+
onDelete: 'CASCADE',
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
Transcription._repository = Transcription
|
|
32
|
+
|
|
33
|
+
return Transcription
|
|
34
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
exports.model = (sequelize, DataTypes) => {
|
|
2
|
+
const V = sequelize.define(
|
|
3
|
+
'ChannelMessageTypeDaily',
|
|
4
|
+
{
|
|
5
|
+
EnterpriseId: { type: DataTypes.UUID, allowNull: false },
|
|
6
|
+
ChannelId: { type: DataTypes.UUID, allowNull: true },
|
|
7
|
+
channelName: { type: DataTypes.STRING },
|
|
8
|
+
day: { type: DataTypes.DATEONLY, allowNull: false },
|
|
9
|
+
messageType: { type: DataTypes.STRING }, // 'text' | 'audio'
|
|
10
|
+
total: { type: DataTypes.BIGINT },
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
tableName: 'v_channel_message_type_daily',
|
|
14
|
+
timestamps: false,
|
|
15
|
+
freezeTableName: true,
|
|
16
|
+
},
|
|
17
|
+
)
|
|
18
|
+
V.removeAttribute('id')
|
|
19
|
+
return V
|
|
20
|
+
}
|
package/src/models/skill.js
CHANGED
|
@@ -117,6 +117,11 @@ const SkillModel = (sequelize, types) => {
|
|
|
117
117
|
Skill.hasMany(models.DialogNodes)
|
|
118
118
|
Skill.hasMany(models.DialogNodesPreReports)
|
|
119
119
|
Skill.hasMany(models.InactivityTriggers)
|
|
120
|
+
Skill.hasOne(models.Transcription, {
|
|
121
|
+
foreignKey: 'SkillId',
|
|
122
|
+
onDelete: 'CASCADE',
|
|
123
|
+
hooks: true
|
|
124
|
+
});
|
|
120
125
|
}
|
|
121
126
|
|
|
122
127
|
return Skill
|