From 2d62c4c4385bc51ff72739ca90135f15dba3eee2 Mon Sep 17 00:00:00 2001 From: Pranav Kakaraparti Date: Sun, 20 Aug 2023 12:46:56 -0500 Subject: [PATCH] feat: add new GET endpoint --- server/handlers/trustHandler/index.js | 20 +++++++++++++++++++- server/models/Trust.js | 25 +++++++++++++++++++++++++ server/routes/trustRouter.js | 2 ++ server/services/TrustService.js | 9 +++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/server/handlers/trustHandler/index.js b/server/handlers/trustHandler/index.js index 0cb5370d..aef7541c 100644 --- a/server/handlers/trustHandler/index.js +++ b/server/handlers/trustHandler/index.js @@ -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, @@ -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) => { @@ -93,4 +110,5 @@ module.exports = { trustRelationshipAccept, trustRelationshipDecline, trustRelationshipDelete, + trustRelationshipGetById }; diff --git a/server/models/Trust.js b/server/models/Trust.js index 6832a2ec..9b1d0660 100644 --- a/server/models/Trust.js +++ b/server/models/Trust.js @@ -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 diff --git a/server/routes/trustRouter.js b/server/routes/trustRouter.js index 14d54e79..25bda30c 100644 --- a/server/routes/trustRouter.js +++ b/server/routes/trustRouter.js @@ -13,6 +13,7 @@ const { trustRelationshipDelete, trustRelationshipDecline, trustRelationshipAccept, + trustRelationshipGetById, trustPost, } = require('../handlers/trustHandler'); @@ -27,6 +28,7 @@ router.post( handlerWrapper(trustRelationshipDecline), ); router.delete('/:trustRelationshipId', handlerWrapper(trustRelationshipDelete)); +router.get('/:trustRelationshipId', handlerWrapper(trustRelationshipGetById)) routerWrapper.use( '/trust_relationships', diff --git a/server/services/TrustService.js b/server/services/TrustService.js index 8a5e34b7..205b0c86 100644 --- a/server/services/TrustService.js +++ b/server/services/TrustService.js @@ -109,6 +109,15 @@ class TrustService { trustRelationshipId, }); } + + async trustRelationshipGetById({ walletLoginId, trustRelationshipId}){ + return this._trust.getTrustRelationshipById({ + walletId: walletLoginId, + trustRelationshipId + }) } +} + + module.exports = TrustService;