Skip to content

Commit

Permalink
fix: get trust relationships issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Kpoke committed May 21, 2024
1 parent e563a49 commit 585b2be
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 78 deletions.
31 changes: 1 addition & 30 deletions server/models/Trust.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-unused-vars */
const Joi = require('joi');
const log = require('loglevel');
const TrustRepository = require('../repositories/TrustRepository');
Expand Down Expand Up @@ -49,35 +48,7 @@ class Trust {
if (request_type) {
filter.and.push({ request_type });
}
return this._trustRepository.getByFilter(filter, { offset, limit });
}

/*
* Get all trust relationships by filters, setting filter to undefined to allow all data
*/
async getAllTrustRelationships({
walletId,
state,
type,
request_type,
offset,
limit,
sort_by,
order,
}) {
const filter = {
and: [{ 'originator_wallet.id': walletId }],
};
if (state) {
filter.and.push({ state });
}
if (type) {
filter.and.push({ type });
}
if (request_type) {
filter.and.push({ request_type });
}
return this._trustRepository.getAllByFilter(filter, {
return this._trustRepository.getByFilter(filter, {
offset,
limit,
sort_by,
Expand Down
73 changes: 51 additions & 22 deletions server/services/TrustService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const Session = require('../infra/database/Session');
const WalletService = require('./WalletService');
const EventService = require('./EventService');
const EventEnums = require('../utils/event-enum');
const HttpError = require('../utils/HttpError');
const Wallet = require('../models/Wallet');

class TrustService {
constructor() {
Expand All @@ -13,12 +15,22 @@ class TrustService {

async getTrustRelationships(
loggedInWalletId,
{ walletId, state, type, request_type, offset, limit,sort_by, order },
{ walletId, state, type, request_type, offset, limit, sort_by, order },
) {
// check if wallet exists first
// throws error if no wallet matching walletId exists
const walletService = new WalletService();
await walletService.getWallet(loggedInWalletId, walletId);
const hasControl = await walletService.hasControlOver(
loggedInWalletId,
walletId,
);

if (!hasControl) {
throw new HttpError(
422,
'You do not have permission to update this wallet',
);
}

return this._trust.getTrustRelationships({
walletId,
Expand All @@ -28,31 +40,48 @@ class TrustService {
offset,
limit,
sort_by,
order
order,
});
}

async getAllTrustRelationships({
walletId,
state,
type,
request_type,
offset,
limit,
sort_by,
order,

}) {
return this._trust.getAllTrustRelationships({
// limit and offset not feasible using the current implementation
// except if done manually or coming up with a single query
async getAllTrustRelationships({ walletId, state, type, request_type }) {
const walletModel = new Wallet(this._session);
const { wallets } = await walletModel.getAllWallets(
walletId,
state,
type,
request_type,
offset,
limit,
sort_by,
order
undefined,
undefined,
'created_at',
'desc',
);

const alltrustRelationships = [];

await Promise.all(
wallets.map(async (w) => {
const trustRelationships = await this.getTrustRelationships({
walletId: w.id,
state,
type,
request_type,
});
alltrustRelationships.push(...trustRelationships);
}),
);

// remove possible duplicates
const ids = {};
const finalTrustRelationships = [];

alltrustRelationships.forEach((tr) => {
if (!ids[tr.id]) {
finalTrustRelationships.push(tr);
ids[tr.id] = 1;
}
});

return finalTrustRelationships;
}

async createTrustRelationship({
Expand Down
91 changes: 65 additions & 26 deletions server/services/TrustService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const TrustService = require('./TrustService');
const WalletService = require('./WalletService');
const Trust = require('../models/Trust');
const EventService = require('./EventService');
const Wallet = require('../models/Wallet');

const { expect } = chai;

Expand All @@ -29,6 +30,10 @@ describe('TrustService', () => {
.stub(WalletService.prototype, 'getWallet')
.resolves({ id: 'walletId' });

const hasControlOverStub = sinon
.stub(WalletService.prototype, 'hasControlOver')
.resolves(true);

const trustRelationship = await trustService.getTrustRelationships(
authenticatedWalletId,
{
Expand Down Expand Up @@ -58,9 +63,15 @@ describe('TrustService', () => {
limit: 1,
offset: 0,
sort_by: 'sort_by',
order: 'order'
order: 'order',
}),
).eql(true);
expect(
hasControlOverStub.calledOnceWithExactly(
authenticatedWalletId,
'walletId',
),
).eql(true);
});

it('acceptTrustRequestSentToMe', async () => {
Expand Down Expand Up @@ -189,39 +200,67 @@ describe('TrustService', () => {
});

it('getAllTrustRelationships', async () => {
const data = {
result: [
const getAllWalletsStub = sinon
.stub(Wallet.prototype, 'getAllWallets')
.resolves({ wallets: [{ id: 'id1' }, { id: 'id2' }] });
const getTrustRelationshipsStub = sinon.stub(
TrustService.prototype,
'getTrustRelationships',
);
getTrustRelationshipsStub
.onFirstCall()
.resolves([
{ id: 'trustId1' },
{ id: 'trustId2' },
{ id: 'trustId3' },
{ id: 'trustId4' },
],
count: 4,
};

const getAllTrustRelationshipsStub = sinon.stub(
TrustService.prototype,
'getAllTrustRelationships',
);
getAllTrustRelationshipsStub.resolves(data);
]);
getTrustRelationshipsStub
.onSecondCall()
.resolves([
{ id: 'trustId1' },
{ id: 'trustId2' },
{ id: 'trustId5' },
{ id: 'trustId6' },
]);

const trustRelationships = await trustService.getAllTrustRelationships({
state: 'requested',
type: 'send',
request_type: 'send',
limit: 10,
offset: 0,
walletId: 'walletId',
state: 'state',
type: 'type',
request_type: 'request_type',
});

expect(trustRelationships).eql(data);

expect(trustRelationships).eql([
{ id: 'trustId1' },
{ id: 'trustId2' },
{ id: 'trustId3' },
{ id: 'trustId4' },
{ id: 'trustId5' },
{ id: 'trustId6' },
]);
expect(
getAllTrustRelationshipsStub.calledWithExactly({
state: 'requested',
type: 'send',
request_type: 'send',
limit: 10,
offset: 0,
getAllWalletsStub.calledOnceWithExactly(
'walletId',
undefined,
undefined,
'created_at',
'desc',
),
).eql(true);
expect(
getTrustRelationshipsStub.getCall(0).calledWithExactly({
walletId: 'id1',
state: 'state',
type: 'type',
request_type: 'request_type',
}),
).eql(true);
expect(
getTrustRelationshipsStub.getCall(1).calledWithExactly({
walletId: 'id2',
state: 'state',
type: 'type',
request_type: 'request_type',
}),
).eql(true);
});
Expand Down

0 comments on commit 585b2be

Please sign in to comment.