Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Beniamiiin committed Sep 29, 2023
1 parent 076481e commit 3918b48
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
43 changes: 38 additions & 5 deletions cmd/proposal_state_service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions internal/db/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
4 changes: 2 additions & 2 deletions internal/services/vote_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions migrations/1_users.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3918b48

Please sign in to comment.