Skip to content

Commit

Permalink
Baditsa/changing workshops status (#694)
Browse files Browse the repository at this point in the history
* Endpoint UpdateStatus is added. Checking workshop status for create application is added

* Notification was added. Update status method was modified a bit

* Status field was added to filter

* 1) UpdateStatus method was moved to WorkshopService.cs
2) WorkshopUpdateStatusException was added

* Default value for workshops status filter was changed to 0

* Change workshop status filter type

* Fix tests

* Fix not elasticSearch filter

* Remove default value for status

* Tests for apllicationService Create() method

* Tests for UpdateStatus anf Update methods

* Update V2

* Update checking allow new application

* Update changing workshop status approach

* Update tests

* Improve filter by status

* Improve tests

* Replace GetAll on Get

* Improvements

* Fix sonar issues
  • Loading branch information
valerabad authored Jul 5, 2022
1 parent 67161b4 commit c906c60
Show file tree
Hide file tree
Showing 19 changed files with 479 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Newtonsoft.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace OutOfSchool.Services.Enums
namespace OutOfSchool.Common.Enums
{
[JsonConverter(typeof(StringEnumConverter))]
public enum WorkshopStatus
{
Open = 1,
Closed,
}
}
}
11 changes: 11 additions & 0 deletions OutOfSchool/OutOfSchool.ElasticsearchData/ESWorkshopProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using Nest;
using OutOfSchool.Common;
using OutOfSchool.Common.Enums;
using OutOfSchool.ElasticsearchData.Enums;
using OutOfSchool.ElasticsearchData.Models;

Expand Down Expand Up @@ -155,6 +156,16 @@ private QueryContainer CreateQueryFromFilter(WorkshopFilterES filter)
};
}

if (filter.Statuses.Any())
{
var box = filter.Statuses.Select(x => (object)x);
queryContainer &= new TermsQuery()
{
Field = Infer.Field<WorkshopES>(w => w.Status),
Terms = box,
};
}

