Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions bot/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ const cancelOrder = async (
if (order.hash) await cancelHoldInvoice({ hash: order.hash });

order.status = 'CANCELED';
order.canceled_by = user._id;
order.canceled_by = user._id.toString();
await order.save();
OrderEvents.orderUpdated(order);
// we sent a private message to the user
Expand Down Expand Up @@ -789,7 +789,10 @@ const cancelOrder = async (
if (counterPartyUser == null)
throw new Error('counterPartyUser was not found');

const updateOrder = await setCooperativeCancelFlag(order._id, initiator);
const updateOrder = await setCooperativeCancelFlag(
order._id.toString(),
initiator,
);

// If the call returns null, the flag was already set (or order is missing),
// so we treat it as a duplicate request.
Expand All @@ -811,12 +814,12 @@ const cancelOrder = async (
if (updateOrder.hash) await cancelHoldInvoice({ hash: updateOrder.hash });

updateOrder.status = 'CANCELED';
updateOrder.canceled_by = String(user._id);
updateOrder.canceled_by = user._id.toString();
await updateOrder.save();

let seller = initiatorUser;
let i18nCtxSeller = ctx.i18n;
if (order.seller_id == counterPartyUser._id) {
if (order.seller_id === counterPartyUser._id.toString()) {
seller = counterPartyUser;
i18nCtxSeller = i18nCtxCP;
}
Expand Down Expand Up @@ -940,7 +943,9 @@ const release = async (
// actually completed. Otherwise a failed settle would leave a stale
// RELEASED dispute that makes solvers believe the seller already
// released (see bot/modules/dispute/actions.ts).
const dispute = await Dispute.findOne({ order_id: currentOrder._id });
const dispute = await Dispute.findOne({
order_id: currentOrder._id.toString(),
});
if (dispute) {
dispute.status = 'RELEASED';
await dispute.save();
Expand Down
8 changes: 4 additions & 4 deletions bot/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const invoicePaymentRequestMessage = async (
parse_mode: 'Markdown',
});

await ctx.telegram.sendMessage(user.tg_id, order._id, {
await ctx.telegram.sendMessage(user.tg_id, order._id.toString(), {
reply_markup: {
inline_keyboard: [
[
Expand Down Expand Up @@ -443,7 +443,7 @@ const beginTakeBuyMessage = async (
},
]);

await bot.telegram.sendMessage(seller.tg_id, order._id, {
await bot.telegram.sendMessage(seller.tg_id, order._id.toString(), {
reply_markup: {
inline_keyboard: [
[
Expand Down Expand Up @@ -543,7 +543,7 @@ const onGoingTakeBuyMessage = async (
days: ageInDays,
}),
);
await bot.telegram.sendMessage(buyer.tg_id, order._id, {
await bot.telegram.sendMessage(buyer.tg_id, order._id.toString(), {
reply_markup: {
inline_keyboard: [
[{ text: i18nBuyer.t('continue'), callback_data: 'addInvoiceBtn' }],
Expand Down Expand Up @@ -575,7 +575,7 @@ const beginTakeSellMessage = async (
ctx.i18n.t('you_took_someone_order', { expirationTime }),
{ parse_mode: 'MarkdownV2' },
);
await bot.telegram.sendMessage(buyer.tg_id, order._id, {
await bot.telegram.sendMessage(buyer.tg_id, order._id.toString(), {
reply_markup: {
inline_keyboard: [
[
Expand Down
6 changes: 3 additions & 3 deletions bot/modules/block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { Telegraf } from 'telegraf';
import { CommunityContext } from '../community/communityContext';
import { logger } from '../../../logger';

const commands = require('./commands');
const messages = require('./messages');
const { userMiddleware } = require('../../middleware/user');
import * as commands from './commands';
import * as messages from './messages';
import { userMiddleware } from '../../middleware/user';

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ToRyVand is this related to this PR? looks like an unrelated improvement

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The require()import conversion was needed for TypeScript to transitively type-check commands.ts and messages.ts within the block module — with require(), tsc skips those files and misses type errors introduced by the mongoose 9 stricter types. It's a direct consequence of enabling stricter compilation for mongoose 9 compatibility.


export const configure = (bot: Telegraf<CommunityContext>) => {
bot.command('block', userMiddleware, async ctx => {
Expand Down
2 changes: 1 addition & 1 deletion bot/modules/community/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export const withdrawEarnings = async (ctx: CommunityContext) => {
// changeVisibility, updateCommunity).
const community = await Community.findOne({
_id: ctx.match?.[1],
creator_id: ctx.user._id,
creator_id: ctx.user._id.toString(),
enabled: { $ne: false },
});
if (!community) return ctx.reply(ctx.i18n.t('community_not_found'));
Expand Down
12 changes: 6 additions & 6 deletions bot/modules/community/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { CommunityContext } from './communityContext';
import { Telegraf } from 'telegraf';
import { getUserI18nContext } from '../../../util';

async function getOrderCountByCommunity(): Promise<number[]> {
async function getOrderCountByCommunity(): Promise<Record<string, number>> {
const data = await Order.aggregate([
{ $group: { _id: '$community_id', total: { $count: {} } } },
]);
Expand Down Expand Up @@ -65,7 +65,7 @@ export const setComm = async (ctx: MainContext) => {
return await ctx.reply(ctx.i18n.t('community_not_found'));
}

user.default_community_id = community._id;
user.default_community_id = community._id.toString();
await user.save();

await ctx.reply(ctx.i18n.t('operation_successful'));
Expand Down Expand Up @@ -102,7 +102,7 @@ export const myComms = async (ctx: MainContext) => {
const { user } = ctx;

const communities = await Community.find({
creator_id: user._id,
creator_id: user._id.toString(),
enabled: { $ne: false },
});

Expand Down Expand Up @@ -161,7 +161,7 @@ export const updateCommunity = async (
if (!(await validateObjectId(ctx, id))) return;
const community = await Community.findOne({
_id: id,
creator_id: user._id,
creator_id: user._id.toString(),
enabled: { $ne: false },
});

Expand Down Expand Up @@ -243,7 +243,7 @@ export const deleteCommunity = async (ctx: CommunityContext) => {
if (!(await validateObjectId(ctx, id))) return;
const community = await Community.findOne({
_id: id,
creator_id: ctx.user._id,
creator_id: ctx.user._id.toString(),
enabled: { $ne: false },
});

Expand Down Expand Up @@ -366,7 +366,7 @@ export const changeVisibility = async (ctx: CommunityContext) => {
if (!(await validateObjectId(ctx, id))) return;
const community = await Community.findOne({
_id: id,
creator_id: ctx.user._id,
creator_id: ctx.user._id.toString(),
enabled: { $ne: false },
});

Expand Down
2 changes: 1 addition & 1 deletion bot/modules/community/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export const earningsMessage = async (ctx: MainContext) => {
// We check if there is a payment scheduled for this community
const isScheduled = await PendingPayment.findOne({
community_id: communityId,
attempts: { $lt: process.env.PAYMENT_ATTEMPTS },
attempts: { $lt: Number(process.env.PAYMENT_ATTEMPTS) },
paid: false,
});
if (isScheduled)
Expand Down
12 changes: 6 additions & 6 deletions bot/modules/community/scenes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,9 @@ const createCommunitySteps = {
const user = await User.findOne({ username });
if (user) {
solvers.push({
id: user._id,
id: user._id.toString(),
username: user.username,
} as IUsernameId);
} as unknown as IUsernameId);
}
}
} else {
Expand Down Expand Up @@ -745,9 +745,9 @@ export const updateSolversCommunityWizard = new Scenes.WizardScene(
if (user == null) throw new Error('user not found');
if (user) {
solvers.push({
id: user._id,
id: user._id.toString(),
username: user.username,
} as IUsernameId);
} as unknown as IUsernameId);
botUsers.push(username);
} else {
notBotUsers.push(username);
Expand Down Expand Up @@ -1010,8 +1010,8 @@ export const addEarningsInvoiceWizard = new Scenes.WizardScene(
return await ctx.reply(ctx.i18n.t('invoice_with_incorrect_amount'));

const isScheduled = await PendingPayment.findOne({
community_id: community._id,
attempts: { $lt: process.env.PAYMENT_ATTEMPTS },
community_id: community._id.toString(),
attempts: { $lt: Number(process.env.PAYMENT_ATTEMPTS) },
paid: false,
is_invoice_expired: false,
});
Expand Down
10 changes: 8 additions & 2 deletions bot/modules/dispute/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,16 @@ export const takeDispute = async (ctx: MainContext): Promise<void> => {
if (seller === null) throw new Error('seller not found');
const initiator = order.buyer_dispute ? 'buyer' : 'seller';
const buyerDisputes = await Dispute.countDocuments({
$or: [{ buyer_id: buyer._id }, { seller_id: buyer._id }],
$or: [
{ buyer_id: buyer._id.toString() },
{ seller_id: buyer._id.toString() },
],
});
const sellerDisputes = await Dispute.countDocuments({
$or: [{ buyer_id: seller._id }, { seller_id: seller._id }],
$or: [
{ buyer_id: seller._id.toString() },
{ seller_id: seller._id.toString() },
],
});

dispute.solver_id = solver.id;
Expand Down
18 changes: 12 additions & 6 deletions bot/modules/dispute/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const handleDispute = async (ctx: MainContext, orderId: string) => {
const seller = await User.findOne({ _id: order.seller_id });
if (seller === null) throw new Error('seller was not found');
let initiator: 'seller' | 'buyer' = 'seller';
if (user._id == order.buyer_id) initiator = 'buyer';
if (user._id.toString() == order.buyer_id) initiator = 'buyer';

order.previous_dispute_status = order.status;
if (initiator === 'seller') order.seller_dispute = true;
Expand All @@ -53,11 +53,17 @@ export const handleDispute = async (ctx: MainContext, orderId: string) => {
// If a user disputes is equal to MAX_DISPUTES, we ban the user
const buyerDisputes =
(await Dispute.countDocuments({
$or: [{ buyer_id: buyer._id }, { seller_id: buyer._id }],
$or: [
{ buyer_id: buyer._id.toString() },
{ seller_id: buyer._id.toString() },
],
})) + 1;
const sellerDisputes =
(await Dispute.countDocuments({
$or: [{ buyer_id: seller._id }, { seller_id: seller._id }],
$or: [
{ buyer_id: seller._id.toString() },
{ seller_id: seller._id.toString() },
],
})) + 1;
const maxDisputes = Number(process.env.MAX_DISPUTES);
// if MAX_DISPUTES is not specified or can't be parsed as number, following
Expand Down Expand Up @@ -164,13 +170,13 @@ const deleteDispute = async (ctx: MainContext) => {

// We check if this dispute is from a community we validate that
// the solver is running this command
if (dispute && dispute.solver_id != admin._id) {
if (dispute && dispute.solver_id != admin._id.toString()) {
return await globalMessages.notAuthorized(ctx);
}
}

if (user._id == dispute.buyer_id) dispute.buyer_id = null;
if (user._id == dispute.seller_id) dispute.seller_id = null;
if (user._id.toString() == dispute.buyer_id) dispute.buyer_id = null;
if (user._id.toString() == dispute.seller_id) dispute.seller_id = null;
await dispute.save();

await ctx.reply(ctx.i18n.t('operation_successful'));
Expand Down
2 changes: 1 addition & 1 deletion bot/modules/orders/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ async function enterWizard(

const isMaxPending = async (user: UserDocument) => {
const pendingOrders = await Order.countDocuments({
creator_id: user._id,
creator_id: user._id.toString(),
status: 'PENDING',
});
const maxPendingOrders = process.env.MAX_PENDING_ORDERS;
Expand Down
4 changes: 2 additions & 2 deletions bot/modules/orders/takeOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const takebuy = async (
const { randomImage } = generateRandomImage(user._id.toString());

order.status = 'WAITING_PAYMENT';
order.seller_id = user._id;
order.seller_id = user._id.toString();
order.taken_at = new Date(Date.now());

order.random_image = randomImage;
Expand Down Expand Up @@ -160,7 +160,7 @@ export const takesell = async (
if (!(await meetsCounterpartyRequirements(ctx, user, seller))) return;

order.status = 'WAITING_BUYER_INVOICE';
order.buyer_id = user._id;
order.buyer_id = user._id.toString();
order.taken_at = new Date(Date.now());

// Reserve a take slot (enforces the cap and increments the counter under a
Expand Down
12 changes: 9 additions & 3 deletions bot/ordersActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,10 @@ const getOrder = async (

const where = {
_id: orderId,
$or: [{ seller_id: user._id }, { buyer_id: user._id }],
$or: [
{ seller_id: user._id.toString() },
{ buyer_id: user._id.toString() },
],
};

const order = await Order.findOne(where).exec();
Expand All @@ -349,7 +352,10 @@ const getOrders = async (user: UserDocument, status?: string) => {
const where: any = {
$and: [
{
$or: [{ buyer_id: user._id }, { seller_id: user._id }],
$or: [
{ buyer_id: user._id.toString() },
{ seller_id: user._id.toString() },
],
},
],
};
Expand Down Expand Up @@ -396,7 +402,7 @@ const getNewRangeOrderPayload = async (order: IOrder) => {
paymentMethod: order.payment_method,
status: 'PENDING',
priceMargin: order.price_margin,
range_parent_id: order._id,
range_parent_id: order._id.toString(),
tgChatId: order.tg_chat_id,
tgOrderMessage: order.tg_order_message,
community_id: order.community_id,
Expand Down
4 changes: 2 additions & 2 deletions bot/scenes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ const addInvoicePHIWizard = new Scenes.WizardScene(
}

const isScheduled = await PendingPayment.findOne({
order_id: order._id,
attempts: { $lt: process.env.PAYMENT_ATTEMPTS },
order_id: order._id.toString(),
attempts: { $lt: Number(process.env.PAYMENT_ATTEMPTS) },
is_invoice_expired: false,
});
// Block update if payment is already in-flight (confirmed is handled above)
Expand Down
Loading
Loading