81 lines
2.1 KiB
Go
81 lines
2.1 KiB
Go
package profileHandler
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime/debug"
|
|
|
|
"gitea.cybertalant.ru/VisionCareerMiniapp/MiniappGoService/internal/application/constants"
|
|
tele "gopkg.in/telebot.v4"
|
|
)
|
|
|
|
func (t *handler) universalDocumentHandler(ctx tele.Context) error {
|
|
op := "profileHandler/universalDocumentHandler"
|
|
// Start recovering
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
t.logger.Error(fmt.Sprintf("%v - Panic recovered: %v\nStack trace:\n%s", op, r, debug.Stack()))
|
|
}
|
|
}()
|
|
// Common variables
|
|
msg := ""
|
|
val := ctx.Message().Document
|
|
needUpdateUser := true
|
|
mainKeys := new(tele.ReplyMarkup)
|
|
// Check user
|
|
var id int64
|
|
var username *string
|
|
|
|
if ctx.Chat().Username == "" {
|
|
username = nil
|
|
} else {
|
|
username = &ctx.Chat().Username
|
|
}
|
|
id = ctx.Chat().ID
|
|
|
|
user, err := t.profileService.CheckUser(id, username)
|
|
if err != nil {
|
|
t.logger.Error(fmt.Sprintf("%v: %v", op, err.Error()))
|
|
return nil
|
|
}
|
|
// Check document and fetch file
|
|
if val == nil {
|
|
return ctx.Send(constants.BadResumeBotMessage)
|
|
} else {
|
|
reader, err := t.bot.File(&val.File)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to fetch file: %w", err)
|
|
}
|
|
defer reader.Close()
|
|
val.File.FileReader = reader
|
|
}
|
|
// Check active status and save current value
|
|
if user.ActiveStatus == constants.WaitResumeStatus {
|
|
// Save file
|
|
resumePath, err := t.profileService.SaveResume(user, val.File.FilePath, val.File.FileReader, val.File.FileSize)
|
|
if err != nil {
|
|
return ctx.Send(err.Error())
|
|
}
|
|
// Save resume file path and set new active stage
|
|
user.ResumePath = &resumePath
|
|
user.ActiveStatus = constants.WaitAnswerQuestionsStatus
|
|
// Set menu
|
|
mainKeys = &tele.ReplyMarkup{ResizeKeyboard: true}
|
|
answerQuestionsBtn := mainKeys.Text(constants.AnswerQuestionsBotButton)
|
|
mainKeys.Reply(
|
|
mainKeys.Row(answerQuestionsBtn),
|
|
)
|
|
|
|
msg = constants.UploadedResumeBotMessage
|
|
}
|
|
// Set new user data
|
|
if needUpdateUser {
|
|
err = t.profileService.UpdateUser(user)
|
|
if err != nil {
|
|
t.logger.Error(fmt.Sprintf("%v: %v", op, err.Error()))
|
|
return ctx.Send(constants.UpdateUserErrorBotMessage)
|
|
}
|
|
}
|
|
|
|
return ctx.Send(msg, mainKeys)
|
|
}
|