Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ services:
NS_MARIADB_DATABASE: app
TRAQ_ORIGIN: ${TRAQ_ORIGIN}
TRAQ_BOT_TOKEN: ${TRAQ_BOT_TOKEN}
CREATE_TICKET_CHANNEL_ID: ${CREATE_TICKET_CHANNEL_ID}
TRAQ_USER_MANAGER_ID: ${TRAQ_USER_MANAGER_ID:-ramdos}
CREATE_TICKET_CHANNEL_ID: ${CREATE_TICKET_CHANNEL_ID:-019b3b02-3d31-7439-9069-d28f5de68f47}
TRAQ_CHANNEL_TICKET_CREATE: ${TRAQ_CHANNEL_TICKET_CREATE:-019b3b02-3d31-7439-9069-d28f5de68f47}
TRAQ_CHANNEL_TICKET_UPDATE: ${TRAQ_CHANNEL_TICKET_UPDATE:-019b3b02-3d31-7439-9069-d28f5de68f47}
TRAQ_CHANNEL_NOTE_INCOMING: ${TRAQ_CHANNEL_NOTE_INCOMING:-019b3b02-3d31-7439-9069-d28f5de68f47}
TRAQ_CHANNEL_NOTE_OUTGOING: ${TRAQ_CHANNEL_NOTE_OUTGOING:-019b3b02-3d31-7439-9069-d28f5de68f47}
TRAQ_CHANNEL_NOTE_OTHER: ${TRAQ_CHANNEL_NOTE_OTHER:-019b3b02-3d31-7439-9069-d28f5de68f47}
TRAQ_CHANNEL_REVIEW_NOTIFY: ${TRAQ_CHANNEL_REVIEW_NOTIFY:-019b3b02-3d31-7439-9069-d28f5de68f47}

depends_on:
db:
condition: service_healthy
Expand All @@ -31,8 +39,8 @@ services:
environment:
MYSQL_ROOT_PASSWORD: pass
MYSQL_DATABASE: app
expose:
- "3306"
ports:
- "3306:3306"
healthcheck:
test: mariadb --user=root --password=pass --execute "SHOW DATABASES;"
interval: 1s
Expand All @@ -45,4 +53,4 @@ services:
- "8081:8080"
depends_on:
db:
condition: service_healthy
condition: service_healthy
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/ogen-go/ogen v1.18.0
github.com/pressly/goose/v3 v3.26.0
github.com/ras0q/goalie v0.6.0
github.com/robfig/cron/v3 v3.0.1
github.com/sashabaranov/go-openai v1.41.2
github.com/traPtitech/go-traq v0.0.0-20251201015624-285ca186fc5e
github.com/traPtitech/traq-ws-bot v1.2.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ github.com/ras0q/goalie v0.6.0 h1:/Lr0+z3UKOEyfg37TaRIubVJ6LPHCu35tceoUDz8OFw=
github.com/ras0q/goalie v0.6.0/go.mod h1:73FIdgMtezqT7ckMo9Vu70D1E0V4qAaPkNkBA+KCzPQ=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
Expand Down
12 changes: 10 additions & 2 deletions infrastructure/injector/injector.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package injector

import (
"fmt"

"github.com/jmoiron/sqlx"
"github.com/traP-jp/anshin-techo-backend/internal/api"
"github.com/traP-jp/anshin-techo-backend/internal/handler"
Expand All @@ -14,8 +16,14 @@ type Dependencies struct {
}

func InjectServer(deps Dependencies) (*api.Server, error) {
repo := repository.New(deps.DB, deps.Bot)
h := handler.New(repo)
repo := repository.New(deps.DB) //, deps.Bot)

botService, ok := deps.Bot.(*bot.Service)
if !ok {
return nil, fmt.Errorf("failed to cast bot client to service")
}

h := handler.New(repo, botService)
s, err := api.NewServer(h, h)
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@ import (

"github.com/labstack/echo/v4"
"github.com/traP-jp/anshin-techo-backend/internal/api"
"github.com/traP-jp/anshin-techo-backend/internal/service/bot"
"github.com/traP-jp/anshin-techo-backend/internal/repository"
)

type Handler struct {
repo *repository.Repository
bot *bot.Service
}

func New(
repo *repository.Repository,
bot *bot.Service,
) *Handler {
return &Handler{
//photo,
repo: repo,
bot: bot,
}
}

Expand Down
58 changes: 54 additions & 4 deletions internal/handler/tickets.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ func (h *Handler) CreateTicket(ctx context.Context, req *api.CreateTicketReq) (a
return nil, fmt.Errorf("get created ticket from repository: %w", err)
}

go func() {
defer func() {
if r := recover(); r != nil {
fmt.Printf("[PANIC] Recovered in NotifyTicketCreated: %v\n", r)
}
}()

if h.bot == nil {
fmt.Printf("[Error] h.bot is nil! Notification skipped.\n")
return
}

bgCtx := context.Background()
if err := h.bot.NotifyTicketCreated(bgCtx, ticket); err != nil {
fmt.Printf("failed to notify ticket creation: %v\n", err)
}
}()

res := &api.Ticket{
ID: ticket.ID,
Title: ApplyCensorIfNeed(role, ticket.Title),
Expand Down Expand Up @@ -230,9 +248,9 @@ func (h *Handler) GetTicketByID(ctx context.Context, params api.GetTicketByIDPar
}
res := &api.GetTicketByIDOK{
ID: ticket.ID,
Title: ApplyCensorIfNeed(role, ticket.Title),
Title: ApplyCensorIfNeed(role, ticket.Title),
Description: api.OptString{
Value: ApplyCensorIfNeed(role, ticket.Description.String),
Value: ApplyCensorIfNeed(role, ticket.Description.String),
Set: ticket.Description.Valid,
},
Due: api.OptNilDate{Value: ticket.Due.Time, Set: ticket.Due.Valid, Null: !ticket.Due.Valid},
Expand Down Expand Up @@ -354,6 +372,38 @@ func (h *Handler) UpdateTicketByID(ctx context.Context, req api.OptUpdateTicketB
return nil, fmt.Errorf("update ticket in repository: %w", err)
}

updatedTicket, err := h.repo.GetTicketByID(ctx, id)
if err != nil {

fmt.Printf("[ERROR] failed to fetch updated ticket for notification: %v\n", err)
} else {

go func() {
defer func() {
if r := recover(); r != nil {
fmt.Printf("[PANIC] Recovered in NotifyTicketUpdated: %v\n", r)
}
}()

fmt.Println("============ BOT DEBUG START ============")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

デバッグ出力は消しておいて欲しいです!

if h.bot == nil {
fmt.Println("[DEBUG] h.bot is NIL! ")
fmt.Println("============ BOT DEBUG END ============")
return
}
fmt.Println("[DEBUG] h.bot is ALIVE! ")

fmt.Println("[DEBUG] Sending notification to traQ...")

err := h.bot.NotifyTicketUpdated(context.Background(), updatedTicket)
if err != nil {
fmt.Printf("[ERROR] Notification FAILED: %v\n", err)
} else {
fmt.Println("[SUCCESS] Notification SENT successfully! ")
}
fmt.Println("============ BOT DEBUG END ============")
}()
}
return &api.UpdateTicketByIDOK{}, nil
}

Expand Down Expand Up @@ -384,7 +434,7 @@ func convertRepositoryNote(note *repository.Note, reviews []*repository.Review,
Type: noteType,
Status: api.OptNoteStatus{Value: noteStatus, Set: true},
Author: note.UserID,
Content: ApplyCensorIfNeed(role, note.Content),
Content: ApplyCensorIfNeed(role, note.Content),
Reviews: apiReviews,
CreatedAt: api.OptDateTime{
Value: note.CreatedAt,
Expand Down Expand Up @@ -425,4 +475,4 @@ func toAPINoteStatus(status string) (api.NoteStatus, error) {
default:
return "", fmt.Errorf("unknown note status: %s", status)
}
}
}
6 changes: 5 additions & 1 deletion internal/repository/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ func (r *Repository) GetConfig(ctx context.Context) (*Config, error) {
RevisePrompt string `db:"revise_prompt"`
NotesentHour int `db:"notesent_hour"`
OverdueDay []byte `db:"overdue_day"`
}{}
}{
RevisePrompt: "",
NotesentHour: 0,
OverdueDay: nil,
}

if err := r.db.GetContext(ctx, &row, `SELECT revise_prompt, notesent_hour, overdue_day FROM configs WHERE id = 1`); err != nil {
if err == sql.ErrNoRows {
Expand Down
12 changes: 7 additions & 5 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package repository

import (
"github.com/jmoiron/sqlx"
"github.com/traP-jp/anshin-techo-backend/internal/service/bot"
)

type Repository struct {
db *sqlx.DB
bot bot.Client
//bot bot.Client
}

func New(db *sqlx.DB, botClient bot.Client) *Repository {
return &Repository{db: db, bot: botClient}
}
// func New(db *sqlx.DB, botClient bot.Client) *Repository {
// return &Repository{db: db, bot: botClient}
// }
func New(db *sqlx.DB, ) *Repository {
return &Repository{db: db}
}
35 changes: 27 additions & 8 deletions internal/repository/tickets.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"database/sql"
"fmt"
"os"
"strings"
"time"
)
Expand Down Expand Up @@ -247,13 +246,13 @@ func (r *Repository) CreateTicket(ctx context.Context, params CreateTicketParams
if err := tx.Commit(); err != nil {
return 0, fmt.Errorf("failed to commit transaction: %w", err)
}
botMessage := fmt.Sprintf("## 新しいチケット(ID: %d)が作成されました\nタイトル: %s\n担当者: @%s\n副担当: %v\n関係者: %v\nタグ: %v\n締め切り: %v\n%s", ticketID, params.Title, params.Assignee, params.SubAssignees, params.Stakeholders, params.Tags, params.Due.Time, params.Description.String)
if err := r.bot.PostMessage(ctx, os.Getenv("CREATE_TICKET_CHANNEL_ID"), botMessage); err != nil {
fmt.Printf("failed to send ticket creation notification: %v\n", err)
}
if err := r.bot.PostDirectMessage(ctx, "2e0c6679-166f-455a-b8b0-35cdfd257256", botMessage); err != nil {
fmt.Printf("failed to send ticket creation notification: %v\n", err)
}
// botMessage := fmt.Sprintf("## 新しいチケット(ID: %d)が作成されました\nタイトル: %s\n担当者: @%s\n副担当: %v\n関係者: %v\nタグ: %v\n締め切り: %v\n%s", ticketID, params.Title, params.Assignee, params.SubAssignees, params.Stakeholders, params.Tags, params.Due.Time, params.Description.String)
// if err := r.bot.PostMessage(ctx, os.Getenv("CREATE_TICKET_CHANNEL_ID"), botMessage); err != nil {
// fmt.Printf("failed to send ticket creation notification: %v\n", err)
// }
// if err := r.bot.PostDirectMessage(ctx, "2e0c6679-166f-455a-b8b0-35cdfd257256", botMessage); err != nil {
// fmt.Printf("failed to send ticket creation notification: %v\n", err)
// }

return ticketID, nil
}
Expand Down Expand Up @@ -411,3 +410,23 @@ func (r *Repository) DeleteTicket(ctx context.Context, ticketID int64) error {

return nil
}

func (r *Repository) GetIncompleteTickets(ctx context.Context) ([]Ticket, error) {
var tickets []Ticket
query := `SELECT * FROM tickets WHERE status NOT IN ('completed', 'forgotten')`
if err := r.db.SelectContext(ctx, &tickets, query); err != nil {
return nil, err
}

return tickets, nil
}

func (r *Repository) GetTicketsByStatus(ctx context.Context, status string) ([]Ticket, error) {
var tickets []Ticket
query := `SELECT * FROM tickets WHERE status = ?`
if err := r.db.SelectContext(ctx, &tickets, query, status); err != nil {
return nil, err
}

return tickets, nil
}
Loading