Skip to content

Commit

Permalink
Surface when we encounter invalid sequences of events
Browse files Browse the repository at this point in the history
Refs #2100
  • Loading branch information
thewilkybarkid committed Nov 22, 2024
1 parent 9adb976 commit 7a696a8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
34 changes: 24 additions & 10 deletions src/Comments/Queries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Array, Either, Equal, Option, pipe, Record } from 'effect'
import { Array, Data, Either, Equal, Option, pipe, Record } from 'effect'
import type { Orcid } from 'orcid-id-ts'
import type { Uuid } from '../types/index.js'
import type { InputForCommentZenodoRecord } from './Context.js'
Expand Down Expand Up @@ -161,12 +161,17 @@ const buildInputForCommentZenodoRecord = (
return Option.all({ authorId, prereviewId, persona, comment, competingInterests })
}

class UnexpectedSequenceOfEvents extends Data.TaggedError('UnexpectedSequenceOfEvents') {}

export const GetACommentInNeedOfADoi = (
events: ReadonlyArray<{ readonly event: CommentEvent; readonly resourceId: Uuid.Uuid }>,
): Option.Option<{
commentId: Uuid.Uuid
inputForCommentZenodoRecord: InputForCommentZenodoRecord
}> => {
): Either.Either<
Option.Option<{
commentId: Uuid.Uuid
inputForCommentZenodoRecord: InputForCommentZenodoRecord
}>,
UnexpectedSequenceOfEvents
> => {
const hasADoi = new Set()

for (const { event, resourceId } of events.toReversed()) {
Expand All @@ -176,12 +181,21 @@ export const GetACommentInNeedOfADoi = (
}

if (event._tag === 'CommentPublicationWasRequested' && !hasADoi.has(resourceId)) {
return Option.all({
commentId: Option.some(resourceId),
inputForCommentZenodoRecord: buildInputForCommentZenodoRecord(events, resourceId),
})
return pipe(
buildInputForCommentZenodoRecord(events, resourceId),
Option.match({
onNone: () => Either.left(new UnexpectedSequenceOfEvents()),
onSome: inputForCommentZenodoRecord =>
Either.right(
Option.some({
commentId: resourceId,
inputForCommentZenodoRecord,
}),
),
}),
)
}
}

return Option.none()
return Either.right(Option.none())
}
2 changes: 2 additions & 0 deletions src/Comments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export const ReactToCommentEvents: Layer.Layer<
pipe(
eventStore.getAllEvents,
Effect.andThen(events => Queries.GetACommentInNeedOfADoi(events)),
Effect.flatten,
Effect.andThen(
flow(
React.AssignCommentADoiWhenPublicationWasRequested,
Expand Down Expand Up @@ -165,6 +166,7 @@ export const ReactToCommentEvents: Layer.Layer<
pipe(
eventStore.getAllEvents,
Effect.andThen(events => Queries.GetACommentInNeedOfADoi(events)),
Effect.flatten,
Effect.andThen(React.AssignCommentADoiWhenPublicationWasRequested),
Effect.tapError(() =>
Effect.annotateLogs(Effect.logError('ReactToCommentEvents failed'), { commentId }),
Expand Down
8 changes: 5 additions & 3 deletions test/Comments/Queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,9 @@ describe('GetACommentInNeedOfADoi', () => {
const actual = _.GetACommentInNeedOfADoi(Array.map(events, event => ({ event, resourceId })))

expect(actual).toStrictEqual(
Option.some({ commentId: resourceId, inputForCommentZenodoRecord: expectedInputForCommentZenodoRecord }),
Either.right(
Option.some({ commentId: resourceId, inputForCommentZenodoRecord: expectedInputForCommentZenodoRecord }),
),
)
})

Expand All @@ -357,14 +359,14 @@ describe('GetACommentInNeedOfADoi', () => {

const actual = _.GetACommentInNeedOfADoi(Array.map(events, event => ({ event, resourceId })))

expect(actual).toStrictEqual(Option.none())
expect(actual).toStrictEqual(Either.right(Option.none()))
})

test('ignores comments for which publication has not been requested', () => {
const actual = _.GetACommentInNeedOfADoi(
Array.map(eventsNeededToRequestPublication, event => ({ event, resourceId })),
)

expect(actual).toStrictEqual(Option.none())
expect(actual).toStrictEqual(Either.right(Option.none()))
})
})

0 comments on commit 7a696a8

Please sign in to comment.