Skip to content

Commit

Permalink
refactor: delete credentials by default when deleting exchange (#767)
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Richardson <[email protected]>

BREAKING CHANGE: the credentials associated with a credential exchange record are now deleted by default when deleting a credential exchange record. If you only want to delete the credential exchange record and not the associated credentials, you can pass the deleteAssociatedCredentials to the deleteById method:

```ts
await agent.credentials.deleteById('credentialExchangeId', { 
	deleteAssociatedCredentials: false 
})
```
  • Loading branch information
NB-MikeRichardson authored May 27, 2022
1 parent c03c0a3 commit 656ed73
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -816,8 +816,7 @@ describe('CredentialService', () => {
})

it('deleteAssociatedCredential parameter should call deleteCredential in indyHolderService with credentialId', async () => {
const deleteCredentialMock = indyHolderService.deleteCredential as jest.Mock<Promise<void>, [string]>

const deleteCredentialMock = mockFunction(indyHolderService.deleteCredential)
const credentialRecord = mockCredentialRecord()
mockFunction(credentialRepository.getById).mockReturnValue(Promise.resolve(credentialRecord))

Expand All @@ -826,6 +825,17 @@ describe('CredentialService', () => {
})
expect(deleteCredentialMock).toHaveBeenNthCalledWith(1, credentialRecord.credentials[0].credentialRecordId)
})

it('deleteAssociatedCredentials not set - defaults to true , credential still deleted by default', async () => {
const deleteCredentialMock = mockFunction(indyHolderService.deleteCredential)

const credentialRecord = mockCredentialRecord()
mockFunction(credentialRepository.getById).mockReturnValue(Promise.resolve(credentialRecord))

// deleteAssociatedCredentials not set - defaults to true
await credentialService.delete(credentialRecord)
expect(deleteCredentialMock).toHaveBeenNthCalledWith(1, credentialRecord.credentials[0].credentialRecordId)
})
})

describe('declineOffer', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -806,15 +806,26 @@ describe('CredentialService', () => {
})

it('deleteAssociatedCredential parameter should call deleteCredential in indyHolderService with credentialId', async () => {
const storeCredentialMock = indyHolderService.deleteCredential as jest.Mock<Promise<void>, [string]>
const deleteCredentialMock = mockFunction(indyHolderService.deleteCredential)

const credentialRecord = mockCredentialRecord()
mockFunction(credentialRepository.getById).mockReturnValue(Promise.resolve(credentialRecord))

await credentialService.delete(credentialRecord, {
deleteAssociatedCredentials: true,
})
expect(storeCredentialMock).toHaveBeenNthCalledWith(1, credentialRecord.credentials[0].credentialRecordId)
expect(deleteCredentialMock).toHaveBeenNthCalledWith(1, credentialRecord.credentials[0].credentialRecordId)
})

it('deleteAssociatedCredentials not set - defaults to true , credential still deleted by default', async () => {
const deleteCredentialMock = mockFunction(indyHolderService.deleteCredential)

const credentialRecord = mockCredentialRecord()
mockFunction(credentialRepository.getById).mockReturnValue(Promise.resolve(credentialRecord))

// deleteAssociatedCredentials not set - defaults to true
await credentialService.delete(credentialRecord)
expect(deleteCredentialMock).toHaveBeenNthCalledWith(1, credentialRecord.credentials[0].credentialRecordId)
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { InboundMessageContext } from '../../../agent/models/InboundMessage
import type { Logger } from '../../../logger'
import type { DidCommMessageRepository } from '../../../storage'
import type { MediationRecipientService } from '../../routing'
import type { CredentialFormatService } from '../formats/CredentialFormatService'
import type { DidResolverService } from './../../dids/services/DidResolverService'
import type { CredentialStateChangedEvent } from './../CredentialEvents'
import type { CredentialProtocolVersion } from './../CredentialProtocolVersion'
Expand All @@ -24,7 +25,6 @@ import type {
NegotiateProposalOptions,
ProposeCredentialOptions,
} from './../CredentialsModuleOptions'
import type { CredentialFormatService } from './../formats/CredentialFormatService'
import type { CredentialFormats, HandlerAutoAcceptOptions } from './../formats/models/CredentialFormatServiceOptions'
import type {
V1CredentialProblemReportMessage,
Expand Down Expand Up @@ -252,7 +252,9 @@ export abstract class CredentialService {
public async delete(credentialRecord: CredentialExchangeRecord, options?: DeleteCredentialOptions): Promise<void> {
await this.credentialRepository.delete(credentialRecord)

if (options?.deleteAssociatedCredentials) {
const deleteAssociatedCredentials = options?.deleteAssociatedCredentials ?? true

if (deleteAssociatedCredentials) {
for (const credential of credentialRecord.credentials) {
const formatService: CredentialFormatService = this.getFormatService(credential.credentialRecordType)
await formatService.deleteCredentialById(credential.credentialRecordId)
Expand Down

0 comments on commit 656ed73

Please sign in to comment.