Skip to content

Commit

Permalink
reactor : add crm configuration (#667)
Browse files Browse the repository at this point in the history
* feat : Add DynamicsCrmDbContext

* chore : Remove useless code

* chore : Handling code odors

* chore : Handling code odors

* chore : Handling code odors

* chore : Handling code odors

* reactor :  add crm  configuration

* chore : Dealing with bad odors

* chore : Dealing with bad odors
  • Loading branch information
wzh425 authored Aug 22, 2023
1 parent de44ad1 commit b83ab5b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Utils.DynamicsCrm.Core;

public static class ServiceCollectionExtensions
{
public static IServiceCollection AddCrmConfiguration(this IServiceCollection services, Action<CrmConfiguration> crmConfigurationAction)
{
var crmConfiguration = new CrmConfiguration();
crmConfigurationAction.Invoke(crmConfiguration);

services.TryAddSingleton<ICrmConfiguration>(crmConfiguration);

return services;
}
}
3 changes: 3 additions & 0 deletions src/Utils/DynamicsCrm/Masa.Utils.DynamicsCrm.Core/_Imports.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
global using System.ComponentModel.DataAnnotations.Schema;
global using Masa.BuildingBlocks.Ddd.Domain.Entities;
global using Masa.BuildingBlocks.Authentication.Identity;
global using Masa.Utils.DynamicsCrm.Core.Configurations;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.DependencyInjection.Extensions;
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,20 @@ private static void AuditEntityHandler(EntityEntry entity, Guid systemUserId, Gu

private Guid GetUserId()
{
var defaultUserId = _crmConfiguration?.SystemUserId ?? Guid.Empty;

if (_userContext == null)
return Guid.Empty;
return defaultUserId;

var tUserId = _userContext.GetUserId<TUserId>();

var userId = _userContext.GetUserId<TUserId>();
if (tUserId is not Guid)
return defaultUserId;

if (!(userId is Guid))
return Guid.Empty;
var userId = tUserId as Guid?;
if (userId == null || userId == Guid.Empty)
return defaultUserId;

return userId as Guid? ?? _crmConfiguration?.SystemUserId ?? Guid.Empty;
return userId.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private void HandleNavigationEntry(IEnumerable<NavigationEntry> navigationEntrie

private void HandleDependent(object dependentEntry)
{
var userId = _userContext?.GetUserId<Guid>() ?? _crmConfiguration?.SystemUserId ?? Guid.Empty;
var userId = GetUserId();

var entityEntry = _context.Entry(dependentEntry);
entityEntry.State = EntityState.Modified;
Expand All @@ -79,15 +79,21 @@ private void HandleDependent(object dependentEntry)

private Guid GetUserId()
{
var defaultUserId = _crmConfiguration?.SystemUserId ?? Guid.Empty;

if (_userContext == null)
return Guid.Empty;
return defaultUserId;

var tUserId = _userContext.GetUserId<TUserId>();

var userId = _userContext.GetUserId<TUserId>();
if (tUserId is not Guid)
return defaultUserId;

if (!(userId is Guid))
return Guid.Empty;
var userId = tUserId as Guid?;
if (userId == null || userId == Guid.Empty)
return defaultUserId;

return userId as Guid? ?? _crmConfiguration?.SystemUserId ?? Guid.Empty;
return userId.Value;
}
}

0 comments on commit b83ab5b

Please sign in to comment.