Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes to follow orders NIP proposal #584

Merged
merged 4 commits into from
Oct 18, 2024
Merged
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
14 changes: 5 additions & 9 deletions bot/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ const waitPayment = async (ctx, bot, buyer, seller, order, buyerInvoice) => {
);
await messages.takeSellWaitingSellerToPayMessage(ctx, bot, buyer, order);
}
await order.save();
// We update the nostr event
OrderEvents.orderUpdated(order);
await order.save();
} catch (error) {
logger.error(`Error in waitPayment: ${error}`);
}
Expand Down Expand Up @@ -333,8 +331,7 @@ const cancelAddInvoice = async (ctx, order, job) => {
}
} else {
await messages.successCancelOrderMessage(ctx, user, order, i18nCtx);
}
OrderEvents.orderUpdated(order);
}
}
} catch (error) {
logger.error(error);
Expand Down Expand Up @@ -507,8 +504,7 @@ const cancelShowHoldInvoice = async (ctx, order, job) => {
}
} else {
await messages.successCancelOrderMessage(ctx, user, order, i18nCtx);
}
OrderEvents.orderUpdated(order);
}
}
} catch (error) {
logger.error(error);
Expand Down Expand Up @@ -648,6 +644,8 @@ const cancelOrder = async (ctx, orderId, user) => {
);
await messages.refundCooperativeCancelMessage(ctx, seller, i18nCtxSeller);
logger.info(`Order ${order._id} was cancelled cooperatively!`);
logger.info('cancelOrder => OrderEvents.orderUpdated(order);');
OrderEvents.orderUpdated(order);
} else {
await messages.initCooperativeCancelMessage(ctx, order);
await messages.counterPartyWantsCooperativeCancelMessage(
Expand All @@ -658,7 +656,6 @@ const cancelOrder = async (ctx, orderId, user) => {
);
}
await order.save();
OrderEvents.orderUpdated(order);
} catch (error) {
logger.error(error);
}
Expand All @@ -682,7 +679,6 @@ const fiatSent = async (ctx, orderId, user) => {
order.status = 'FIAT_SENT';
const seller = await User.findOne({ _id: order.seller_id });
await order.save();
OrderEvents.orderUpdated(order);
// We sent messages to both parties
// We need to create i18n context for each user
const i18nCtxBuyer = await getUserI18nContext(user);
Expand Down
4 changes: 1 addition & 3 deletions bot/modules/dispute/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const {
const messages = require('./messages');
const globalMessages = require('../../messages');
const { logger } = require('../../../logger');
const OrderEvents = require('../../modules/events/orders');
const { removeAtSymbol } = require('../../../util');

const dispute = async ctx => {
Expand Down Expand Up @@ -35,14 +34,13 @@ const dispute = async ctx => {
if (user._id == order.buyer_id) initiator = 'buyer';

order[`${initiator}_dispute`] = true;
order.previous_dispute_status = order.status
order.previous_dispute_status = order.status;
order.status = 'DISPUTE';
const sellerToken = Math.floor(Math.random() * 899 + 100);
const buyerToken = Math.floor(Math.random() * 899 + 100);
order.buyer_dispute_token = buyerToken;
order.seller_dispute_token = sellerToken;
await order.save();
OrderEvents.orderUpdated(order);

// If this is a non community order, we may ban the user globally
if (order.community_id) {
Expand Down
6 changes: 4 additions & 2 deletions bot/modules/orders/takeOrder.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ exports.takebuy = async (ctx, bot, orderId) => {
// if the user don't do anything
order.status = 'WAITING_PAYMENT';
order.seller_id = user._id;
order.taken_at = Date.now();
order.taken_at = new Date(Date.now());
await order.save();
order.status = 'in-progress';
OrderEvents.orderUpdated(order);
// We delete the messages related to that order from the channel
await deleteOrderFromChannel(order, bot.telegram);
Expand All @@ -79,9 +80,10 @@ exports.takesell = async (ctx, bot, orderId) => {
if (!(await validateTakeSellOrder(ctx, bot, user, order))) return;
order.status = 'WAITING_BUYER_INVOICE';
order.buyer_id = user._id;
order.taken_at = Date.now();
order.taken_at = new Date(Date.now());

await order.save();
order.status = 'in-progress';
OrderEvents.orderUpdated(order);
// We delete the messages related to that order from the channel
await deleteOrderFromChannel(order, bot.telegram);
Expand Down
3 changes: 0 additions & 3 deletions bot/scenes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const messages = require('./messages');
const { isPendingPayment } = require('../ln');
const { logger } = require('../logger');
const { resolvLightningAddress } = require('../lnurl/lnurl-pay');
const OrderEvents = require('./modules/events/orders');

const addInvoiceWizard = new Scenes.WizardScene(
'ADD_INVOICE_WIZARD_SCENE_ID',
Expand All @@ -23,10 +22,8 @@ const addInvoiceWizard = new Scenes.WizardScene(
order.fiat_code,
expirationTime
);

order.status = 'WAITING_BUYER_INVOICE';
await order.save();
OrderEvents.orderUpdated(order);

return ctx.wizard.next();
} catch (error) {
Expand Down
3 changes: 1 addition & 2 deletions bot/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,6 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
order.status = 'FROZEN';
order.action_by = ctx.admin._id;
await order.save();
OrderEvents.orderUpdated(order);

if (order.secret) await settleHoldInvoice({ secret: order.secret });

Expand Down Expand Up @@ -370,6 +369,7 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
order.status = 'CANCELED_BY_ADMIN';
order.canceled_by = ctx.admin._id;
await order.save();
order.status = 'CANCELED';
OrderEvents.orderUpdated(order);
const buyer = await User.findOne({ _id: order.buyer_id });
const seller = await User.findOne({ _id: order.seller_id });
Expand Down Expand Up @@ -502,7 +502,6 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont

order.status = 'COMPLETED_BY_ADMIN';
await order.save();
OrderEvents.orderUpdated(order);
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");
Expand Down
2 changes: 1 addition & 1 deletion ln/resubscribe_invoices.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const resubscribeInvoices = async bot => {
}
logger.info(`Invoices resubscribed: ${invoicesReSubscribed}`);
} catch (error) {
logger.error(`ResubcribeInvoice catch: ${error.toString()}`);
logger.error(`ResubscribeInvoice catch: ${error.toString()}`);
return false;
}
};
Expand Down
3 changes: 0 additions & 3 deletions ln/subscribe_invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const messages = require('../bot/messages');
const ordersActions = require('../bot/ordersActions');
const { getUserI18nContext, getEmojiRate, decimalRound } = require('../util');
const { logger } = require('../logger');
const OrderEvents = require('../bot/modules/events/orders');

const subscribeInvoice = async (bot, id, resub) => {
try {
Expand Down Expand Up @@ -50,7 +49,6 @@ const subscribeInvoice = async (bot, id, resub) => {
}
order.invoice_held_at = Date.now();
order.save();
OrderEvents.orderUpdated(order);
}
if (invoice.is_confirmed) {
const order = await Order.findOne({ hash: id });
Expand All @@ -76,7 +74,6 @@ const payHoldInvoice = async (bot, order) => {
try {
order.status = 'PAID_HOLD_INVOICE';
await order.save();
OrderEvents.orderUpdated(order);
const buyerUser = await User.findOne({ _id: order.buyer_id });
const sellerUser = await User.findOne({ _id: order.seller_id });
// We need two i18n contexts to send messages to each user
Expand Down
Loading