diff --git a/domain/room.go b/domain/room.go index bea2a581..4027c79e 100644 --- a/domain/room.go +++ b/domain/room.go @@ -41,6 +41,7 @@ type RoomRepository interface { GetRoom(roomID uuid.UUID, excludeEventID uuid.UUID) (*Room, error) GetAllRooms(start time.Time, end time.Time, excludeEventID uuid.UUID) ([]*Room, error) + GetAllVerifiedRooms(start time.Time, end time.Time, excludeEventID uuid.UUID) ([]*Room, error) IsRoomAdmins(roomID uuid.UUID, info *ConInfo) bool } diff --git a/infra/db/room.go b/infra/db/room.go index 9b385e2f..4233586e 100644 --- a/infra/db/room.go +++ b/infra/db/room.go @@ -16,7 +16,6 @@ func roomFullPreload(tx *gorm.DB) *gorm.DB { return tx.Preload("Events").Preload("Admins").Preload("CreatedBy") } - //go:generate go run github.com/fuji8/gotypeconverter/cmd/gotypeconverter@latest -s CreateRoomParams -d Room -o converter.go . type CreateRoomParams struct { domain.WriteRoomParams @@ -25,7 +24,6 @@ type CreateRoomParams struct { CreatedBy uuid.UUID } - //go:generate go run github.com/fuji8/gotypeconverter/cmd/gotypeconverter@latest -s UpdateRoomParams -d Room -o converter.go . type UpdateRoomParams struct { domain.WriteRoomParams @@ -89,6 +87,21 @@ func (repo GormRepository) GetAllRooms(start, end time.Time, excludeEventID uuid return r, nil } +func (repo GormRepository) GetAllVerifiedRooms(start, end time.Time, excludeEventID uuid.UUID) ([]*domain.Room, error) { + var rooms []*Room + var err error + if excludeEventID == uuid.Nil { + rooms, err = getAllVerifiedRooms(roomFullPreload(repo.db), start, end) + } else { + rooms, err = getAllVerifiedRooms(roomExcludeEventPreload(repo.db, excludeEventID), start, end) + } + if err != nil { + return nil, defaultErrorHandling(err) + } + r := ConvSPRoomToSPdomainRoom(rooms) + return r, nil +} + func createRoom(db *gorm.DB, roomParams CreateRoomParams) (*Room, error) { room := ConvCreateRoomParamsToRoom(roomParams) err := db.Create(&room).Error @@ -132,3 +145,18 @@ func getAllRooms(db *gorm.DB, start, end time.Time) ([]*Room, error) { err := db.Debug().Order("time_start").Find(&rooms).Error return rooms, err } + +func getAllVerifiedRooms(db *gorm.DB, start, end time.Time) ([]*Room, error) { + rooms := make([]*Room, 0) + if !start.IsZero() { + db = db.Where("time_start >= ?", start) + } + if !end.IsZero() { + db = db.Where("time_end <= ?", end) + } + + db.Where("verified = ?", true) + + err := db.Debug().Order("time_start").Find(&rooms).Error + return rooms, err +} diff --git a/repository/room.go b/repository/room.go index c076e851..3d2883e1 100644 --- a/repository/room.go +++ b/repository/room.go @@ -79,6 +79,11 @@ func (repo *Repository) GetAllRooms(start time.Time, end time.Time, excludeEvent return rs, defaultErrorHandling(err) } +func (repo *Repository) GetAllVerifiedRooms(start time.Time, end time.Time, excludeEventID uuid.UUID) ([]*domain.Room, error) { + rs, err := repo.GormRepo.GetAllVerifiedRooms(start, end, excludeEventID) + return rs, defaultErrorHandling(err) +} + func (repo *Repository) IsRoomAdmins(roomID uuid.UUID, info *domain.ConInfo) bool { room, err := repo.GetRoom(roomID, uuid.Nil) if err != nil { diff --git a/router/middleware.go b/router/middleware.go index 77feb07d..367f8f23 100644 --- a/router/middleware.go +++ b/router/middleware.go @@ -3,6 +3,7 @@ package router import ( "encoding/json" "errors" + "fmt" "net/http" "strconv" "time" @@ -11,6 +12,7 @@ import ( "github.com/traPtitech/knoQ/router/logging" "github.com/traPtitech/knoQ/router/presentation" "github.com/traPtitech/knoQ/utils" + "github.com/traPtitech/knoQ/utils/tz" "github.com/gofrs/uuid" "go.uber.org/zap" @@ -225,6 +227,37 @@ func (h *Handlers) WebhookEventHandler(c echo.Context, reqBody, resBody []byte) _ = utils.RequestWebhook(content, h.WebhookSecret, h.ActivityChannelID, h.WebhookID, 1) } +func (h *Handlers) WebhookRoomHandler(next echo.HandlerFunc) echo.HandlerFunc { + return func(c echo.Context) error { + s, _ := time.Parse(time.TimeOnly, "06:00:00") + t := time.Now().In(tz.JST) + now := time.Date(t.Year(), t.Month(), t.Day(), s.Hour(), s.Minute(), s.Second(), 0, tz.JST) + tomorrow := now.AddDate(0, 0, 1) + + todaysRoomsBefore, _ := h.Repo.GetAllVerifiedRooms(now, tomorrow, uuid.Nil) + + println("RoomsBefore") + for _, room := range todaysRoomsBefore { + fmt.Printf("%v\n", *room) + } + + err := next(c) + + todaysRoomsAfter, _ := h.Repo.GetAllVerifiedRooms(now, tomorrow, uuid.Nil) + + println("RoomsAfter") + for _, room := range todaysRoomsAfter { + fmt.Printf("%v\n", *room) + } + + content := utils.GenerateTodaysEventContent(now, todaysRoomsAfter, nil, h.Origin) + + print(content) + + return err + } +} + // getRequestUserID sessionからuserを返します func getRequestUserID(c echo.Context) (uuid.UUID, error) { sess, err := session.Get("session", c) @@ -281,3 +314,8 @@ func setMaxAgeMinus(c echo.Context) { } c.SetCookie(sess) } + +func isTodaysVerifiedRoomsUpdated(todaysRoomsBefore, todaysRoomsAfter []*domain.Room) bool { + + return false +} diff --git a/router/router.go b/router/router.go index 9855d1f1..4a4e6a11 100644 --- a/router/router.go +++ b/router/router.go @@ -114,7 +114,7 @@ func (h *Handlers) SetupRoute() *echo.Echo { // サービス管理者権限が必要 roomsAPIWithPrivilegeAuth := roomsAPI.Group("", h.PrivilegeUserMiddleware) { - roomsAPIWithPrivilegeAuth.POST("/all", h.HandleCreateVerifedRooms) + roomsAPIWithPrivilegeAuth.POST("/all", h.HandleCreateVerifedRooms, h.WebhookRoomHandler) roomsAPIWithPrivilegeAuth.POST("/:roomid/verified", h.HandleVerifyRoom) roomsAPIWithPrivilegeAuth.DELETE("/:roomid/verified", h.HandleUnVerifyRoom) } diff --git a/utils/cron.go b/utils/cron.go index 85677b6a..4083626e 100644 --- a/utils/cron.go +++ b/utils/cron.go @@ -32,7 +32,7 @@ func InitPostEventToTraQ(repo *db.GormRepository, secret, channelID, webhookID, fmt.Println(err) } events, _ := repo.GetAllEvents(expr) - message := createMessage(now, rooms, events, origin) + message := GenerateTodaysEventContent(now, rooms, events, origin) err = RequestWebhook(message, secret, channelID, webhookID, 1) if err != nil { fmt.Println(err) @@ -99,7 +99,7 @@ func makeRoomAvailableByTimeTable(rooms []*domain.Room, timeTables []timeTable, return roomAvailable } -func createMessage(t time.Time, rooms []*domain.Room, events []*db.Event, origin string) string { +func GenerateTodaysEventContent(t time.Time, rooms []*domain.Room, events []*db.Event, origin string) string { date := t.In(tz.JST).Format("01/02(Mon)") combined := map[bool]string{ true: "(併用可)",