2026-05-19 11:00:28 +00:00
|
|
|
// Code scaffolded by goctl. Safe to edit.
|
|
|
|
|
// goctl 1.10.1
|
|
|
|
|
|
|
|
|
|
package svc
|
|
|
|
|
|
|
|
|
|
import (
|
2026-05-20 07:01:08 +00:00
|
|
|
"context"
|
|
|
|
|
|
2026-05-19 11:00:28 +00:00
|
|
|
"gateway/internal/config"
|
2026-05-20 07:01:08 +00:00
|
|
|
redislib "gateway/internal/library/redis"
|
2026-05-19 12:56:32 +00:00
|
|
|
"gateway/internal/library/validate"
|
2026-05-20 13:03:59 +00:00
|
|
|
domrepo "gateway/internal/model/member/domain/repository"
|
2026-05-20 07:01:08 +00:00
|
|
|
dommember "gateway/internal/model/member/domain/usecase"
|
|
|
|
|
memberusecase "gateway/internal/model/member/usecase"
|
|
|
|
|
domnotif "gateway/internal/model/notification/domain/usecase"
|
|
|
|
|
notifusecase "gateway/internal/model/notification/usecase"
|
|
|
|
|
"gateway/internal/worker/notification_retry"
|
2026-05-19 11:00:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ServiceContext struct {
|
2026-05-19 12:56:32 +00:00
|
|
|
Config config.Config
|
|
|
|
|
Validator validate.Validate
|
2026-05-20 07:01:08 +00:00
|
|
|
// Redis is the process-wide client (one pool per Addr); nil when Redis.Host is empty.
|
|
|
|
|
Redis *redislib.Client
|
|
|
|
|
// Notifier is nil when Mongo is not configured (local scaffold without DB).
|
|
|
|
|
Notifier domnotif.NotifierUseCase
|
|
|
|
|
// NotificationAdmin is nil when Mongo is not configured.
|
|
|
|
|
NotificationAdmin domnotif.AdminNotifierUseCase
|
|
|
|
|
// NotificationRetry runs async delivery when Mongo + Redis are configured.
|
|
|
|
|
NotificationRetry *notification_retry.Runner
|
2026-05-20 13:03:59 +00:00
|
|
|
|
|
|
|
|
// MemberOTP is the atomic OTP usecase (Generate / Verify / Invalidate).
|
|
|
|
|
// nil when Redis is not configured. Logic layer composes it with the
|
|
|
|
|
// Notifier + Profile flips; usecases MUST NOT call other usecases.
|
|
|
|
|
MemberOTP dommember.OTPUseCase
|
|
|
|
|
// MemberTOTP is the atomic TOTP usecase; nil when Member.TOTP.SecretKEK
|
|
|
|
|
// is unset or Redis is missing.
|
|
|
|
|
MemberTOTP dommember.TOTPUseCase
|
|
|
|
|
// MemberVerifyRate exposes resend-cooldown / daily-cap helpers for the
|
|
|
|
|
// logic layer.
|
|
|
|
|
MemberVerifyRate domrepo.VerifyRateStore
|
|
|
|
|
// MemberProfile flips BusinessEmail/Phone verified flags; consumed by
|
|
|
|
|
// the logic layer after a successful OTP confirmation.
|
|
|
|
|
MemberProfile domrepo.ProfileRepository
|
2026-05-19 11:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewServiceContext(c config.Config) *ServiceContext {
|
2026-05-19 12:56:32 +00:00
|
|
|
v, err := validate.NewWithDefaultEN()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-20 07:01:08 +00:00
|
|
|
rds, err := redislib.NewClient(c.Redis)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sc := &ServiceContext{
|
2026-05-19 12:56:32 +00:00
|
|
|
Config: c,
|
|
|
|
|
Validator: v,
|
2026-05-20 07:01:08 +00:00
|
|
|
Redis: rds,
|
|
|
|
|
}
|
|
|
|
|
if c.Mongo.Host != "" {
|
|
|
|
|
mod, err := notifusecase.NewModuleFromParam(notifusecase.FactoryParam{
|
|
|
|
|
MongoConf: &c.Mongo,
|
|
|
|
|
Redis: rds,
|
|
|
|
|
Config: c.Notification,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
sc.Notifier = mod.Notifier
|
|
|
|
|
sc.NotificationAdmin = mod.Admin
|
|
|
|
|
sc.NotificationRetry = notification_retry.NewRunner(mod.RetryWorker)
|
|
|
|
|
}
|
2026-05-20 13:03:59 +00:00
|
|
|
if rds != nil && rds.Zero() != nil {
|
2026-05-20 07:01:08 +00:00
|
|
|
memberMod, err := memberusecase.NewModuleFromParam(memberusecase.ModuleParam{
|
2026-05-20 13:03:59 +00:00
|
|
|
Redis: rds,
|
|
|
|
|
Config: c.Member,
|
2026-05-20 07:01:08 +00:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2026-05-20 13:03:59 +00:00
|
|
|
sc.MemberOTP = memberMod.OTP
|
|
|
|
|
sc.MemberTOTP = memberMod.TOTP
|
|
|
|
|
sc.MemberVerifyRate = memberMod.VerifyRate
|
|
|
|
|
sc.MemberProfile = memberMod.Profile
|
2026-05-20 07:01:08 +00:00
|
|
|
}
|
|
|
|
|
return sc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// StartWorkers launches background workers (notification retry, etc.).
|
|
|
|
|
func (sc *ServiceContext) StartWorkers(ctx context.Context) {
|
|
|
|
|
if sc.NotificationRetry != nil {
|
|
|
|
|
sc.NotificationRetry.Start(ctx)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// StopWorkers waits for background workers to shut down.
|
|
|
|
|
func (sc *ServiceContext) StopWorkers() {
|
|
|
|
|
if sc.NotificationRetry != nil {
|
|
|
|
|
sc.NotificationRetry.Stop()
|
2026-05-19 11:00:28 +00:00
|
|
|
}
|
|
|
|
|
}
|