-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Novu notification system (backend + frontend + docs) #49
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
Open
teetangh
wants to merge
1
commit into
dev
Choose a base branch
from
feat/novu-notification-system
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,312 @@ | ||
| import 'package:backend/services/novu/novu_service.dart'; | ||
| import 'package:backend/services/novu/novu_workflows.dart'; | ||
|
|
||
| /// Static notification trigger functions for fire-and-forget use. | ||
| /// | ||
| /// Each method builds the payload for a specific Novu workflow and delegates | ||
| /// to [NovuService.triggerWorkflow]. All methods return a `bool` indicating | ||
| /// whether the trigger succeeded. | ||
| /// | ||
| /// Usage: | ||
| /// ```dart | ||
| /// unawaited(NotificationTriggers.appointmentBooked(novuService, ...)); | ||
| /// ``` | ||
| class NotificationTriggers { | ||
| NotificationTriggers._(); // prevent instantiation | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Appointment workflows | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| /// Notify the **consultant** that a booking has been scheduled. | ||
| static Future<bool> appointmentBooked( | ||
| NovuService service, { | ||
| required String consultantUserId, | ||
| required String consulteeUserName, | ||
| required String appointmentType, | ||
| required String appointmentDate, | ||
| String? planTitle, | ||
| String? appointmentId, | ||
| }) async { | ||
| final payload = <String, dynamic>{ | ||
| 'consulteeUserName': consulteeUserName, | ||
| 'appointmentType': appointmentType, | ||
| 'appointmentDate': appointmentDate, | ||
| }; | ||
| if (planTitle != null) payload['planTitle'] = planTitle; | ||
| if (appointmentId != null) payload['appointmentId'] = appointmentId; | ||
|
|
||
| return service.triggerWorkflow( | ||
| workflowId: NovuWorkflows.appointmentBooked, | ||
| subscriberId: consultantUserId, | ||
| payload: payload, | ||
| transactionId: | ||
| appointmentId != null ? 'appt-booked-$appointmentId' : null, | ||
| ); | ||
| } | ||
|
|
||
| /// Notify the **other party** that an appointment has been cancelled. | ||
| static Future<bool> appointmentCancelled( | ||
| NovuService service, { | ||
| required String recipientUserId, | ||
| required String cancelledByName, | ||
| required String appointmentType, | ||
| required String appointmentDate, | ||
| String? reason, | ||
| String? appointmentId, | ||
| }) async { | ||
| final payload = <String, dynamic>{ | ||
| 'cancelledByName': cancelledByName, | ||
| 'appointmentType': appointmentType, | ||
| 'appointmentDate': appointmentDate, | ||
| }; | ||
| if (reason != null) payload['reason'] = reason; | ||
| if (appointmentId != null) payload['appointmentId'] = appointmentId; | ||
|
|
||
| return service.triggerWorkflow( | ||
| workflowId: NovuWorkflows.appointmentCancelled, | ||
| subscriberId: recipientUserId, | ||
| payload: payload, | ||
| transactionId: | ||
| appointmentId != null ? 'appt-cancelled-$appointmentId' : null, | ||
| ); | ||
| } | ||
|
|
||
| /// Notify the **other party** that an appointment has been rescheduled. | ||
| static Future<bool> appointmentRescheduled( | ||
| NovuService service, { | ||
| required String recipientUserId, | ||
| required String rescheduledByName, | ||
| required String appointmentType, | ||
| required String originalDate, | ||
| String? appointmentId, | ||
| }) async { | ||
| final payload = <String, dynamic>{ | ||
| 'rescheduledByName': rescheduledByName, | ||
| 'appointmentType': appointmentType, | ||
| 'originalDate': originalDate, | ||
| }; | ||
| if (appointmentId != null) payload['appointmentId'] = appointmentId; | ||
|
|
||
| return service.triggerWorkflow( | ||
| workflowId: NovuWorkflows.appointmentRescheduled, | ||
| subscriberId: recipientUserId, | ||
| payload: payload, | ||
| transactionId: | ||
| appointmentId != null ? 'appt-rescheduled-$appointmentId' : null, | ||
| ); | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Payment workflows | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| /// Notify the **consultee** of a successful payment. | ||
| static Future<bool> paymentSuccess( | ||
| NovuService service, { | ||
| required String consulteeUserId, | ||
| required String amount, | ||
| required String currency, | ||
| String? appointmentType, | ||
| String? consultantName, | ||
| String? paymentId, | ||
| }) async { | ||
| final payload = <String, dynamic>{ | ||
| 'amount': amount, | ||
| 'currency': currency, | ||
| }; | ||
| if (appointmentType != null) payload['appointmentType'] = appointmentType; | ||
| if (consultantName != null) payload['consultantName'] = consultantName; | ||
| if (paymentId != null) payload['paymentId'] = paymentId; | ||
|
|
||
| return service.triggerWorkflow( | ||
| workflowId: NovuWorkflows.paymentSuccess, | ||
| subscriberId: consulteeUserId, | ||
| payload: payload, | ||
| transactionId: paymentId != null ? 'pay-success-$paymentId' : null, | ||
| ); | ||
| } | ||
|
|
||
| /// Notify the **consultee** of a failed payment. | ||
| static Future<bool> paymentFailed( | ||
| NovuService service, { | ||
| required String consulteeUserId, | ||
| required String amount, | ||
| required String currency, | ||
| String? reason, | ||
| String? paymentId, | ||
| }) async { | ||
| final payload = <String, dynamic>{ | ||
| 'amount': amount, | ||
| 'currency': currency, | ||
| }; | ||
| if (reason != null) payload['reason'] = reason; | ||
| if (paymentId != null) payload['paymentId'] = paymentId; | ||
|
|
||
| return service.triggerWorkflow( | ||
| workflowId: NovuWorkflows.paymentFailed, | ||
| subscriberId: consulteeUserId, | ||
| payload: payload, | ||
| transactionId: paymentId != null ? 'pay-failed-$paymentId' : null, | ||
| ); | ||
| } | ||
|
|
||
| /// Notify the **consultee** that a refund has been processed. | ||
| static Future<bool> refundProcessed( | ||
| NovuService service, { | ||
| required String consulteeUserId, | ||
| required String amount, | ||
| required String currency, | ||
| String? reason, | ||
| String? refundId, | ||
| }) async { | ||
| final payload = <String, dynamic>{ | ||
| 'amount': amount, | ||
| 'currency': currency, | ||
| }; | ||
| if (reason != null) payload['reason'] = reason; | ||
| if (refundId != null) payload['refundId'] = refundId; | ||
|
|
||
| return service.triggerWorkflow( | ||
| workflowId: NovuWorkflows.refundProcessed, | ||
| subscriberId: consulteeUserId, | ||
| payload: payload, | ||
| transactionId: refundId != null ? 'refund-$refundId' : null, | ||
| ); | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Booking request workflow | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| /// Notify the **consultant** of a new pending booking request. | ||
| static Future<bool> newBookingRequest( | ||
| NovuService service, { | ||
| required String consultantUserId, | ||
| required String consulteeUserName, | ||
| required String appointmentType, | ||
| String? message, | ||
| String? appointmentId, | ||
| }) async { | ||
| final payload = <String, dynamic>{ | ||
| 'consulteeUserName': consulteeUserName, | ||
| 'appointmentType': appointmentType, | ||
| }; | ||
| if (message != null) payload['message'] = message; | ||
| if (appointmentId != null) payload['appointmentId'] = appointmentId; | ||
|
|
||
| return service.triggerWorkflow( | ||
| workflowId: NovuWorkflows.newBookingRequest, | ||
| subscriberId: consultantUserId, | ||
| payload: payload, | ||
| transactionId: | ||
| appointmentId != null ? 'booking-req-$appointmentId' : null, | ||
| ); | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Review workflow | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| /// Notify the **consultant** that they received a new review. | ||
| static Future<bool> newReviewReceived( | ||
| NovuService service, { | ||
| required String consultantUserId, | ||
| required String reviewerName, | ||
| required int rating, | ||
| String? reviewText, | ||
| }) async { | ||
| final payload = <String, dynamic>{ | ||
| 'reviewerName': reviewerName, | ||
| 'rating': rating, | ||
| }; | ||
| if (reviewText != null) payload['reviewText'] = reviewText; | ||
|
|
||
| return service.triggerWorkflow( | ||
| workflowId: NovuWorkflows.newReviewReceived, | ||
| subscriberId: consultantUserId, | ||
| payload: payload, | ||
| ); | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Support workflow | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| /// Send the **user** a confirmation after creating a support ticket. | ||
| static Future<bool> supportTicketCreated( | ||
| NovuService service, { | ||
| required String userId, | ||
| required String ticketTitle, | ||
| required String ticketId, | ||
| }) async { | ||
| final payload = <String, dynamic>{ | ||
| 'ticketTitle': ticketTitle, | ||
| 'ticketId': ticketId, | ||
| }; | ||
|
|
||
| return service.triggerWorkflow( | ||
| workflowId: NovuWorkflows.supportTicketCreated, | ||
| subscriberId: userId, | ||
| payload: payload, | ||
| transactionId: 'support-$ticketId', | ||
| ); | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Feedback workflow | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| /// Notify the **admin** that new feedback has been submitted. | ||
| static Future<bool> feedbackReceived( | ||
| NovuService service, { | ||
| required String adminUserId, | ||
| required String userName, | ||
| required String feedbackTitle, | ||
| String? category, | ||
| int? rating, | ||
| }) async { | ||
| final payload = <String, dynamic>{ | ||
| 'userName': userName, | ||
| 'feedbackTitle': feedbackTitle, | ||
| }; | ||
| if (category != null) payload['category'] = category; | ||
| if (rating != null) payload['rating'] = rating; | ||
|
|
||
| return service.triggerWorkflow( | ||
| workflowId: NovuWorkflows.feedbackReceived, | ||
| subscriberId: adminUserId, | ||
| payload: payload, | ||
| ); | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Dispute workflow | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| /// Notify the **consultant** that a dispute has been created. | ||
| static Future<bool> disputeCreated( | ||
| NovuService service, { | ||
| required String consultantUserId, | ||
| required String disputeId, | ||
| required String amount, | ||
| required String currency, | ||
| String? reason, | ||
| String? dueBy, | ||
| }) async { | ||
| final payload = <String, dynamic>{ | ||
| 'disputeId': disputeId, | ||
| 'amount': amount, | ||
| 'currency': currency, | ||
| }; | ||
| if (reason != null) payload['reason'] = reason; | ||
| if (dueBy != null) payload['dueBy'] = dueBy; | ||
|
|
||
| return service.triggerWorkflow( | ||
| workflowId: NovuWorkflows.disputeCreated, | ||
| subscriberId: consultantUserId, | ||
| payload: payload, | ||
| transactionId: 'dispute-$disputeId', | ||
| ); | ||
| } | ||
| } | ||
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,35 @@ | ||
| import 'dart:io'; | ||
|
|
||
| /// Novu configuration loaded from environment variables. | ||
| /// | ||
| /// Required env vars: | ||
| /// - `NOVU_SECRET_KEY` — Backend API key (server-side only) | ||
| /// - `NOVU_APP_ID` — Application identifier | ||
| /// | ||
| /// Optional env vars: | ||
| /// - `NOVU_API_URL` — Defaults to `https://api.novu.co/v1` | ||
| class NovuConfig { | ||
| /// Creates a [NovuConfig], falling back to environment variables when | ||
| /// explicit values are not provided. | ||
| NovuConfig({ | ||
| String? secretKey, | ||
| String? apiUrl, | ||
| String? appId, | ||
| }) : secretKey = secretKey ?? Platform.environment['NOVU_SECRET_KEY'] ?? '', | ||
| apiUrl = apiUrl ?? | ||
| Platform.environment['NOVU_API_URL'] ?? | ||
| 'https://api.novu.co/v1', | ||
| appId = appId ?? Platform.environment['NOVU_APP_ID'] ?? ''; | ||
|
|
||
| /// The Novu backend secret key used for API authentication. | ||
| final String secretKey; | ||
|
|
||
| /// The base URL for the Novu API (e.g. `https://api.novu.co/v1`). | ||
| final String apiUrl; | ||
|
|
||
| /// The Novu application identifier. | ||
| final String appId; | ||
|
|
||
| /// Whether the minimum required configuration (secret key) is present. | ||
| bool get isConfigured => secretKey.isNotEmpty; | ||
| } |
Oops, something went wrong.
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.
For improved readability and conciseness, you can use a collection-if to construct the payload map. This is a more modern and idiomatic Dart approach compared to creating the map and then conditionally adding elements. This suggestion applies to all other methods in this file as well.