Skip to content

Commit

Permalink
Merge pull request #7 from fagbokforlaget/fix-data-can-be-null
Browse files Browse the repository at this point in the history
  • Loading branch information
nataszczypiora committed Jul 27, 2023
2 parents 46f3280 + 832bb81 commit b23046f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/async-logger-provider.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export abstract class AsyncLoggerProvider {
abstract log(subject: string, data: Record<string, unknown>): void;
abstract log(subject: string, data: Record<string, unknown>): Promise<void>;
}
2 changes: 0 additions & 2 deletions src/message-dispatcher.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ export class MessageDispatcherInterceptor implements NestInterceptor {

return next.handle().pipe(
tap(async (data) => {
if (!data) return;

const request = context.switchToHttp().getRequest();

let ids = options.objectIdGetter(request, data);
Expand Down
24 changes: 23 additions & 1 deletion test/message-dispatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('Message Dispatcher', () => {
});
});

describe('for nonexisting object', () => {
describe('for nonexisting data object', () => {
it('should not create nor send a message on nonexistant id', async () => {
const res = await request(app.getHttpServer())
.get(`/test/nodata`)
Expand All @@ -109,6 +109,28 @@ describe('Message Dispatcher', () => {
expect(res.text).toEqual('');
expect(transport.log).not.toHaveBeenCalled();
});

it('should send message when no data returned but object is taken from request', async () => {
const id = '123';
const res = await request(app.getHttpServer())
.get(`/test/noreturn/${id}`)
.expect(204);

expect(res.text).toEqual('');
expect(transport.log).toHaveBeenCalledWith(subject, {
action: { type: 'urn:forlagshuset:action:object', verb: 'deleted' },
object: {
id,
type: 'urn:forlagshuset:object:erudio:namespace',
},
payload: undefined,
service: {
id: serviceId,
type: 'urn:forlagshuset:service:app',
},
timestamp: expect.anything(),
});
});
});
});
});
15 changes: 14 additions & 1 deletion test/mocks/message-dispatcher-test.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get, Param } from '@nestjs/common';
import { Controller, Get, HttpCode, Param, Req } from '@nestjs/common';
import {
Action,
MessageEventEmitter,
Expand Down Expand Up @@ -33,6 +33,19 @@ export class MessageDispatcherTestController {
return;
}

@MessageEventEmitter({
objectIdGetter: (request) => request.obj,
action: Action.DELETED,
})
@Get('/noreturn/:id')
@HttpCode(204)
async testNoReturn(
@Param() params: { id: string },
@Req() req: any,
): Promise<void> {
req.obj = params.id;
}

@MessageEventEmitter({
objectIdGetter: (request) => request.params.id,
action: Action.CREATED,
Expand Down

0 comments on commit b23046f

Please sign in to comment.