Skip to content

Commit

Permalink
Merge branch 'release/v0.1.0'
Browse files Browse the repository at this point in the history
* release/v0.1.0:
  feat: allows update discord id when update user profile
  fix: remove progress in earn
  • Loading branch information
namnhce committed Jun 2, 2023
2 parents 0686401 + 913e5a9 commit bbeb83b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 39 deletions.
1 change: 0 additions & 1 deletion pkg/handler/notion/earn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
60 changes: 23 additions & 37 deletions pkg/handler/profile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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, ""))
Expand Down Expand Up @@ -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,
}

Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion pkg/model/notion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
32 changes: 32 additions & 0 deletions pkg/service/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"io"
"net/http"
"strings"

"github.com/bwmarrin/discordgo"
"github.com/dwarvesf/fortress-api/pkg/config"
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/service/discord/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bbeb83b

Please sign in to comment.