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
|
||||||
// Initialize services
|
// Initialize services
|
||||||
notificationService := notification.NewService()
|
notificationService := notification.NewService(
|
||||||
|
cfg.EvolutionApiUrl,
|
||||||
|
cfg.EvolutionApiKey,
|
||||||
|
cfg.WhatsappInstanceSP,
|
||||||
|
cfg.WhatsappInstanceMG,
|
||||||
|
)
|
||||||
profissionaisService := profissionais.NewService(queries)
|
profissionaisService := profissionais.NewService(queries)
|
||||||
financeService := finance.NewService(queries, profissionaisService)
|
financeService := finance.NewService(queries, profissionaisService)
|
||||||
authService := auth.NewService(queries, profissionaisService, cfg)
|
authService := auth.NewService(queries, profissionaisService, cfg)
|
||||||
|
|
|
||||||
|
|
@ -448,7 +448,7 @@ func (s *Service) AssignProfessional(ctx context.Context, agendaID uuid.UUID, pr
|
||||||
baseUrl,
|
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)
|
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,
|
tipoEventoNome,
|
||||||
logisticaMsg,
|
logisticaMsg,
|
||||||
)
|
)
|
||||||
|
// Passa a agendaRegiao para definir o numero
|
||||||
if err := s.notification.SendWhatsApp(phone, msg); err != nil {
|
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
|
// 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)
|
log.Printf("[Notification] Erro ao enviar para %s: %v", p.Nome, err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,10 @@ type Config struct {
|
||||||
S3Bucket string
|
S3Bucket string
|
||||||
S3Region string
|
S3Region string
|
||||||
FrontendURL string
|
FrontendURL string
|
||||||
|
EvolutionApiUrl string
|
||||||
|
EvolutionApiKey string
|
||||||
|
WhatsappInstanceSP string
|
||||||
|
WhatsappInstanceMG string
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadConfig() *Config {
|
func LoadConfig() *Config {
|
||||||
|
|
@ -48,6 +52,10 @@ func LoadConfig() *Config {
|
||||||
S3Bucket: getEnv("S3_BUCKET", ""),
|
S3Bucket: getEnv("S3_BUCKET", ""),
|
||||||
S3Region: getEnv("S3_REGION", "nyc1"),
|
S3Region: getEnv("S3_REGION", "nyc1"),
|
||||||
FrontendURL: getEnv("FRONTEND_URL", "http://localhost:3000"),
|
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"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,15 +12,16 @@ import (
|
||||||
type Service struct {
|
type Service struct {
|
||||||
apiURL string
|
apiURL string
|
||||||
apiKey string
|
apiKey string
|
||||||
instance string
|
instanceSP string
|
||||||
|
instanceMG string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewService() *Service {
|
func NewService(apiURL, apiKey, instanceSP, instanceMG string) *Service {
|
||||||
// Hardcoded configuration as per user request
|
|
||||||
return &Service{
|
return &Service{
|
||||||
apiURL: "https://others-evolution-api.nsowe9.easypanel.host",
|
apiURL: apiURL,
|
||||||
apiKey: "429683C4C977415CAAFCCE10F7D57E11",
|
apiKey: apiKey,
|
||||||
instance: "NANDO",
|
instanceSP: instanceSP,
|
||||||
|
instanceMG: instanceMG,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -29,13 +30,18 @@ type MessageRequest struct {
|
||||||
Text string `json:"text"`
|
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)
|
cleanNumber := cleanPhoneNumber(number)
|
||||||
if cleanNumber == "" {
|
if cleanNumber == "" {
|
||||||
return fmt.Errorf("número de telefone inválido ou vazio")
|
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{
|
payload := MessageRequest{
|
||||||
Number: cleanNumber,
|
Number: cleanNumber,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue