Skip to content

Commit

Permalink
Merge pull request #470 from Kpoke/fix/get_trust_relationship
Browse files Browse the repository at this point in the history
fix: get trust relationships issue
  • Loading branch information
Kpoke authored Jun 5, 2024
2 parents e563a49 + dfd41c8 commit 41e169f
Show file tree
Hide file tree
Showing 11 changed files with 356 additions and 195 deletions.
81 changes: 36 additions & 45 deletions server/handlers/trustHandler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const TrustService = require('../services/TrustService');
const JWTService = require('../services/JWTService');
const TrustRelationshipEnums = require('../utils/trust-enums');


describe('trustRouter', () => {
let app;
const authenticatedWalletId = uuid.v4();
Expand Down Expand Up @@ -193,48 +192,41 @@ describe('trustRouter', () => {
expect(res.body.message).match(/request_type.*one.*of/);
});

it('successfully', async () => {
const limit = 10;
const offset = 0;
const count = 1;
const orderBy = 'created_at';
const order = 'desc';

const getAllTrustRelationshipsStub = sinon
.stub(TrustService.prototype, 'getAllTrustRelationships')
.resolves({
result: [{ id: trustId }],
count
it('successfully', async () => {
const limit = 10;
const offset = 0;
const orderBy = 'created_at';
const order = 'desc';

const getAllTrustRelationshipsStub = sinon
.stub(TrustService.prototype, 'getAllTrustRelationships')
.resolves([{ id: trustId }]);

const res = await request(app).get(
`/trust_relationships?type=${TrustRelationshipEnums.ENTITY_TRUST_TYPE.send}&request_type=${TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.send}&state=${TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted}&limit=${limit}&offset=${offset}&order=${order}&sort_by=${orderBy}`,
);

expect(res).property('statusCode').eq(200);
expect(res.body.trust_relationships).to.have.lengthOf(1);
expect(res.body.trust_relationships[0]).to.eql({ id: trustId });

expect(getAllTrustRelationshipsStub).to.have.been.calledWith({
state: TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted,
type: TrustRelationshipEnums.ENTITY_TRUST_TYPE.send,
request_type: TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.send,
limit,
offset,
order,
sort_by: orderBy,
walletId: authenticatedWalletId,
});

const res = await request(app).get(
`/trust_relationships?type=${TrustRelationshipEnums.ENTITY_TRUST_TYPE.send}&request_type=${TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.send}&state=${TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted}&limit=${limit}&offset=${offset}&order=${order}&sort_by=${orderBy}`,
);

expect(res).property('statusCode').eq(200);
expect(res.body.trust_relationships).to.have.lengthOf(1);
expect(res.body.trust_relationships[0]).to.eql({ id: trustId });

expect(getAllTrustRelationshipsStub).to.have.been.calledWith({
state: TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted,
type: TrustRelationshipEnums.ENTITY_TRUST_TYPE.send,
request_type: TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.send,
limit,
offset,
order,
sort_by: orderBy,
walletId: authenticatedWalletId,
});
});
});




describe('get /trust_relationships/:id', () => {
it('missed parameters -- relationshipId must be a guid', async () => {
const res = await request(app).get(
`/trust_relationships/trustRelationshipId`,
`/trust_relationships/trustRelationshipId`,
);
expect(res).property('statusCode').eq(422);
expect(res.body.message).match(/trustRelationshipId.*GUID/);
Expand All @@ -244,21 +236,20 @@ describe('trustRouter', () => {
const trustRelationshipId = uuid.v4();

const trustRelationshipGetByIdStub = sinon
.stub(TrustService.prototype, 'trustRelationshipGetById')
.resolves({id: trustRelationshipId});
.stub(TrustService.prototype, 'trustRelationshipGetById')
.resolves({ id: trustRelationshipId });

const res = await request(app).get(
`/trust_relationships/${trustRelationshipId}`,
`/trust_relationships/${trustRelationshipId}`,
);

expect(res).property('statusCode').eq(200);
expect(
trustRelationshipGetByIdStub.calledOnceWithExactly({
walletLoginId: authenticatedWalletId,
trustRelationshipId,
}),
trustRelationshipGetByIdStub.calledOnceWithExactly({
walletLoginId: authenticatedWalletId,
trustRelationshipId,
}),
).eql(true);
});

})
});
});
12 changes: 4 additions & 8 deletions server/handlers/trustHandler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,28 @@ const trustGet = async (req, res) => {
state,
type,
request_type,
limit,
limit,
offset,
sort_by,
order
order,
} = validatedQuery;

const { wallet_id } = req;
const trustService = new TrustService();
const {
result: trust_relationships,
count: total,
} = await trustService.getAllTrustRelationships({
const trust_relationships = await trustService.getAllTrustRelationships({
walletId: wallet_id,
state,
type,
request_type,
offset,
limit,
sort_by,
order
order,
});

res.status(200).json({
trust_relationships,
query: { limit, offset, sort_by, order, state, type, request_type },
total,
});
};

Expand Down
11 changes: 8 additions & 3 deletions server/handlers/walletHandler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ describe('walletRouter', () => {
describe('get /wallets', () => {
it('no limit parameter(1000 as default)', async () => {
const res = await request(app).get('/wallets');
expect(res).property('statusCode').eq(200);
});
expect(res).property('statusCode').eq(200);
});

it('successfully', async () => {
const walletId = uuid.v4();
Expand Down Expand Up @@ -105,19 +105,24 @@ describe('walletRouter', () => {
it('successfully', async () => {
const getTrustRelationshipsStub = sinon
.stub(TrustService.prototype, 'getTrustRelationships')
.resolves([{ id: trustRelationshipId }]);
.resolves({ result: [{ id: trustRelationshipId }], count: 1 });
const res = await request(app).get(
`/wallets/${walletId}/trust_relationships?state=${TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.requested}`,
);
expect(res).property('statusCode').eq(200);
expect(res.body.trust_relationships).lengthOf(1);
expect(res.body.total).eql(1);
expect(res.body.trust_relationships[0].id).eql(trustRelationshipId);
expect(
getTrustRelationshipsStub.calledOnceWithExactly(authenticatedWalletId, {
walletId,
state: TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.requested,
type: undefined,
request_type: undefined,
limit: 500,
offset: 0,
sort_by: 'created_at',
order: 'desc',
}),
).eql(true);
});
Expand Down
23 changes: 18 additions & 5 deletions server/handlers/walletHandler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,21 @@ const walletGetTrustRelationships = async (req, res) => {
});
const validatedQuery = await walletGetTrustRelationshipsSchema.validateAsync(
req.query,
{
abortEarly: false,
},
{ abortEarly: false },
);

const { wallet_id: walletId } = validatedParams;
const { wallet_id: loggedInWalletId } = req;
const { state, type, request_type } = validatedQuery;
const {
state,
type,
request_type,
limit,
offset,
sort_by,
order,
} = validatedQuery;

const trustService = new TrustService();
const trust_relationships = await trustService.getTrustRelationships(
loggedInWalletId,
Expand All @@ -90,10 +97,16 @@ const walletGetTrustRelationships = async (req, res) => {
state,
type,
request_type,
limit,
offset,
sort_by,
order,
},
);
res.status(200).json({
trust_relationships,
trust_relationships: trust_relationships.result,
query: { limit, offset, sort_by, order, state, type, request_type },
total: trust_relationships.count,
});
};

Expand Down
6 changes: 6 additions & 0 deletions server/handlers/walletHandler/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ const walletGetTrustRelationshipsSchema = Joi.object({
request_type: Joi.string().valid(
...Object.values(TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE),
),
offset: Joi.number().integer().min(0).default(0),
limit: Joi.number().integer().min(1).max(2000).default(500),
sort_by: Joi.string()
.valid('state', 'created_at', 'updated_at')
.default('created_at'),
order: Joi.string().valid('asc', 'desc').default('desc'),
});

const walletPostSchema = Joi.object({
Expand Down
54 changes: 24 additions & 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 All @@ -16,19 +15,7 @@ class Trust {
return this._trustRepository.getById(id);
}

/*
* Get trust relationships by filters, setting filter to undefined to allow all data
*/
async getTrustRelationships({
walletId,
state,
type,
request_type,
offset,
limit,
sort_by,
order,
}) {
static getTrustRelationshipFilter({ walletId, state, type, request_type }) {
const filter = {
and: [
{
Expand All @@ -49,13 +36,14 @@ class Trust {
if (request_type) {
filter.and.push({ request_type });
}
return this._trustRepository.getByFilter(filter, { offset, limit });

return filter;
}

/*
* Get all trust relationships by filters, setting filter to undefined to allow all data
* Get trust relationships by filters, setting filter to undefined to allow all data
*/
async getAllTrustRelationships({
async getTrustRelationships({
walletId,
state,
type,
Expand All @@ -65,26 +53,32 @@ class Trust {
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, {
const filter = Trust.getTrustRelationshipFilter({
walletId,
state,
type,
request_type,
});

return this._trustRepository.getByFilter(filter, {
offset,
limit,
sort_by,
order,
});
}

async getTrustRelationshipsCount({ walletId, state, type, request_type }) {
const filter = Trust.getTrustRelationshipFilter({
walletId,
state,
type,
request_type,
});

return this._trustRepository.countByFilter(filter);
}

/*
* Get all relationships which has been accepted
*/
Expand Down
10 changes: 10 additions & 0 deletions server/models/Trust.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ describe('Trust Model', () => {
expect(trustRepositoryStub.getByFilter).calledOnceWithExactly(filter, {
limit: 10,
offset: 1,
sort_by: undefined,
order: undefined,
});
});

Expand All @@ -69,6 +71,8 @@ describe('Trust Model', () => {
{
limit: 10,
offset: 1,
sort_by: undefined,
order: undefined,
},
);
});
Expand All @@ -89,6 +93,8 @@ describe('Trust Model', () => {
{
limit: 10,
offset: 11,
sort_by: undefined,
order: undefined,
},
);
});
Expand All @@ -109,6 +115,8 @@ describe('Trust Model', () => {
{
limit: 101,
offset: 1,
sort_by: undefined,
order: undefined,
},
);
});
Expand All @@ -133,6 +141,8 @@ describe('Trust Model', () => {
{
limit: 100,
offset: 0,
sort_by: undefined,
order: undefined,
},
);
});
Expand Down
Loading

0 comments on commit 41e169f

Please sign in to comment.