Skip to content

Commit

Permalink
Merge pull request #396 from pranavkparti/get-trust-relationship-id
Browse files Browse the repository at this point in the history
Get trust relationship by id endpoint
  • Loading branch information
Kpoke authored Aug 21, 2023
2 parents 779e070 + 3a2c596 commit 4a0d084
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 1 deletion.
31 changes: 31 additions & 0 deletions server/handlers/trustHandler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,35 @@ describe('trustRouter', () => {
});
});
});

describe('get /trust_relationships/:id', () => {
it('missed parameters -- relationshipId must be a guid', async () => {
const res = await request(app).get(
`/trust_relationships/trustRelationshipId`,
);
expect(res).property('statusCode').eq(422);
expect(res.body.message).match(/trustRelationshipId.*GUID/);
});

it('successfully', async () => {
const trustRelationshipId = uuid.v4();

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

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

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

})
});
20 changes: 19 additions & 1 deletion server/handlers/trustHandler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ const trustPost = async (req, res) => {
res.status(200).json(trustRelationship);
};

const trustRelationshipGetById = async (req, res) => {
await trustRelationshipIdSchema.validateAsync(req.params, {
abortEarly: false,
});

const { trustRelationshipId } = req.params
const trustService = new TrustService()

const trustRelationship = await trustService.trustRelationshipGetById({
walletLoginId: req.wallet_id,
trustRelationshipId
})

res.status(200).json(trustRelationship)
}


const trustRelationshipAccept = async (req, res) => {
await trustRelationshipIdSchema.validateAsync(req.params, {
abortEarly: false,
Expand All @@ -56,7 +73,7 @@ const trustRelationshipAccept = async (req, res) => {
trustRelationshipId,
walletLoginId: req.wallet_id,
});
res.json(json);
res.status(200).json(json);
};

const trustRelationshipDecline = async (req, res) => {
Expand Down Expand Up @@ -93,4 +110,5 @@ module.exports = {
trustRelationshipAccept,
trustRelationshipDecline,
trustRelationshipDelete,
trustRelationshipGetById
};
25 changes: 25 additions & 0 deletions server/models/Trust.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,31 @@ class Trust {
return false;
}

async getTrustRelationshipById({ walletId, trustRelationshipId}) {
const filter = {
and: [
{
or: [
{ actor_wallet_id: walletId },
{ target_wallet_id: walletId },
{ originator_wallet_id: walletId },
],
},
{
'wallet_trust.id': trustRelationshipId,
},
],
};

const [trustRelationship] = await this._trustRepository.getByFilter(filter)

if(!trustRelationship){
throw new HttpError(404, 'No such trust relationship exists or it is not associated with the current wallet.')
}

return trustRelationship
}

// NOT YET IN USE
// /*
// * Get all the trust relationships I have requested
Expand Down
31 changes: 31 additions & 0 deletions server/models/Trust.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -906,4 +906,35 @@ describe('Trust Model', () => {
expect(getTrustRelationshipsTrustedStub).not.called;
});
});

describe('getTrustRelationshipById', () => {
const walletId = uuid();
const trustRelationshipId = uuid()
const filter = {
and: [
{
or: [
{ actor_wallet_id: walletId },
{ target_wallet_id: walletId },
{ originator_wallet_id: walletId },
],
},
{
'wallet_trust.id': trustRelationshipId,
},
],
};

it('should get relationship', async () => {
trustRepositoryStub.getByFilter.resolves(['trustRelationship']);
const result = await trustModel.getTrustRelationshipById({
walletId,
trustRelationshipId
});
expect(result).eql('trustRelationship');
expect(trustRepositoryStub.getByFilter).calledOnceWithExactly(
{...filter}
);
});
})
});
2 changes: 2 additions & 0 deletions server/routes/trustRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
trustRelationshipDelete,
trustRelationshipDecline,
trustRelationshipAccept,
trustRelationshipGetById,
trustPost,
} = require('../handlers/trustHandler');

Expand All @@ -27,6 +28,7 @@ router.post(
handlerWrapper(trustRelationshipDecline),
);
router.delete('/:trustRelationshipId', handlerWrapper(trustRelationshipDelete));
router.get('/:trustRelationshipId', handlerWrapper(trustRelationshipGetById))

routerWrapper.use(
'/trust_relationships',
Expand Down
9 changes: 9 additions & 0 deletions server/services/TrustService.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ class TrustService {
trustRelationshipId,
});
}

async trustRelationshipGetById({ walletLoginId, trustRelationshipId}){
return this._trust.getTrustRelationshipById({
walletId: walletLoginId,
trustRelationshipId
})
}

}


module.exports = TrustService;
19 changes: 19 additions & 0 deletions server/services/TrustService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,23 @@ describe('TrustService', () => {
).eql(true);
});
});

describe('trustRelationshipGetById', async () => {
const trustRelationshipGetByIdStub = sinon
.stub(Trust.prototype, 'trustRelationshipGetById')
.resolves('trustRelationship');

const trustRelationship = await trustService.trustRelationshipGetById({
walletId: 'walletId',
trustRelationshipId: 'id'
});

expect(trustRelationship).eql('trustRelationship');
expect(
trustRelationshipGetByIdStub.calledOnceWithExactly({
walletId: 'walletId',
trustRelationshipId: 'id'
}),
).eql(true);
})
});

0 comments on commit 4a0d084

Please sign in to comment.