Skip to content

Commit

Permalink
Merge pull request #493 from pranavkparti/trust-rel-ordering-update
Browse files Browse the repository at this point in the history
Trust relationships ordering update
  • Loading branch information
Kpoke authored Dec 12, 2024
2 parents 609d784 + dc25aa5 commit 7beb8c2
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 50 deletions.
40 changes: 27 additions & 13 deletions server/handlers/walletHandler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,42 +74,56 @@ const walletGetTrustRelationships = async (req, res) => {
});
const validatedQuery = await walletGetTrustRelationshipsSchema.validateAsync(
req.query,
{ abortEarly: false },
{
abortEarly: false,
},
);
const walletService = new WalletService();

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

const { wallets:managedWallets } = await walletService.getAllWallets(
const { wallets: managedWallets } = await walletService.getAllWallets(
loggedInWalletId,
{
limit:'10',
offset:'0',
limit: '1000',
offset: '0',
},
null,
sortBy,
orderBy,
null,
null
null,
);
const trustService = new TrustService();
const {result: trust_relationships, count: total} = await trustService.getTrustRelationships(
const {
result: trust_relationships,
count: total,
} = await trustService.getTrustRelationships(
loggedInWalletId,
managedWallets,
{
walletId,
state,
type,
request_type,
limit,
offset,
sort_by,
limit,
offset,
sort_by,
order,
search
search,
},
);
res.status(200).json({
Expand Down
31 changes: 22 additions & 9 deletions server/models/Trust.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Trust {
*/
async getTrustRelationships({
walletId,
managedWallets=[],
managedWallets = [],
state,
type,
request_type,
Expand All @@ -55,13 +55,14 @@ class Trust {
search,
order,
}) {
const managedWalletIds = managedWallets.map(wallet => wallet.id);
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 });
Expand Down Expand Up @@ -92,9 +93,19 @@ class Trust {
],
});
}
return this._trustRepository.getByFilter(filter, { offset, limit, sort_by, order });
return this._trustRepository.getByFilter(
filter,
{
offset,
limit,
sort_by,
order,
},
walletId,
managedWalletIds,
);
}

async getTrustRelationshipsCount({ walletId, state, type, request_type }) {
const filter = Trust.getTrustRelationshipFilter({
walletId,
Expand Down Expand Up @@ -235,8 +246,8 @@ class Trust {
// check if I (current wallet) can add a new trust like this
async checkDuplicateRequest({ walletId, trustRelationship }) {
let trustRelationships = await this.getTrustRelationships({ walletId });
if (trustRelationships.result) {
trustRelationships = trustRelationships.result;
if (trustRelationships.result) {
trustRelationships = trustRelationships.result;
}
if (
trustRelationship.type ===
Expand Down Expand Up @@ -380,8 +391,10 @@ class Trust {

async updateTrustState(trustRelationship, state) {
const trustRelationshipToUpdate = { ...trustRelationship };
const now = new Date();
const formattedDate = `${(now.getMonth() + 1).toString().padStart(2, '0')}/${now
const now = new Date();
const formattedDate = `${(now.getMonth() + 1)
.toString()
.padStart(2, '0')}/${now
.getDate()
.toString()
.padStart(2, '0')}/${now.getFullYear()}`;
Expand Down
25 changes: 19 additions & 6 deletions server/models/Trust.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,17 @@ describe('Trust Model', () => {
offset: 1,
});
expect(result).eql(['relationship1']);
expect(trustRepositoryStub.getByFilter).calledOnceWithExactly(filter, {
limit: 10,
offset: 1,
order: undefined,
sort_by: undefined,
});
expect(trustRepositoryStub.getByFilter).calledOnceWithExactly(
filter,
{
limit: 10,
offset: 1,
order: undefined,
sort_by: undefined,
},
walletId,
managedWalletIds,
);
});

it('should get relationships -- state', async () => {
Expand All @@ -87,6 +92,8 @@ describe('Trust Model', () => {
order: undefined,
sort_by: undefined,
},
walletId,
managedWalletIds,
);
});

Expand All @@ -111,6 +118,8 @@ describe('Trust Model', () => {
order: undefined,
sort_by: undefined,
},
walletId,
managedWalletIds,
);
});

Expand All @@ -135,6 +144,8 @@ describe('Trust Model', () => {
order: undefined,
sort_by: undefined,
},
walletId,
managedWalletIds,
);
});

Expand Down Expand Up @@ -163,6 +174,8 @@ describe('Trust Model', () => {
order: undefined,
sort_by: undefined,
},
walletId,
managedWalletIds,
);
});
});
Expand Down
50 changes: 38 additions & 12 deletions server/repositories/TrustRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ class TrustRepository extends BaseRepository {
return list;
}

async getByFilter(filter, limitOptions) {
async getByFilter(
filter,
limitOptions = {},
loggedInWalletId = '',
managedWalletIds = [],
) {
let promise = this._session
.getDB()
.select(
Expand Down Expand Up @@ -101,15 +106,7 @@ class TrustRepository extends BaseRepository {
)
.where((builder) => this.whereBuilder(filter, builder));

const count = await this._session.getDB().from(promise.as('p')).count('*');

if (limitOptions && limitOptions.limit) {
promise = promise.limit(limitOptions.limit);
}

if (limitOptions && limitOptions.offset) {
promise = promise.offset(limitOptions.offset);
}
const count = await this._session.getDB().from(promise.as('p')).count('*');

let order = 'desc';
let column = 'created_at';
Expand All @@ -130,9 +127,38 @@ class TrustRepository extends BaseRepository {
promise = promise.offset(limitOptions.offset);
}
}
promise = promise.orderBy(column, order);

const result = await promise;
// order by new column priority
// priority is 1 when state is requested and
// target is current wallet or one of its managed wallets
if (managedWalletIds.length > 0 && loggedInWalletId) {
promise = promise.select(
this._session.getDB().raw(
`CASE
WHEN wallet_trust.state = 'requested' AND
(wallet_trust.target_wallet_id = ? OR wallet_trust.target_wallet_id IN (${managedWalletIds
.map(() => '?')
.join(',')})) THEN 1
ELSE 0
END AS priority`,
[loggedInWalletId, ...managedWalletIds],
),
);
promise = promise.orderBy([
{ column: 'priority', order: 'desc' },
{ column, order }, // secondary
{ column: 'id', order }, // tertiary sort, prevent duplicate records among pages
]);
} else {
// normal ordering for other endpoints
promise = promise.orderBy(column, order);
}

let result = await promise;

// remove priority column from result
// eslint-disable-next-line no-unused-vars
result = result.map(({ priority, ...rest }) => rest);

return { result, count: +count[0].count };
}
Expand Down
40 changes: 30 additions & 10 deletions server/services/TrustService.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@ class TrustService {
async getTrustRelationships(
loggedInWalletId,
managedWallets,
{ walletId, state, type, request_type, offset, limit,sort_by, order, search },
{
walletId,
state,
type,
request_type,
offset,
limit,
sort_by,
order,
search,
},
) {
// check if wallet exists first
// throws error if no wallet matching walletId exists
Expand All @@ -43,7 +53,7 @@ class TrustService {
limit,
sort_by,
order,
search
search,
});

// const count = await this._trust.getTrustRelationshipsCount({
Expand All @@ -58,7 +68,13 @@ class TrustService {

// limit and offset not feasible using the current implementation
// except if done manually or coming up with a single query
async getAllTrustRelationships({ walletId, state, type, request_type, search }) {
async getAllTrustRelationships({
walletId,
state,
type,
request_type,
search,
}) {
const walletModel = new Wallet(this._session);
const { wallets } = await walletModel.getAllWallets(
walletId,
Expand All @@ -72,13 +88,17 @@ class TrustService {
const managedWallets = [];
await Promise.all(
wallets.map(async (w) => {
const trustRelationships = await this.getTrustRelationships(walletId, managedWallets,{
walletId: w.id,
state,
type,
request_type,
search
});
const trustRelationships = await this.getTrustRelationships(
walletId,
managedWallets,
{
walletId: w.id,
state,
type,
request_type,
search,
},
);
alltrustRelationships.push(...trustRelationships.result);
}),
);
Expand Down

0 comments on commit 7beb8c2

Please sign in to comment.