diff --git a/pkg/handler/notion/earn.go b/pkg/handler/notion/earn.go index cf9e918fc..a8859abcd 100644 --- a/pkg/handler/notion/earn.go +++ b/pkg/handler/notion/earn.go @@ -85,7 +85,6 @@ func (h *handler) ListEarns(c *gin.Context) { ID: r.ID, Name: name, Reward: int(*props["Reward 🧊"].Number), - Progress: int(*props["Progress"].Number * 100), Tags: tags, PICs: employees, Status: props["Status"].Status.Name, diff --git a/pkg/handler/profile/profile.go b/pkg/handler/profile/profile.go index f02a7a174..dbb5b96da 100644 --- a/pkg/handler/profile/profile.go +++ b/pkg/handler/profile/profile.go @@ -3,15 +3,13 @@ package profile import ( "errors" "fmt" - "net/http" - "path/filepath" - "strings" - "github.com/bwmarrin/discordgo" "github.com/gin-gonic/gin" "golang.org/x/exp/slices" "gorm.io/gorm" "gorm.io/gorm/utils" + "net/http" + "path/filepath" "github.com/dwarvesf/fortress-api/pkg/config" "github.com/dwarvesf/fortress-api/pkg/controller" @@ -152,16 +150,30 @@ func (h *handler) UpdateInfo(c *gin.Context) { } tx, done := h.repo.NewTransaction() + // Get discord info + discordMember, err := h.service.Discord.GetMemberByUsername(input.DiscordName) + if err != nil { + l.Error(err, "failed to get discord info") + c.JSON(http.StatusInternalServerError, view.CreateResponse[any](nil, nil, done(err), input, "")) + return + } + + discordID := "" + if discordMember != nil { + discordID = discordMember.User.ID + } // Update social accounts saInput := socialAccountInput{ GithubID: input.GithubID, NotionID: input.NotionID, + DiscordID: discordID, NotionName: input.NotionName, NotionEmail: input.NotionEmail, DiscordName: input.DiscordName, LinkedInName: input.LinkedInName, } + if err := h.updateSocialAccounts(tx.DB(), saInput, employee.ID); err != nil { l.Error(err, "failed to update employee") c.JSON(http.StatusInternalServerError, view.CreateResponse[any](nil, nil, done(err), input, "")) @@ -758,19 +770,24 @@ func (h *handler) SubmitOnboardingForm(c *gin.Context) { } // Get discord info - discordMember, err := h.getDiscordInfo(input.DiscordName) + discordMember, err := h.service.Discord.GetMemberByUsername(input.DiscordName) if err != nil { l.Error(err, "failed to get discord info") c.JSON(http.StatusInternalServerError, view.CreateResponse[any](nil, nil, done(err), input, "")) return } + discordID := "" + if discordMember != nil { + discordID = discordMember.User.ID + } + // Update social accounts saInput := socialAccountInput{ GithubID: input.GithubID, NotionName: input.NotionName, DiscordName: input.DiscordName, - DiscordID: discordMember.User.ID, + DiscordID: discordID, LinkedInName: input.LinkedInName, } @@ -878,37 +895,6 @@ func (h *handler) createBasecampAccount(employee *model.Employee) error { return nil } -func (h *handler) getDiscordInfo(discordName string) (*discordgo.Member, error) { - if len(discordName) == 0 { - return nil, nil - } - - discordNameParts := strings.Split(discordName, "#") - - guildMembers, err := h.service.Discord.SearchMember(discordNameParts[0]) - if err != nil { - return nil, err - } - - var discordMember *discordgo.Member - for _, m := range guildMembers { - if len(discordNameParts) == 1 { - if m.User.Username == discordNameParts[0] { - discordMember = m - } - break - } - if len(discordNameParts) > 1 { - if m.User.Username == discordNameParts[0] && m.User.Discriminator == discordNameParts[1] { - discordMember = m - } - break - } - } - - return discordMember, nil -} - func (h *handler) assignDiscordRole(discordMember *discordgo.Member) error { if discordMember == nil { return errs.ErrInvalidDiscordMemberInfo diff --git a/pkg/model/notion.go b/pkg/model/notion.go index 0fa006bf1..872f657d6 100644 --- a/pkg/model/notion.go +++ b/pkg/model/notion.go @@ -29,7 +29,6 @@ type NotionEarn struct { ID string `json:"id"` Name string `json:"name"` Reward int `json:"reward"` - Progress int `json:"progress"` Priority string `json:"priority"` Tags []string `json:"tags"` PICs []Employee `json:"pics"` diff --git a/pkg/service/discord/discord.go b/pkg/service/discord/discord.go index 57bd78185..b5cbf8ce7 100644 --- a/pkg/service/discord/discord.go +++ b/pkg/service/discord/discord.go @@ -5,6 +5,7 @@ import ( "encoding/json" "io" "net/http" + "strings" "github.com/bwmarrin/discordgo" "github.com/dwarvesf/fortress-api/pkg/config" @@ -192,6 +193,37 @@ func (d *discordClient) RemoveRole(userID string, roleID string) error { return d.session.GuildMemberRoleRemove(d.cfg.Discord.IDs.DwarvesGuild, userID, roleID) } +func (d *discordClient) GetMemberByUsername(username string) (*discordgo.Member, error) { + if len(username) == 0 { + return nil, nil + } + + discordNameParts := strings.Split(username, "#") + + guildMembers, err := d.SearchMember(discordNameParts[0]) + if err != nil { + return nil, err + } + + var discordMember *discordgo.Member + for _, m := range guildMembers { + if len(discordNameParts) == 1 { + if m.User.Username == discordNameParts[0] { + discordMember = m + } + break + } + if len(discordNameParts) > 1 { + if m.User.Username == discordNameParts[0] && m.User.Discriminator == discordNameParts[1] { + discordMember = m + } + break + } + } + + return discordMember, nil +} + type Roles discordgo.Roles func (r Roles) DwarvesRoles() []*discordgo.Role { diff --git a/pkg/service/discord/service.go b/pkg/service/discord/service.go index ebe230d3f..fb5b91f8d 100644 --- a/pkg/service/discord/service.go +++ b/pkg/service/discord/service.go @@ -10,6 +10,7 @@ type IService interface { PostBirthdayMsg(msg string) (model.DiscordMessage, error) GetMembers() ([]*discordgo.Member, error) GetMember(userID string) (*discordgo.Member, error) + GetMemberByUsername(username string) (*discordgo.Member, error) SearchMember(discordName string) ([]*discordgo.Member, error) GetRoles() (Roles, error) AddRole(userID, roleID string) error