Skip to content

Commit

Permalink
fix: Using existing transmission/activity IDs should return HTTP 422 (#…
Browse files Browse the repository at this point in the history
…960)

Introduced a bug when implementing Transmissions, changed the existing
activities check to only check on the dialog.
Reverted to check the entire table.

Also added the same check that was missing for Transmission IDs

## Related Issue(s)

- #959
  • Loading branch information
oskogstad authored Aug 5, 2024
1 parent 2caf7fd commit 01789b1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Digdir.Domain.Dialogporten.Domain.Dialogs.Entities;
using Digdir.Domain.Dialogporten.Domain.Dialogs.Entities.Activities;
using Digdir.Domain.Dialogporten.Domain.Dialogs.Entities.Attachments;
using Digdir.Domain.Dialogporten.Domain.Dialogs.Entities.Transmissions;
using FluentValidation.Results;
using MediatR;
using OneOf;
Expand Down Expand Up @@ -121,6 +122,12 @@ public async Task<CreateDialogResult> Handle(CreateDialogCommand request, Cancel
_domainContext.AddError(DomainFailure.EntityExists<DialogAttachment>(existingAttachmentIds));
}

var existingTransmissionIds = await _db.GetExistingIds(dialog.Transmissions, cancellationToken);
if (existingTransmissionIds.Count != 0)
{
_domainContext.AddError(DomainFailure.EntityExists<DialogTransmission>(existingTransmissionIds));
}

await _db.Dialogs.AddAsync(dialog, cancellationToken);

var saveResult = await _unitOfWork.SaveChangesAsync(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,14 @@ public async Task<UpdateDialogResult> Handle(UpdateDialogCommand request, Cancel
_mapper.Map(request.Dto, dialog);
ValidateTimeFields(dialog);

AppendActivity(dialog, request.Dto);
await AppendActivity(dialog, request.Dto, cancellationToken);
VerifyActivityRelations(dialog);

AppendTransmission(dialog, request.Dto);
VerifyActivityTransmissionRelations(dialog);
await AppendTransmission(dialog, request.Dto, cancellationToken);
VerifyTransmissionRelations(dialog);

VerifyActivityTransmissionRelations(dialog);

dialog.SearchTags
.Merge(request.Dto.SearchTags,
destinationKeySelector: x => x.Value,
Expand Down Expand Up @@ -205,19 +206,16 @@ private void ValidateTimeFields(DialogEntity dialog)
}
}

private void AppendActivity(DialogEntity dialog, UpdateDialogDto dto)
private async Task AppendActivity(DialogEntity dialog, UpdateDialogDto dto, CancellationToken cancellationToken)
{
var newDialogActivities = _mapper.Map<List<DialogActivity>>(dto.Activities);

var existingIds = dialog.Activities.Select(x => x.Id).ToList();

existingIds = existingIds.Intersect(newDialogActivities.Select(x => x.Id)).ToList();
var existingIds = await _db.GetExistingIds(newDialogActivities, cancellationToken);
if (existingIds.Count != 0)
{
_domainContext.AddError(
nameof(UpdateDialogDto.Activities),
$"Entity '{nameof(DialogActivity)}' with the following key(s) already exists: ({string.Join(", ", existingIds)}).");
return;
}

dialog.Activities.AddRange(newDialogActivities);
Expand Down Expand Up @@ -280,19 +278,16 @@ private void VerifyActivityRelations(DialogEntity dialog)
}
}

private void AppendTransmission(DialogEntity dialog, UpdateDialogDto dto)
private async Task AppendTransmission(DialogEntity dialog, UpdateDialogDto dto, CancellationToken cancellationToken)
{
var newDialogTransmissions = _mapper.Map<List<DialogTransmission>>(dto.Transmissions);

var existingIds = dialog.Transmissions.Select(x => x.Id).ToList();

existingIds = existingIds.Intersect(newDialogTransmissions.Select(x => x.Id)).ToList();
var existingIds = await _db.GetExistingIds(newDialogTransmissions, cancellationToken);
if (existingIds.Count != 0)
{
_domainContext.AddError(
nameof(UpdateDialogDto.Transmissions),
$"Entity '{nameof(DialogTransmission)}' with the following key(s) already exists: ({string.Join(", ", existingIds)}).");
return;
}

dialog.Transmissions.AddRange(newDialogTransmissions);
Expand All @@ -303,7 +298,11 @@ private void AppendTransmission(DialogEntity dialog, UpdateDialogDto dto)

private void VerifyTransmissionRelations(DialogEntity dialog)
{
var relatedTransmissionIds = dialog.Transmissions.Where(x => x.RelatedTransmissionId is not null).Select(x => x.RelatedTransmissionId).ToList();
var relatedTransmissionIds = dialog.Transmissions
.Where(x => x.RelatedTransmissionId is not null)
.Select(x => x.RelatedTransmissionId)
.ToList();

if (relatedTransmissionIds.Count == 0)
{
return;
Expand Down

0 comments on commit 01789b1

Please sign in to comment.