From 3d227ba0377f90e4615ed64337740582d3d66b50 Mon Sep 17 00:00:00 2001 From: Sergey Nikitin Date: Thu, 22 Jun 2023 22:16:46 +0200 Subject: [PATCH 1/2] fix(sonar): Use chain operator --- src/chart/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/chart/index.js b/src/chart/index.js index ca878a73..9a8a2f06 100644 --- a/src/chart/index.js +++ b/src/chart/index.js @@ -401,8 +401,7 @@ const renderInstallsCumulativeChart = (drawer, rows) => { if (date === currentDate) { res[currentDayIndex].count++; } else { - const lastDayCount = - (res[currentDayIndex] && res[currentDayIndex].count) || 1; + const lastDayCount = res?.[currentDayIndex]?.count ?? 1; currentDayIndex++; currentDate = date; From 43a196b0be2474e3709558dc7053c3ed42174dd4 Mon Sep 17 00:00:00 2001 From: Sergey Nikitin Date: Thu, 22 Jun 2023 22:24:47 +0200 Subject: [PATCH 2/2] fix(sonar): Wrap input into object --- src/telegram/actions/fund.ts | 33 ++++++++++++------------ src/telegram/api/tgapi.ts | 50 +++++++++++++++--------------------- src/telegram/api/types.ts | 29 +++++++++++++++++++++ 3 files changed, 66 insertions(+), 46 deletions(-) diff --git a/src/telegram/actions/fund.ts b/src/telegram/actions/fund.ts index 40fb0673..9c5ed909 100644 --- a/src/telegram/actions/fund.ts +++ b/src/telegram/actions/fund.ts @@ -199,24 +199,25 @@ export class FundAction extends GenericAction { const title = this.text.t(LabelId.DonationTitle, lang); const description = this.text.t(LabelId.DonationDescription, lang); const label = this.text.t(LabelId.DonationLabel, lang); - const photo = { - url: BOT_LOGO, - height: 1024, - width: 1024, + + const invoice = { + chatId, + amount: amount * 100, + meta: String(donationId), + token, + title, + description, + label, + payload: getDonationDtoString(donationId, chatId, prefix.id), + photo: { + url: BOT_LOGO, + height: 1024, + width: 1024, + }, + forumThreadId, }; return this.bot - .sendInvoice( - chatId, - amount * 100, - String(donationId), - token, - title, - description, - label, - getDonationDtoString(donationId, chatId, prefix.id), - photo, - forumThreadId - ) + .sendInvoice(invoice) .then(() => logger.info(`${prefix.getPrefix()} Invoice sent`)) .catch((err) => { logger.error(`${prefix.getPrefix()} Unable to send the invoice`, err); diff --git a/src/telegram/api/tgapi.ts b/src/telegram/api/tgapi.ts index b75ff2c5..b05485a6 100644 --- a/src/telegram/api/tgapi.ts +++ b/src/telegram/api/tgapi.ts @@ -12,6 +12,7 @@ import { TgCore, TgFile, TgInlineKeyboardButton, + TgInvoice, TgLeaveChatSchema, TgMessage, TgSetWebHookSchema, @@ -156,46 +157,35 @@ export class TelegramApi { } // TODO add tests - public sendInvoice( - chatId: number, - amount: number, - meta: string, - token: string, - title: string, - description: string, - label: string, - payload: string, - photo: { - url: string; - height: number; - width: number; - }, - forumThreadId?: number - ): Promise { + public sendInvoice(opts: TgInvoice): Promise { const data: InvoiceDto = { - chat_id: chatId, + chat_id: opts.chatId, currency: "EUR", - title, - description, - payload: payload, + title: opts.title, + description: opts.description, + payload: opts.payload, prices: [ { - label: label, - amount: amount, + label: opts.label, + amount: opts.amount, }, ], - provider_token: token, - start_parameter: meta, - photo_url: photo.url, - photo_width: photo.width, - photo_height: photo.height, + provider_token: opts.token, + start_parameter: opts.meta, + photo_url: opts.photo.url, + photo_width: opts.photo.width, + photo_height: opts.photo.height, }; - if (forumThreadId) { - data.message_thread_id = forumThreadId; + if (opts.forumThreadId) { + data.message_thread_id = opts.forumThreadId; } - return this.request("sendInvoice", data, chatId); + return this.request( + "sendInvoice", + data, + opts.chatId + ); } public leaveChat(chatId: number) { diff --git a/src/telegram/api/types.ts b/src/telegram/api/types.ts index be854aa1..8205afe6 100644 --- a/src/telegram/api/types.ts +++ b/src/telegram/api/types.ts @@ -286,3 +286,32 @@ export const TgLeaveChatSchema = z .describe("Telegram leave chat schema"); export type TgLeaveChatSchema = z.infer; + +const TgPhotoSchema = z + .object({ + url: z.string(), + height: z.number(), + width: z.number(), + }) + .describe("Telegram photo object schema"); + +export type TgPhoto = z.infer; + +const TgInvoiceSchema = z + .intersection( + LabeledPriceSchema, + z.object({ + chatId: z.number(), + meta: z.string(), + token: z.string(), + title: z.string(), + description: z.string(), + payload: z.string(), + photo: TgPhotoSchema, + forumThreadId: z.optional(z.number()), + }) + ) + + .describe("Telegram invoice schema"); + +export type TgInvoice = z.infer;