Skip to content

Commit 29ea016

Browse files
Add functionality to create workspace users and handle exceptions
1 parent d212cf4 commit 29ea016

File tree

1 file changed

+20
-76
lines changed

1 file changed

+20
-76
lines changed

DocuSign.Workspaces/DocuSign.Workspaces/Domain/CarePlans/CarePlansService.cs

Lines changed: 20 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Threading.Tasks;
66
using DocuSign.eSign.Model;
77
using Docusign.IAM.SDK.Models.Components;
8-
using Docusign.IAM.SDK.Models.Errors;
98
using DocuSign.Workspaces.Domain.CarePlans.Model;
109
using DocuSign.Workspaces.Infrastructure.Services.Interfaces;
1110
using Microsoft.Extensions.Logging;
@@ -72,7 +71,26 @@ public async Task<List<CareDocumentsModel>> SubmitToPhysician(SubmitToPhysicians
7271
{
7372
const string sentStatus = "sent";
7473
var documents = new List<CareDocumentsModel>();
75-
await EnsureWorkspaceUserAsync(model);
74+
75+
var userForCreate = new WorkspaceUserForCreate
76+
{
77+
Email = model.Email,
78+
FirstName = model.Physician.Name,
79+
LastName = ""
80+
};
81+
await ExecuteDocuSignCallAsync(
82+
"WorkspaceUsers.AddWorkspaceUserAsync",
83+
new
84+
{
85+
accountRepository.AccountId,
86+
model.Physician.WorkspaceId,
87+
model.Email
88+
},
89+
() => docuSignApiProvider.WorkspaceUsers.AddWorkspaceUserAsync(
90+
accountRepository.AccountId,
91+
model.Physician.WorkspaceId,
92+
userForCreate));
93+
7694

7795
foreach (var document in model.Documents)
7896
{
@@ -203,80 +221,6 @@ await ExecuteDocuSignCallAsync(
203221
return documents;
204222
}
205223

206-
private async Task EnsureWorkspaceUserAsync(SubmitToPhysiciansModel model)
207-
{
208-
if (string.IsNullOrWhiteSpace(model.Email) || string.IsNullOrWhiteSpace(model.Physician?.WorkspaceId))
209-
{
210-
return;
211-
}
212-
213-
if (await WorkspaceContainsUserAsync(model.Physician.WorkspaceId, model.Email))
214-
{
215-
return;
216-
}
217-
218-
var userForCreate = new WorkspaceUserForCreate
219-
{
220-
Email = model.Email,
221-
FirstName = model.Physician.Name,
222-
LastName = ""
223-
};
224-
225-
try
226-
{
227-
await ExecuteDocuSignCallAsync(
228-
"WorkspaceUsers.AddWorkspaceUserAsync",
229-
new
230-
{
231-
accountRepository.AccountId,
232-
model.Physician.WorkspaceId,
233-
model.Email
234-
},
235-
() => docuSignApiProvider.WorkspaceUsers.AddWorkspaceUserAsync(
236-
accountRepository.AccountId,
237-
model.Physician.WorkspaceId,
238-
userForCreate));
239-
}
240-
catch (APIException)
241-
{
242-
if (!await WorkspaceContainsUserAsync(model.Physician.WorkspaceId, model.Email))
243-
{
244-
throw;
245-
}
246-
}
247-
}
248-
249-
private async Task<bool> WorkspaceContainsUserAsync(string workspaceId, string email)
250-
{
251-
var request = new Docusign.IAM.SDK.Models.Requests.GetWorkspaceUsersRequest
252-
{
253-
AccountId = accountRepository.AccountId,
254-
WorkspaceId = workspaceId
255-
};
256-
var users = await ExecuteDocuSignCallAsync(
257-
"WorkspaceUsers.GetWorkspaceUsersAsync",
258-
new
259-
{
260-
accountRepository.AccountId,
261-
WorkspaceId = workspaceId,
262-
LookupEmail = email
263-
},
264-
() => docuSignApiProvider.WorkspaceUsers.GetWorkspaceUsersAsync(request));
265-
266-
return users.Users?.Any(u =>
267-
!string.IsNullOrWhiteSpace(u.Email) &&
268-
string.Equals(u.Email, email, StringComparison.OrdinalIgnoreCase)) == true;
269-
}
270-
271-
private async Task ExecuteDocuSignCallAsync(string operation, object context, Func<Task> operationCall)
272-
{
273-
await ExecuteDocuSignCallAsync<object>(operation, context, async () =>
274-
{
275-
await operationCall();
276-
return null;
277-
});
278-
}
279-
280224
private async Task<T> ExecuteDocuSignCallAsync<T>(string operation, object context, Func<Task<T>> operationCall)
281225
{
282226
var stopwatch = Stopwatch.StartNew();

0 commit comments

Comments
 (0)