Skip to content

Commit 040378b

Browse files
authored
Merge pull request #47 from GarageGroup/feature/improve-owner-get
Feature/improve owner get
2 parents b86af12 + a1696ea commit 040378b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+552
-200
lines changed

Internal.Support.Bot.sln

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Api", "src\service\CrmConta
4747
EndProject
4848
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "src\service\CrmContact\Test\Test.csproj", "{897511EC-27DE-4A86-B0D3-5DA5DCD0C36C}"
4949
EndProject
50-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CrmUser", "CrmUser", "{A1B43D14-F416-47D4-BCDB-2E047124F563}"
50+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CrmOwner", "CrmOwner", "{A1B43D14-F416-47D4-BCDB-2E047124F563}"
5151
EndProject
52-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Contract", "src\service\CrmUser\Contract\Contract.csproj", "{9FCF0D94-88CA-405F-864A-710E7380B892}"
52+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Contract", "src\service\CrmOwner\Contract\Contract.csproj", "{9FCF0D94-88CA-405F-864A-710E7380B892}"
5353
EndProject
54-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Api", "src\service\CrmUser\Api\Api.csproj", "{45AA67DF-491D-493F-AB04-5D17D82A12BD}"
54+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Api", "src\service\CrmOwner\Api\Api.csproj", "{45AA67DF-491D-493F-AB04-5D17D82A12BD}"
5555
EndProject
56-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "src\service\CrmUser\Test\Test.csproj", "{CAC26758-7536-4892-A55C-C21B9BB0B193}"
56+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "src\service\CrmOwner\Test\Test.csproj", "{CAC26758-7536-4892-A55C-C21B9BB0B193}"
5757
EndProject
5858
Global
5959
GlobalSection(SolutionConfigurationPlatforms) = preSolution

src/app/AzureFunc/Applicaton/BotFlow/Flow.IncidentCreate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ private static IBotBuilder UseIncidentCreateFlow(this IBotBuilder botBuilder)
2222
.With(
2323
UseDataverseApi().With(UseSqlApi()).UseCrmContactApi())
2424
.With(
25-
UseDataverseApi().UseCrmUserApi())
25+
UseDataverseApi().With(UseSqlApi()).UseCrmOwnerApi())
2626
.With(
2727
UseDataverseApi().UseCrmIncidentApi())
2828
.With(

src/app/AzureFunc/AzureFunc.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<ProjectReference Include="..\..\service\CrmContact\Api\Api.csproj" />
2323
<ProjectReference Include="..\..\service\CrmCustomer\Api\Api.csproj" />
2424
<ProjectReference Include="..\..\service\CrmIncident\Api\Api.csproj" />
25-
<ProjectReference Include="..\..\service\CrmUser\Api\Api.csproj" />
25+
<ProjectReference Include="..\..\service\CrmOwner\Api\Api.csproj" />
2626
<ProjectReference Include="..\..\service\Gpt\Api\Api.csproj" />
2727
</ItemGroup>
2828

src/endpoint/Incident.Create/Flow/Flow.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<ProjectReference Include="..\..\..\service\CrmContact\Contract\Contract.csproj" />
1515
<ProjectReference Include="..\..\..\service\CrmCustomer\Contract\Contract.csproj" />
1616
<ProjectReference Include="..\..\..\service\CrmIncident\Contract\Contract.csproj" />
17-
<ProjectReference Include="..\..\..\service\CrmUser\Contract\Contract.csproj" />
17+
<ProjectReference Include="..\..\..\service\CrmOwner\Contract\Contract.csproj" />
1818
<ProjectReference Include="..\..\..\service\Gpt\Contract\Contract.csproj" />
1919
</ItemGroup>
2020

src/endpoint/Incident.Create/Flow/Flow/Flow.Run.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal static ValueTask<Unit> RunAsync(
1111
this IBotContext context,
1212
ICrmCustomerApi crmCustomerApi,
1313
ICrmContactApi crmContactApi,
14-
ICrmUserApi crmUserApi,
14+
ICrmOwnerApi crmOwnerApi,
1515
ICrmIncidentApi crmIncidentApi,
1616
ISupportGptApi supportGptApi,
1717
IncidentCreateFlowOption option,
@@ -34,7 +34,7 @@ internal static ValueTask<Unit> RunAsync(
3434

3535
return context.CreateChatFlow("IncidentCreate")
3636
.RunFlow(
37-
crmCustomerApi, crmContactApi, crmUserApi, crmIncidentApi, supportGptApi, option)
37+
crmCustomerApi, crmContactApi, crmOwnerApi, crmIncidentApi, supportGptApi, option)
3838
.CompleteValueAsync(cancellationToken);
3939
}
4040
}

