Skip to content

Commit

Permalink
Merge pull request #480 from samwel141/master
Browse files Browse the repository at this point in the history
enable get trust relationships for the managed wallets
  • Loading branch information
Kpoke authored Sep 10, 2024
2 parents 1b8d320 + 55e815b commit 87b2b21
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 90 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@
"engines": {
"node": ">= 16"
}
}
}
2 changes: 1 addition & 1 deletion server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ app.get('*', function (req, res) {
res.status(200).send(version);
});

module.exports = app;
module.exports = app;
13 changes: 7 additions & 6 deletions server/handlers/walletHandler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,25 @@ describe('walletRouter', () => {
it('successfully', async () => {
const getTrustRelationshipsStub = sinon
.stub(TrustService.prototype, 'getTrustRelationships')
.resolves({ result: [{ id: trustRelationshipId }], count: 1 });
.resolves({result:[{ id: trustRelationshipId }], count: 1});
const res = await request(app).get(
`/wallets/${walletId}/trust_relationships?state=${TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.requested}`,
);
const managedWallets = [];
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, {
getTrustRelationshipsStub.calledOnceWithExactly(authenticatedWalletId, managedWallets, {
walletId,
state: TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.requested,
type: undefined,
request_type: undefined,
limit: 500,
offset: 0,
sort_by: 'created_at',
order: 'desc',
limit: 500,
offset: 0,
sort_by: 'created_at',
order: 'desc'
}),
).eql(true);
});
Expand Down
40 changes: 24 additions & 16 deletions server/handlers/walletHandler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,37 +76,45 @@ const walletGetTrustRelationships = async (req, res) => {
req.query,
{ abortEarly: false },
);
const walletService = new WalletService();

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

const { wallets:managedWallets } = await walletService.getAllWallets(
loggedInWalletId,
{
limit:'10',
offset:'0',
},
null,
sortBy,
orderBy,
null,
null
);
const trustService = new TrustService();
const trust_relationships = await trustService.getTrustRelationships(
const {result: trust_relationships, count: total} = await trustService.getTrustRelationships(
loggedInWalletId,
managedWallets,
{
walletId,
state,
type,
request_type,
limit,
offset,
sort_by,
order,
limit,
offset,
sort_by,
order
},
);
res.status(200).json({
trust_relationships: trust_relationships.result,
trust_relationships,
query: { limit, offset, sort_by, order, state, type, request_type },
total: trust_relationships.count,
total,
});
};

Expand Down
4 changes: 1 addition & 3 deletions server/handlers/walletHandler/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ const walletGetTrustRelationshipsSchema = Joi.object({
),
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'),
sort_by: Joi.string().valid( 'state', 'created_at', 'updated_at').default('created_at'),
order: Joi.string().valid('asc', 'desc').default('desc'),
});

Expand Down
75 changes: 55 additions & 20 deletions server/models/Trust.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Trust {
*/
async getTrustRelationships({
walletId,
managedWallets=[],
state,
type,
request_type,
Expand All @@ -53,21 +54,37 @@ class Trust {
sort_by,
order,
}) {
const filter = Trust.getTrustRelationshipFilter({
walletId,
state,
type,
request_type,
});

return this._trustRepository.getByFilter(filter, {
offset,
limit,
sort_by,
order,
const managedWalletIds = managedWallets.map(wallet => wallet.id);
const orConditions = [
{ actor_wallet_id: walletId },
{ target_wallet_id: walletId },
{ originator_wallet_id: walletId },
];

managedWalletIds.forEach((managedWalletId) => {
orConditions.push({ actor_wallet_id: managedWalletId });
orConditions.push({ target_wallet_id: managedWalletId });
orConditions.push({ originator_wallet_id: managedWalletId });
});
const filter = {
and: [
{
or: orConditions,
},
],
};
if (state) {
filter.and.push({ state });
}
if (type) {
filter.and.push({ type });
}
if (request_type) {
filter.and.push({ request_type });
}
return this._trustRepository.getByFilter(filter, { offset, limit, sort_by, order });
}

async getTrustRelationshipsCount({ walletId, state, type, request_type }) {
const filter = Trust.getTrustRelationshipFilter({
walletId,
Expand Down Expand Up @@ -194,7 +211,10 @@ class Trust {

// check if I (current wallet) can add a new trust like this
async checkDuplicateRequest({ walletId, trustRelationship }) {
const trustRelationships = await this.getTrustRelationships({ walletId });
let trustRelationships = await this.getTrustRelationships({ walletId });
if (trustRelationships.result) {
trustRelationships = trustRelationships.result;
}
if (
trustRelationship.type ===
TrustRelationshipEnums.ENTITY_TRUST_TYPE.send ||
Expand Down Expand Up @@ -236,9 +256,12 @@ class Trust {
}

async checkManageCircle({ walletId, trustRelationship }) {
const trustRelationshipTrusted = await this.getTrustRelationshipsTrusted(
let trustRelationshipTrusted = await this.getTrustRelationshipsTrusted(
walletId,
);
if (trustRelationshipTrusted.result) {
trustRelationshipTrusted = trustRelationshipTrusted.result;
}
// just manage type of trust relationship
if (
trustRelationship.type === TrustRelationshipEnums.ENTITY_TRUST_TYPE.manage
Expand Down Expand Up @@ -319,7 +342,10 @@ class Trust {
const allTrustRelationships = [];
await Promise.all(
allWallets.map(async (wallet) => {
const list = await this.getTrustRelationships({ walletId: wallet.id });
let list = await this.getTrustRelationships({ walletId: wallet.id });
if (list.result) {
list = list.result;
}
allTrustRelationships.push(...list);
}),
);
Expand Down Expand Up @@ -348,9 +374,12 @@ class Trust {
* Accept a trust relationship request
*/
async acceptTrustRequestSentToMe({ trustRelationshipId, walletId }) {
const trustRelationships = await this.getTrustRelationshipsRequestedToMe(
let trustRelationships = await this.getTrustRelationshipsRequestedToMe(
walletId,
);
if (trustRelationships.result) {
trustRelationships = trustRelationships.result;
}
const trustRelationship = trustRelationships.reduce((a, c) => {
if (c.id === trustRelationshipId) {
return c;
Expand All @@ -376,9 +405,12 @@ class Trust {
* Decline a trust relationship request
*/
async declineTrustRequestSentToMe({ walletId, trustRelationshipId }) {
const trustRelationships = await this.getTrustRelationshipsRequestedToMe(
let trustRelationships = await this.getTrustRelationshipsRequestedToMe(
walletId,
);
if (trustRelationships.result) {
trustRelationships = trustRelationships.result;
}
const trustRelationship = trustRelationships.reduce((a, c) => {
if (c.id === trustRelationshipId) {
return c;
Expand Down Expand Up @@ -406,7 +438,7 @@ class Trust {
const trustRelationships = await this._trustRepository.getByFilter({
'wallet_trust.id': trustRelationshipId,
});
const [trustRelationship] = trustRelationships;
const [trustRelationship] = trustRelationships.result;

if (!trustRelationship) {
throw new HttpError(
Expand Down Expand Up @@ -440,9 +472,12 @@ class Trust {
),
);

const trustRelationships = await this.getTrustRelationshipsTrusted(
let trustRelationships = await this.getTrustRelationshipsTrusted(
walletLoginId,
);
if (trustRelationships.result) {
trustRelationships = trustRelationships.result;
}
// check if the trust exist
if (
trustRelationships.some((trustRelationship) => {
Expand Down
Loading

0 comments on commit 87b2b21

Please sign in to comment.