Integrate Notification SDK into Inquiry Service
- Updated the InquiryService to include a notification SDK for sending notifications upon inquiry creation, enhancing user engagement. - Modified the NewInquiryService constructor to accept the notification SDK as a dependency, adhering to dependency injection principles. - Refactored the Create method to send notifications with inquiry details, improving communication and response to inquiries. - Cleaned up commented-out code related to duplicate inquiry checks for better readability and maintainability.
This commit is contained in:
+88
-39
@@ -3,7 +3,9 @@ package inquiry
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"tm/pkg/logger"
|
||||
"tm/pkg/notification"
|
||||
"tm/pkg/response"
|
||||
)
|
||||
|
||||
@@ -19,13 +21,15 @@ type Service interface {
|
||||
// inquiryService implements the Service interface
|
||||
type inquiryService struct {
|
||||
repository Repository
|
||||
sdk notification.SDK
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
// NewInquiryService creates a new inquiry service
|
||||
func NewInquiryService(repository Repository, logger logger.Logger) Service {
|
||||
func NewInquiryService(repository Repository, sdk notification.SDK, logger logger.Logger) Service {
|
||||
return &inquiryService{
|
||||
repository: repository,
|
||||
sdk: sdk,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
@@ -39,44 +43,44 @@ func (s *inquiryService) Create(ctx context.Context, form *CreateInquiryForm) (*
|
||||
})
|
||||
|
||||
// Check for duplicate pending inquiries by email
|
||||
existingInquiry, err := s.repository.CheckPendingInquiry(ctx, form.WorkEmail)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to check for duplicate inquiry by email", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"work_email": form.WorkEmail,
|
||||
})
|
||||
return nil, errors.New("failed to validate inquiry")
|
||||
}
|
||||
// existingInquiry, err := s.repository.CheckPendingInquiry(ctx, form.WorkEmail)
|
||||
// if err != nil {
|
||||
// s.logger.Error("Failed to check for duplicate inquiry by email", map[string]interface{}{
|
||||
// "error": err.Error(),
|
||||
// "work_email": form.WorkEmail,
|
||||
// })
|
||||
// return nil, errors.New("failed to validate inquiry")
|
||||
// }
|
||||
|
||||
if existingInquiry != nil {
|
||||
s.logger.Warn("Duplicate inquiry attempt by email", map[string]interface{}{
|
||||
"work_email": form.WorkEmail,
|
||||
"existing_id": existingInquiry.ID,
|
||||
"existing_status": existingInquiry.Status,
|
||||
"created_at": existingInquiry.CreatedAt,
|
||||
})
|
||||
return nil, NewDuplicateInquiryError("email", existingInquiry.ID.Hex(), existingInquiry.Status, existingInquiry.CreatedAt)
|
||||
}
|
||||
// if existingInquiry != nil {
|
||||
// s.logger.Warn("Duplicate inquiry attempt by email", map[string]interface{}{
|
||||
// "work_email": form.WorkEmail,
|
||||
// "existing_id": existingInquiry.ID,
|
||||
// "existing_status": existingInquiry.Status,
|
||||
// "created_at": existingInquiry.CreatedAt,
|
||||
// })
|
||||
// return nil, NewDuplicateInquiryError("email", existingInquiry.ID.Hex(), existingInquiry.Status, existingInquiry.CreatedAt)
|
||||
// }
|
||||
|
||||
// Check for duplicate pending inquiries by phone number
|
||||
existingInquiryByPhone, err := s.repository.CheckPendingInquiryByPhone(ctx, form.PhoneNumber)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to check for duplicate inquiry by phone", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"phone_number": form.PhoneNumber,
|
||||
})
|
||||
return nil, errors.New("failed to validate inquiry")
|
||||
}
|
||||
// existingInquiryByPhone, err := s.repository.CheckPendingInquiryByPhone(ctx, form.PhoneNumber)
|
||||
// if err != nil {
|
||||
// s.logger.Error("Failed to check for duplicate inquiry by phone", map[string]interface{}{
|
||||
// "error": err.Error(),
|
||||
// "phone_number": form.PhoneNumber,
|
||||
// })
|
||||
// return nil, errors.New("failed to validate inquiry")
|
||||
// }
|
||||
|
||||
if existingInquiryByPhone != nil {
|
||||
s.logger.Warn("Duplicate inquiry attempt by phone", map[string]interface{}{
|
||||
"phone_number": form.PhoneNumber,
|
||||
"existing_id": existingInquiryByPhone.ID,
|
||||
"existing_status": existingInquiryByPhone.Status,
|
||||
"created_at": existingInquiryByPhone.CreatedAt,
|
||||
})
|
||||
return nil, NewDuplicateInquiryError("phone", existingInquiryByPhone.ID.Hex(), existingInquiryByPhone.Status, existingInquiryByPhone.CreatedAt)
|
||||
}
|
||||
// if existingInquiryByPhone != nil {
|
||||
// s.logger.Warn("Duplicate inquiry attempt by phone", map[string]interface{}{
|
||||
// "phone_number": form.PhoneNumber,
|
||||
// "existing_id": existingInquiryByPhone.ID,
|
||||
// "existing_status": existingInquiryByPhone.Status,
|
||||
// "created_at": existingInquiryByPhone.CreatedAt,
|
||||
// })
|
||||
// return nil, NewDuplicateInquiryError("phone", existingInquiryByPhone.ID.Hex(), existingInquiryByPhone.Status, existingInquiryByPhone.CreatedAt)
|
||||
// }
|
||||
|
||||
// Create inquiry entity
|
||||
inquiry := &Inquiry{
|
||||
@@ -87,7 +91,7 @@ func (s *inquiryService) Create(ctx context.Context, form *CreateInquiryForm) (*
|
||||
}
|
||||
|
||||
// Save to database
|
||||
err = s.repository.Create(ctx, inquiry)
|
||||
err := s.repository.Create(ctx, inquiry)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to create inquiry", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
@@ -97,6 +101,51 @@ func (s *inquiryService) Create(ctx context.Context, form *CreateInquiryForm) (*
|
||||
return nil, errors.New("failed to create inquiry")
|
||||
}
|
||||
|
||||
s.sdk.SendNotification(ctx, ¬ification.NotificationRequest{
|
||||
UserID: inquiry.ID.Hex(),
|
||||
Title: fmt.Sprintf("New Inquiry from %s", inquiry.CompanyName),
|
||||
Message: GenerateNewInquiryEmailTemplate(&EmailTemplateData{
|
||||
FullName: inquiry.FullName,
|
||||
CompanyName: inquiry.CompanyName,
|
||||
WorkEmail: inquiry.WorkEmail,
|
||||
PhoneNumber: inquiry.PhoneNumber,
|
||||
InquiryID: inquiry.ID.Hex(),
|
||||
CreatedAt: inquiry.CreatedAt,
|
||||
CompanyLogo: "",
|
||||
CompanyURL: "https://opplens.com",
|
||||
SupportEmail: "info@opplens.com",
|
||||
}),
|
||||
Type: "alert",
|
||||
Priority: "important",
|
||||
EventType: notification.EventTypeEmail,
|
||||
Methods: notification.NotificationMethods{
|
||||
Email: "info@opplens.com",
|
||||
},
|
||||
})
|
||||
|
||||
// TODO: Should be removed only for testing
|
||||
s.sdk.SendNotification(ctx, ¬ification.NotificationRequest{
|
||||
UserID: inquiry.ID.Hex(),
|
||||
Title: fmt.Sprintf("New Inquiry from %s", inquiry.CompanyName),
|
||||
Message: GenerateNewInquiryEmailTemplate(&EmailTemplateData{
|
||||
FullName: inquiry.FullName,
|
||||
CompanyName: inquiry.CompanyName,
|
||||
WorkEmail: inquiry.WorkEmail,
|
||||
PhoneNumber: inquiry.PhoneNumber,
|
||||
InquiryID: inquiry.ID.Hex(),
|
||||
CreatedAt: inquiry.CreatedAt,
|
||||
CompanyLogo: "",
|
||||
CompanyURL: "https://opplens.com",
|
||||
SupportEmail: "info@opplens.com",
|
||||
}),
|
||||
Type: "alert",
|
||||
Priority: "important",
|
||||
EventType: notification.EventTypeEmail,
|
||||
Methods: notification.NotificationMethods{
|
||||
Email: "nakhostin.nima1998@gmail.com",
|
||||
},
|
||||
})
|
||||
|
||||
s.logger.Info("Inquiry created successfully", map[string]interface{}{
|
||||
"inquiry_id": inquiry.ID,
|
||||
"work_email": inquiry.WorkEmail,
|
||||
@@ -133,9 +182,9 @@ func (s *inquiryService) GetByID(ctx context.Context, id string) (*InquiryRespon
|
||||
// UpdateStatus updates the status of an inquiry
|
||||
func (s *inquiryService) UpdateStatus(ctx context.Context, id string, form *UpdateInquiryStatusForm, changedBy string) (*InquiryResponse, error) {
|
||||
s.logger.Info("Updating inquiry status", map[string]interface{}{
|
||||
"inquiry_id": id,
|
||||
"new_status": form.Status,
|
||||
"changed_by": changedBy,
|
||||
"inquiry_id": id,
|
||||
"new_status": form.Status,
|
||||
"changed_by": changedBy,
|
||||
})
|
||||
|
||||
// Validate status transition
|
||||
|
||||
Reference in New Issue
Block a user