Skip to content

Commit

Permalink
feat: LatestClaimValue method
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Apr 7, 2020
1 parent 12ff8eb commit 8df54b8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/Docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,8 @@ console.log(identity.receivedClaims) // [Claim(type: 'name', value: 'Alice'), ..
```graphql
query {
identity(did: "did:example:123") {
name: latestClaimValue(type: "name")
profilePicture: latestClaimValue(type: "profilePicture")
receivedClaims {
type
value
Expand Down
24 changes: 24 additions & 0 deletions packages/daf-core/src/entities/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,28 @@ export class Identity extends BaseEntity {
claim => claim.subject,
)
receivedClaims: Claim[]

/**
* Convenience method
*
* const name = await identity.getLatestClaimValue({type: 'name'})
*
* @param where
*/
async getLatestClaimValue(where: { type: string }): Promise<String> {
const claim = await Claim.findOne({
where: {
...where,
subject: this.did,
},
order: {
issuanceDate: 'DESC',
},
})
return claim?.value
}

shortDid() {
return `${this.did.slice(0, 15)}...${this.did.slice(-4)}`
}
}
9 changes: 5 additions & 4 deletions packages/daf-core/src/graphql/graphql-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ export const resolvers = {
},

Identity: {
shortId: async (identity: Identity) => `${identity.did.slice(0, 15)}...${identity.did.slice(-4)}`,
profileImage: async (identity: Identity) => null,
shortDid: async (identity: Identity) => (await Identity.findOne(identity.did)).shortDid(),
latestClaimValue: async (identity: Identity, args: { type: string }) =>
(await Identity.findOne(identity.did)).getLatestClaimValue({ type: args.type }),
sentMessages: async (identity: Identity) =>
(await Identity.findOne(identity.did, { relations: ['sentMessages'] })).sentMessages,
receivedMessages: async (identity: Identity) =>
Expand Down Expand Up @@ -263,8 +264,8 @@ export const typeDefs = `
extend type Identity {
shortId: String!
profileImage: String
shortDid: String!
latestClaimValue(type: String): String
sentMessages: [Message]
receivedMessages: [Message]
issuedPresentations: [Presentation]
Expand Down

0 comments on commit 8df54b8

Please sign in to comment.