Skip to content

Commit

Permalink
Merge pull request #331 from n0th1ng-else/sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
n0th1ng-else committed Jun 22, 2023
2 parents e30ac73 + 43a196b commit 57d4a72
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 48 deletions.
3 changes: 1 addition & 2 deletions src/chart/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
33 changes: 17 additions & 16 deletions src/telegram/actions/fund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
50 changes: 20 additions & 30 deletions src/telegram/api/tgapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
TgCore,
TgFile,
TgInlineKeyboardButton,
TgInvoice,
TgLeaveChatSchema,
TgMessage,
TgSetWebHookSchema,
Expand Down Expand Up @@ -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<TgMessage> {
public sendInvoice(opts: TgInvoice): Promise<TgMessage> {
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<TgMessage, InvoiceDto>("sendInvoice", data, chatId);
return this.request<TgMessage, InvoiceDto>(
"sendInvoice",
data,
opts.chatId
);
}

public leaveChat(chatId: number) {
Expand Down
29 changes: 29 additions & 0 deletions src/telegram/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,32 @@ export const TgLeaveChatSchema = z
.describe("Telegram leave chat schema");

export type TgLeaveChatSchema = z.infer<typeof TgLeaveChatSchema>;

const TgPhotoSchema = z
.object({
url: z.string(),
height: z.number(),
width: z.number(),
})
.describe("Telegram photo object schema");

export type TgPhoto = z.infer<typeof TgPhotoSchema>;

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<typeof TgInvoiceSchema>;

0 comments on commit 57d4a72

Please sign in to comment.