|
1 | 1 | const AppController = require("../../controller/AppController") |
2 | | -const DatabaseQuerys = require("../../controller/DatabaseQuerys") |
| 2 | + |
| 3 | +const extractSubstring = require("../../scripts/extractSubstring") |
| 4 | +const deleteMessage = require("../../scripts/deleteMessage") |
3 | 5 |
|
4 | 6 | async function onCallbackQuery(ctx) { |
5 | 7 |
|
6 | | - //In case that the cancel button is pressed |
7 | | - if (ctx.update.callback_query.data.includes("co_")) { |
| 8 | + const userID = ctx.from.id |
8 | 9 |
|
9 | | - //Get dataType |
10 | | - const dataType = extractSubstring(ctx.update.callback_query.data, "dt_", "i_") |
11 | | - |
12 | | - switch (dataType) { |
13 | | - case "text": |
14 | | - //Get text position |
15 | | - const textIndex = parseInt(extractSubstring(ctx.update.callback_query.data, "i_", "")) |
16 | | - //Make it null |
17 | | - ctx.session.textsForAdd[textIndex] = null |
18 | | - break; |
| 10 | + const prefix = ctx.update.callback_query.data.substring(0, 3) |
| 11 | + /** |
| 12 | + * * "db_" = database selection |
| 13 | + * * "pr_" = propierty selection |
| 14 | + * * "vl_" = value selection |
| 15 | + */ |
| 16 | + |
| 17 | + switch (prefix) { |
| 18 | + case "db_": { |
| 19 | + await AppController().t_responses(ctx).respondWithOfPropierties(userID) |
| 20 | + |
| 21 | + //Delete the previous message |
| 22 | + deleteMessage(ctx, ctx.update.callback_query.message.message_id) |
19 | 23 |
|
20 | | - case "image": |
21 | | - //Get image position |
22 | | - const position = parseInt(extractSubstring(ctx.update.callback_query.data, "i_", "")) |
23 | | - //Make it null |
24 | | - ctx.session.imagesForAdd[position] = null |
| 24 | + break; |
| 25 | + } |
25 | 26 |
|
26 | | - default: |
27 | | - break; |
| 27 | + case "pr_": { |
| 28 | + |
| 29 | + //Check if cancel operation button is pressed |
| 30 | + if (extractSubstring(ctx.update.callback_query.data, "pr_", "in_") === "co_") { |
| 31 | + ctx.reply("Operation canceled", {parse_mode: "HTML"}) |
| 32 | + //Delete the previous message |
| 33 | + deleteMessage(ctx, ctx.update.callback_query.message.message_id) |
| 34 | + return |
28 | 35 | } |
29 | | - deleteMessage(ctx, ctx.update.callback_query.message.message_id) |
30 | | - ctx.reply(`Operation canceled 👍`, {parse_mode: "HTML"}) |
31 | | - return |
32 | | - } |
33 | 36 |
|
34 | | - //Get database id |
35 | | - const databaseID = extractSubstring(ctx.update.callback_query.data, "db_", "dt_") |
36 | | - |
37 | | - //Get dataType |
38 | | - const dataType = extractSubstring(ctx.update.callback_query.data, "dt_", "i_") |
| 37 | + //Check if send button is pressed |
| 38 | + if (extractSubstring(ctx.update.callback_query.data, "pr_", "in_") === "sd_") { |
| 39 | + |
| 40 | + //Get data |
| 41 | + const index = extractSubstring(ctx.update.callback_query.data, "in_", false) |
| 42 | + const data = ctx.session.dataForAdd[index] |
| 43 | + |
| 44 | + let response |
| 45 | + |
| 46 | + switch (data.type) { |
| 47 | + case "text": { |
| 48 | + //Get what text want the user add |
| 49 | + const text = data.data.title |
| 50 | + |
| 51 | + response = await AppController().addMessageToNotionDatabase(userID, data.databaseID, data) |
| 52 | + |
| 53 | + if (response.status !== "error") { |
| 54 | + ctx.reply(`<strong>${text.length > 20 ? text + "\n\n</strong>" : text + "</strong> "}added to <strong>${response.databaseTitle}</strong> database 👍`, {parse_mode: "HTML"}) |
| 55 | + } |
| 56 | + |
| 57 | + break; |
| 58 | + } |
| 59 | + |
| 60 | + case "image": |
| 61 | + //Get what image want the user add |
| 62 | + const image = data.data |
| 63 | + image.title = image.title ? image.title : image.file_path |
| 64 | + |
| 65 | + response = await AppController().addImageToDatabase(userID, data.databaseID, `https://api.telegram.org/file/bot${process.env.BOT_TOKEN}/${image.file_path}`, image.title, data.propiertiesValues) |
| 66 | + |
| 67 | + if (response.status !== "error") { |
| 68 | + ctx.reply(`<strong>${image.title.length > 20 ? image.title + "\n\n</strong>" : image.title + "</strong> "}added to <strong>${response.databaseTitle}</strong> database 👍`, {parse_mode: "HTML"}) |
| 69 | + } |
| 70 | + |
| 71 | + break; |
| 72 | + |
| 73 | + default: |
| 74 | + reportError(ctx) |
| 75 | + break; |
| 76 | + } |
| 77 | + |
| 78 | + // Change this data on array for null |
| 79 | + ctx.session.dataForAdd[index] = null |
| 80 | + |
| 81 | + //Report in error case |
| 82 | + if (response.status === "error") { |
| 83 | + deleteMessage(ctx, ctx.update.callback_query.message.message_id) |
| 84 | + reportError(ctx) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + //Delete DB selector |
| 89 | + deleteMessage(ctx, ctx.update.callback_query.message.message_id) |
| 90 | + |
| 91 | + return |
| 92 | + } |
39 | 93 |
|
40 | | - let response |
| 94 | + await AppController().t_responses(ctx).respondWithPropiertyValues() |
41 | 95 |
|
42 | | - switch (dataType) { |
43 | | - case "text": |
44 | | - //Get what text want the user add |
45 | | - const textIndex = parseInt(extractSubstring(ctx.update.callback_query.data, "i_", "")) |
46 | | - const text = ctx.session.textsForAdd[textIndex] |
| 96 | + //Delete the previous message |
| 97 | + deleteMessage(ctx, ctx.update.callback_query.message.message_id) |
47 | 98 |
|
48 | | - response = await AppController().addMessageToNotionDatabase(ctx.from.id, databaseID, text) |
49 | | - |
50 | | - ctx.reply(`<strong>${text.length > 20 ? text + "\n\n</strong>" : text + "</strong> "}added to <strong>${response.databaseTitle}</strong> database 👍`, {parse_mode: "HTML"}) |
51 | | - |
52 | | - // Change this text on array for null |
53 | | - ctx.session.textsForAdd[textIndex] = null |
54 | 99 | break; |
| 100 | + } |
55 | 101 |
|
56 | | - case "image": |
57 | | - //Get what image want the user add |
58 | | - const index = parseInt(extractSubstring(ctx.update.callback_query.data, "i_", "")) |
59 | | - const image = ctx.session.imagesForAdd[index] |
60 | | - const imageTitle = image.title ? image.title : image.file_path |
| 102 | + case "vl_": { |
| 103 | + //Get data index |
| 104 | + const index = parseInt(extractSubstring(ctx.update.callback_query.data, "pi_", "")) |
| 105 | + |
| 106 | + //If done or cancel operation button pressed |
| 107 | + if (extractSubstring(ctx.update.callback_query.data, "vl_", "pi_") === "dn_") { |
61 | 108 |
|
62 | | - response = await AppController().addImageToDatabase(ctx.from.id, databaseID, `https://api.telegram.org/file/bot${process.env.BOT_TOKEN}/${image.file_path}`, imageTitle) |
| 109 | + //Set false in the case that the app was waiting for a value |
| 110 | + ctx.session.waitingForPropiertyValue = false |
63 | 111 |
|
64 | | - ctx.reply(`<strong>${imageTitle.length > 20 ? imageTitle + "\n\n</strong>" : imageTitle + "</strong> "}added to <strong>${response.databaseTitle}</strong> database 👍`, {parse_mode: "HTML"}) |
| 112 | + //Delete the previous message |
| 113 | + await deleteMessage(ctx, ctx.update.callback_query.message.message_id) |
65 | 114 |
|
66 | | - // Change this image on array for null |
67 | | - ctx.session.imagesForAdd[index] = null |
68 | | - break; |
69 | | - |
70 | | - default: |
71 | | - reportError(ctx) |
72 | | - break; |
73 | | - } |
| 115 | + AppController().t_responses(ctx).respondWithOfPropierties(userID, ctx.session.dataForAdd[index].listOfPropiertiesQuery) |
| 116 | + return |
| 117 | + } |
74 | 118 |
|
75 | | - //Report in error case |
76 | | - if (response.status === "error") { |
77 | | - deleteMessage(ctx, ctx.update.callback_query.message.message_id) |
78 | | - reportError(ctx) |
79 | | - return |
80 | | - } |
| 119 | + //Get propierty id |
| 120 | + const propiertyID = extractSubstring(ctx.update.callback_query.data, "pr_", "pi_") |
81 | 121 |
|
82 | | - //Delete DB selector |
83 | | - deleteMessage(ctx, ctx.update.callback_query.message.message_id) |
84 | | -} |
| 122 | + //Get propierty data |
| 123 | + const propierty = Object.values(ctx.session.dataForAdd[index].propierties).find(prop => { |
| 124 | + return prop.id === propiertyID |
| 125 | + }) |
85 | 126 |
|
86 | | -//Extract substring function |
87 | | -function extractSubstring(str, start, end) { |
| 127 | + //Get optionID |
| 128 | + const optionID = extractSubstring(ctx.update.callback_query.data, "vl_", "pr_") |
88 | 129 |
|
89 | | - const position = str.indexOf(start) + start.length; |
| 130 | + //If not exists the propierties values propierty, create it |
| 131 | + if (!ctx.session.dataForAdd[index].propiertiesValues) { |
| 132 | + ctx.session.dataForAdd[index].propiertiesValues = {} |
| 133 | + } |
90 | 134 |
|
91 | | - if (!end) { |
92 | | - return str.substring(position, str.length) |
93 | | - } |
94 | | - return str.substring(position, str.indexOf(end, position)); |
95 | | -} |
| 135 | + const propiertyValue = ctx.session.dataForAdd[index].propiertiesValues[propiertyID] |
| 136 | + |
| 137 | + switch (propierty.type) { |
| 138 | + case "multi_select": { |
| 139 | + //Get data |
| 140 | + const data = propierty.multi_select.options.find(option => { |
| 141 | + return option.id === optionID |
| 142 | + }) |
| 143 | + |
| 144 | + if (propiertyValue) { |
| 145 | + // If the array don't include the propierty id, add it |
| 146 | + if (!Object.keys(ctx.session.dataForAdd[index].propiertiesValues[propiertyID]).includes(data)) { |
| 147 | + ctx.session.dataForAdd[index].propiertiesValues[propiertyID] = [...propiertyValue, data] |
| 148 | + } |
| 149 | + } else { |
| 150 | + ctx.session.dataForAdd[index].propiertiesValues[propiertyID] = [data] |
| 151 | + } |
| 152 | + |
| 153 | + ctx.reply(`<strong>${data.name}</strong> value added`, {parse_mode: "HTML"}) |
| 154 | + |
| 155 | + break; |
| 156 | + } |
| 157 | + |
| 158 | + case "checkbox": { |
| 159 | + // Get the boolean of the optionID |
| 160 | + const p_data = JSON.parse(optionID) |
| 161 | + |
| 162 | + //Add it to the values |
| 163 | + ctx.session.dataForAdd[index].propiertiesValues[propiertyID] = p_data |
| 164 | + |
| 165 | + //Reply |
| 166 | + await ctx.reply(`<strong>${propierty.name}</strong> is <strong>${p_data ? "checked" : "unchecked"}</strong>`, {parse_mode: "HTML"}) |
| 167 | + |
| 168 | + //Delete the checked selector |
| 169 | + await deleteMessage(ctx, ctx.update.callback_query.message.message_id) |
| 170 | + |
| 171 | + //Reply with propierties list |
| 172 | + AppController().t_responses(ctx).respondWithOfPropierties(userID, ctx.session.dataForAdd[index].listOfPropiertiesQuery) |
| 173 | + |
| 174 | + break; |
| 175 | + } |
| 176 | + |
| 177 | + case "select": { |
| 178 | + //Get data |
| 179 | + const data = propierty.select.options.find(option => { |
| 180 | + return option.id === optionID |
| 181 | + }) |
| 182 | + |
| 183 | + ctx.session.dataForAdd[index].propiertiesValues[propiertyID] = data |
| 184 | + |
| 185 | + //Reply |
| 186 | + await ctx.reply(`<strong>${data.name}</strong> value added`, {parse_mode: "HTML"}) |
| 187 | + |
| 188 | + //Delete the checked selector |
| 189 | + await deleteMessage(ctx, ctx.update.callback_query.message.message_id) |
| 190 | + |
| 191 | + //Reply with propierties list |
| 192 | + AppController().t_responses(ctx).respondWithOfPropierties(userID, ctx.session.dataForAdd[index].listOfPropiertiesQuery) |
| 193 | + break; |
| 194 | + |
| 195 | + } |
96 | 196 |
|
97 | | -// Delete message function |
98 | | -async function deleteMessage(ctx, messageId) { |
99 | | - try { |
100 | | - await ctx.api.deleteMessage(ctx.chat.id, messageId) |
101 | | - } catch (error) { |
102 | | - console.log(error) |
| 197 | + default: |
| 198 | + reportError(ctx) |
| 199 | + break; |
| 200 | + } |
| 201 | + |
| 202 | + break; |
| 203 | + } |
| 204 | + |
| 205 | + default: |
| 206 | + reportError(ctx) |
| 207 | + break; |
103 | 208 | } |
104 | 209 | } |
105 | 210 |
|
|
0 commit comments