Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rest): added filters to getAllCredentials #166

Merged
merged 6 commits into from
Sep 12, 2022

Conversation

jimezesinachi
Copy link
Contributor

No description provided.

@jimezesinachi jimezesinachi requested a review from a team as a code owner September 9, 2022 12:32
@codecov-commenter
Copy link

codecov-commenter commented Sep 9, 2022

Codecov Report

Merging #166 (e0e4031) into main (dcd8a85) will decrease coverage by 0.08%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #166      +/-   ##
==========================================
- Coverage   86.24%   86.15%   -0.09%     
==========================================
  Files          21       21              
  Lines        1163     1163              
  Branches      260      257       -3     
==========================================
- Hits         1003     1002       -1     
- Misses        146      147       +1     
  Partials       14       14              
Impacted Files Coverage Δ
packages/rest/src/routes/routes.ts 86.58% <ø> (ø)
...rc/controllers/connections/ConnectionController.ts 81.48% <100.00%> (-2.45%) ⬇️
...rc/controllers/credentials/CredentialController.ts 87.62% <100.00%> (+0.26%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Signed-off-by: Jim Ezesinachi <jim@animo.id>
Signed-off-by: Jim Ezesinachi <jim@animo.id>
@TimoGlastra TimoGlastra changed the title Issue#157 feat(rest): added filters to getAllCredentials Sep 9, 2022
Copy link
Contributor

@TimoGlastra TimoGlastra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome. Tested locally and it works good! Left some notes on how to make it a bit more efficient when working with large amounts of records

public async getAllCredentials(
@Query('threadId') threadId?: string,
@Query('connectionId') connectionId?: string,
@Query('state') state?: string,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work if we use CredentialState as the type here? Maybe that'll give better validation and documentation in the swagger ui?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

let credentials = await this.agent.credentials.getAll()

if (state) credentials = credentials.filter((c) => c.state === state)
if (threadId) credentials = credentials.filter((c) => c.threadId === threadId)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see this is also done like this in the connections controller, but we should actually filter in the query, to not retrieve records that aren't needed.

We should expose this in AFJ (I'll create an issue for this), but for now we can do it like this:

    const credentialRepository = this.agent.dependencyManager.resolve(CredentialRepository)

    const credentials = await credentialRepository.findByQuery({
      connectionId,
      threadId,
      state,
    })

    return credentials.map((c) => c.toJSON())

When this is merged, could you also make a PR to update this for the connections controller?

@janrtvld FYI

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -39,6 +39,48 @@ describe('CredentialController', () => {
})
})

describe('Get all credentials by state', () => {
test('should return all credentials', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we mock the results, we aren't testing whether te query is correctly applied here.

I think we should spy on the credential repository and see if it was called with the correct parameters:

  describe('Get all credentials by state', () => {
    test('should return all credentials', async () => {
      const credentialRepository = bobAgent.dependencyManager.resolve(CredentialRepository)
      const findByQuerySpy = jest.spyOn(credentialRepository, 'findByQuery').mockResolvedValueOnce([testCredential])

      const response = await request(app).get('/credentials').query({ state: testCredential.state })

      expect(findByQuerySpy).toBeCalledWith({
        state: testCredential.state,
      })

      expect(response.statusCode).toBe(200)
      expect(response.body).toEqual([testCredential].map(objectToJson))
    })
  })

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! I'll make the changes now

Signed-off-by: Jim Ezesinachi <jim@animo.id>
Copy link
Contributor

@TimoGlastra TimoGlastra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 🚀 🚀

Signed-off-by: Jim Ezesinachi <jim@animo.id>
Copy link
Contributor

@TimoGlastra TimoGlastra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@TimoGlastra
Copy link
Contributor

DCO is failing for some commits (seems the last two).

Can you sign off these commits by following these instructions: https://github.com/hyperledger/aries-rfcs/blob/main/contributing.md#how-to-sign-off-previous-commits? Then I'll merge

Signed-off-by: Jim Ezesinachi <jim@animo.id>
Signed-off-by: Jim Ezesinachi <jim@animo.id>
@TimoGlastra TimoGlastra merged commit af7ec19 into openwallet-foundation:main Sep 12, 2022
@jimezesinachi jimezesinachi deleted the issue#157 branch September 13, 2022 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants