Skip to content
Snippets Groups Projects
Commit ca7ffa9c authored by gfalileev's avatar gfalileev
Browse files

MP-314: should be only one active invitation to become a manager for the property

parent 1da3d621
Branches
Tags
No related merge requests found
......@@ -2,10 +2,10 @@ package service
import (
"context"
"errors"
"time"
"gitlab.mashroom.com/mashroom/backend/common/api/grpcctx"
"gitlab.mashroom.com/mashroom/backend/common/api/grpcerr"
"gitlab.mashroom.com/mashroom/backend/common/generic/dbfilter"
"gitlab.mashroom.com/mashroom/backend/common/generic/log"
......@@ -14,6 +14,7 @@ import (
"gitlab.mashroom.com/mashroom/backend/invitation/internal/notifications"
"gitlab.mashroom.com/mashroom/backend/invitation/internal/service/render"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
var (
......@@ -57,6 +58,15 @@ func (srv *serviceImpl) InvitationCreate(
Message: req.GetMessage(),
}
if req.GetType() == invitation.InvitationType_INVITATION_TYPE_LANDLORD_TO_MANAGER {
err := srv.handleLandlordToManagerInvitationCreated(ctx, req.GetProperty().GetPropertyId(), req.GetHostId())
if err != nil {
log.WithError(err).Errorf("handleLandlordToManagerInvitationCreated failed: %+v", m)
return nil, err
}
}
err := srv.Repository.InvitationSave(ctx, m)
if err != nil {
log.WithError(err).Errorf("CreateInvitation model creation failed: %+v", m)
......@@ -73,6 +83,38 @@ func (srv *serviceImpl) InvitationCreate(
}, nil
}
func (srv *serviceImpl) handleLandlordToManagerInvitationCreated(
ctx context.Context,
property, host string,
) error {
invs, err := srv.Repository.InvitationListByFilters(
ctx,
dbfilter.Eq("property.id", property),
dbfilter.Eq("type", invitation.InvitationType_INVITATION_TYPE_LANDLORD_TO_MANAGER),
dbfilter.In("status",
invitation.InvitationStatus_INVITATION_STATUS_PENDING,
invitation.InvitationStatus_INVITATION_STATUS_ACCEPTED,
),
dbfilter.Eq("host_id", host),
)
if err != nil && !errors.Is(err, mongo.ErrNoDocuments) {
return err
}
for _, inv := range invs {
inv.Status = invitation.InvitationStatus_INVITATION_STATUS_CANCELED
err = srv.Repository.InvitationUpdateStatus(ctx, inv.ID.Hex(), inv.Status)
if err != nil {
return err
}
notifications.HandleInvitationStatusChange(ctx, srv.Connections.Notification, inv)
}
return nil
}
func (srv *serviceImpl) InvitationGet(
ctx context.Context,
req *invitation.InvitationGetRequest,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment