Skip to content

Commit

Permalink
bot/start: fixing types & import/exports
Browse files Browse the repository at this point in the history
  • Loading branch information
Mersho committed Oct 25, 2023
1 parent 64fdfb0 commit ab9caf2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 20 deletions.
46 changes: 26 additions & 20 deletions bot/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ import { I18n, I18nContext } from '@grammyjs/i18n';
import { Message } from 'typegram'
import { UserDocument } from '../models/user'
import { FilterQuery } from 'mongoose';

const { limit } = require('@grammyjs/ratelimiter');
const schedule = require('node-schedule');
const {
import { limit } from "@grammyjs/ratelimiter"
import schedule from 'node-schedule';
import {
Order,
User,
PendingPayment,
Community,
Dispute,
Config,
} = require('../models');
} from '../models';
const { getCurrenciesWithPrice, deleteOrderFromChannel } = require('../util');
const {
commandArgsMiddleware,
Expand Down Expand Up @@ -56,7 +55,7 @@ const {
validateInvoice,
validateLightningAddress,
} = require('./validations');
const messages = require('./messages');
import * as messages from './messages';
const {
attemptPendingPayments,
cancelOrders,
Expand All @@ -66,7 +65,8 @@ const {
deleteCommunity,
nodeInfo,
} = require('../jobs');
const logger = require('../logger');
import { logger } from '../logger';
import { IUsernameId } from '../models/community';

export interface MainContext extends Context {
match: Array<string> | null;
Expand All @@ -75,7 +75,7 @@ export interface MainContext extends Context {
admin: UserDocument;
}

interface OrderQuery {
export interface OrderQuery {
status?: string;
buyer_id?: string;
seller_id?: string;
Expand Down Expand Up @@ -125,6 +125,7 @@ const askForConfirmation = async (user: UserDocument, command: string) => {
return [];
} catch (error) {
logger.error(error);
return null;
}
};

Expand All @@ -137,7 +138,7 @@ has the same condition.
The problem mentioned above is similar to this issue:
https://github.com/telegraf/telegraf/issues/1319#issuecomment-766360594
*/
const ctxUpdateAssertMsg = "ctx.update.message.text is not available.";
export const ctxUpdateAssertMsg = "ctx.update.message.text is not available.";

const initialize = (botToken: string, options: Partial<Telegraf.Options<MainContext>>): Telegraf<MainContext> => {
const i18n = new I18n({
Expand Down Expand Up @@ -256,11 +257,11 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
throw new Error(ctxUpdateAssertMsg);
}
const params = ctx.update.message.text.split(' ');
const [command, orderId] = params.filter((el) => el);
const [command, orderId] = params.filter((el: string) => el);

if (!orderId) {
const orders = await askForConfirmation(ctx.user, command);
if (!orders.length) return await ctx.reply(`${command} <order Id>`);
if (orders === null || orders.length === 0) return await ctx.reply(`${command} <order Id>`);

return await messages.showConfirmationButtons(ctx, orders, command);
} else if (!(await validateObjectId(ctx, orderId))) {
Expand Down Expand Up @@ -325,9 +326,10 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont

order.status = 'CANCELED_BY_ADMIN';
order.canceled_by = ctx.admin._id;
await order.save();
const buyer = await User.findOne({ _id: order.buyer_id });
const seller = await User.findOne({ _id: order.seller_id });
await order.save();
if (buyer === null || seller === null) throw Error("buyer and/or seller were not found in DB");
// we sent a private message to the admin
await messages.successCancelOrderMessage(ctx, ctx.admin, order, ctx.i18n);
// we sent a private message to the seller
Expand All @@ -347,11 +349,11 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
throw new Error(ctxUpdateAssertMsg);
}
const params = ctx.update.message.text.split(' ');
const [command, orderId] = params.filter((el) => el);
const [command, orderId] = params.filter((el: string) => el);

if (!orderId) {
const orders = await askForConfirmation(ctx.user, command);
if (!orders.length) return await ctx.reply(`${command} <order Id>`);
if (orders === null || orders.length === 0) return await ctx.reply(`${command} <order Id>`);

return await messages.showConfirmationButtons(ctx, orders, command);
} else if (!(await validateObjectId(ctx, orderId))) {
Expand Down Expand Up @@ -424,9 +426,10 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
}

order.status = 'COMPLETED_BY_ADMIN';
await order.save();
const buyer = await User.findOne({ _id: order.buyer_id });
const seller = await User.findOne({ _id: order.seller_id });
await order.save();
if (buyer === null || seller === null) throw Error("buyer and/or seller were not found in DB");
// we sent a private message to the admin
await messages.successCompleteOrderMessage(ctx, order);
// we sent a private message to the seller
Expand Down Expand Up @@ -454,7 +457,7 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont

const buyer = await User.findOne({ _id: order.buyer_id });
const seller = await User.findOne({ _id: order.seller_id });

if (buyer === null || seller === null) throw Error("buyer and/or seller were not found in DB");
await messages.checkOrderMessage(ctx, order, buyer, seller);
} catch (error) {
logger.error(error);
Expand Down Expand Up @@ -523,11 +526,11 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
throw new Error(ctxUpdateAssertMsg);
}
const params = ctx.update.message.text.split(' ');
const [command, orderId] = params.filter((el) => el);
const [command, orderId] = params.filter((el: string) => el);

if (!orderId) {
const orders = await askForConfirmation(ctx.user, command);
if (!orders.length) return await ctx.reply(`${command} <order Id>`);
if (orders === null || orders.length === 0) return await ctx.reply(`${command} <order Id>`);

return await messages.showConfirmationButtons(ctx, orders, command);
} else if (!(await validateObjectId(ctx, orderId))) {
Expand Down Expand Up @@ -565,6 +568,7 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
const community = await Community.findById(
ctx.admin.default_community_id
);
if (community === null) throw Error("Community was not found in DB");
community.banned_users.push({
id: user._id,
username: user.username,
Expand Down Expand Up @@ -608,8 +612,9 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
const community = await Community.findById(
ctx.admin.default_community_id
);
community.banned_users = community.banned_users.filter(
(el: any) => el.id !== user.id
if (community === null) throw Error("Community was not found in DB");
community.banned_users = community.banned_users.toObject().filter(
(el: IUsernameId) => el.id !== user.id
);
await community.save();
} else {
Expand Down Expand Up @@ -821,6 +826,7 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
bot.command('info', userMiddleware, async (ctx: MainContext) => {
try {
const config = await Config.findOne({});
if (config === null) throw Error("Config was not found in DB");
await messages.showInfoMessage(ctx, ctx.user, config);
} catch (error) {
logger.error(error);
Expand Down
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"devDependencies": {
"@types/node": "^20.5.0",
"@types/qrcode": "^1.5.2",
"@types/node-schedule": "^2.1.0",
"chai": "^4.3.4",
"chokidar": "^3.5.3",
"eslint": "^8.15.0",
Expand Down

0 comments on commit ab9caf2

Please sign in to comment.