Skip to content

Commit

Permalink
chore: fixes after revert
Browse files Browse the repository at this point in the history
  • Loading branch information
nklomp committed Mar 29, 2023
1 parent 7c555dc commit d77ff41
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
82 changes: 82 additions & 0 deletions packages/data-store/src/__tests__/contact.store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,47 @@ describe('Database entities test', () => {
expect(result.length).toEqual(1)
})

it('should get whole contacts by filter', async () => {
const contact = {
name: 'test_name',
alias: 'test_alias',
uri: 'example.com',
identities: [
{
alias: 'test_alias1',
roles: [IdentityRoleEnum.ISSUER],
identifier: {
type: CorrelationIdentifierEnum.DID,
correlationId: 'example_did1',
},
},
{
alias: 'test_alias2',
roles: [IdentityRoleEnum.VERIFIER],
identifier: {
type: CorrelationIdentifierEnum.DID,
correlationId: 'example_did2',
},
},
],
}
const savedContact = await contactStore.addContact(contact)
expect(savedContact).toBeDefined()

const args = {
filter: [{
identities: {
identifier: {
correlationId: 'example_did1',
}
}
}],
}
const result = await contactStore.getContacts(args)

expect(result[0].identities.length).toEqual(2)
})

it('should get contacts by name', async () => {
const contact = {
name: 'test_name',
Expand Down Expand Up @@ -475,6 +516,47 @@ describe('Database entities test', () => {
expect(result.length).toEqual(1)
})

it('should get whole identities by filter', async () => {
const contact = {
name: 'test_name',
alias: 'test_alias',
uri: 'example.com',
}
const savedContact = await contactStore.addContact(contact)
expect(savedContact).toBeDefined()

const alias = 'test_alias1'
const identity1 = {
alias,
roles: [IdentityRoleEnum.ISSUER, IdentityRoleEnum.VERIFIER],
identifier: {
type: CorrelationIdentifierEnum.DID,
correlationId: 'example_did1',
},
metadata: [
{
label: 'label1',
value: 'example_value',
},
{
label: 'label2',
value: 'example_value',
},
],
}
const savedIdentity1 = await contactStore.addIdentity({ contactId: savedContact.id, identity: identity1 })
expect(savedIdentity1).toBeDefined()

const args = {
filter: [{ metadata: { label: 'label1' } }],
}

const result = await contactStore.getIdentities(args)

expect(result[0]).toBeDefined()
expect(result[0].metadata!.length).toEqual(2)
})

it('should add identity to contact', async () => {
const contact = {
name: 'test_name',
Expand Down
18 changes: 15 additions & 3 deletions packages/data-store/src/contact/ContactStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { ConnectionEntity } from '../entities/contact/ConnectionEntity'
import { BaseConfigEntity } from '../entities/contact/BaseConfigEntity'
import { OpenIdConfigEntity } from '../entities/contact/OpenIdConfigEntity'
import { DidAuthConfigEntity } from '../entities/contact/DidAuthConfigEntity'
import { DataSource } from 'typeorm'
import { DataSource, In } from 'typeorm'

const debug = Debug('sphereon:typeorm:contact-store')

Expand All @@ -59,10 +59,16 @@ export class ContactStore extends AbstractContactStore {
}

getContacts = async (args?: IGetContactsArgs): Promise<Array<IContact>> => {
const result = await (await this.dbConnection).getRepository(ContactEntity).find({
const initialResult = await (await this.dbConnection).getRepository(ContactEntity).find({
...(args?.filter && { where: args?.filter }),
})

const result = await (await this.dbConnection).getRepository(ContactEntity).find({
where: {
id: In(initialResult.map((contact: ContactEntity) => contact.id))
}
})

return result.map((contact: ContactEntity) => this.contactFrom(contact))
}

Expand Down Expand Up @@ -137,10 +143,16 @@ export class ContactStore extends AbstractContactStore {
}

getIdentities = async (args?: IGetIdentitiesArgs): Promise<Array<IIdentity>> => {
const result = await (await this.dbConnection).getRepository(IdentityEntity).find({
const initialResult = await (await this.dbConnection).getRepository(IdentityEntity).find({
...(args?.filter && { where: args?.filter }),
})

const result = await (await this.dbConnection).getRepository(IdentityEntity).find({
where: {
id: In(initialResult.map((identity: IdentityEntity) => identity.id))
}
})

return result.map((identity: IdentityEntity) => this.identityFrom(identity))
}

Expand Down

0 comments on commit d77ff41

Please sign in to comment.