Skip to content

Publish project.created NATS event on project creation#190

Merged
jeroenrinzema merged 6 commits intomainfrom
copilot/check-nats-event-on-project-creation
Mar 6, 2026
Merged

Publish project.created NATS event on project creation#190
jeroenrinzema merged 6 commits intomainfrom
copilot/check-nats-event-on-project-creation

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 6, 2026

No NATS event was published when a project was created, preventing async processes from reacting to project creation.

Changes

  • Event schema (internal/pubsub/schemas/events.go): Added EventProjectCreated constant, ProjectEvent struct, and ProjectEventsProcess(organizationID) subject function
  • Stream bootstrap (internal/pubsub/consumer/bootstrap.go, consumer.go): Added projects stream and projects-events-process consumer
  • Controller (internal/http/controllers/v1/management/projects.go): Added pubsub.Publisher to ProjectsController; publishes project.created before tx.Commit(). If the publish fails, the request fails and the transaction is rolled back, ensuring consistency between NATS and the database.
  • Tests: Added TestCreateProjectPublishesNATSEvent using a recording publisher to verify event subject, name, and payload. Added TestCreateProjectRollbackOnPublishFailure to verify that a publish failure rolls back the transaction and returns an error.

Event payload

Published to subject projects.events.<organization_id>:

{
  "id": "<project_id>",
  "name": "project.created",
  "organization_id": "<org_id>",
  "data": {
    "name": "My Project",
    "timezone": "America/New_York",
    "locale": "en-US"
  }
}
Original prompt

Please check if an event is published to nats when a new project is created. I would like to be able to trigger other async processes on project creation.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits March 6, 2026 10:58
- Add EventProjectCreated constant and ProjectEvent schema
- Add ProjectEventsProcess subject function
- Add StreamProjects and ConsumerProjectEventsProcess constants
- Bootstrap project events stream and consumer
- Add pubsub.Publisher to ProjectsController
- Publish project.created event in CreateProject handler
- Add test for NATS event publication on project creation

Co-authored-by: jeroenrinzema <3440116+jeroenrinzema@users.noreply.github.com>
The project is already committed to the database when the NATS event
is published, so a publish failure should not fail the HTTP response.

Co-authored-by: jeroenrinzema <3440116+jeroenrinzema@users.noreply.github.com>
Co-authored-by: jeroenrinzema <3440116+jeroenrinzema@users.noreply.github.com>
Copilot AI changed the title [WIP] Check NATS event publication on new project creation Publish project.created NATS event on project creation Mar 6, 2026
The publish now happens inside the transaction scope. If it fails, the
handler returns an error and the deferred tx.Rollback() cleans up.
Added TestCreateProjectRollbackOnPublishFailure to verify this behavior.

Co-authored-by: jeroenrinzema <3440116+jeroenrinzema@users.noreply.github.com>
@jeroenrinzema jeroenrinzema marked this pull request as ready for review March 6, 2026 12:48
@jeroenrinzema jeroenrinzema merged commit 797a60f into main Mar 6, 2026
4 checks passed
@jeroenrinzema jeroenrinzema deleted the copilot/check-nats-event-on-project-creation branch March 6, 2026 12:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants