233 lines
8.0 KiB
Go
233 lines
8.0 KiB
Go
package profileHandler
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime/debug"
|
|
"strings"
|
|
|
|
"gitea.cybertalant.ru/VisionCareerMiniapp/MiniappGoService/internal/application/constants"
|
|
"gitea.cybertalant.ru/VisionCareerMiniapp/MiniappGoService/internal/application/types"
|
|
"gitea.cybertalant.ru/VisionCareerMiniapp/MiniappGoService/internal/application/utils"
|
|
tele "gopkg.in/telebot.v4"
|
|
)
|
|
|
|
func (t *handler) universalTextHandler(ctx tele.Context) error {
|
|
op := "profileHandler/universalTextHandler"
|
|
// 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.Text()
|
|
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 active status and save current value
|
|
if val == constants.ResetBotCommand {
|
|
if user.ResumePath != nil {
|
|
err := t.profileService.DeleteResume(*user.ResumePath)
|
|
if err != nil {
|
|
return ctx.Send(constants.ErrorResetBotMessage)
|
|
}
|
|
}
|
|
user = &types.User{
|
|
ID: user.ID,
|
|
Username: user.Username,
|
|
ActiveStatus: constants.DefaultStatus,
|
|
TargetRole: nil,
|
|
ResumePath: nil,
|
|
WorkExperience: nil,
|
|
WorkFormat: nil,
|
|
SalaryRange: nil,
|
|
}
|
|
msg = constants.SuccessResetBotMessage
|
|
} else if val == constants.HelpBotCommand {
|
|
needUpdateUser = false
|
|
msg = constants.HelpBotMessage
|
|
} else if (user.ActiveStatus == constants.DefaultStatus) && (val == constants.StartBotCommand) {
|
|
user.ActiveStatus = constants.WaitPickJobOrMarketStatus
|
|
// Set menu
|
|
mainKeys = &tele.ReplyMarkup{ResizeKeyboard: true}
|
|
jobSearchBtn := mainKeys.Text(constants.JobSearchBotButton)
|
|
// TODO: marketAnalyticsBtn := mainKeys.Text(constants.MarketAnalyticsBotButton)
|
|
mainKeys.Reply(
|
|
mainKeys.Row(jobSearchBtn),
|
|
// TODO: mainKeys.Row(marketAnalyticsBtn),
|
|
)
|
|
|
|
msg = constants.StartBotMessage
|
|
} else if (user.ActiveStatus != constants.DefaultStatus) && (val == constants.StartBotCommand) {
|
|
needUpdateUser = false
|
|
msg = constants.SecondStartBotMessage
|
|
} else if (user.ActiveStatus == constants.WaitPickJobOrMarketStatus) && (val == constants.JobSearchBotButton) {
|
|
user.ActiveStatus = constants.WaitPickResumeStatus
|
|
// Set menu
|
|
mainKeys = &tele.ReplyMarkup{ResizeKeyboard: true}
|
|
uploadResumeBtn := mainKeys.Text(constants.UploadResumeBotButton)
|
|
skipBtn := mainKeys.Text(constants.SkipBotButton)
|
|
mainKeys.Reply(
|
|
mainKeys.Row(uploadResumeBtn),
|
|
mainKeys.Row(skipBtn),
|
|
)
|
|
|
|
msg = constants.StartProfileCompletionBotMessage
|
|
} else if (user.ActiveStatus == constants.WaitPickResumeStatus) && (val == constants.SkipBotButton) {
|
|
user.ActiveStatus = constants.WaitAnswerQuestionsStatus
|
|
// Set menu
|
|
mainKeys = &tele.ReplyMarkup{ResizeKeyboard: true}
|
|
answerQuestionsBtn := mainKeys.Text(constants.AnswerQuestionsBotButton)
|
|
mainKeys.Reply(
|
|
mainKeys.Row(answerQuestionsBtn),
|
|
)
|
|
|
|
msg = constants.SkipResumeBotMessage
|
|
} else if (user.ActiveStatus == constants.WaitPickResumeStatus) && (val == constants.UploadResumeBotButton) {
|
|
user.ActiveStatus = constants.WaitResumeStatus
|
|
// Set menu
|
|
mainKeys = &tele.ReplyMarkup{RemoveKeyboard: true}
|
|
|
|
msg = constants.WaitResumeBotMessage
|
|
} else if (user.ActiveStatus == constants.WaitResumeStatus) && (val == constants.UploadResumeBotButton) {
|
|
needUpdateUser = false
|
|
// Set menu
|
|
mainKeys = &tele.ReplyMarkup{RemoveKeyboard: true}
|
|
|
|
msg = constants.WaitResumeBotMessage
|
|
} else if (user.ActiveStatus == constants.WaitAnswerQuestionsStatus) && (val == constants.AnswerQuestionsBotButton) {
|
|
user.ActiveStatus = constants.WaitWorkFormatStatus
|
|
// Set menu
|
|
mainKeys = &tele.ReplyMarkup{ResizeKeyboard: true}
|
|
remoteBtn := mainKeys.Text(constants.RemoteWorkFormat.String())
|
|
hybridBtn := mainKeys.Text(constants.HybridWorkFormat.String())
|
|
onSiteBtn := mainKeys.Text(constants.OnSiteWorkFormat.String())
|
|
defaultBtn := mainKeys.Text(constants.DefaultWorkFormat.String())
|
|
mainKeys.Reply(
|
|
mainKeys.Row(remoteBtn),
|
|
mainKeys.Row(hybridBtn),
|
|
mainKeys.Row(onSiteBtn),
|
|
mainKeys.Row(defaultBtn),
|
|
)
|
|
|
|
msg = constants.StartAnswerQuestionsBotMessage
|
|
} else if user.ActiveStatus == constants.WaitWorkFormatStatus && constants.WorkFormat(val).Valid() {
|
|
wf := constants.WorkFormat(val)
|
|
user.WorkFormat = &wf
|
|
user.ActiveStatus = constants.WaitTargetRoleStatus
|
|
// Set menu
|
|
mainKeys = &tele.ReplyMarkup{RemoveKeyboard: true}
|
|
|
|
msg = constants.AcceptWorkFormatBotMessage
|
|
} else if user.ActiveStatus == constants.WaitTargetRoleStatus {
|
|
user.TargetRole = &val
|
|
user.ActiveStatus = constants.WaitSalaryRangeStatus
|
|
// Set menu
|
|
mainKeys = &tele.ReplyMarkup{ResizeKeyboard: true}
|
|
firstStepBtn := mainKeys.Text(constants.FirstStepSalaryRange.String())
|
|
secondStepBtn := mainKeys.Text(constants.SecondStepSalaryRange.String())
|
|
thirdStepBtn := mainKeys.Text(constants.ThirdStepSalaryRange.String())
|
|
fourthStepBtn := mainKeys.Text(constants.FourthStepSalaryRange.String())
|
|
defaultStepBtn := mainKeys.Text(constants.DefaultSalaryRange.String())
|
|
mainKeys.Reply(
|
|
mainKeys.Row(firstStepBtn),
|
|
mainKeys.Row(secondStepBtn),
|
|
mainKeys.Row(thirdStepBtn),
|
|
mainKeys.Row(fourthStepBtn),
|
|
mainKeys.Row(defaultStepBtn),
|
|
)
|
|
|
|
msg = constants.AcceptTargetRoleBotMessage
|
|
} else if user.ActiveStatus == constants.WaitSalaryRangeStatus && constants.SalaryRange(val).Valid() {
|
|
sr := constants.SalaryRange(val)
|
|
user.SalaryRange = &sr
|
|
user.ActiveStatus = constants.WaitWorkExperienceStatus
|
|
// Set menu
|
|
mainKeys = &tele.ReplyMarkup{RemoveKeyboard: true}
|
|
|
|
msg = constants.AcceptSalaryRangeBotMessage
|
|
} else if user.ActiveStatus == constants.WaitWorkExperienceStatus {
|
|
user.WorkExperience = &val
|
|
user.ActiveStatus = constants.FinishedStatus
|
|
|
|
if !utils.ValidateWorkExperience(&val) {
|
|
return ctx.Send(constants.InvalidWorkExperienceErrorBotMessage)
|
|
}
|
|
// Set menu
|
|
mainKeys = &tele.ReplyMarkup{RemoveKeyboard: true}
|
|
|
|
msg = constants.FinishedBotMessage
|
|
} else if val == constants.ShowProfileBotCommand && !utils.CheckProfileCompletion(user) {
|
|
needUpdateUser = false
|
|
// Set menu
|
|
mainKeys = &tele.ReplyMarkup{RemoveKeyboard: true}
|
|
|
|
msg = constants.CanNotShowProfileBotMessage
|
|
} else if val == constants.ShowProfileBotCommand && utils.CheckProfileCompletion(user) {
|
|
needUpdateUser = false
|
|
// Set menu
|
|
mainKeys = &tele.ReplyMarkup{RemoveKeyboard: true}
|
|
var resume string
|
|
|
|
if user.ResumePath == nil {
|
|
resume = constants.ErrorEmoji
|
|
} else {
|
|
resume = constants.SuccessEmoji
|
|
}
|
|
|
|
msg = fmt.Sprintf(
|
|
"<b>Резюме</b>: %v\n<b>Зарплатная вилка</b>: %v\n<b>Должность</b>: <i>%v</i>\n<b>Опыт работы</b>: <i>%v</i>\n<b>Формат работы</b>: %v\n",
|
|
resume, user.SalaryRange, *user.TargetRole, *user.WorkExperience, user.WorkFormat,
|
|
)
|
|
} else if val == constants.ShowVacanciesBotCommand && !utils.CheckProfileCompletion(user) {
|
|
needUpdateUser = false
|
|
// Set menu
|
|
mainKeys = &tele.ReplyMarkup{RemoveKeyboard: true}
|
|
|
|
msg = constants.CanNotShowVacanciesBotMessage
|
|
} else if val == constants.ShowVacanciesBotCommand && utils.CheckProfileCompletion(user) {
|
|
vacancies, err := t.profileService.GetVacancies(user.ID)
|
|
|
|
if err != nil {
|
|
t.logger.Error(fmt.Sprintf("%v: %v", op, err.Error()))
|
|
return ctx.Send(constants.GetVacanciesErrorBotMessage)
|
|
} else if len(vacancies) == 0 {
|
|
return ctx.Send(constants.NoVacanciesBotMessage)
|
|
}
|
|
needUpdateUser = false
|
|
// Set menu
|
|
mainKeys = &tele.ReplyMarkup{RemoveKeyboard: true}
|
|
|
|
msg = strings.Join(vacancies, "\n")
|
|
} else {
|
|
needUpdateUser = false
|
|
msg = constants.UnknownCommandBotMessage
|
|
}
|
|
// 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)
|
|
}
|