Skip to content

Commit

Permalink
fix: refactor trust module
Browse files Browse the repository at this point in the history
  • Loading branch information
Kpoke committed Jun 12, 2022
1 parent e87d375 commit 88d60f5
Show file tree
Hide file tree
Showing 10 changed files with 459 additions and 396 deletions.
2 changes: 1 addition & 1 deletion __tests__/auth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Transfer = require('../server/models/Transfer');
const TrustRelationship = require('../server/models/TrustRelationship');
chai.use(require('chai-uuid'));

describe.only('Authentication', () => {
describe('Authentication', () => {
let bearerToken;
let bearerTokenB;

Expand Down
2 changes: 1 addition & 1 deletion __tests__/e2e/__tests__/wallets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { password } = testData.wallet;
const limit = 50;
const url = `/wallets?limit=${limit}`;

describe.only('Wallets (Wallet API)', function () {
describe('Wallets (Wallet API)', function () {
before(async () => {
bearer = await getSession(wallet, password);
});
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@
},
"homepage": "https://github.com/Greenstand/treetracker-wallet-api#readme",
"engines": {
"node": ">= 12 < 13"
"node": ">= 16"
}
}
107 changes: 35 additions & 72 deletions server/handlers/trustHandler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const Joi = require('joi');
const WalletService = require('../services/WalletService');
const TrustService = require('../services/TrustService');
const Session = require('../database/Session');
const TrustRelationship = require('../utils/trust-enums');

const trustGetQuerySchema = Joi.object({
Expand All @@ -17,6 +15,7 @@ const trustPostSchema = Joi.object({
.required()
.valid(...Object.keys(TrustRelationship.ENTITY_TRUST_REQUEST_TYPE)),
requestee_wallet: Joi.string().required(),
requester_wallet: Joi.string(),
});

const trustRelationshipIdSchema = Joi.object({
Expand All @@ -29,66 +28,33 @@ const trustGet = async (req, res) => {
});

const { state, type, request_type, limit, offset } = req.query;
const session = new Session();
const walletService = new WalletService(session);
const trustService = new TrustService(session);
const wallet = await walletService.getById(res.locals.wallet_id);
const trust_relationships = await wallet.getTrustRelationships(
const trustService = new TrustService();
const trustRelationships = await trustService.getAllTrustRelationships({
walletId: req.wallet_id,
state,
type,
request_type,
Number(offset || 0),
Number(limit || 0),
);
const subWallets = await wallet.getSubWallets();
for (const sw of subWallets) {
const trustRelationships = await sw.getTrustRelationships(
req.query.state,
req.query.type,
req.query.request_type,
);
for (const tr of trustRelationships) {
if (trust_relationships.every((e) => e.id !== tr.id)) {
trust_relationships.push(tr);
}
}
}

const trust_relationships_json = [];
for (const t of trust_relationships) {
const j = await trustService.convertToResponse(t);
trust_relationships_json.push(j);
}
offset,
limit,
});

res.status(200).json({
trust_relationships: trust_relationships_json,
trust_relationships: trustRelationships,
});
};

const trustPost = async (req, res) => {
await trustPostSchema.validateAsync(req.body, { abortEarly: false });

const session = new Session();
const walletService = new WalletService(session);
const trustService = new TrustService(session);
const wallet = await walletService.getById(res.locals.wallet_id);
const requesteeWallet = await walletService.getByName(
req.body.requestee_wallet,
);
let requesterWallet = wallet;
if (req.body.requester_wallet) {
requesterWallet = await walletService.getByName(req.body.requester_wallet);
}

const trust_relationship = await wallet.requestTrustFromAWallet(
req.body.trust_request_type,
requesterWallet,
requesteeWallet,
);
const trust_relationship_json = await trustService.convertToResponse(
trust_relationship,
);
res.status(200).json(trust_relationship_json);
const trustService = new TrustService();
const trustRelationship = await trustService.createTrustRelationship({
walletId: req.wallet_id,
requesteeWallet: req.body.requestee_wallet,
requesterWallet: req.body.requester_wallet,
trustRequestType: req.body.trust_request_type,
});

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

const trustRelationshipAccept = async (req, res) => {
Expand All @@ -97,13 +63,12 @@ const trustRelationshipAccept = async (req, res) => {
});

const { trustRelationshipId } = req.params;
const session = new Session();
const walletService = new WalletService(session);
const trustService = new TrustService(session);
const wallet = await walletService.getById(res.locals.wallet_id);
const json = await wallet.acceptTrustRequestSentToMe(trustRelationshipId);
const json2 = await trustService.convertToResponse(json);
res.status(200).json(json2);
const trustService = new TrustService();
const json = await trustService.acceptTrustRequestSentToMe({
trustRelationshipId,
walletId: req.wallet_id,
});
res.status(200).json(json);
};

const trustRelationshipDecline = async (req, res) => {
Expand All @@ -112,13 +77,12 @@ const trustRelationshipDecline = async (req, res) => {
});

const { trustRelationshipId } = req.params;
const session = new Session();
const walletService = new WalletService(session);
const trustService = new TrustService(session);
const wallet = await walletService.getById(res.locals.wallet_id);
const json = await wallet.declineTrustRequestSentToMe(trustRelationshipId);
const json2 = await trustService.convertToResponse(json);
res.status(200).json(json2);
const trustService = new TrustService();
const json = await trustService.declineTrustRequestSentToMe({
walletId: req.wallet_id,
trustRelationshipId,
});
res.status(200).json(json);
};

const trustRelationshipDelete = async (req, res) => {
Expand All @@ -127,13 +91,12 @@ const trustRelationshipDelete = async (req, res) => {
});

const { trustRelationshipId } = req.params;
const session = new Session();
const walletService = new WalletService(session);
const trustService = new TrustService(session);
const wallet = await walletService.getById(res.locals.wallet_id);
const json = await wallet.cancelTrustRequestSentToMe(trustRelationshipId);
const json2 = await trustService.convertToResponse(json);
res.status(200).json(json2);
const trustService = new TrustService();
const json = await trustService.cancelTrustRequestSentToMe({
walletId: req.wallet_id,
trustRelationshipId,
});
res.status(200).json(json);
};

module.exports = {
Expand Down
Loading

0 comments on commit 88d60f5

Please sign in to comment.