Add leads handlers, WhatsApp integration, and test files
This commit is contained in:
96
apps/go-crm/internal/whatsapp/fake.go
Normal file
96
apps/go-crm/internal/whatsapp/fake.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package whatsapp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type FakeConnector struct {
|
||||
mu sync.RWMutex
|
||||
connected map[int64]bool
|
||||
qrChan map[int64]chan QRFrame
|
||||
qrQueue map[int64][]QRFrame
|
||||
connectErr error
|
||||
}
|
||||
|
||||
func NewFakeConnector() *FakeConnector {
|
||||
return &FakeConnector{
|
||||
connected: make(map[int64]bool),
|
||||
qrChan: make(map[int64]chan QRFrame),
|
||||
qrQueue: make(map[int64][]QRFrame),
|
||||
}
|
||||
}
|
||||
|
||||
func (f *FakeConnector) SetConnectError(err error) {
|
||||
f.connectErr = err
|
||||
}
|
||||
|
||||
func (f *FakeConnector) QueueQR(clientID int64, frames ...QRFrame) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
f.qrQueue[clientID] = append(f.qrQueue[clientID], frames...)
|
||||
}
|
||||
|
||||
func (f *FakeConnector) Connect(ctx context.Context, clientID int64) (<-chan QRFrame, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
|
||||
if f.connectErr != nil {
|
||||
return nil, f.connectErr
|
||||
}
|
||||
|
||||
// Already connected — return immediately.
|
||||
if f.connected[clientID] {
|
||||
ch := make(chan QRFrame, 1)
|
||||
ch <- QRFrame{State: StateConnected}
|
||||
close(ch)
|
||||
return ch, nil
|
||||
}
|
||||
|
||||
// QR session already in progress — reuse same channel (mirrors real adapter behaviour).
|
||||
if ch, ok := f.qrChan[clientID]; ok {
|
||||
return ch, nil
|
||||
}
|
||||
|
||||
// New session — create channel and seed with QR frame(s).
|
||||
ch := make(chan QRFrame, 20)
|
||||
f.qrChan[clientID] = ch
|
||||
|
||||
queue := f.qrQueue[clientID]
|
||||
if len(queue) == 0 {
|
||||
ch <- QRFrame{QR: "fake-qr-code-for-testing", State: StateWaitingQR}
|
||||
} else {
|
||||
for _, frame := range queue {
|
||||
ch <- frame
|
||||
}
|
||||
}
|
||||
|
||||
return ch, nil
|
||||
}
|
||||
|
||||
func (f *FakeConnector) Disconnect(ctx context.Context, clientID int64) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
|
||||
if ch, ok := f.qrChan[clientID]; ok {
|
||||
close(ch)
|
||||
delete(f.qrChan, clientID)
|
||||
}
|
||||
delete(f.connected, clientID)
|
||||
delete(f.qrQueue, clientID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *FakeConnector) IsConnected(ctx context.Context, clientID int64) (bool, error) {
|
||||
f.mu.RLock()
|
||||
defer f.mu.RUnlock()
|
||||
return f.connected[clientID], nil
|
||||
}
|
||||
|
||||
func (f *FakeConnector) MarkConnected(clientID int64) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
f.connected[clientID] = true
|
||||
}
|
||||
|
||||
func (f *FakeConnector) SetClientDB(db interface{}) {}
|
||||
Reference in New Issue
Block a user