Skip to content

Commit

Permalink
fix(core): repository event when calling deleteById (openwallet-found…
Browse files Browse the repository at this point in the history
…ation#1356)

Signed-off-by: Ariel Gentile <gentilester@gmail.com>
  • Loading branch information
genaris committed Mar 6, 2023
1 parent c133538 commit 953069a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
9 changes: 8 additions & 1 deletion packages/core/src/storage/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,19 @@ export class Repository<T extends BaseRecord<any, any, any>> {
}

/**
* Delete record by id. Returns null if no record is found
* Delete record by id. Throws {RecordNotFoundError} if no record is found
* @param id the id of the record to delete
* @returns
*/
public async deleteById(agentContext: AgentContext, id: string): Promise<void> {
await this.storageService.deleteById(agentContext, this.recordClass, id)

this.eventEmitter.emit<RecordDeletedEvent<T>>(agentContext, {
type: RepositoryEventTypes.RecordDeleted,
payload: {
record: { id, type: this.recordClass.type },
},
})
}

/** @inheritDoc {StorageService#getById} */
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/storage/RepositoryEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ export interface RecordUpdatedEvent<T extends BaseRecord<any, any, any>> extends
export interface RecordDeletedEvent<T extends BaseRecord<any, any, any>> extends BaseEvent {
type: typeof RepositoryEventTypes.RecordDeleted
payload: {
record: T
record: T | { id: string; type: string }
}
}
22 changes: 22 additions & 0 deletions packages/core/src/storage/__tests__/Repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,28 @@ describe('Repository', () => {

expect(storageMock.deleteById).toBeCalledWith(agentContext, TestRecord, 'test-id')
})

it(`should emit deleted event`, async () => {
const eventListenerMock = jest.fn()
eventEmitter.on<RecordDeletedEvent<TestRecord>>(RepositoryEventTypes.RecordDeleted, eventListenerMock)

const record = getRecord({ id: 'test-id' })

await repository.deleteById(agentContext, record.id)

expect(eventListenerMock).toHaveBeenCalledWith({
type: 'RecordDeleted',
metadata: {
contextCorrelationId: 'mock',
},
payload: {
record: expect.objectContaining({
id: record.id,
type: record.type,
}),
},
})
})
})

describe('getById()', () => {
Expand Down

0 comments on commit 953069a

Please sign in to comment.