diff --git a/internal/api/grpc/admin/domain_policy.go b/internal/api/grpc/admin/domain_policy.go index 78288b235e..c86ca13723 100644 --- a/internal/api/grpc/admin/domain_policy.go +++ b/internal/api/grpc/admin/domain_policy.go @@ -68,12 +68,12 @@ func (s *Server) UpdateCustomDomainPolicy(ctx context.Context, req *admin_pb.Upd }, nil } -func (s *Server) ResetCustomDomainPolicyTo(ctx context.Context, req *admin_pb.ResetCustomDomainPolicyToDefaultRequest) (*admin_pb.ResetCustomDomainPolicyToDefaultResponse, error) { - err := s.command.RemoveOrgDomainPolicy(ctx, req.OrgId) +func (s *Server) ResetCustomDomainPolicyToDefault(ctx context.Context, req *admin_pb.ResetCustomDomainPolicyToDefaultRequest) (*admin_pb.ResetCustomDomainPolicyToDefaultResponse, error) { + details, err := s.command.RemoveOrgDomainPolicy(ctx, req.OrgId) if err != nil { return nil, err } - return nil, nil //TOOD: return data + return &admin_pb.ResetCustomDomainPolicyToDefaultResponse{Details: object.DomainToChangeDetailsPb(details)}, nil } func domainPolicyToDomain(userLoginMustBeDomain, validateOrgDomains, smtpSenderAddressMatchesInstanceDomain bool) *domain.DomainPolicy { @@ -161,6 +161,14 @@ func (s *Server) GetCustomOrgIAMPolicy(ctx context.Context, req *admin_pb.GetCus return &admin_pb.GetCustomOrgIAMPolicyResponse{Policy: policy_grpc.DomainPolicyToOrgIAMPb(policy)}, nil } +func (s *Server) ResetCustomOrgIAMPolicyToDefault(ctx context.Context, req *admin_pb.ResetCustomOrgIAMPolicyToDefaultRequest) (*admin_pb.ResetCustomOrgIAMPolicyToDefaultResponse, error) { + details, err := s.command.RemoveOrgDomainPolicy(ctx, req.OrgId) + if err != nil { + return nil, err + } + return &admin_pb.ResetCustomOrgIAMPolicyToDefaultResponse{Details: object.DomainToChangeDetailsPb(details)}, nil +} + func updateOrgIAMPolicyToDomain(req *admin_pb.UpdateOrgIAMPolicyRequest) *domain.DomainPolicy { return &domain.DomainPolicy{ UserLoginMustBeDomain: req.UserLoginMustBeDomain, diff --git a/internal/command/org_policy_domain.go b/internal/command/org_policy_domain.go index 3b1f1354a3..7b49c02413 100644 --- a/internal/command/org_policy_domain.go +++ b/internal/command/org_policy_domain.go @@ -71,21 +71,28 @@ func (c *Commands) ChangeOrgDomainPolicy(ctx context.Context, resourceOwner stri return orgWriteModelToDomainPolicy(existingPolicy), nil } -func (c *Commands) RemoveOrgDomainPolicy(ctx context.Context, orgID string) error { +func (c *Commands) RemoveOrgDomainPolicy(ctx context.Context, orgID string) (*domain.ObjectDetails, error) { if orgID == "" { - return caos_errs.ThrowInvalidArgument(nil, "Org-3H8fs", "Errors.ResourceOwnerMissing") + return nil, caos_errs.ThrowInvalidArgument(nil, "Org-3H8fs", "Errors.ResourceOwnerMissing") } existingPolicy, err := c.orgDomainPolicyWriteModelByID(ctx, orgID) if err != nil { - return err + return nil, err } if existingPolicy.State == domain.PolicyStateUnspecified || existingPolicy.State == domain.PolicyStateRemoved { - return caos_errs.ThrowNotFound(nil, "ORG-Dvsh3", "Errors.Org.DomainPolicy.NotFound") + return nil, caos_errs.ThrowNotFound(nil, "ORG-Dvsh3", "Errors.Org.DomainPolicy.NotFound") } orgAgg := OrgAggregateFromWriteModel(&existingPolicy.PolicyDomainWriteModel.WriteModel) - _, err = c.eventstore.Push(ctx, org.NewDomainPolicyRemovedEvent(ctx, orgAgg)) - return err + pushedEvents, err := c.eventstore.Push(ctx, org.NewDomainPolicyRemovedEvent(ctx, orgAgg)) + if err != nil { + return nil, err + } + err = AppendAndReduce(existingPolicy, pushedEvents...) + if err != nil { + return nil, err + } + return writeModelToObjectDetails(&existingPolicy.PolicyDomainWriteModel.WriteModel), nil } func (c *Commands) getOrgDomainPolicy(ctx context.Context, orgID string) (*domain.DomainPolicy, error) { diff --git a/internal/command/org_policy_domain_test.go b/internal/command/org_policy_domain_test.go index 249a0283ac..17f7420570 100644 --- a/internal/command/org_policy_domain_test.go +++ b/internal/command/org_policy_domain_test.go @@ -384,13 +384,16 @@ func TestCommandSide_RemoveDomainPolicy(t *testing.T) { r := &Commands{ eventstore: tt.fields.eventstore, } - err := r.RemoveOrgDomainPolicy(tt.args.ctx, tt.args.orgID) + got, err := r.RemoveOrgDomainPolicy(tt.args.ctx, tt.args.orgID) if tt.res.err == nil { assert.NoError(t, err) } if tt.res.err != nil && !tt.res.err(err) { t.Errorf("got wrong err: %v ", err) } + if tt.res.err == nil { + assert.Equal(t, tt.res.want, got) + } }) } } diff --git a/internal/command/policy_org_model.go b/internal/command/policy_org_model.go index e989dd6e2f..73434489ea 100644 --- a/internal/command/policy_org_model.go +++ b/internal/command/policy_org_model.go @@ -33,6 +33,8 @@ func (wm *PolicyDomainWriteModel) Reduce() error { if e.SMTPSenderAddressMatchesInstanceDomain != nil { wm.SMTPSenderAddressMatchesInstanceDomain = *e.SMTPSenderAddressMatchesInstanceDomain } + case *policy.DomainPolicyRemovedEvent: + wm.State = domain.PolicyStateRemoved } } return wm.WriteModel.Reduce()