Skip to content

Commit 04a09b0

Browse files
committed
refactor QueueService
1 parent 43a0850 commit 04a09b0

File tree

3 files changed

+11
-18
lines changed

3 files changed

+11
-18
lines changed

server/handlers/walletHandler/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const csvtojson = require('csvtojson');
2-
const queueService = require('../../services/QueueService');
2+
const QueueService = require('../../services/QueueService');
33

44
const WalletService = require('../../services/WalletService');
55
const TrustService = require('../../services/TrustService');
@@ -162,7 +162,7 @@ const walletPost = async (req, res) => {
162162
);
163163
}
164164

165-
await queueService.sendWalletCreationNotification(returnedWallet);
165+
await QueueService.sendWalletCreationNotification(returnedWallet);
166166

167167
res.status(201).json(returnedWallet);
168168
};

server/services/QueueService.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,15 @@ const queue = require('treetracker-wallet-app/packages/queue');
33
const knex = require('../infra/database/knex');
44

55
class QueueService {
6-
constructor() {
7-
this.queue = queue;
8-
this.knex = knex;
9-
}
10-
116
/**
127
* Publishes a wallet creation event to the queue.
138
* @param {Object} wallet - The wallet object that was just created.
149
* @returns {Promise<void>}
1510
*/
16-
sendWalletCreationNotification(wallet) {
11+
static async sendWalletCreationNotification(wallet) {
1712
try {
18-
this.queue.publish({
19-
pgClient: this.knex,
13+
queue.publish({
14+
pgClient: knex,
2015
channel: 'wallet_created',
2116
data: {
2217
walletId: wallet.id,
@@ -33,4 +28,4 @@ class QueueService {
3328
}
3429
}
3530

36-
module.exports = new QueueService();
31+
module.exports = QueueService;

server/services/QueueService.spec.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
const sinon = require('sinon');
22
const { expect } = require('chai');
33
const log = require('loglevel');
4-
54
const queue = require('treetracker-wallet-app/packages/queue');
5+
const knex = require('../infra/database/knex');
6+
const QueueService = require('./QueueService');
67

78
describe('QueueService', () => {
89
let publishStub;
@@ -17,9 +18,6 @@ describe('QueueService', () => {
1718
sinon.restore();
1819
});
1920

20-
const queueService = require('./QueueService');
21-
const knex = require('../infra/database/knex');
22-
2321
describe('sendWalletCreationNotification', () => {
2422
it('should publish a wallet creation message to the queue', async () => {
2523
const wallet = {
@@ -28,7 +26,7 @@ describe('QueueService', () => {
2826
createdAt: '2025-04-29T00:00:00Z',
2927
};
3028

31-
await queueService.sendWalletCreationNotification(wallet);
29+
await QueueService.sendWalletCreationNotification(wallet);
3230

3331
expect(publishStub.calledOnce).to.be.true;
3432
expect(publishStub.firstCall.args[0]).to.deep.equal({
@@ -42,7 +40,7 @@ describe('QueueService', () => {
4240
});
4341
});
4442

45-
it('should not throw error if publish fails', () => {
43+
it('should not throw error if publish fails', async () => {
4644
publishStub.throws(new Error('publish failed'));
4745

4846
const wallet = {
@@ -53,7 +51,7 @@ describe('QueueService', () => {
5351

5452
let errorCaught = false;
5553
try {
56-
queueService.sendWalletCreationNotification(wallet);
54+
await QueueService.sendWalletCreationNotification(wallet);
5755
} catch (err) {
5856
errorCaught = true;
5957
}

0 commit comments

Comments
 (0)