feat(notificacoes): envios de whatsapp dinamicos por regiao (SP e MG)
Implementa suporte a multiplas instancias da Evolution API via .env. O servico agora verifica a origiem do evento e roteia o disparo para garantir que cada franquia use seu proprio numero comercial.
This commit is contained in:
parent
a516308017
commit
a8230769e6
4 changed files with 33 additions and 14 deletions
|
|
@ -71,7 +71,12 @@ func main() {
|
|||
|
||||
// Initialize services
|
||||
// Initialize services
|
||||
notificationService := notification.NewService()
|
||||
notificationService := notification.NewService(
|
||||
cfg.EvolutionApiUrl,
|
||||
cfg.EvolutionApiKey,
|
||||
cfg.WhatsappInstanceSP,
|
||||
cfg.WhatsappInstanceMG,
|
||||
)
|
||||
profissionaisService := profissionais.NewService(queries)
|
||||
financeService := finance.NewService(queries, profissionaisService)
|
||||
authService := auth.NewService(queries, profissionaisService, cfg)
|
||||
|
|
|
|||
|
|
@ -448,7 +448,7 @@ func (s *Service) AssignProfessional(ctx context.Context, agendaID uuid.UUID, pr
|
|||
baseUrl,
|
||||
)
|
||||
|
||||
if err := s.notification.SendWhatsApp(prof.Whatsapp.String, msg); err != nil {
|
||||
if err := s.notification.SendWhatsApp(prof.Whatsapp.String, msg, agenda.Regiao.String); err != nil {
|
||||
log.Printf("[Notification] Falha ao enviar WhatsApp para %s: %v", prof.Nome, err)
|
||||
}
|
||||
}()
|
||||
|
|
@ -702,8 +702,8 @@ func (s *Service) NotifyLogistics(ctx context.Context, agendaID uuid.UUID, passe
|
|||
tipoEventoNome,
|
||||
logisticaMsg,
|
||||
)
|
||||
|
||||
if err := s.notification.SendWhatsApp(phone, msg); err != nil {
|
||||
// Passa a agendaRegiao para definir o numero
|
||||
if err := s.notification.SendWhatsApp(phone, msg, agenda.Regiao.String); err != nil {
|
||||
// Não logar erro para todos se for falha de validação de numero, mas logar warning
|
||||
log.Printf("[Notification] Erro ao enviar para %s: %v", p.Nome, err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,10 @@ type Config struct {
|
|||
S3Bucket string
|
||||
S3Region string
|
||||
FrontendURL string
|
||||
EvolutionApiUrl string
|
||||
EvolutionApiKey string
|
||||
WhatsappInstanceSP string
|
||||
WhatsappInstanceMG string
|
||||
}
|
||||
|
||||
func LoadConfig() *Config {
|
||||
|
|
@ -48,6 +52,10 @@ func LoadConfig() *Config {
|
|||
S3Bucket: getEnv("S3_BUCKET", ""),
|
||||
S3Region: getEnv("S3_REGION", "nyc1"),
|
||||
FrontendURL: getEnv("FRONTEND_URL", "http://localhost:3000"),
|
||||
EvolutionApiUrl: getEnv("EVOLUTION_API_URL", "https://others-evolution-api.nsowe9.easypanel.host"),
|
||||
EvolutionApiKey: getEnv("EVOLUTION_API_KEY", "429683C4C977415CAAFCCE10F7D57E11"),
|
||||
WhatsappInstanceSP: getEnv("WHATSAPP_INSTANCE_SP", "NANDO"),
|
||||
WhatsappInstanceMG: getEnv("WHATSAPP_INSTANCE_MG", "NANDO"),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -10,17 +10,18 @@ import (
|
|||
)
|
||||
|
||||
type Service struct {
|
||||
apiURL string
|
||||
apiKey string
|
||||
instance string
|
||||
apiURL string
|
||||
apiKey string
|
||||
instanceSP string
|
||||
instanceMG string
|
||||
}
|
||||
|
||||
func NewService() *Service {
|
||||
// Hardcoded configuration as per user request
|
||||
func NewService(apiURL, apiKey, instanceSP, instanceMG string) *Service {
|
||||
return &Service{
|
||||
apiURL: "https://others-evolution-api.nsowe9.easypanel.host",
|
||||
apiKey: "429683C4C977415CAAFCCE10F7D57E11",
|
||||
instance: "NANDO",
|
||||
apiURL: apiURL,
|
||||
apiKey: apiKey,
|
||||
instanceSP: instanceSP,
|
||||
instanceMG: instanceMG,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -29,13 +30,18 @@ type MessageRequest struct {
|
|||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
func (s *Service) SendWhatsApp(number string, message string) error {
|
||||
func (s *Service) SendWhatsApp(number string, message string, regiao string) error {
|
||||
cleanNumber := cleanPhoneNumber(number)
|
||||
if cleanNumber == "" {
|
||||
return fmt.Errorf("número de telefone inválido ou vazio")
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/message/sendText/%s", s.apiURL, s.instance)
|
||||
instance := s.instanceSP // default
|
||||
if regiao == "MG" {
|
||||
instance = s.instanceMG
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/message/sendText/%s", s.apiURL, instance)
|
||||
|
||||
payload := MessageRequest{
|
||||
Number: cleanNumber,
|
||||
|
|
|
|||
Loading…
Reference in a new issue