Appove flow changed
This commit is contained in:
parent
9059840e77
commit
65eddf6fd8
308
main.go
308
main.go
@ -15,6 +15,7 @@ import (
|
||||
"github.com/pocketbase/pocketbase/apis"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/mailer"
|
||||
"github.com/pocketbase/pocketbase/tools/types"
|
||||
)
|
||||
|
||||
type EmailConfigurationResponse struct {
|
||||
@ -165,80 +166,6 @@ func main() {
|
||||
})
|
||||
|
||||
// Hook to send an email after a user is created
|
||||
app.OnRecordAfterCreateRequest().Add(func(e *core.RecordCreateEvent) error {
|
||||
|
||||
if e.Record.Collection().Name == "users" {
|
||||
// Retrieve user details
|
||||
name := e.Record.GetString("username")
|
||||
email := e.Record.GetString("email")
|
||||
adminName := "Admin" // Replace with the admin's actual name
|
||||
|
||||
// Email subject
|
||||
subject := "New User Registration Pending Review"
|
||||
|
||||
// Email body with Approve/Reject buttons
|
||||
body := fmt.Sprintf(`
|
||||
<html>
|
||||
<body>
|
||||
<p>Dear %s,</p>
|
||||
<p>A new user has registered and is awaiting your review.</p>
|
||||
<p><strong>Details:</strong></p>
|
||||
<p>Name: %s<br>
|
||||
Email: %s</p>
|
||||
<p>Please review the registration and take appropriate action:</p>
|
||||
<p>
|
||||
<a href="https://pb.venbait.in/api/custom/approve?userId=%s" style="padding: 10px 15px; background-color: green; color: white; text-decoration: none; border-radius: 5px;">Approve</a>
|
||||
<a href="https://pb.venbait.in/api/custom/reject?userId=%s" style="padding: 10px 15px; background-color: red; color: white; text-decoration: none; border-radius: 5px;">Reject</a>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
`, adminName, name, email, e.Record.Id, e.Record.Id)
|
||||
|
||||
// Define the target type (e.g., "smtp")
|
||||
targetType := "admin_receive_mail"
|
||||
|
||||
// Fetch the email configuration record where type matches targetType
|
||||
emailConfig, err := app.Dao().FindFirstRecordByData("email_configuration", "type", targetType)
|
||||
if err != nil {
|
||||
log.Printf("Failed to fetch email configuration for type '%s': %v", targetType, err)
|
||||
return fmt.Errorf("email configuration not found for type '%s'", targetType)
|
||||
}
|
||||
|
||||
// Get the email address
|
||||
adminEmail := emailConfig.GetString("email")
|
||||
if adminEmail == "" {
|
||||
log.Printf("No email address configured for type '%s'", targetType)
|
||||
return fmt.Errorf("no email address configured for type '%s'", targetType)
|
||||
}
|
||||
|
||||
// Create the email message
|
||||
message := &mailer.Message{
|
||||
From: mail.Address{
|
||||
Name: "FCSC",
|
||||
Address: app.Settings().Meta.SenderAddress,
|
||||
},
|
||||
To: []mail.Address{
|
||||
{
|
||||
Name: adminName,
|
||||
Address: adminEmail,
|
||||
},
|
||||
},
|
||||
Subject: subject,
|
||||
HTML: body,
|
||||
}
|
||||
|
||||
// Send the email
|
||||
err = app.NewMailClient().Send(message)
|
||||
if err != nil {
|
||||
log.Printf("Failed to send admin notification email: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
log.Println("Admin notification email sent successfully.")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
||||
|
||||
e.Router.GET("/api/custom/approve", func(c echo.Context) error {
|
||||
@ -483,6 +410,118 @@ Email: %s</p>
|
||||
return nil
|
||||
})
|
||||
|
||||
app.OnRecordAfterCreateRequest().Add(func(e *core.RecordCreateEvent) error {
|
||||
if e.Record.Collection().Name == "feedback" {
|
||||
// Retrieve feedback details
|
||||
feedback := e.Record
|
||||
userId := feedback.GetString("userId")
|
||||
emojiRating := feedback.GetString("emoji_rating")
|
||||
easeOfUse := feedback.GetString("ease_of_use")
|
||||
quality := feedback.GetString("quality")
|
||||
design := feedback.GetString("design")
|
||||
redundancy := feedback.GetString("redundancy")
|
||||
additionalFeedback := feedback.GetString("feedback")
|
||||
|
||||
if additionalFeedback == "" {
|
||||
additionalFeedback = "No additional feedback provided."
|
||||
}
|
||||
|
||||
// Fetch user details using userId
|
||||
user, err := app.Dao().FindRecordById("users", userId)
|
||||
if err != nil {
|
||||
log.Printf("Error fetching user details: %v\n", err)
|
||||
return err
|
||||
}
|
||||
|
||||
userName := user.GetString("username")
|
||||
|
||||
// Extract and format the creation time
|
||||
createdTime := e.Record.Get("created").(types.DateTime) // Extract created time
|
||||
feedbackDateTime := createdTime.Time().UTC() // Convert to time.Time in UTC
|
||||
|
||||
// Format the date and time
|
||||
formattedDate := feedbackDateTime.Format("02-01-2006") // DD-MM-YYYY
|
||||
formattedDateTime := feedbackDateTime.Format("02.01.2006 15:04 UTC") // DD.MM.YYYY HH:mm UTC
|
||||
|
||||
// Admin details
|
||||
adminName := "FCSC"
|
||||
|
||||
// Email subject
|
||||
subject := "UAE Stats Feedback"
|
||||
|
||||
// Email body in HTML format
|
||||
body := fmt.Sprintf(`
|
||||
<html>
|
||||
<body>
|
||||
<p>Dear %s,</p>
|
||||
<p> You have received new feedback from a user through the mobile application.</p>
|
||||
<p><b>User Details:</b></p>
|
||||
<ul>
|
||||
<li><b>Name</b> %s</li>
|
||||
<li><b>Date of Submission:</b> %s</li>
|
||||
<li><b>Time of Submission:</b> %s</li>
|
||||
</ul>
|
||||
<p><b>Feedback:</b></p>
|
||||
<pre>
|
||||
1. How was your experience with us today? Rating: %s
|
||||
2. How did we perform in key areas?
|
||||
1. Ease of Use: %s
|
||||
2. Quality: %s
|
||||
3. Design: %s
|
||||
4. Redundancy: %s
|
||||
3. Additional Feedback:
|
||||
1. %s
|
||||
</pre>
|
||||
<p>Thank you,</p>
|
||||
<p>The FCSC App Team</p>
|
||||
</body>
|
||||
</html>
|
||||
`, adminName, userName, formattedDate, formattedDateTime, emojiRating, easeOfUse, quality, design, redundancy, additionalFeedback)
|
||||
|
||||
// Define the target type (e.g., "smtp")
|
||||
targetType := "feedback_receive_mail"
|
||||
|
||||
// Fetch the email configuration record where type matches targetType
|
||||
emailConfig, err := app.Dao().FindFirstRecordByData("email_configuration", "type", targetType)
|
||||
if err != nil {
|
||||
log.Printf("Failed to fetch email configuration for type '%s': %v", targetType, err)
|
||||
return fmt.Errorf("email configuration not found for type '%s'", targetType)
|
||||
}
|
||||
|
||||
// Get the email address
|
||||
feedbackEmail := emailConfig.GetString("email")
|
||||
if feedbackEmail == "" {
|
||||
log.Printf("No email address configured for type '%s'", targetType)
|
||||
return fmt.Errorf("no email address configured for type '%s'", targetType)
|
||||
}
|
||||
// Create the email message
|
||||
message := &mailer.Message{
|
||||
From: mail.Address{
|
||||
Name: "FCSC",
|
||||
Address: app.Settings().Meta.SenderAddress,
|
||||
},
|
||||
To: []mail.Address{
|
||||
{
|
||||
Name: adminName,
|
||||
Address: feedbackEmail,
|
||||
},
|
||||
},
|
||||
Subject: subject,
|
||||
HTML: body,
|
||||
}
|
||||
|
||||
// Send the email
|
||||
err = app.NewMailClient().Send(message)
|
||||
if err != nil {
|
||||
log.Printf("Failed to send feedback email to admin: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
log.Println("Feedback email sent successfully to the admin.")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
app.OnRecordAfterCreateRequest("otp_requests").Add(func(e *core.RecordCreateEvent) error {
|
||||
// Ensure the record has an email field
|
||||
email, ok := e.Record.Get("email").(string)
|
||||
@ -598,18 +637,121 @@ func verifyEmailHandler(c echo.Context) error {
|
||||
|
||||
// Set the verified status to true
|
||||
record.Set("user_mail_verify", true)
|
||||
log.Printf("Failed to verify user: %v", err)
|
||||
// log.Printf("Failed to verify user: %v", err)
|
||||
if err := app.Dao().SaveRecord(record); err != nil {
|
||||
return c.JSON(500, map[string]interface{}{
|
||||
"code": 500,
|
||||
"message": "Failed to verify user.",
|
||||
log.Printf("Failed to update email verification status: %v", err)
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
||||
"error": "Failed to update email verification status",
|
||||
})
|
||||
}
|
||||
|
||||
// Redirect to the login page after successful verification
|
||||
log.Println("Email verification successful")
|
||||
return c.JSON(http.StatusOK, map[string]string{
|
||||
"message": "Email verified successfully!",
|
||||
})
|
||||
response := c.HTML(http.StatusOK, `
|
||||
<html>
|
||||
<head>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
background-color: #f0f8ff;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
height: 100vh;
|
||||
margin: 0;
|
||||
}
|
||||
.message-container {
|
||||
text-align: center;
|
||||
background-color: #e0f7fa;
|
||||
padding: 20px;
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
||||
}
|
||||
.message-container h1 {
|
||||
font-size: 24px;
|
||||
color: #00796b;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
.message-container p {
|
||||
font-size: 16px;
|
||||
color: #004d40;
|
||||
}
|
||||
.icon {
|
||||
font-size: 50px;
|
||||
color: #00796b;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="message-container">
|
||||
<div class="icon">✔</div>
|
||||
<h1>Your registration is pending for Admin Approval</h1>
|
||||
<p>Access will be granted once your account is approved.</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
`)
|
||||
|
||||
// Fetch user details for email notification
|
||||
username := record.GetString("username")
|
||||
userEmail := record.GetString("email")
|
||||
|
||||
// Send admin email asynchronously
|
||||
go func() {
|
||||
err = sendAdminEmail(app, userID, username, userEmail)
|
||||
if err != nil {
|
||||
log.Printf("Failed to send admin email: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
return response
|
||||
|
||||
}
|
||||
|
||||
func sendAdminEmail(app *pocketbase.PocketBase, userID, username, userEmail string) error {
|
||||
adminName := "Admin"
|
||||
subject := "New User Registration Pending Review"
|
||||
body := fmt.Sprintf(`
|
||||
<html>
|
||||
<body>
|
||||
<p>Dear %s,</p>
|
||||
<p>A new user has registered and is awaiting your review.</p>
|
||||
<p><strong>Details:</strong></p>
|
||||
<p>Name: %s<br>
|
||||
Email: %s</p>
|
||||
<p>Please review the registration and take appropriate action:</p>
|
||||
<p>
|
||||
<a href="https://pb.venbait.in/api/custom/approve?userId=%s" style="padding: 10px 15px; background-color: green; color: white; text-decoration: none; border-radius: 5px;">Approve</a>
|
||||
<a href="https://pb.venbait.in/api/custom/reject?userId=%s" style="padding: 10px 15px; background-color: red; color: white; text-decoration: none; border-radius: 5px;">Reject</a>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
`, adminName, username, userEmail, userID, userID)
|
||||
|
||||
emailConfig, err := app.Dao().FindFirstRecordByData("email_configuration", "type", "admin_receive_mail")
|
||||
if err != nil {
|
||||
log.Printf("Failed to fetch email configuration: %v", err)
|
||||
return nil
|
||||
}
|
||||
adminEmail := emailConfig.GetString("email")
|
||||
|
||||
// Send email
|
||||
message := &mailer.Message{
|
||||
From: mail.Address{
|
||||
Name: "FCSC",
|
||||
Address: app.Settings().Meta.SenderAddress,
|
||||
},
|
||||
To: []mail.Address{
|
||||
{Name: adminName, Address: adminEmail},
|
||||
},
|
||||
Subject: subject,
|
||||
HTML: body,
|
||||
}
|
||||
|
||||
if err := app.NewMailClient().Send(message); err != nil {
|
||||
log.Printf("Failed to send admin email: %v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Println("Admin email sent successfully.")
|
||||
return nil
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user