if (!string.IsNullOrWhiteSpace(filter.Workdays))
{
queryContainer &= new NestedQuery()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,7 @@ public class WorkshopES
public List<DateTimeRangeES> DateTimeRanges { get; set; }

public List<TeacherES> Teachers { get; set; }

public WorkshopStatus Status { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;

using OutOfSchool.Common.Enums;
using OutOfSchool.ElasticsearchData.Enums;

namespace OutOfSchool.ElasticsearchData.Models
Expand Down Expand Up @@ -42,5 +42,7 @@ public class WorkshopFilterES
public decimal Latitude { get; set; } = 0;

public decimal Longitude { get; set; } = 0;

public IReadOnlyCollection<WorkshopStatus> Statuses { get; set; } = new List<WorkshopStatus>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Extensions.Options;
using Moq;
using NUnit.Framework;
using OutOfSchool.Common.Enums;
using OutOfSchool.WebApi.Config;
using OutOfSchool.WebApi.Controllers.V1;
using OutOfSchool.WebApi.Extensions;
Expand Down Expand Up @@ -55,10 +56,10 @@ public void OneTimeSetup()
httpContextMoq.Setup(x => x.User.IsInRole("provider"))
.Returns(true);

workshops = FakeWorkshops();
workshop = FakeWorkshop();
provider = FakeProvider();
workshopCards = FakeWorkshopCards();
workshops = WithWorkshops();
workshop = WithWorkshop();
provider = WithProvider();
workshopCards = WithWorkshopCards();

var config = new AppDefaultsConfig();
config.City = "Київ";
Expand All @@ -73,6 +74,7 @@ public void Setup()
providerServiceMoq = new Mock<IProviderService>();
providerAdminService = new Mock<IProviderAdminService>();
localizer = new Mock<IStringLocalizer<SharedResource>>();
providerAdminService = new Mock<IProviderAdminService>();

controller = new WorkshopController(workshopServiceMoq.Object, providerServiceMoq.Object, providerAdminService.Object, localizer.Object, options.Object)
{
Expand Down Expand Up @@ -333,6 +335,91 @@ public async Task UpdateWorkshop_WhenModelIsInvalid_ShouldReturnBadRequestObject
//}
#endregion

#region UpdateStatus
[Test]
public async Task UpdateStatus_WhenModelIsValid_ShouldReturnOkObjectResult()
{
// Arrange
providerServiceMoq.Setup(x => x.GetByUserId(It.IsAny<string>(), It.IsAny<bool>())).ReturnsAsync(provider);

var updateRequest = WithWorkshopStatusDto(workshop.Id, WorkshopStatus.Open);

workshopServiceMoq.Setup(x => x.GetById(updateRequest.WorkshopId))
.ReturnsAsync(workshop);
workshopServiceMoq.Setup(x => x.UpdateStatus(updateRequest))
.ReturnsAsync(updateRequest);

// Act
var result = await controller.UpdateStatus(updateRequest).ConfigureAwait(false) as OkObjectResult;

// Assert
Assert.That(result, Is.Not.Null);
Assert.AreEqual(Ok, result.StatusCode);
}

[Test]
public async Task UpdateStatus_WhenIdDoesNotExist_ReturnsNotFoundResult()
{
// Arrange
var nonExistentId = Guid.NewGuid();
var expected = new NotFoundObjectResult($"There is no Workshop in DB with Id - {nonExistentId}");

var updateRequest = WithWorkshopStatusDto(nonExistentId, WorkshopStatus.Open);

workshopServiceMoq.Setup(x => x.GetById(updateRequest.WorkshopId))
.ReturnsAsync(null as WorkshopDTO);

// Act
var result = await controller.UpdateStatus(updateRequest).ConfigureAwait(false) as NotFoundObjectResult;

// Assert
Assert.That(result, Is.Not.Null);
Assert.AreEqual(expected.Value, result.Value);
}

[Test]
public async Task UpdateStatus_WhenModelIsInvalid_ShouldReturnBadRequest()
{
// Arrange
var updateRequest = WithWorkshopStatusDto(workshop.Id, WorkshopStatus.Closed);

workshop.ProviderOwnership = OwnershipType.Common;

providerServiceMoq.Setup(x => x.GetByUserId(It.IsAny<string>(), It.IsAny<bool>())).ReturnsAsync(provider);
workshopServiceMoq.Setup(x => x.GetById(updateRequest.WorkshopId))
.ReturnsAsync(workshop);
workshopServiceMoq.Setup(x => x.UpdateStatus(updateRequest)).
ThrowsAsync(new ArgumentException(It.IsAny<string>()));

// Act
var result = await controller.UpdateStatus(updateRequest).ConfigureAwait(false) as BadRequestObjectResult;

// Assert
Assert.That(result, Is.Not.Null);
Assert.AreEqual(BadRequest, result.StatusCode);
}

[Test]
public async Task UpdateStatus_WhenProviderHasNoRights_ShouldReturn403ObjectResult()
{
// Arrange
var workShopStatusDto = WithWorkshopStatusDto(workshop.Id, WorkshopStatus.Open);
var notAuthorProvider = new ProviderDto() { Id = It.IsAny<Guid>(), UserId = userId };
workshopServiceMoq.Setup(x => x.GetById(workShopStatusDto.WorkshopId))
.ReturnsAsync(workshop);
providerServiceMoq.Setup(x => x.GetByUserId(It.IsAny<string>(), It.IsAny<bool>())).ReturnsAsync(notAuthorProvider);

// Act
var result = await controller.UpdateStatus(workShopStatusDto) as ObjectResult;

// Assert
workshopServiceMoq.Verify(x => x.Create(workshop), Times.Never);
Assert.IsNotNull(result);
Assert.AreEqual(Forbidden, result.StatusCode);
}

#endregion

#region DeleteWorkshop
//[Test]
//[TestCase(1)]
Expand Down Expand Up @@ -400,7 +487,7 @@ public async Task UpdateWorkshop_WhenModelIsInvalid_ShouldReturnBadRequestObject
//}
#endregion

private WorkshopDTO FakeWorkshop()
private WorkshopDTO WithWorkshop()
{
return new WorkshopDTO()
{
Expand All @@ -409,11 +496,12 @@ private WorkshopDTO FakeWorkshop()
Phone = "1111111111",
WorkshopDescriptionItems = new[]
{
FakeWorkshopDescriptionItem(),
FakeWorkshopDescriptionItem(),
WithWorkshopDescriptionItem(),
WithWorkshopDescriptionItem(),
},
Price = 6000,
WithDisabilityOptions = true,
Status = WorkshopStatus.Open,
ProviderTitle = "ProviderTitle",
DisabilityOptionsDesc = "Desc6",
Website = "website6",
Expand Down Expand Up @@ -467,7 +555,7 @@ private WorkshopDTO FakeWorkshop()
};
}

private List<WorkshopDTO> FakeWorkshops()
private List<WorkshopDTO> WithWorkshops()
{
return new List<WorkshopDTO>()
{
Expand All @@ -478,7 +566,7 @@ private List<WorkshopDTO> FakeWorkshops()
Phone = "1111111111",
WorkshopDescriptionItems = new[]
{
FakeWorkshopDescriptionItem(),
WithWorkshopDescriptionItem(),
},
Price = 1000,
WithDisabilityOptions = true,
Expand Down Expand Up @@ -507,9 +595,9 @@ private List<WorkshopDTO> FakeWorkshops()
Phone = "1111111111",
WorkshopDescriptionItems = new[]
{
FakeWorkshopDescriptionItem(),
FakeWorkshopDescriptionItem(),
FakeWorkshopDescriptionItem(),
WithWorkshopDescriptionItem(),
WithWorkshopDescriptionItem(),
WithWorkshopDescriptionItem(),
},
Price = 2000,
WithDisabilityOptions = true,
Expand Down Expand Up @@ -538,9 +626,9 @@ private List<WorkshopDTO> FakeWorkshops()
Phone = "1111111111",
WorkshopDescriptionItems = new[]
{
FakeWorkshopDescriptionItem(),
FakeWorkshopDescriptionItem(),
FakeWorkshopDescriptionItem(),
WithWorkshopDescriptionItem(),
WithWorkshopDescriptionItem(),
WithWorkshopDescriptionItem(),
},
Price = 3000,
WithDisabilityOptions = true,
Expand All @@ -565,8 +653,8 @@ private List<WorkshopDTO> FakeWorkshops()
Phone = "1111111111",
WorkshopDescriptionItems = new[]
{
FakeWorkshopDescriptionItem(),
FakeWorkshopDescriptionItem(),
WithWorkshopDescriptionItem(),
WithWorkshopDescriptionItem(),
},
Price = 4000,
WithDisabilityOptions = true,
Expand All @@ -591,7 +679,7 @@ private List<WorkshopDTO> FakeWorkshops()
Phone = "1111111111",
WorkshopDescriptionItems = new[]
{
FakeWorkshopDescriptionItem(),
WithWorkshopDescriptionItem(),
},
Price = 5000,
WithDisabilityOptions = true,
Expand All @@ -616,7 +704,7 @@ private List<WorkshopDTO> FakeWorkshops()
};
}

private ProviderDto FakeProvider()
private ProviderDto WithProvider()
{
return new ProviderDto()
{
Expand All @@ -625,9 +713,9 @@ private ProviderDto FakeProvider()
};
}

private List<WorkshopCard> FakeWorkshopCards()
private List<WorkshopCard> WithWorkshopCards()
{
var list = FakeWorkshops();
var list = WithWorkshops();
var eSlist = new List<WorkshopCard>();
foreach (var item in list)
{
Expand All @@ -637,7 +725,7 @@ private List<WorkshopCard> FakeWorkshopCards()
return eSlist;
}

private WorkshopDescriptionItemDto FakeWorkshopDescriptionItem()
private WorkshopDescriptionItemDto WithWorkshopDescriptionItem()
{
var id = Guid.NewGuid();
return new WorkshopDescriptionItemDto
Expand All @@ -647,5 +735,14 @@ private WorkshopDescriptionItemDto FakeWorkshopDescriptionItem()
Description = $"test description text sentence for id: {id.ToString()}",
};
}

private WorkshopStatusDto WithWorkshopStatusDto(Guid workshopDtoId, WorkshopStatus workshopStatus)
{
return new WorkshopStatusDto()
{
WorkshopId = workshopDtoId,
Status = workshopStatus,
};
}
}
}
Loading

0 comments on commit c906c60

Please sign in to comment.