58 lines
1.6 KiB
Go
58 lines
1.6 KiB
Go
package notification
|
|
|
|
import (
|
|
"jannex/telegram-bot-manager/modules/database"
|
|
"jannex/telegram-bot-manager/modules/logger"
|
|
"jannex/telegram-bot-manager/modules/structs"
|
|
"jannex/telegram-bot-manager/modules/telegram"
|
|
"jannex/telegram-bot-manager/modules/utils"
|
|
|
|
"git.ex.umbach.dev/Alex/roese-utils/rslogger"
|
|
"git.ex.umbach.dev/Alex/roese-utils/rsutils"
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func SendNotification(c *fiber.Ctx) error {
|
|
// swagger:operation POST /v1/notification notification sendNotification
|
|
// ---
|
|
// summary: Send notification to users
|
|
// consumes:
|
|
// - application/json
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - name: body
|
|
// in: body
|
|
// description: Notification body
|
|
// required: true
|
|
// schema:
|
|
// "$ref": "#/definitions/NotificationBody"
|
|
// responses:
|
|
// '200':
|
|
// description: OK
|
|
// '400':
|
|
// description: Bad request
|
|
|
|
var body structs.NotificationBody
|
|
|
|
if err := rsutils.BodyParserHelper(c, &body); err != nil {
|
|
return c.SendStatus(fiber.StatusBadRequest)
|
|
}
|
|
|
|
var foundVerifiedUsers []structs.VerifiedUser
|
|
|
|
// get all verified users by body userIds
|
|
database.DB.Where("user_id IN ?", body.UserIds).Find(&foundVerifiedUsers)
|
|
|
|
formattedMessage := utils.GetNotificationIconByType(body.Type) + " " + body.Message
|
|
|
|
// send message to all verified users
|
|
for _, user := range foundVerifiedUsers {
|
|
telegram.SendNotification(int64(user.ChatId), formattedMessage)
|
|
}
|
|
|
|
logger.AddSystemLog(rslogger.LogTypeInfo, "Sent notification: %s to %d users: %v", formattedMessage, len(foundVerifiedUsers), utils.GetUserIds(foundVerifiedUsers))
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
}
|