diff --git a/cmd/proposal_state_service/main.go b/cmd/proposal_state_service/main.go index 9d897cc..3b759c3 100644 --- a/cmd/proposal_state_service/main.go +++ b/cmd/proposal_state_service/main.go @@ -53,7 +53,7 @@ func main() { if len(proposalsNeedToBeUpdated) == 0 { logger.Info("no proposals to update") } else { - updatedProposals := updateProposals(proposalsNeedToBeUpdated, proposalRepository, logger) + updatedProposals := updateProposals(proposalsNeedToBeUpdated, userRepository, proposalRepository, voteService, logger) for _, proposal := range updatedProposals { sendNotifications(proposal, userRepository, config, logger) @@ -86,6 +86,10 @@ func getProposalsNeedToBeUpdated( continue } + if len(votes) == 0 { + continue + } + yesVotes, noVotes := calculateYesAndNoVotes(votes, config.YesVotesToOvercomeNo) votedUsers := getVotedUsers(votes, userRepository, logger) votedSeeders := getVotedSeeders(votedUsers) @@ -120,12 +124,12 @@ func getVotedUsers(votes []services.Vote, userRepository repositories.UserReposi votedUsers := []*models.User{} for _, vote := range votes { - user, err := userRepository.GetOneByTelegramNickname(vote.Username) + user, err := userRepository.GetOneByTelegramID(vote.UserID) if err != nil { logger.Errorw("failed to get user", "error", err) continue } else if user == nil { - logger.Errorw("user not found", "username", vote.Username) + logger.Errorw("user not found", "userID", vote.UserID) continue } @@ -169,7 +173,9 @@ func updateProposalStatus( func updateProposals( proposals []*models.Proposal, + userRepository repositories.UserRepository, proposalRepository repositories.ProposalRepository, + voteService services.VoteService, logger *zap.SugaredLogger, ) []*models.Proposal { var updatedProposals []*models.Proposal @@ -179,9 +185,36 @@ func updateProposals( if err != nil { logger.Errorw("failed to update proposal", "error", err) - } else { - updatedProposals = append(updatedProposals, proposal) + continue } + + votes, err := voteService.GetVotes(proposal.Poll.ID) + if err != nil { + logger.Errorw("failed to get votes", "error", err) + continue + } + + backersIDs := make([]int64, 0) + for _, vote := range votes { + if vote.Option == "yes" { + backersIDs = append(backersIDs, vote.UserID) + } + } + + user := &models.User{ + Name: proposal.NomineeName, + TelegramNickname: proposal.NomineeTelegramNickname, + Role: models.UserRoleGuest, + BackersID: backersIDs, + } + + _, err = userRepository.Create(user) + if err != nil { + logger.Errorw("failed to create user", "error", err) + continue + } + + updatedProposals = append(updatedProposals, proposal) } return updatedProposals diff --git a/internal/db/models/user.go b/internal/db/models/user.go index 60f36fb..a04c8a5 100644 --- a/internal/db/models/user.go +++ b/internal/db/models/user.go @@ -34,6 +34,7 @@ type User struct { DiscordID int `json:"discord_id"` Role UserRole `json:"role" pg:"type:UserRole,notnull,default:'guest'"` Proposals []Proposal `json:"proposals" pg:"rel:has-many,fk:user_id"` + BackersID []int64 `json:"backers_id" pg:",array"` NominatorID int `json:"nominator_id"` TempProposal Proposal `json:"temp_proposal"` TelegramState TelegramState `json:"telegram_state"` diff --git a/internal/services/vote_service.go b/internal/services/vote_service.go index 6ccbab3..de40afb 100644 --- a/internal/services/vote_service.go +++ b/internal/services/vote_service.go @@ -18,8 +18,8 @@ type poll struct { } type Vote struct { - Username string `json:"username"` - Option string `json:"option"` + UserID int64 `json:"user_id"` + Option string `json:"option"` } type service struct { diff --git a/migrations/1_users.up.sql b/migrations/1_users.up.sql index c8f06d0..669d7c4 100644 --- a/migrations/1_users.up.sql +++ b/migrations/1_users.up.sql @@ -7,6 +7,7 @@ CREATE TABLE IF NOT EXISTS users ( telegram_nickname VARCHAR NOT NULL, discord_id BIGINT, role UserRole NOT NULL DEFAULT 'guest', + backers_id VARCHAR[] NOT NULL DEFAULT '{}'::VARCHAR[], nominator_id INTEGER, temp_proposal JSONB, telegram_state JSONB