Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update main #18

Merged
merged 5 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func main() {
groupSvc := group.NewService(groupRepo, userRepo, cacheRepo, &conf.Group, logger.Named("groupSvc"))

selectionRepo := selection.NewRepository(db)
selectionSvc := selection.NewService(selectionRepo, cacheRepo, &conf.Selection, logger.Named("selectionSvc"))
selectionSvc := selection.NewService(selectionRepo, groupRepo, cacheRepo, &conf.Selection, logger.Named("selectionSvc"))

countRepo := count.NewRepository(db)
countSvc := count.NewService(countRepo, logger.Named("countSvc"))
Expand Down
21 changes: 21 additions & 0 deletions internal/group/group.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ func (s *serviceImpl) DeleteMember(_ context.Context, in *proto.DeleteMemberGrou
return nil, err
}

if group.IsConfirmed {
s.log.Named("DeleteMember").Error("Group is confirmed", zap.String("user_id", in.UserId))
return nil, status.Error(codes.PermissionDenied, "Group is confirmed, so you cannot delete member")
}

if in.LeaderId != group.LeaderID.String() {
s.log.Named("DeleteMember").Error("Requested leader_id is not leader of this group", zap.String("leader_id", in.LeaderId))
return nil, status.Error(codes.PermissionDenied, "requested leader_id is not leader of this group")
Expand Down Expand Up @@ -263,6 +268,11 @@ func (s *serviceImpl) Leave(_ context.Context, in *proto.LeaveGroupRequest) (*pr
return nil, err
}

if group.IsConfirmed {
s.log.Named("Leave").Error("Group is confirmed", zap.String("user_id", in.UserId))
return nil, status.Error(codes.PermissionDenied, "Group is confirmed, so you cannot leave")
}

if in.UserId == group.LeaderID.String() {
s.log.Named("Leave").Error("User is the leader of the group", zap.String("user_id", in.UserId))
return nil, status.Error(codes.PermissionDenied, "You are the group leader, so you cannot leave")
Expand Down Expand Up @@ -328,6 +338,17 @@ func (s *serviceImpl) Leave(_ context.Context, in *proto.LeaveGroupRequest) (*pr
}

func (s *serviceImpl) Join(_ context.Context, in *proto.JoinGroupRequest) (*proto.JoinGroupResponse, error) {
group, err := s.findByUserId(in.UserId)
if err != nil {
s.log.Named("Join").Error("findByUserId group: ", zap.Error(err))
return nil, err
}

if group.IsConfirmed {
s.log.Named("Join").Error("Group is confirmed", zap.String("user_id", in.UserId))
return nil, status.Error(codes.PermissionDenied, "Group is confirmed, so you cannot leave to join other groups")
}

joiningGroup := &model.Group{}
if err := s.repo.FindByToken(in.Token, joiningGroup); err != nil {
s.log.Named("Join").Error("FindByToken joiningGroup TX: ", zap.Error(err))
Expand Down
65 changes: 54 additions & 11 deletions internal/selection/selection.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/google/uuid"
"github.com/isd-sgcu/rpkm67-backend/config"
"github.com/isd-sgcu/rpkm67-backend/internal/cache"
"github.com/isd-sgcu/rpkm67-backend/internal/group"
proto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/selection/v1"
"github.com/isd-sgcu/rpkm67-model/model"
"go.uber.org/zap"
Expand All @@ -20,22 +21,34 @@ type Service interface {

type serviceImpl struct {
proto.UnimplementedSelectionServiceServer
repo Repository
cache cache.Repository
conf *config.SelectionConfig
log *zap.Logger
repo Repository
groupRepo group.Repository
cache cache.Repository
conf *config.SelectionConfig
log *zap.Logger
}

func NewService(repo Repository, cache cache.Repository, conf *config.SelectionConfig, log *zap.Logger) Service {
func NewService(repo Repository, groupRepo group.Repository, cache cache.Repository, conf *config.SelectionConfig, log *zap.Logger) Service {
return &serviceImpl{
repo: repo,
cache: cache,
conf: conf,
log: log,
repo: repo,
groupRepo: groupRepo,
cache: cache,
conf: conf,
log: log,
}
}

func (s *serviceImpl) Create(ctx context.Context, in *proto.CreateSelectionRequest) (*proto.CreateSelectionResponse, error) {
isConfirmed, err := s.isGroupConfirmed(in.GroupId)
if err != nil {
s.log.Named("Create").Error(fmt.Sprintf("isGroupConfirmed: group_id=%s", in.GroupId), zap.Error(err))
return nil, status.Error(codes.Internal, err.Error())
}
if isConfirmed {
s.log.Named("Create").Error(fmt.Sprintf("Failed to create selection: group_id=%s", in.GroupId), zap.Error(err))
return nil, status.Error(codes.InvalidArgument, "Group is confirmed, cannot create selection")
}

groupUUID, err := uuid.Parse(in.GroupId)
if err != nil {
s.log.Named("Create").Error(fmt.Sprintf("Parse group id: %s", in.GroupId), zap.Error(err))
Expand Down Expand Up @@ -130,7 +143,17 @@ func (s *serviceImpl) FindByGroupId(ctx context.Context, in *proto.FindByGroupId
}

func (s *serviceImpl) Delete(ctx context.Context, in *proto.DeleteSelectionRequest) (*proto.DeleteSelectionResponse, error) {
err := s.repo.Delete(in.GroupId, in.BaanId)
isConfirmed, err := s.isGroupConfirmed(in.GroupId)
if err != nil {
s.log.Named("Create").Error(fmt.Sprintf("isGroupConfirmed: group_id=%s", in.GroupId), zap.Error(err))
return nil, status.Error(codes.Internal, err.Error())
}
if isConfirmed {
s.log.Named("Create").Error(fmt.Sprintf("Failed to create selection: group_id=%s", in.GroupId), zap.Error(err))
return nil, status.Error(codes.InvalidArgument, "Group is confirmed, cannot delete selection")
}

err = s.repo.Delete(in.GroupId, in.BaanId)
if err != nil {
s.log.Named("Delete").Error(fmt.Sprintf("Delete: group_id=%s, baan_id=%s", in.GroupId, in.BaanId), zap.Error(err))
return nil, status.Error(codes.Internal, err.Error())
Expand Down Expand Up @@ -184,9 +207,19 @@ func (s *serviceImpl) CountByBaanId(ctx context.Context, in *proto.CountByBaanId
}

func (s *serviceImpl) Update(ctx context.Context, in *proto.UpdateSelectionRequest) (*proto.UpdateSelectionResponse, error) {
isConfirmed, err := s.isGroupConfirmed(in.GroupId)
if err != nil {
s.log.Named("Create").Error(fmt.Sprintf("isGroupConfirmed: group_id=%s", in.GroupId), zap.Error(err))
return nil, status.Error(codes.Internal, err.Error())
}
if isConfirmed {
s.log.Named("Create").Error(fmt.Sprintf("Failed to create selection: group_id=%s", in.GroupId), zap.Error(err))
return nil, status.Error(codes.InvalidArgument, "Group is confirmed, cannot update selection")
}

oldSelections := &[]model.Selection{}

err := s.repo.FindByGroupId(in.GroupId, oldSelections)
err = s.repo.FindByGroupId(in.GroupId, oldSelections)
if err != nil {
s.log.Named("Update").Error(fmt.Sprintf("FindByGroupId: group_id=%s", in.GroupId), zap.Error(err))
return nil, status.Error(codes.Internal, err.Error())
Expand Down Expand Up @@ -254,3 +287,13 @@ func (s *serviceImpl) Update(ctx context.Context, in *proto.UpdateSelectionReque

return &res, nil
}

func (s *serviceImpl) isGroupConfirmed(groupID string) (bool, error) {
group := &model.Group{}
if err := s.groupRepo.FindOne(groupID, group); err != nil {
s.log.Named("isGroupConfirmed").Error(fmt.Sprintf("FindOne: group_id=%s", groupID), zap.Error(err))
return false, err
}

return group.IsConfirmed, nil
}
Loading