Skip to content

Commit 8edb00b

Browse files
committed
refactor: Extract cooperative cancellation flag setting into a new function and update the cancelOrder command to utilize it.
1 parent ea490df commit 8edb00b

1 file changed

Lines changed: 27 additions & 21 deletions

File tree

bot/commands.ts

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ import { UserDocument } from '../models/user';
2525
import { HasTelegram, MainContext } from './start';
2626
import { CommunityContext } from './modules/community/communityContext';
2727

28+
const setCooperativeCancelFlag = async (
29+
orderId: string,
30+
role: 'buyer' | 'seller',
31+
): Promise<IOrder | null> => {
32+
let propName = '';
33+
34+
if (role === 'buyer') {
35+
propName = 'buyer_cooperativecancel';
36+
} else if (role === 'seller') {
37+
propName = 'seller_cooperativecancel';
38+
}
39+
40+
const update = { [propName]: true };
41+
return await Order.findOneAndUpdate({ _id: orderId }, update, { new: true });
42+
};
43+
2844
const waitPayment = async (
2945
ctx: MainContext,
3046
bot: HasTelegram,
@@ -687,7 +703,7 @@ const cancelOrder = async (
687703
return await messages.badStatusOnCancelOrderMessage(ctx);
688704

689705
// If the order is active we start a cooperative cancellation
690-
let counterPartyUser, initiator, counterParty;
706+
let counterPartyUser, initiator: 'buyer' | 'seller', counterParty;
691707

692708
const initiatorUser = user;
693709
if (initiatorUser._id == order.buyer_id) {
@@ -702,30 +718,21 @@ const cancelOrder = async (
702718
if (counterPartyUser == null)
703719
throw new Error('counterPartyUser was not found');
704720

705-
const initiatorCooperativeCancelProperty =
706-
initiator == 'seller'
707-
? 'seller_cooperativecancel'
708-
: 'buyer_cooperativecancel';
709-
710-
if (order[initiatorCooperativeCancelProperty])
711-
return await messages.shouldWaitCooperativeCancelMessage(
712-
ctx,
713-
initiatorUser,
714-
);
715-
716-
order[initiatorCooperativeCancelProperty] = true;
721+
const updateOrder = await setCooperativeCancelFlag(order._id, initiator);
722+
if (!updateOrder) return;
717723

718724
const i18nCtxCP = await getUserI18nContext(counterPartyUser);
719725
// If the counter party already requested a cooperative cancel order
720726
if (
721727
counterParty == 'seller'
722-
? order.seller_cooperativecancel
723-
: order.buyer_cooperativecancel
728+
? updateOrder.seller_cooperativecancel
729+
: updateOrder.buyer_cooperativecancel
724730
) {
725731
// If we already have a holdInvoice we cancel it and return the money
726732
if (order.hash) await cancelHoldInvoice({ hash: order.hash });
727733

728-
order.status = 'CANCELED';
734+
updateOrder.status = 'CANCELED';
735+
await updateOrder.save();
729736
let seller = initiatorUser;
730737
let i18nCtxSeller = ctx.i18n;
731738
if (order.seller_id == counterPartyUser._id) {
@@ -736,29 +743,28 @@ const cancelOrder = async (
736743
await messages.successCancelOrderMessage(
737744
ctx,
738745
initiatorUser,
739-
order,
746+
updateOrder,
740747
ctx.i18n,
741748
);
742749
await messages.okCooperativeCancelMessage(
743750
ctx,
744751
counterPartyUser,
745-
order,
752+
updateOrder,
746753
i18nCtxCP,
747754
);
748755
await messages.refundCooperativeCancelMessage(ctx, seller, i18nCtxSeller);
749-
logger.info(`Order ${order._id} was cancelled cooperatively!`);
756+
logger.info(`Order ${updateOrder._id} was cancelled cooperatively!`);
750757
logger.info('cancelOrder => OrderEvents.orderUpdated(order);');
751758
OrderEvents.orderUpdated(order);
752759
} else {
753760
await messages.initCooperativeCancelMessage(ctx, order);
754761
await messages.counterPartyWantsCooperativeCancelMessage(
755762
ctx,
756763
counterPartyUser,
757-
order,
764+
updateOrder,
758765
i18nCtxCP,
759766
);
760767
}
761-
await order.save();
762768
} catch (error) {
763769
logger.error(error);
764770
}

0 commit comments

Comments
 (0)