102 lines
2.4 KiB
Go
102 lines
2.4 KiB
Go
package controller
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"ticket-pimp/internal/domain"
|
|
"ticket-pimp/internal/storage/db"
|
|
"time"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
func (wc *WorkflowController) InitTask(t *domain.Task) error {
|
|
var (
|
|
token = os.Getenv("DISCORD_TOKEN")
|
|
channel = os.Getenv("TASKS_CHANNEL")
|
|
)
|
|
discord, err := discordgo.New("Bot " + token)
|
|
if err != nil {
|
|
log.Fatalf("unable to create discord session: %v", err)
|
|
// [ ] Что делать, если не получилось создать задачу?
|
|
}
|
|
|
|
if err := discord.Open(); err != nil {
|
|
// [ ] Что делать, если не получилось создать задачу?
|
|
log.Printf("cannot open the session: %v", err)
|
|
}
|
|
|
|
// dbtask, err := wc.q.InsertTask(context.TODO(), pgtype.Text{String: st.ID, Valid: true})
|
|
dbtask, err := wc.q.InsertTask(context.TODO(), db.InsertTaskParams{
|
|
Creator: pgtype.Text{String: t.Creator, Valid: true},
|
|
CreatorLink: pgtype.Text{
|
|
String: t.CreatorLink,
|
|
Valid: true,
|
|
},
|
|
Description: pgtype.Text{
|
|
String: t.Description,
|
|
Valid: true,
|
|
},
|
|
})
|
|
if err != nil {
|
|
log.Println("unable to insert task")
|
|
}
|
|
|
|
content := fmt.Sprintf(
|
|
"## TaskID: %d\nCreated by: %s\n\n%s",
|
|
dbtask.ID,
|
|
t.Creator,
|
|
t.Description,
|
|
)
|
|
|
|
msg := discordgo.MessageSend{
|
|
Content: content,
|
|
Components: []discordgo.MessageComponent{
|
|
discordgo.ActionsRow{
|
|
Components: []discordgo.MessageComponent{
|
|
discordgo.Button{
|
|
Label: "Start",
|
|
Style: discordgo.SuccessButton,
|
|
Disabled: false,
|
|
CustomID: "task_start",
|
|
},
|
|
discordgo.Button{
|
|
Label: "Close",
|
|
Style: discordgo.DangerButton,
|
|
Disabled: true,
|
|
CustomID: "task_close",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
st, err := discord.ChannelMessageSendComplex(channel, &msg)
|
|
if err != nil {
|
|
log.Println("unable to send task message")
|
|
}
|
|
|
|
_ = dbtask
|
|
_ = st
|
|
return err
|
|
|
|
}
|
|
|
|
func (wc *WorkflowController) UpdateTask(id string, opt int) {
|
|
switch opt {
|
|
case 0:
|
|
wc.q.StartTask(context.TODO(), db.StartTaskParams{
|
|
StartedAt: pgtype.Timestamptz{Time: time.Now(), InfinityModifier: 0, Valid: true},
|
|
Messageid: pgtype.Text{String: id, Valid: true},
|
|
})
|
|
case 1:
|
|
wc.q.CloseTask(context.TODO(), db.CloseTaskParams{
|
|
ClosedAt: pgtype.Timestamptz{Time: time.Now(), InfinityModifier: 0, Valid: true},
|
|
Messageid: pgtype.Text{String: id, Valid: true},
|
|
})
|
|
}
|
|
}
|