Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit c34cd69

Browse files
valentinewallacetanx
authored andcommitted
Notify the user if they receive an invoice that is settled.
Tell them how much they've been paid, and give them the option of navigating to the details view for the invoice.
1 parent 469f3e6 commit c34cd69

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/action/transaction.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,31 @@ class TransactionAction {
134134
async subscribeInvoices() {
135135
const stream = this._grpc.sendStreamCommand('subscribeInvoices');
136136
await new Promise((resolve, reject) => {
137-
stream.on('data', () => this.update());
137+
stream.on('data', invoice => this._receiveInvoice(invoice));
138138
stream.on('end', resolve);
139139
stream.on('error', reject);
140140
stream.on('status', status => log.info(`Invoices update: ${status}`));
141141
});
142142
}
143+
144+
//
145+
// Helper functions
146+
//
147+
148+
async _receiveInvoice(invoice) {
149+
await this.update();
150+
if (invoice.settled) {
151+
const invoiceId = toHex(invoice.r_hash);
152+
let inv = this._store.computedTransactions.find(
153+
invoice => invoice.id === invoiceId
154+
);
155+
this._notification.display({
156+
msg: `Invoice success: received ${inv.amountLabel}`,
157+
handler: () => this.select({ item: inv }),
158+
handlerLbl: 'View details',
159+
});
160+
}
161+
}
143162
}
144163

145164
export default TransactionAction;

test/unit/action/transaction.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,31 @@ describe('Action Transactions Unit Tests', () => {
222222
expect(transaction.update, 'was called once');
223223
});
224224

225+
it('should notify the user on settled invoice', async () => {
226+
store.computedTransactions = [{ id: 'cdab' }];
227+
onStub.withArgs('data').yields({
228+
settled: true,
229+
r_hash: Buffer.from('cdab', 'hex'),
230+
});
231+
onStub.withArgs('end').yields();
232+
grpc.sendStreamCommand
233+
.withArgs('subscribeInvoices')
234+
.returns({ on: onStub });
235+
await transaction.subscribeInvoices();
236+
expect(notification.display, 'was called once');
237+
});
238+
239+
it('should not notify the user on an unsettled invoice', async () => {
240+
onStub.withArgs('data').yields({ settled: false });
241+
onStub.withArgs('end').yields();
242+
grpc.sendStreamCommand
243+
.withArgs('subscribeInvoices')
244+
.returns({ on: onStub });
245+
await transaction.subscribeInvoices();
246+
expect(transaction.update, 'was called once');
247+
expect(notification.display, 'was not called');
248+
});
249+
225250
it('should reject in case of error', async () => {
226251
onStub.withArgs('error').yields(new Error('Boom!'));
227252
grpc.sendStreamCommand

0 commit comments

Comments
 (0)