Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into feat/json-tranform-…
Browse files Browse the repository at this point in the history
…validator
  • Loading branch information
Moriarty committed Jun 17, 2022
2 parents d8174fc + 1f8b6ab commit ff330a2
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ export interface CredentialProtocolMsgReturnType<MessageType extends AgentMessag

export interface DeleteCredentialOptions {
deleteAssociatedCredentials: boolean
deleteAssociatedDidCommMessages: boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ import { V1CredentialPreview } from './messages/V1CredentialPreview'
export class V1CredentialService extends CredentialService<[IndyCredentialFormat]> {
private connectionService: ConnectionService
private formatService: IndyCredentialFormatService
private didCommMessageRepository: DidCommMessageRepository
private mediationRecipientService: MediationRecipientService

public constructor(
Expand All @@ -74,7 +73,7 @@ export class V1CredentialService extends CredentialService<[IndyCredentialFormat
credentialRepository: CredentialRepository,
formatService: IndyCredentialFormatService
) {
super(credentialRepository, eventEmitter, dispatcher, agentConfig)
super(credentialRepository, didCommMessageRepository, eventEmitter, dispatcher, agentConfig)
this.connectionService = connectionService
this.formatService = formatService
this.didCommMessageRepository = didCommMessageRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { EventEmitter } from '../../../../../agent/EventEmitter'
import { InboundMessageContext } from '../../../../../agent/models/InboundMessageContext'
import { Attachment, AttachmentData } from '../../../../../decorators/attachment/Attachment'
import { AriesFrameworkError } from '../../../../../error'
import { DidCommMessageRole } from '../../../../../storage'
import { DidCommMessageRecord, DidCommMessageRole } from '../../../../../storage'
import { DidCommMessageRepository } from '../../../../../storage/didcomm/DidCommMessageRepository'
import { JsonTransformer } from '../../../../../utils'
import { JsonEncoder } from '../../../../../utils/JsonEncoder'
Expand Down Expand Up @@ -125,6 +125,12 @@ const credentialIssueMessage = new V1IssueCredentialMessage({
credentialAttachments: [offerAttachment],
})

const didCommMessageRecord = new DidCommMessageRecord({
associatedRecordId: '04a2c382-999e-4de9-a1d2-9dec0b2fa5e4',
message: {},
role: DidCommMessageRole.Receiver,
})

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const getAgentMessageMock = async (options: GetAgentMessageOptions<any>) => {
if (options.messageClass === V1ProposeCredentialMessage) {
Expand Down Expand Up @@ -223,6 +229,11 @@ describe('V1CredentialService', () => {
mockFunction(connectionService.getById).mockResolvedValue(connection)
mockFunction(didCommMessageRepository.findAgentMessage).mockImplementation(getAgentMessageMock)
mockFunction(didCommMessageRepository.getAgentMessage).mockImplementation(getAgentMessageMock)
mockFunction(didCommMessageRepository.findByQuery).mockResolvedValue([
didCommMessageRecord,
didCommMessageRecord,
didCommMessageRecord,
])

credentialService = new V1CredentialService(
connectionService,
Expand Down Expand Up @@ -820,6 +831,7 @@ describe('V1CredentialService', () => {

await credentialService.delete(credentialRecord, {
deleteAssociatedCredentials: true,
deleteAssociatedDidCommMessages: false,
})

expect(deleteCredentialMock).toHaveBeenNthCalledWith(1, credentialRecord.credentials[0].credentialRecordId)
Expand All @@ -833,6 +845,7 @@ describe('V1CredentialService', () => {

await credentialService.delete(credentialRecord, {
deleteAssociatedCredentials: false,
deleteAssociatedDidCommMessages: false,
})

expect(deleteCredentialMock).not.toHaveBeenCalled()
Expand All @@ -848,6 +861,17 @@ describe('V1CredentialService', () => {

expect(deleteCredentialMock).toHaveBeenNthCalledWith(1, credentialRecord.credentials[0].credentialRecordId)
})
it('deleteAssociatedDidCommMessages should default to true', async () => {
const deleteCredentialMock = mockFunction(indyCredentialFormatService.deleteCredentialById)

const credentialRecord = mockCredentialRecord()
mockFunction(credentialRepository.getById).mockResolvedValue(credentialRecord)

await credentialService.delete(credentialRecord)

expect(deleteCredentialMock).toHaveBeenNthCalledWith(1, credentialRecord.credentials[0].credentialRecordId)
expect(didCommMessageRepository.delete).toHaveBeenCalledTimes(3)
})
})

describe('declineOffer', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import {
export class V2CredentialService<CFs extends CredentialFormat[] = CredentialFormat[]> extends CredentialService<CFs> {
private connectionService: ConnectionService
private credentialFormatCoordinator: CredentialFormatCoordinator<CFs>
private didCommMessageRepository: DidCommMessageRepository
protected didCommMessageRepository: DidCommMessageRepository
private mediationRecipientService: MediationRecipientService
private formatServiceMap: { [key: string]: CredentialFormatService }

Expand All @@ -74,7 +74,7 @@ export class V2CredentialService<CFs extends CredentialFormat[] = CredentialForm
credentialRepository: CredentialRepository,
indyCredentialFormatService: IndyCredentialFormatService
) {
super(credentialRepository, eventEmitter, dispatcher, agentConfig)
super(credentialRepository, didCommMessageRepository, eventEmitter, dispatcher, agentConfig)
this.connectionService = connectionService
this.didCommMessageRepository = didCommMessageRepository
this.mediationRecipientService = mediationRecipientService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Dispatcher } from '../../../../../agent/Dispatcher'
import { EventEmitter } from '../../../../../agent/EventEmitter'
import { InboundMessageContext } from '../../../../../agent/models/InboundMessageContext'
import { Attachment, AttachmentData } from '../../../../../decorators/attachment/Attachment'
import { DidCommMessageRepository } from '../../../../../storage'
import { DidCommMessageRecord, DidCommMessageRole, DidCommMessageRepository } from '../../../../../storage'
import { JsonTransformer } from '../../../../../utils'
import { JsonEncoder } from '../../../../../utils/JsonEncoder'
import { AckStatus } from '../../../../common/messages/AckMessage'
Expand Down Expand Up @@ -151,6 +151,12 @@ const credentialIssueMessage = new V2IssueCredentialMessage({
})
credentialIssueMessage.setThread({ threadId: 'somethreadid' })

const didCommMessageRecord = new DidCommMessageRecord({
associatedRecordId: '04a2c382-999e-4de9-a1d2-9dec0b2fa5e4',
message: {},
role: DidCommMessageRole.Receiver,
})

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const getAgentMessageMock = async (options: GetAgentMessageOptions<any>) => {
if (options.messageClass === V2ProposeCredentialMessage) {
Expand Down Expand Up @@ -236,6 +242,11 @@ describe('CredentialService', () => {
mockFunction(connectionService.getById).mockResolvedValue(connection)
mockFunction(didCommMessageRepository.findAgentMessage).mockImplementation(getAgentMessageMock)
mockFunction(didCommMessageRepository.getAgentMessage).mockImplementation(getAgentMessageMock)
mockFunction(didCommMessageRepository.findByQuery).mockResolvedValue([
didCommMessageRecord,
didCommMessageRecord,
didCommMessageRecord,
])

credentialService = new V2CredentialService(
connectionService,
Expand Down Expand Up @@ -764,6 +775,7 @@ describe('CredentialService', () => {

await credentialService.delete(credentialRecord, {
deleteAssociatedCredentials: true,
deleteAssociatedDidCommMessages: false,
})

expect(deleteCredentialMock).toHaveBeenNthCalledWith(1, credentialRecord.credentials[0].credentialRecordId)
Expand All @@ -777,6 +789,7 @@ describe('CredentialService', () => {

await credentialService.delete(credentialRecord, {
deleteAssociatedCredentials: false,
deleteAssociatedDidCommMessages: false,
})

expect(deleteCredentialMock).not.toHaveBeenCalled()
Expand All @@ -792,6 +805,17 @@ describe('CredentialService', () => {

expect(deleteCredentialMock).toHaveBeenNthCalledWith(1, credentialRecord.credentials[0].credentialRecordId)
})
it('deleteAssociatedDidCommMessages should default to true', async () => {
const deleteCredentialMock = mockFunction(indyCredentialFormatService.deleteCredentialById)

const credentialRecord = mockCredentialRecord()
mockFunction(credentialRepository.getById).mockResolvedValue(credentialRecord)

await credentialService.delete(credentialRecord)

expect(deleteCredentialMock).toHaveBeenNthCalledWith(1, credentialRecord.credentials[0].credentialRecordId)
expect(didCommMessageRepository.delete).toHaveBeenCalledTimes(3)
})
})

describe('declineOffer', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,10 @@ describe('v2 credentials', () => {
const holderService = aliceAgent.injectionContainer.resolve(IndyHolderService)

const deleteCredentialSpy = jest.spyOn(holderService, 'deleteCredential')
await aliceAgent.credentials.deleteById(holderCredential.id, { deleteAssociatedCredentials: true })
await aliceAgent.credentials.deleteById(holderCredential.id, {
deleteAssociatedCredentials: true,
deleteAssociatedDidCommMessages: true,
})
expect(deleteCredentialSpy).toHaveBeenNthCalledWith(1, holderCredential.credentials[0].credentialRecordId)

return expect(aliceAgent.credentials.getById(holderCredential.id)).rejects.toThrowError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Dispatcher } from '../../../agent/Dispatcher'
import type { EventEmitter } from '../../../agent/EventEmitter'
import type { InboundMessageContext } from '../../../agent/models/InboundMessageContext'
import type { Logger } from '../../../logger'
import type { DidCommMessageRepository } from '../../../storage'
import type { ProblemReportMessage } from '../../problem-reports'
import type { CredentialStateChangedEvent } from '../CredentialEvents'
import type {
Expand All @@ -30,18 +31,21 @@ import { CredentialEventTypes } from './../CredentialEvents'

export abstract class CredentialService<CFs extends CredentialFormat[]> {
protected credentialRepository: CredentialRepository
protected didCommMessageRepository: DidCommMessageRepository
protected eventEmitter: EventEmitter
protected dispatcher: Dispatcher
protected agentConfig: AgentConfig
protected logger: Logger

public constructor(
credentialRepository: CredentialRepository,
didCommMessageRepository: DidCommMessageRepository,
eventEmitter: EventEmitter,
dispatcher: Dispatcher,
agentConfig: AgentConfig
) {
this.credentialRepository = credentialRepository
this.didCommMessageRepository = didCommMessageRepository
this.eventEmitter = eventEmitter
this.dispatcher = dispatcher
this.agentConfig = agentConfig
Expand Down Expand Up @@ -190,13 +194,23 @@ export abstract class CredentialService<CFs extends CredentialFormat[]> {
await this.credentialRepository.delete(credentialRecord)

const deleteAssociatedCredentials = options?.deleteAssociatedCredentials ?? true
const deleteAssociatedDidCommMessages = options?.deleteAssociatedDidCommMessages ?? true

if (deleteAssociatedCredentials) {
for (const credential of credentialRecord.credentials) {
const formatService = this.getFormatServiceForRecordType(credential.credentialRecordType)
await formatService.deleteCredentialById(credential.credentialRecordId)
}
}

if (deleteAssociatedDidCommMessages) {
const didCommMessages = await this.didCommMessageRepository.findByQuery({
associatedRecordId: credentialRecord.id,
})
for (const didCommMessage of didCommMessages) {
await this.didCommMessageRepository.delete(didCommMessage)
}
}
}

/**
Expand Down

0 comments on commit ff330a2

Please sign in to comment.