-
-
Notifications
You must be signed in to change notification settings - Fork 27
Post updated event serialization #1598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mike182uk
merged 3 commits into
main
from
cursor/BER-3164-post-updated-event-serialization-d792
Feb 18, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,30 @@ | ||
| import type { Post } from '@/post/post.entity'; | ||
| import type { SerializableEvent } from '@/events/event'; | ||
|
|
||
| export class PostUpdatedEvent { | ||
| constructor(private readonly post: Post) {} | ||
| export class PostUpdatedEvent implements SerializableEvent { | ||
| constructor(private readonly postId: number) {} | ||
|
|
||
| getPost(): Post { | ||
| return this.post; | ||
| getPostId(): number { | ||
| return this.postId; | ||
| } | ||
|
|
||
| getName(): string { | ||
| return PostUpdatedEvent.getName(); | ||
| } | ||
|
|
||
| static getName(): string { | ||
| return 'post.updated'; | ||
| } | ||
|
|
||
| toJSON(): Record<string, unknown> { | ||
| return { | ||
| postId: this.postId, | ||
| }; | ||
| } | ||
|
|
||
| static fromJSON(data: Record<string, unknown>): PostUpdatedEvent { | ||
| if (typeof data.postId !== 'number') { | ||
| throw new Error('postId must be a number'); | ||
| } | ||
| return new PostUpdatedEvent(data.postId); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { PostUpdatedEvent } from '@/post/post-updated.event'; | ||
|
|
||
| describe('PostUpdatedEvent', () => { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cursoragent These tests should be consistent with the other events There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Restructured the
|
||
| describe('getName', () => { | ||
| it('should return the event name from static method', () => { | ||
| expect(PostUpdatedEvent.getName()).toBe('post.updated'); | ||
| }); | ||
|
|
||
| it('should return the event name from instance method', () => { | ||
| const event = new PostUpdatedEvent(123); | ||
|
|
||
| expect(event.getName()).toBe('post.updated'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getPostId', () => { | ||
| it('should return the post id', () => { | ||
| const event = new PostUpdatedEvent(123); | ||
|
|
||
| expect(event.getPostId()).toBe(123); | ||
| }); | ||
| }); | ||
|
|
||
| describe('toJSON', () => { | ||
| it('should serialize the event to JSON', () => { | ||
| const event = new PostUpdatedEvent(123); | ||
|
|
||
| expect(event.toJSON()).toEqual({ | ||
| postId: 123, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('fromJSON', () => { | ||
| it('should deserialize the event from JSON', () => { | ||
| const event = PostUpdatedEvent.fromJSON({ | ||
| postId: 123, | ||
| }); | ||
|
|
||
| expect(event.getPostId()).toBe(123); | ||
| }); | ||
|
|
||
| it('should throw an error if postId is missing', () => { | ||
| expect(() => PostUpdatedEvent.fromJSON({})).toThrow( | ||
| 'postId must be a number', | ||
| ); | ||
| }); | ||
|
|
||
| it('should throw an error if postId is not a number', () => { | ||
| expect(() => | ||
| PostUpdatedEvent.fromJSON({ | ||
| postId: 'not a number', | ||
| }), | ||
| ).toThrow('postId must be a number'); | ||
| }); | ||
|
|
||
| it('should throw an error if postId is null', () => { | ||
| expect(() => | ||
| PostUpdatedEvent.fromJSON({ | ||
| postId: null, | ||
| }), | ||
| ).toThrow('postId must be a number'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('round-trip serialization', () => { | ||
| it('should correctly serialize and deserialize', () => { | ||
| const original = new PostUpdatedEvent(999); | ||
| const json = original.toJSON(); | ||
| const restored = PostUpdatedEvent.fromJSON(json); | ||
|
|
||
| expect(restored.getPostId()).toBe(original.getPostId()); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cursoragent lets add the non-static version like in the other events whilst we are here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Taking a look!