Skip to content

Commit

Permalink
fix: Get Friends duplicated addresses on response (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinszuchet authored Jan 17, 2025
1 parent 99e3e10 commit 12ee265
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/adapters/rpc-server/services/get-friends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ export function getFriendsService({ components: { logs, db } }: RPCServiceContex

return async function (request: GetFriendsPayload, context: RpcServerContext): Promise<PaginatedUsersResponse> {
const { pagination, status: _status } = request
const { address: loggedUserAddress } = context
try {
const [friends, total] = await Promise.all([
db.getFriends(context.address, { pagination }),
db.getFriendsCount(context.address)
db.getFriends(loggedUserAddress, { pagination }),
db.getFriendsCount(loggedUserAddress)
])

// TODO: retrieve peers and filter by connectivity status
// connecting to NATS and maintaining the same logic as stats/peers

return {
users: friends.map((friend: Friendship) => ({ address: friend.address_requested })),
users: friends.map((friend) => ({
address: friend.address_requested === loggedUserAddress ? friend.address_requester : friend.address_requested
})),
paginationData: {
total,
page: getPage(pagination?.limit || FRIENDSHIPS_PER_PAGE, pagination?.offset)
Expand Down
8 changes: 6 additions & 2 deletions test/unit/adapters/rpc-server/services/get-friends.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ describe('getFriendsService', () => {
})

it('should return the correct list of friends with pagination data', async () => {
const mockFriends = [createMockFriendship('0x123', '0x456'), createMockFriendship('0x123', '0x789')]
const mockFriends = [
createMockFriendship('0x123', '0x456'),
createMockFriendship('0x123', '0x789'),
createMockFriendship('0x987', '0x123')
]
const totalFriends = 2

mockDb.getFriends.mockResolvedValueOnce(mockFriends)
Expand All @@ -27,7 +31,7 @@ describe('getFriendsService', () => {
const response = await getFriends({ pagination: { limit: 10, offset: 0 } }, rpcContext)

expect(response).toEqual({
users: [{ address: '0x456' }, { address: '0x789' }],
users: [{ address: '0x456' }, { address: '0x789' }, { address: '0x987' }],
paginationData: {
total: totalFriends,
page: 1
Expand Down

0 comments on commit 12ee265

Please sign in to comment.