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
67 changes: 67 additions & 0 deletions bot/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ const showHoldInvoice = async (
order.fiat_amount,
order.random_image,
order.is_golden_honey_badger,
order._id,
);
} catch (error) {
logger.error(`Error in showHoldInvoice: ${error}`);
Expand Down Expand Up @@ -582,6 +583,71 @@ const cancelShowHoldInvoice = async (
}
};

const markAsPending = async (ctx: CommunityContext, orderId: string) => {
try {
ctx.deleteMessage();
ctx.scene.leave();

const order = await Order.findOne({ _id: orderId });
if (!order) return;

const user = await User.findOne({ _id: order.seller_id });
if (!user) return;

const i18nCtx = await getUserI18nContext(user);

// Verify that the user clicking the button is the seller
if (order.seller_id?.toString() !== ctx.from?.id.toString()) {
return await messages.genericErrorMessage(ctx, user, i18nCtx);
}

// Only allow marking as pending for orders in WAITING_PAYMENT status
if (order.status !== 'WAITING_PAYMENT') {
return await messages.genericErrorMessage(ctx, user, i18nCtx);
}

// Cancel the hold invoice
if (order.hash) {
await cancelHoldInvoice({ hash: order.hash });
}

const buyerUser = await User.findOne({ _id: order.buyer_id });
if (!buyerUser) throw new Error('buyerUser was not found');

logger.info(
`Seller Id ${user.id} marked Order Id: ${order._id} as pending, republishing to the channel`,
);

// Reset order to PENDING status
order.taken_at = null;
order.status = 'PENDING';

if (!!order.min_amount && !!order.max_amount) {
order.fiat_amount = undefined;
}

if (order.price_from_api) {
order.amount = 0;
order.fee = 0;
order.hash = null;
order.secret = null;
}

if (order.type === 'buy') {
order.seller_id = null;
await messages.publishBuyOrderMessage(ctx, buyerUser, order, i18nCtx);
} else {
order.buyer_id = null;
await messages.publishSellOrderMessage(ctx, user, order, i18nCtx);
}

await order.save();
await messages.successCancelOrderMessage(ctx, user, order, i18nCtx);
} catch (error) {
logger.error(error);
}
};

/**
*
* This triggers a scene asking for a new invoice after a payment to a buyer failed
Expand Down Expand Up @@ -878,6 +944,7 @@ export {
waitPayment,
addInvoice,
cancelShowHoldInvoice,
markAsPending,
showHoldInvoice,
addInvoicePHI,
cancelOrder,
Expand Down
20 changes: 20 additions & 0 deletions bot/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ const showHoldInvoiceMessage = async (
fiatAmount: IOrder['fiat_amount'],
randomImage: string,
isGoldenHoneyBadger: boolean = false,
orderId?: string,
) => {
try {
const currency = getCurrency(fiatCode);
Expand Down Expand Up @@ -509,6 +510,25 @@ const showHoldInvoiceMessage = async (
parse_mode: 'MarkdownV2',
},
]);

if (orderId) {
await ctx.telegram.sendMessage(ctx.from!.id, orderId, {
reply_markup: {
inline_keyboard: [
[
{
text: ctx.i18n.t('mark_as_pending'),
callback_data: `markPendingBtn_${orderId}`,
},
{
text: ctx.i18n.t('cancel'),
callback_data: `cancelShowHoldInvoiceBtn`,
},
],
],
},
});
}
} catch (error) {
logger.error(error);
if (error instanceof ImageProcessingError) {
Expand Down
12 changes: 12 additions & 0 deletions bot/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
cancelAddInvoice,
addInvoice,
cancelShowHoldInvoice,
markAsPending,
showHoldInvoice,
addInvoicePHI,
cancelOrder,
Expand Down Expand Up @@ -847,6 +848,17 @@ const initialize = (
},
);

bot.action(
/^markPendingBtn_([0-9a-f]{24})$/,
userMiddleware,
async (ctx: CommunityContext) => {
if (ctx.match === null) {
throw new Error('ctx.match should not be null');
}
await markAsPending(ctx, ctx.match[1]);
},
);

bot.action(
/^showStarBtn\(([1-5]),(\w{24})\)$/,
userMiddleware,
Expand Down
3 changes: 2 additions & 1 deletion locales/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -668,4 +668,5 @@ user_order_is_blocked_by_user_taker: You can't take this order because you block
user_taker_is_blocked_by_user_order: You can't take this order because its maker blocked you
check_solvers: Your community ${communityName} does not have any solvers. Please add at least one within ${remainingDays} days to prevent the community from being disabled.
check_solvers_last_warning: Your community ${communityName} does not have any solvers. Please add at least one today to prevent the community from being disabled.
image_processing_error: We had an error processing the image, please wait few minutes and try again
image_processing_error: We had an error processing the image, please wait few minutes and try again
mark_as_pending: Mark as pending
3 changes: 2 additions & 1 deletion locales/es.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -665,4 +665,5 @@ user_order_is_blocked_by_user_taker: No puedes aceptar esta oferta porque has bl
user_taker_is_blocked_by_user_order: No puedes aceptar esta oferta porque su creador te ha bloqueado
image_processing_error: Hemos tenido un error procesando la imagen, por favor espera unos minutos y vuelve a intentarlo.
check_solvers: Tu comunidad ${communityName} no tiene ningún solucionador. Agregue al menos uno dentro de ${remainingDays} días para evitar que se deshabilite la comunidad.
check_solvers_last_warning: Tu comunidad ${communityName} no tiene ningún solucionador. Agregue al menos uno hoy para evitar que la comunidad quede inhabilitada.
check_solvers_last_warning: Tu comunidad ${communityName} no tiene ningún solucionador. Agregue al menos uno hoy para evitar que la comunidad quede inhabilitada.
mark_as_pending: Marcar como pendiente
Loading