-
Notifications
You must be signed in to change notification settings - Fork 132
Add settled_by_admin field in order.ts model and improve getDetailedOrder output. #747
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
Changes from all commits
fa27c1d
a8c9520
70e2038
a07edbe
7e9a117
80e8ada
3412a52
d804ffd
d790c63
80bbedb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import 'dotenv/config'; | ||
| import { connect as mongoConnect } from '../db_connect'; | ||
| import Order from '../models/order'; | ||
| import { logger } from '../logger'; | ||
|
|
||
| const migrate = async () => { | ||
| try { | ||
| const mongoose = mongoConnect(); | ||
| await new Promise((resolve, reject) => { | ||
| mongoose.connection.once('open', resolve); | ||
| mongoose.connection.on('error', reject); | ||
| }); | ||
|
|
||
| logger.info('Connected to MongoDB for migration.'); | ||
|
|
||
| const query = { status: 'COMPLETED_BY_ADMIN' }; | ||
| const update = { | ||
| $set: { | ||
| status: 'SUCCESS', | ||
| settled_by_admin: true, | ||
| }, | ||
| }; | ||
|
|
||
| const result = await Order.updateMany(query, update); | ||
|
|
||
| logger.info(`Migration completed.`); | ||
| logger.info(`Matched: ${result.matchedCount} orders.`); | ||
| logger.info(`Modified: ${result.modifiedCount} orders.`); | ||
|
|
||
| await mongoose.connection.close(); | ||
| logger.info('Database connection closed.'); | ||
| process.exit(0); | ||
| } catch (error) { | ||
| logger.error(`Migration failed: ${error}`); | ||
| process.exit(1); | ||
| } | ||
| }; | ||
|
|
||
| migrate(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -609,4 +609,110 @@ describe('Bot Initialization', () => { | |
| ctx.reply.calledWithExactly('This is an unknown command.'), | ||
| ).to.be.equal(true); | ||
| }); | ||
|
|
||
| it('should set settled_by_admin when admin settles with secret', async () => { | ||
| const orderMock = { | ||
| _id: 'orderId', | ||
| status: 'DISPUTE', | ||
| secret: 'secret', | ||
| community_id: null, | ||
| buyer_id: 'buyer', | ||
| seller_id: 'seller', | ||
| save: sinon.stub().resolves(), | ||
| } as any; | ||
|
|
||
| const OrderFindOneStub = sinon.stub().resolves(orderMock); | ||
| const settleHoldInvoiceStub = sinon.stub().resolves(); | ||
|
|
||
| const startModule = proxyquire('../../bot/start', { | ||
| telegraf: { Telegraf: sinon.stub().returns(botStub) }, | ||
| '../models': { | ||
| Order: { findOne: OrderFindOneStub }, | ||
| User: { findOne: sinon.stub().resolves({ id: 'user' }) }, | ||
| Dispute: { findOne: sinon.stub().resolves(null) }, | ||
| }, | ||
| './validations': { | ||
| validateParams: sinon.stub().resolves(['orderId']), | ||
| validateObjectId: sinon.stub().resolves(true), | ||
| }, | ||
| '../ln': { settleHoldInvoice: settleHoldInvoiceStub }, | ||
| './messages': { | ||
| successCompleteOrderMessage: sinon.stub().resolves(), | ||
| successCompleteOrderByAdminMessage: sinon.stub().resolves(), | ||
| }, | ||
| }); | ||
|
|
||
| startModule.initialize('dummy-token', {}); | ||
| const settleOrderCall = botStub.command | ||
| .getCalls() | ||
| .find((c: any) => c.args[0] === 'settleorder'); | ||
| const handler = settleOrderCall.args[2]; | ||
|
|
||
| const ctx = { | ||
| admin: { admin: true }, | ||
| match: ['/settleorder orderId', 'orderId'], | ||
| reply: sinon.stub().resolves(), | ||
| i18n: { t: sinon.stub().returns('Success') }, | ||
| telegram: { sendMessage: sinon.stub().resolves() }, | ||
| }; | ||
|
|
||
| await handler(ctx); | ||
|
|
||
| expect(settleHoldInvoiceStub.calledWith({ secret: 'secret' })).to.be.equal( | ||
| true, | ||
| ); | ||
| expect(orderMock.settled_by_admin).to.be.equal(true); | ||
| expect(orderMock.save.called).to.be.equal(true); | ||
| }); | ||
|
|
||
| it('should not modify order if settleHoldInvoice fails', async () => { | ||
| const orderMock = { | ||
| _id: 'orderId', | ||
| status: 'DISPUTE', | ||
| secret: 'invalidsecret', | ||
| community_id: null, | ||
| buyer_id: 'buyer', | ||
| seller_id: 'seller', | ||
| save: sinon.stub().resolves(), | ||
| } as any; | ||
|
|
||
| const OrderFindOneStub = sinon.stub().resolves(orderMock); | ||
| const settleHoldInvoiceStub = sinon.stub().rejects(new Error('LND failed')); | ||
|
|
||
| const startModule = proxyquire('../../bot/start', { | ||
| telegraf: { Telegraf: sinon.stub().returns(botStub) }, | ||
| '../models': { | ||
| Order: { findOne: OrderFindOneStub }, | ||
| User: { findOne: sinon.stub().resolves({ id: 'user' }) }, | ||
| Dispute: { findOne: sinon.stub().resolves(null) }, | ||
| }, | ||
| './validations': { | ||
| validateParams: sinon.stub().resolves(['orderId']), | ||
| validateObjectId: sinon.stub().resolves(true), | ||
| }, | ||
| '../ln': { settleHoldInvoice: settleHoldInvoiceStub }, | ||
| './messages': { | ||
| successCompleteOrderMessage: sinon.stub().resolves(), | ||
| successCompleteOrderByAdminMessage: sinon.stub().resolves(), | ||
| }, | ||
| }); | ||
|
|
||
| startModule.initialize('dummy-token', {}); | ||
| const settleOrderCall = botStub.command | ||
| .getCalls() | ||
| .find((c: any) => c.args[0] === 'settleorder'); | ||
| const handler = settleOrderCall.args[2]; | ||
|
|
||
| const ctx = { | ||
| admin: { admin: true }, | ||
| match: ['/settleorder orderId', 'orderId'], | ||
| reply: sinon.stub().resolves(), | ||
| i18n: { t: sinon.stub().returns('Success') }, | ||
| }; | ||
|
|
||
| await handler(ctx); | ||
|
|
||
| expect(orderMock.save.called).to.be.equal(false); | ||
| expect(orderMock.settled_by_admin).to.equal(undefined); | ||
| }); | ||
|
Comment on lines
+668
to
+717
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test stub behavior doesn't match production Per the relevant code snippet from In practice, if LN settlement fails:
Consider either:
🤖 Prompt for AI Agents |
||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.