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:
n.nakhostin
2025-09-21 12:23:26 +03:30
parent 5906904caf
commit c22d98d4bb
4 changed files with 377 additions and 42 deletions
+1 -1
View File
@@ -157,7 +157,7 @@ func main() {
tenderService := tender.NewTenderService(tenderRepository, companyService, logger)
feedbackService := feedback.NewFeedbackService(feedbackRepo, tenderService, logger)
tenderApprovalService := tender_approval.NewTenderApprovalService(tenderApprovalRepository, tenderService, logger)
inquiryService := inquiry.NewInquiryService(inquiryRepository, logger)
inquiryService := inquiry.NewInquiryService(inquiryRepository, notificationSDK, logger)
flagService := assets.NewFlagService(logger, conf.Assets.FlagsPath)
notificationService := notification.NewNotificationService(notificationSDK, userService, customerService, logger)
logger.Info("Services initialized successfully", map[string]interface{}{
+286
View File
@@ -0,0 +1,286 @@
package inquiry
import (
"fmt"
"strings"
"time"
)
// EmailTemplateData represents the data needed for the email template
type EmailTemplateData struct {
FullName string
CompanyName string
WorkEmail string
PhoneNumber string
InquiryID string
CreatedAt int64
CompanyLogo string
CompanyURL string
SupportEmail string
}
// GenerateNewInquiryEmailTemplate generates a beautiful HTML email template for new inquiry notifications
func GenerateNewInquiryEmailTemplate(data *EmailTemplateData) string {
// Format the creation date
createdAt := time.Unix(data.CreatedAt, 0).Format("January 2, 2006 at 3:04 PM")
html := `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>New Inquiry Request - {{COMPANY_NAME}}</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
line-height: 1.6;
color: #333;
background-color: #f8fafc;
}
.email-container {
max-width: 600px;
margin: 16px auto;
background-color: #ffffff;
border-radius: 12px;
overflow: hidden;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.header {
background: linear-gradient(0deg, #5750F1 0%, #e0e0e0 100%);
padding: 40px 30px;
text-align: center;
color: white;
}
.logo {
max-width: 200px;
height: auto;
margin-bottom: 20px;
border-radius: 8px;
}
.header h1 {
font-size: 28px;
font-weight: 700;
margin-bottom: 10px;
text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.header p {
font-size: 16px;
opacity: 0.9;
font-weight: 300;
}
.content {
padding: 40px 30px;
}
.inquiry-card {
background-color: #f8fafc;
border: 1px solid #e2e8f0;
border-radius: 12px;
padding: 30px;
margin: 30px 0;
position: relative;
}
.inquiry-card::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
height: 4px;
background: linear-gradient(90deg, #5750F1, #e0e0e0);
border-radius: 12px 12px 0 0;
}
.inquiry-id {
background-color: #5750F1;
color: white;
padding: 8px 16px;
border-radius: 20px;
font-size: 12px;
font-weight: 600;
display: inline-block;
margin-bottom: 20px;
letter-spacing: 0.5px;
}
.info-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin-bottom: 20px;
}
.info-item {
background-color: white;
padding: 20px;
border-radius: 8px;
border: 1px solid #e2e8f0;
}
.info-label {
font-size: 12px;
color: #64748b;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.5px;
margin-bottom: 8px;
}
.info-value {
font-size: 16px;
color: #1e293b;
font-weight: 500;
}
.timestamp {
text-align: center;
color: #64748b;
font-size: 14px;
margin-top: 20px;
padding-top: 20px;
border-top: 1px solid #e2e8f0;
}
.footer {
background-color: #f8fafc;
padding: 30px;
text-align: center;
border-top: 1px solid #e2e8f0;
}
.footer p {
color: #64748b;
font-size: 14px;
margin-bottom: 10px;
}
.footer a {
color: #5750F1;
text-decoration: none;
}
.footer a:hover {
text-decoration: underline;
}
@media (max-width: 600px) {
.email-container {
margin: 16px;
border-radius: 0;
}
.content {
padding: 20px;
}
.info-grid {
grid-template-columns: 1fr;
gap: 15px;
}
.header {
padding: 30px 20px;
}
.header h1 {
font-size: 24px;
}
}
</style>
</head>
<body>
<div class="email-container">
<!-- Header -->
<div class="header">
<h1>New Inquiry Received</h1>
<p>A new inquiry has been submitted to the opplens system</p>
</div>
<!-- Content -->
<div class="content">
<div class="inquiry-card">
<div class="inquiry-id">Identifier: {{INQUIRY_ID}}</div>
<div class="info-grid">
<div class="info-item">
<div class="info-label">Full Name</div>
<div class="info-value">{{FULL_NAME}}</div>
</div>
<div class="info-item">
<div class="info-label">Company Name</div>
<div class="info-value">{{COMPANY_NAME}}</div>
</div>
<div class="info-item">
<div class="info-label">Work Email</div>
<div class="info-value">{{WORK_EMAIL}}</div>
</div>
<div class="info-item">
<div class="info-label">Phone Number</div>
<div class="info-value">{{PHONE_NUMBER}}</div>
</div>
</div>
<div class="timestamp">
<strong>Submitted on:</strong> {{CREATED_AT}}
<br>
<p>This email was automatically sent from the Opplens System</p>
</div>
</div>
</div>
</div>
</body>
</html>`
// Replace placeholders with actual data
html = strings.ReplaceAll(html, "{{COMPANY_NAME}}", data.CompanyName)
html = strings.ReplaceAll(html, "{{INQUIRY_ID}}", data.InquiryID)
html = strings.ReplaceAll(html, "{{FULL_NAME}}", data.FullName)
html = strings.ReplaceAll(html, "{{WORK_EMAIL}}", data.WorkEmail)
html = strings.ReplaceAll(html, "{{PHONE_NUMBER}}", data.PhoneNumber)
html = strings.ReplaceAll(html, "{{CREATED_AT}}", createdAt)
return html
}
// GenerateNewInquiryEmailMessage generates a simple text message for the email
func GenerateNewInquiryEmailMessage(data *EmailTemplateData) string {
createdAt := time.Unix(data.CreatedAt, 0).Format("January 2, 2006 at 3:04 PM")
return fmt.Sprintf(`
New Inquiry Received
Inquiry ID: %s
Full Name: %s
Company Name: %s
Work Email: %s
Phone Number: %s
Submitted on: %s
Please check the admin panel for more details.
`,
data.InquiryID,
data.FullName,
data.CompanyName,
data.WorkEmail,
data.PhoneNumber,
createdAt,
)
}
+2 -2
View File
@@ -59,7 +59,7 @@ func (h *Handler) CreateInquiry(c echo.Context) error {
"status": duplicateErr.Status,
"created_at": duplicateErr.CreatedAt,
})
// Return conflict with detailed error information
return c.JSON(409, response.APIResponse{
Success: false,
@@ -116,7 +116,7 @@ func (h *Handler) GetInquiryByID(c echo.Context) error {
// @Router /admin/v1/inquiries/{id}/status [put]
func (h *Handler) UpdateInquiryStatus(c echo.Context) error {
idStr := c.Param("id")
form, err := response.Parse[UpdateInquiryStatusForm](c)
if err != nil {
return response.BadRequest(c, "Invalid request format", "")
+88 -39
View File
@@ -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, &notification.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, &notification.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