diff --git a/cmd/main.go b/cmd/main.go index 01fb1344..8a99bbbb 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -319,7 +319,7 @@ func main() { store, // implements SubscriptionStore ) - learnerNotifSvc := learnernotificationsservice.New(notificationSvc) + learnerNotifSvc := learnernotificationsservice.New(notificationSvc, store, emailTemplateSvc, messengerSvc, cfg.LearnerAppBaseURL) // Chapa service for subscription checkout payments chapaSvc := chapa.NewService( diff --git a/db/migrations/000096_subscription_activated_email_template.down.sql b/db/migrations/000096_subscription_activated_email_template.down.sql new file mode 100644 index 00000000..dd40ca7a --- /dev/null +++ b/db/migrations/000096_subscription_activated_email_template.down.sql @@ -0,0 +1,2 @@ +DELETE FROM email_templates +WHERE slug = 'subscription_activated'; diff --git a/db/migrations/000096_subscription_activated_email_template.up.sql b/db/migrations/000096_subscription_activated_email_template.up.sql new file mode 100644 index 00000000..04c8824c --- /dev/null +++ b/db/migrations/000096_subscription_activated_email_template.up.sql @@ -0,0 +1,36 @@ +INSERT INTO email_templates (slug, name, subject, body_text, body_html, variables, is_system, status) +VALUES ( + 'subscription_activated', + 'Subscription Activated', + 'Your Yimaru Academy subscription is active', + 'Hi{{if .FirstName}} {{.FirstName}}{{end}}, + +Your "{{.PlanName}}" {{.PackageLabel}} subscription is now active{{if .ExpiresLabel}} until {{.ExpiresLabel}}{{else if .IsLifetime}} with lifetime access{{end}}. + +Sign in to start learning: {{.LoginURL}}', + $subscription_html$ + +Yimaru Academy + + +
+ + + + +
+

Yimaru Academy

+

Online Learning Platform

+
+

Subscription active

+

{{if .FirstName}}Hi {{.FirstName}}, {{end}}your {{.PlanName}} {{.PackageLabel}} package is now active.{{if .IsLifetime}} You have lifetime access.{{else if .ExpiresLabel}} It is valid until {{.ExpiresLabel}}.{{end}}

+

Start learning

+

Thank you for subscribing to Yimaru Academy.

+
+

© 2026 Yimaru Academy · All rights reserved

+
$subscription_html$, + '["FirstName", "PlanName", "PackageLabel", "LoginURL", "ExpiresLabel", "IsLifetime"]'::jsonb, + TRUE, + 'ACTIVE' +) +ON CONFLICT (slug) DO NOTHING; diff --git a/internal/domain/email_template.go b/internal/domain/email_template.go index 71ddd005..bc02af44 100644 --- a/internal/domain/email_template.go +++ b/internal/domain/email_template.go @@ -9,8 +9,9 @@ const ( EmailTemplateSlugOTP = "otp" EmailTemplateSlugInvitation = "invitation" EmailTemplateSlugPasswordReset = "password_reset" - EmailTemplateSlugWelcome = "welcome" - EmailTemplateSlugCustomMessage = "custom_message" + EmailTemplateSlugWelcome = "welcome" + EmailTemplateSlugSubscriptionActivated = "subscription_activated" + EmailTemplateSlugCustomMessage = "custom_message" ) type EmailTemplate struct { diff --git a/internal/services/arifpay/service.go b/internal/services/arifpay/service.go index 1bd60908..b26259d7 100644 --- a/internal/services/arifpay/service.go +++ b/internal/services/arifpay/service.go @@ -54,9 +54,9 @@ func (s *ArifpayService) SetLearnerNotifier(notifier *learnernotifications.Servi s.learnerNotifier = notifier } -func (s *ArifpayService) notifyLearnPackageSubscribed(userID int64, plan *domain.SubscriptionPlan) { +func (s *ArifpayService) notifyLearnPackageSubscribed(userID int64, plan *domain.SubscriptionPlan, expiresAt time.Time) { if s.learnerNotifier != nil && plan != nil { - s.learnerNotifier.MaybeNotifyLearnPackageSubscribed(userID, plan.Category, plan.Name) + s.learnerNotifier.MaybeNotifyLearnPackageSubscribed(userID, plan.Category, plan.Name, expiresAt) } } @@ -292,7 +292,7 @@ func (s *ArifpayService) ProcessPaymentWebhook(ctx context.Context, req domain.W if err := s.paymentStore.LinkPaymentToSubscription(ctx, payment.ID, subscription.ID); err != nil { return fmt.Errorf("failed to link payment to subscription: %w", err) } - s.notifyLearnPackageSubscribed(payment.UserID, plan) + s.notifyLearnPackageSubscribed(payment.UserID, plan, expiresAt) } return nil @@ -1048,7 +1048,7 @@ func (s *ArifpayService) VerifyDirectPaymentOTP(ctx context.Context, userID int6 }) if err == nil { s.paymentStore.LinkPaymentToSubscription(ctx, payment.ID, subscription.ID) - s.notifyLearnPackageSubscribed(payment.UserID, plan) + s.notifyLearnPackageSubscribed(payment.UserID, plan, expiresAt) } } } diff --git a/internal/services/chapa/service.go b/internal/services/chapa/service.go index 4745a91b..f39883ba 100644 --- a/internal/services/chapa/service.go +++ b/internal/services/chapa/service.go @@ -373,7 +373,7 @@ func (s *Service) activateSubscription(ctx context.Context, payment *domain.Paym } if s.learnerNotifier != nil { - s.learnerNotifier.MaybeNotifyLearnPackageSubscribed(payment.UserID, plan.Category, plan.Name) + s.learnerNotifier.MaybeNotifyLearnPackageSubscribed(payment.UserID, plan.Category, plan.Name, expiresAt) } return nil diff --git a/internal/services/emailtemplates/brand_templates.go b/internal/services/emailtemplates/brand_templates.go index deead365..3b380976 100644 --- a/internal/services/emailtemplates/brand_templates.go +++ b/internal/services/emailtemplates/brand_templates.go @@ -74,5 +74,14 @@ const ( defaultCustomMessageHTML = brandEmailHeader + `

{{.Subject}}

{{.Message}}
+` + brandEmailFooter + + defaultSubscriptionActivatedSubject = "Your Yimaru Academy subscription is active" + defaultSubscriptionActivatedText = "Hi{{if .FirstName}} {{.FirstName}}{{end}},\n\nYour \"{{.PlanName}}\" {{.PackageLabel}} subscription is now active{{if .ExpiresLabel}} until {{.ExpiresLabel}}{{else if .IsLifetime}} with lifetime access{{end}}.\n\nSign in to start learning: {{.LoginURL}}" + defaultSubscriptionActivatedHTML = brandEmailHeader + ` +

Subscription active

+

{{if .FirstName}}Hi {{.FirstName}}, {{end}}your {{.PlanName}} {{.PackageLabel}} package is now active.{{if .IsLifetime}} You have lifetime access.{{else if .ExpiresLabel}} It is valid until {{.ExpiresLabel}}.{{end}}

+

Start learning

+

Thank you for subscribing to Yimaru Academy.

` + brandEmailFooter ) diff --git a/internal/services/emailtemplates/defaults.go b/internal/services/emailtemplates/defaults.go index 1ce84a6d..f85f2059 100644 --- a/internal/services/emailtemplates/defaults.go +++ b/internal/services/emailtemplates/defaults.go @@ -51,6 +51,15 @@ var defaultTemplates = map[string]domain.EmailTemplate{ Variables: []string{"Subject", "Message"}, Status: domain.EmailTemplateStatusActive, }, + domain.EmailTemplateSlugSubscriptionActivated: { + Slug: domain.EmailTemplateSlugSubscriptionActivated, + Name: "Subscription Activated", + Subject: defaultSubscriptionActivatedSubject, + BodyText: defaultSubscriptionActivatedText, + BodyHTML: defaultSubscriptionActivatedHTML, + Variables: []string{"FirstName", "PlanName", "PackageLabel", "LoginURL", "ExpiresLabel", "IsLifetime"}, + Status: domain.EmailTemplateStatusActive, + }, } func defaultTemplate(slug string) (domain.EmailTemplate, bool) { diff --git a/internal/services/learnernotifications/service.go b/internal/services/learnernotifications/service.go index fe55c797..8cb9be45 100644 --- a/internal/services/learnernotifications/service.go +++ b/internal/services/learnernotifications/service.go @@ -3,19 +3,39 @@ package learnernotifications import ( "context" "fmt" + "strings" "time" "Yimaru-Backend/internal/domain" + "Yimaru-Backend/internal/ports" + emailtemplates "Yimaru-Backend/internal/services/emailtemplates" + "Yimaru-Backend/internal/services/messenger" notificationservice "Yimaru-Backend/internal/services/notification" ) -// Service sends learner-facing in-app and push notifications. +// Service sends learner-facing in-app, push, and email notifications. type Service struct { - notifications *notificationservice.Service + notifications *notificationservice.Service + users ports.UserByID + emailTemplateSvc *emailtemplates.Service + messengerSvc *messenger.Service + learnerAppBaseURL string } -func New(notifications *notificationservice.Service) *Service { - return &Service{notifications: notifications} +func New( + notifications *notificationservice.Service, + users ports.UserByID, + emailTemplateSvc *emailtemplates.Service, + messengerSvc *messenger.Service, + learnerAppBaseURL string, +) *Service { + return &Service{ + notifications: notifications, + users: users, + emailTemplateSvc: emailTemplateSvc, + messengerSvc: messengerSvc, + learnerAppBaseURL: learnerAppBaseURL, + } } func (s *Service) NotifyWelcome(userID int64) { @@ -42,10 +62,19 @@ func (s *Service) NotifyProgramCompleted(userID int64, programName string) { fmt.Sprintf("Amazing achievement! You completed the program \"%s\".", programName)) } -func (s *Service) NotifyLearnPackageSubscribed(userID int64, planName string) { +func (s *Service) NotifySubscriptionActivated(userID int64, planCategory, planName string, expiresAt time.Time) { s.send(userID, domain.NOTIFICATION_TYPE_SUBSCRIPTION_ACTIVATED, domain.NotificationLevelSuccess, "Subscription active", - fmt.Sprintf("Your \"%s\" Learn English package is now active. Enjoy your learning journey!", planName)) + subscriptionActivatedMessage(planCategory, planName)) + s.sendSubscriptionActivatedEmail(userID, planCategory, planName, expiresAt) +} + +// MaybeNotifyLearnPackageSubscribed notifies learners when a paid subscription is activated. +func (s *Service) MaybeNotifyLearnPackageSubscribed(userID int64, planCategory, planName string, expiresAt time.Time) { + if strings.TrimSpace(planCategory) == "" || strings.TrimSpace(planName) == "" { + return + } + s.NotifySubscriptionActivated(userID, planCategory, planName, expiresAt) } func (s *Service) NotifyLearnPackageExpiringSoon(userID int64, planName string, expiresAt time.Time) { @@ -54,12 +83,6 @@ func (s *Service) NotifyLearnPackageExpiringSoon(userID int64, planName string, fmt.Sprintf("Your \"%s\" package expires on %s. Renew to keep uninterrupted access.", planName, expiresAt.Format("Jan 2, 2006"))) } -func (s *Service) MaybeNotifyLearnPackageSubscribed(userID int64, planCategory, planName string) { - if planCategory == string(domain.SubscriptionCategoryLearnEnglish) { - s.NotifyLearnPackageSubscribed(userID, planName) - } -} - func (s *Service) NotifyLMSPracticeMilestones(userID int64, result domain.LMSPracticeCompletionResult) { if result.ModuleCompleted != nil { s.NotifyModuleCompleted(userID, result.ModuleCompleted.Name) @@ -106,3 +129,46 @@ func (s *Service) send(userID int64, notifType domain.NotificationType, level do } }() } + +func (s *Service) sendSubscriptionActivatedEmail(userID int64, planCategory, planName string, expiresAt time.Time) { + if s == nil || s.emailTemplateSvc == nil || s.messengerSvc == nil || s.users == nil || userID == 0 { + return + } + go func() { + ctx := context.Background() + user, err := s.users.GetUserByID(ctx, userID) + if err != nil { + return + } + email := strings.TrimSpace(user.Email) + if email == "" { + return + } + + expiresLabel, isLifetime := subscriptionExpiresLabel(planCategory, expiresAt) + vars := map[string]any{ + "FirstName": user.FirstName, + "PlanName": planName, + "PackageLabel": subscriptionPackageLabel(planCategory), + "LoginURL": s.learnerLoginURL(), + "ExpiresLabel": expiresLabel, + "IsLifetime": isLifetime, + } + rendered, err := s.emailTemplateSvc.Render(ctx, domain.EmailTemplateSlugSubscriptionActivated, vars) + if err != nil { + return + } + _ = s.messengerSvc.SendEmail(ctx, email, rendered.Text, rendered.HTML, rendered.Subject) + }() +} + +func (s *Service) learnerLoginURL() string { + base := strings.TrimRight(strings.TrimSpace(s.learnerAppBaseURL), "/") + if base == "" { + return "https://app.yimaruacademy.com/login" + } + if strings.HasSuffix(base, "/login") { + return base + } + return base + "/login" +} diff --git a/internal/services/learnernotifications/subscription.go b/internal/services/learnernotifications/subscription.go new file mode 100644 index 00000000..bfddc502 --- /dev/null +++ b/internal/services/learnernotifications/subscription.go @@ -0,0 +1,42 @@ +package learnernotifications + +import ( + "strings" + "time" + + "Yimaru-Backend/internal/domain" +) + +func subscriptionPackageLabel(planCategory string) string { + switch strings.ToUpper(strings.TrimSpace(planCategory)) { + case string(domain.SubscriptionCategoryLearnEnglish): + return "Learn English" + case string(domain.SubscriptionCategoryIELTS): + return "IELTS" + case string(domain.SubscriptionCategoryDuolingo): + return "Duolingo" + default: + return "Yimaru Academy" + } +} + +func subscriptionActivatedMessage(planCategory, planName string) string { + label := subscriptionPackageLabel(planCategory) + switch strings.ToUpper(strings.TrimSpace(planCategory)) { + case string(domain.SubscriptionCategoryLearnEnglish): + return "Your \"" + planName + "\" Learn English package is now active. Enjoy your learning journey!" + case string(domain.SubscriptionCategoryIELTS): + return "Your \"" + planName + "\" IELTS package is now active. Start preparing today!" + case string(domain.SubscriptionCategoryDuolingo): + return "Your \"" + planName + "\" Duolingo package is now active. Enjoy your learning journey!" + default: + return "Your \"" + planName + "\" " + label + " package is now active." + } +} + +func subscriptionExpiresLabel(planCategory string, expiresAt time.Time) (expiresLabel string, isLifetime bool) { + if domain.IsLifetimeSubscriptionCategory(planCategory) { + return "", true + } + return expiresAt.Format("Jan 2, 2006"), false +} diff --git a/internal/services/learnernotifications/subscription_test.go b/internal/services/learnernotifications/subscription_test.go new file mode 100644 index 00000000..7e89f9d3 --- /dev/null +++ b/internal/services/learnernotifications/subscription_test.go @@ -0,0 +1,37 @@ +package learnernotifications + +import ( + "testing" + "time" + + "Yimaru-Backend/internal/domain" +) + +func TestSubscriptionPackageLabel(t *testing.T) { + if got := subscriptionPackageLabel("IELTS"); got != "IELTS" { + t.Fatalf("got %q", got) + } + if got := subscriptionPackageLabel("learn_english"); got != "Learn English" { + t.Fatalf("got %q", got) + } +} + +func TestSubscriptionExpiresLabel(t *testing.T) { + expiresAt := time.Date(2026, 12, 31, 0, 0, 0, 0, time.UTC) + label, lifetime := subscriptionExpiresLabel(string(domain.SubscriptionCategoryIELTS), expiresAt) + if lifetime || label != "Dec 31, 2026" { + t.Fatalf("label=%q lifetime=%v", label, lifetime) + } + + label, lifetime = subscriptionExpiresLabel(string(domain.SubscriptionCategoryDuolingo), expiresAt) + if !lifetime || label != "" { + t.Fatalf("label=%q lifetime=%v", label, lifetime) + } +} + +func TestSubscriptionActivatedMessage(t *testing.T) { + msg := subscriptionActivatedMessage(string(domain.SubscriptionCategoryIELTS), "Premium") + if msg == "" || msg == "Premium" { + t.Fatalf("unexpected message: %q", msg) + } +} diff --git a/internal/web_server/handlers/subscriptions.go b/internal/web_server/handlers/subscriptions.go index 8e6535a9..2c5b8764 100644 --- a/internal/web_server/handlers/subscriptions.go +++ b/internal/web_server/handlers/subscriptions.go @@ -372,7 +372,7 @@ func (h *Handler) Subscribe(c *fiber.Ctx) error { }) } if h.learnerNotifSvc != nil { - h.learnerNotifSvc.MaybeNotifyLearnPackageSubscribed(userID, plan.Category, plan.Name) + h.learnerNotifSvc.MaybeNotifyLearnPackageSubscribed(userID, plan.Category, plan.Name, sub.ExpiresAt) } h.recordActivity(c, domain.ActionSubscriptionCreated, domain.ResourceSubscription, &sub.ID, fmt.Sprintf("Created subscription ID: %d", sub.ID), nil)