Skip to content

Commit

Permalink
PR stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Fargekritt committed Feb 6, 2025
1 parent d9d5012 commit 05c0f54
Showing 1 changed file with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,10 @@ public async Task<CreateDialogResult> Handle(CreateDialogCommand request, Cancel
dialog.Org = serviceResourceInformation.OwnOrgShortName;
}

if (request.Dto.IdempotentKey is not null && !string.IsNullOrEmpty(dialog.Org))
var dialogId = await GetExistingDialogIdByIdempotentKey(dialog, cancellationToken);
if (dialogId is null)
{
var dialogId = await _db.Dialogs
.Where(x => x.IdempotentKey == request.Dto.IdempotentKey && x.Org == dialog.Org).Select(x => x.Id)
.FirstOrDefaultAsync(cancellationToken);

if (dialogId != default)
{
return new Conflict(nameof(DialogEntity.IdempotentKey), $"'{request.Dto.IdempotentKey}' already exists with DialogId '{dialogId}'");
}
dialog.IdempotentKey = request.Dto.IdempotentKey;
return new Conflict(nameof(dialog.IdempotentKey), $"'{dialog.IdempotentKey}' already exists with DialogId '{dialogId}'");
}

CreateDialogEndUserContext(request, dialog);
Expand All @@ -119,6 +112,24 @@ public async Task<CreateDialogResult> Handle(CreateDialogCommand request, Cancel
domainError => domainError,
concurrencyError => throw new UnreachableException("Should never get a concurrency error when creating a new dialog"));
}
private async Task<Guid?> GetExistingDialogIdByIdempotentKey(DialogEntity dialog, CancellationToken cancellationToken)
{
if (dialog.IdempotentKey is null || string.IsNullOrEmpty(dialog.Org))
{
return null;
}
var dialogId = await _db.Dialogs
.Where(x => x.IdempotentKey == dialog.IdempotentKey && x.Org == dialog.Org)
.Select(x => x.Id)
.FirstOrDefaultAsync(cancellationToken);

if (dialogId != Guid.Empty)
{
return null;
}

return dialogId;
}

private void CreateDialogEndUserContext(CreateDialogCommand request, DialogEntity dialog)
{
Expand Down

0 comments on commit 05c0f54

Please sign in to comment.