Skip to content

Commit

Permalink
Merge pull request #237 from johanbook/journal-fix
Browse files Browse the repository at this point in the history
Ignore non-user issued commands in journal
  • Loading branch information
johanbook authored Aug 8, 2023
2 parents c520fb0 + a961cc1 commit 39d4415
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions services/api/src/client/context/user-id.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Inject, Injectable } from "@nestjs/common";
import { AsyncLocalStorage } from "node:async_hooks";

import { ApplicationError } from "src/core/error-handling";
import { MissingUserIdError } from "src/core/authentication";

import { REQUEST_CONTEXT_KEY } from "./als.module";
import { RequestContext } from "./request-context.interface";
Expand All @@ -17,7 +17,7 @@ export class UserIdService {
const store = this.als.getStore();

if (!store) {
throw new ApplicationError(
throw new MissingUserIdError(
"Unable to obtain user ID due to missing store",
);
}
Expand Down
1 change: 1 addition & 0 deletions services/api/src/core/authentication/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { MissingUserIdError } from "./missing-user-id.error";
3 changes: 3 additions & 0 deletions services/api/src/core/authentication/missing-user-id.error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ApplicationError } from "src/core/error-handling";

export class MissingUserIdError extends ApplicationError {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";

import { UserIdService } from "src/client/context/user-id.service";
import { MissingUserIdError } from "src/core/authentication";

import { JournalEntry } from "../../../infrastructure/entities/journal-entry.entity";
import { CreateJournalEntryCommand } from "../../contracts/commands/create-journal-entry.command";
Expand All @@ -18,7 +19,19 @@ export class CreateJournalEntryHandler
) {}

async execute(command: CreateJournalEntryCommand) {
const userId = this.userIdService.getUserId();
let userId: string;

// User ID will not be available for system-issued commands (e.g. event handlers)
// This skips logging any commands where user id cannot be found
try {
userId = this.userIdService.getUserId();
} catch (error) {
if (error instanceof MissingUserIdError) {
return;
}

throw error;
}

const newJournalEntry = new JournalEntry();

Expand Down

0 comments on commit 39d4415

Please sign in to comment.