src/endpoint/Incident.Create/Flow/Flow/IncidentCreateChatFlow.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ private static ChatFlow<Unit> RunFlow(
99
this ChatFlow chatFlow,
1010
ICrmCustomerApi crmCustomerApi,
1111
ICrmContactApi crmContactApi,
12-
ICrmUserApi crmUserApi,
12+
ICrmOwnerApi crmOwnerApi,
1313
ICrmIncidentApi crmIncidentApi,
1414
ISupportGptApi supportGptApi,
1515
IncidentCreateFlowOption option)
@@ -31,7 +31,7 @@ private static ChatFlow<Unit> RunFlow(
3131
.AwaitCaseType()
3232
.AwaitPriority()
3333
.AwaitOwner(
34-
crmUserApi)
34+
crmOwnerApi)
3535
.ConfirmIncident()
3636
.CreateIncident(
3737
crmIncidentApi)

src/endpoint/Incident.Create/Flow/IncidentCreateDependency.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace GarageGroup.Internal.Support;
99
public static class IncidentCreateDependency
1010
{
1111
public static IBotBuilder MapIncidentCreateFlow(
12-
this Dependency<IncidentCreateFlowOption, ICrmCustomerApi, ICrmContactApi, ICrmUserApi, ICrmIncidentApi, ISupportGptApi> dependency,
12+
this Dependency<IncidentCreateFlowOption, ICrmCustomerApi, ICrmContactApi, ICrmOwnerApi, ICrmIncidentApi, ISupportGptApi> dependency,
1313
IBotBuilder botBuilder)
1414
{
1515
ArgumentNullException.ThrowIfNull(dependency);
@@ -23,7 +23,7 @@ ValueTask<Unit> InnerInvokeAsync(IBotContext context, CancellationToken cancella
2323
option: dependency.ResolveFirst(context.ServiceProvider),
2424
crmCustomerApi: dependency.ResolveSecond(context.ServiceProvider),
2525
crmContactApi: dependency.ResolveThird(context.ServiceProvider),
26-
crmUserApi: dependency.ResolveFourth(context.ServiceProvider),
26+
crmOwnerApi: dependency.ResolveFourth(context.ServiceProvider),
2727
crmIncidentApi: dependency.ResolveFifth(context.ServiceProvider),
2828
supportGptApi: dependency.ResolveSixth(context.ServiceProvider),
2929
cancellationToken: cancellationToken);

src/endpoint/Incident.Create/Flow/Step.AwaitContact/ContactFindHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ private static LookupValue MapContactItem(ContactItemOut item)
8585

8686
private static LookupValueSetOption MapSearchFailure(Failure<ContactSetGetFailureCode> failure, ILogger logger)
8787
{
88-
logger.LogError("Search contacts failure: {failureCode} {failureMessage}", failure.FailureCode, failure.FailureMessage);
89-
return new(items: new[] { SkipValue }, choiceText: ChooseOrSkip);
88+
logger.LogError("Get last contacts failure: {failureCode} {failureMessage}", failure.FailureCode, failure.FailureMessage);
89+
return new(items: SkipValue.AsFlatArray(), choiceText: ChooseOrSkip);
9090
}
9191

9292
private static BotFlowFailure MapToFlowFailure(Failure<ContactSetGetFailureCode> failure)

src/endpoint/Incident.Create/Flow/Step.AwaitOwner/OwnerAwaitFlowStep.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ namespace GarageGroup.Internal.Support;
55
internal static class OwnerAwaitFlowStep
66
{
77
internal static ChatFlow<IncidentCreateFlowState> AwaitOwner(
8-
this ChatFlow<IncidentCreateFlowState> chatFlow, ICrmUserApi crmUserApi)
8+
this ChatFlow<IncidentCreateFlowState> chatFlow, ICrmOwnerApi crmOwnerApi)
99
=>
10-
chatFlow.SendText(
11-
_ => "Нужно выбрать ответственного")
12-
.AwaitLookupValue(
13-
OwnerAwaitHelper.GetDefaultOwnerAsync,
14-
crmUserApi.SearchUsersOrFailureAsync,
10+
chatFlow.SetTypingStatus().AwaitLookupValue(
11+
crmOwnerApi.GetDefaultOwnersAsync,
12+
crmOwnerApi.SearchUsersOrFailureAsync,
1513
OwnerAwaitHelper.CreateResultMessage,
1614
MapFlowState);
1715

src/endpoint/Incident.Create/Flow/Step.AwaitOwner/OwnerAwaitHelper.cs

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,45 @@
22
using System.Threading;
33
using System.Threading.Tasks;
44
using GarageGroup.Infra.Bot.Builder;
5+
using Microsoft.Extensions.Logging;
56

67
namespace GarageGroup.Internal.Support;
78

89
internal static class OwnerAwaitHelper
910
{
10-
private const string DefaultChooseOwnerMessage = "Выберите себя или введите часть имени для поиска";
11-
1211
private const string ChooseOwnerMessage = "Выберите ответственного или введите часть имени для поиска";
1312

1413
private const string UnsuccessfulSearchResultText = "Не удалось найти ни одного пользователя. Попробуйте уточнить запрос";
1514

1615
private const int MaxUserSetCount = 6;
1716

18-
internal static ValueTask<LookupValueSetOption> GetDefaultOwnerAsync(
19-
IChatFlowContext<IncidentCreateFlowState> context, CancellationToken cancellationToken)
17+
internal static ValueTask<LookupValueSetOption> GetDefaultOwnersAsync(
18+
this ICrmOwnerApi crmOwnerApi,
19+
IChatFlowContext<IncidentCreateFlowState> context,
20+
CancellationToken cancellationToken)
2021
=>
2122
AsyncPipeline.Pipe(
2223
context.FlowState, cancellationToken)
2324
.Pipe(
24-
static flowState => new LookupValueSetOption(
25-
items: new LookupValue(
26-
id: flowState.BotUserId.GetValueOrDefault(),
27-
name: flowState.BotUserName?.OrNullIfWhiteSpace() ?? "Я (по умолчанию)")
28-
.AsFlatArray(),
29-
choiceText: DefaultChooseOwnerMessage));
25+
static state => new LastOwnerSetGetIn(
26+
customerId: state.CustomerId,
27+
userId: state.BotUserId.GetValueOrDefault(),
28+
top: MaxUserSetCount - 1))
29+
.PipeValue(
30+
crmOwnerApi.GetLastAsync)
31+
.OnFailure(
32+
failure => context.Logger.LogError(
33+
"Get last owners failure: {failureCode} {failureMessage}", failure.FailureCode, failure.FailureMessage))
34+
.Fold(
35+
static success => success.Owners.Map(MapOwnerItem),
36+
static _ => default)
37+
.Pipe(
38+
owners => new LookupValueSetOption(
39+
items: owners.InsertBotUser(context.FlowState),
40+
choiceText: ChooseOwnerMessage));
3041

3142
internal static ValueTask<Result<LookupValueSetOption, BotFlowFailure>> SearchUsersOrFailureAsync(
32-
this ICrmUserApi crmUserApi,
43+
this ICrmOwnerApi crmOwnerApi,
3344
IChatFlowContext<IncidentCreateFlowState> context,
3445
string seachText,
3546
CancellationToken cancellationToken)
@@ -38,34 +49,50 @@ internal static ValueTask<Result<LookupValueSetOption, BotFlowFailure>> SearchUs
3849
context.FlowState, cancellationToken)
3950
.HandleCancellation()
4051
.Pipe(
41-
flowState => new UserSetSearchIn(seachText)
52+
flowState => new OwnerSetSearchIn(seachText)
4253
{
4354
Top = MaxUserSetCount
4455
})
4556
.PipeValue(
46-
crmUserApi.SearchAsync)
57+
crmOwnerApi.SearchAsync)
4758
.MapFailure(
4859
MapToFlowFailure)
4960
.MapSuccess(
5061
static @out => new LookupValueSetOption(
51-
items: @out.Users.Map(MapUserItem),
52-
choiceText: @out.Users.IsNotEmpty ? ChooseOwnerMessage : UnsuccessfulSearchResultText));
62+
items: @out.Owners.Map(MapOwnerItem),
63+
choiceText: @out.Owners.IsNotEmpty ? ChooseOwnerMessage : UnsuccessfulSearchResultText));
5364

5465
internal static string CreateResultMessage(IChatFlowContext<IncidentCreateFlowState> context, LookupValue userValue)
5566
=>
5667
$"Ответственный: {context.EncodeHtmlTextWithStyle(userValue.Name, BotTextStyle.Bold)}";
5768

58-
private static LookupValue MapUserItem(UserItemOut item)
69+
private static LookupValue MapOwnerItem(OwnerItemOut item)
5970
=>
6071
new(item.Id, item.FullName);
6172

62-
private static BotFlowFailure MapToFlowFailure(Failure<UserSetSearchFailureCode> failure)
73+
private static FlatArray<LookupValue> InsertBotUser(this FlatArray<LookupValue> values, IncidentCreateFlowState flowState)
74+
{
75+
var builder = FlatArray<LookupValue>.Builder.OfLength(values.Length + 1);
76+
77+
builder[0] = new(
78+
id: flowState.BotUserId.GetValueOrDefault(),
79+
name: flowState.BotUserName?.OrNullIfWhiteSpace() ?? "Я (по умолчанию)");
80+
81+
for (var i = 0; i < values.Length; i++)
82+
{
83+
builder[i + 1] = values[i];
84+
}
85+
86+
return builder.MoveToFlatArray();
87+
}
88+
89+
private static BotFlowFailure MapToFlowFailure(Failure<OwnerSetGetFailureCode> failure)
6390
=>
6491
(failure.FailureCode switch
6592
{
66-
UserSetSearchFailureCode.NotAllowed
93+
OwnerSetGetFailureCode.NotAllowed
6794
=> "При поиске пользователей произошла ошибка. У вашей учетной записи не достаточно разрешений. Обратитесь к администратору приложения",
68-
UserSetSearchFailureCode.TooManyRequests
95+
OwnerSetGetFailureCode.TooManyRequests
6996
=> "Слишком много обращений к сервису. Попробуйте повторить попытку через несколько секунд",
7097
_
7198
=> "При поиске пользователей произошла непредвиденная ошибка. Обратитесь к администратору или повторите попытку позднее"

0 commit comments

Comments
 (0)