feat: logout whatsapp session and remove device
This commit is contained in:
parent
7fb899ac8b
commit
3f911ae321
|
@ -25,7 +25,7 @@ func InitWhatsApp() {
|
|||
dbLog := waLog.Stdout("Database", "DEBUG", true)
|
||||
|
||||
dsn := fmt.Sprintf(
|
||||
"postgres://%s:%s@%s:%s/%s?sslmode=disable",
|
||||
"postgres://%s:%s@%s:%s/%s?sslmode=disable",
|
||||
os.Getenv("DB_USER"),
|
||||
os.Getenv("DB_PASSWORD"),
|
||||
os.Getenv("DB_HOST"),
|
||||
|
@ -80,7 +80,6 @@ func generateQRCode(qrString string) {
|
|||
qrterminal.GenerateHalfBlock(qrString, qrterminal.M, os.Stdout)
|
||||
}
|
||||
|
||||
|
||||
func handleShutdown() {
|
||||
sigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
|
||||
|
@ -109,3 +108,40 @@ func SendWhatsAppMessage(phone, message string) error {
|
|||
log.Printf("WhatsApp message sent successfully to: %s", phone)
|
||||
return nil
|
||||
}
|
||||
|
||||
func LogoutWhatsApp() error {
|
||||
if WhatsAppClient == nil {
|
||||
return fmt.Errorf("WhatsApp client is not initialized")
|
||||
}
|
||||
|
||||
WhatsAppClient.Disconnect()
|
||||
|
||||
err := removeWhatsAppDeviceFromContainer()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to remove device from container: %v", err)
|
||||
}
|
||||
|
||||
err = container.Close()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to close database connection: %v", err)
|
||||
}
|
||||
|
||||
log.Println("WhatsApp client disconnected and session cleared successfully.")
|
||||
return nil
|
||||
}
|
||||
|
||||
func removeWhatsAppDeviceFromContainer() error {
|
||||
deviceStore, err := container.GetFirstDevice()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get WhatsApp device: %v", err)
|
||||
}
|
||||
|
||||
if deviceStore != nil {
|
||||
err := deviceStore.Delete()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to remove device from store: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"log"
|
||||
"rijig/config"
|
||||
"rijig/utils"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func WhatsAppHandler(c *fiber.Ctx) error {
|
||||
userID, ok := c.Locals("userID").(string)
|
||||
if !ok || userID == "" {
|
||||
return utils.ErrorResponse(c, "User is not logged in or invalid session")
|
||||
}
|
||||
|
||||
err := config.LogoutWhatsApp()
|
||||
if err != nil {
|
||||
log.Printf("Error during logout process for user %s: %v", userID, err)
|
||||
return utils.ErrorResponse(c, err.Error())
|
||||
}
|
||||
|
||||
return utils.SuccessResponse(c, nil, "Logged out successfully")
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package presentation
|
||||
|
||||
import (
|
||||
"rijig/internal/handler"
|
||||
"rijig/middleware"
|
||||
"rijig/utils"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func WhatsAppRouter(api fiber.Router) {
|
||||
api.Post("/logout/whastapp", middleware.AuthMiddleware, middleware.RoleMiddleware(utils.RoleAdministrator), handler.WhatsAppHandler)
|
||||
}
|
|
@ -42,5 +42,6 @@ func SetupRoutes(app *fiber.App) {
|
|||
presentation.CoverageAreaRouter(api)
|
||||
presentation.StoreRouter(api)
|
||||
presentation.ProductRouter(api)
|
||||
presentation.WhatsAppRouter(api)
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue