diff --git a/src/marketplace/Offers.Library/Service/OfferSubscriptionService.cs b/src/marketplace/Offers.Library/Service/OfferSubscriptionService.cs index 423b288196..018c25e7e8 100644 --- a/src/marketplace/Offers.Library/Service/OfferSubscriptionService.cs +++ b/src/marketplace/Offers.Library/Service/OfferSubscriptionService.cs @@ -105,8 +105,8 @@ await _mailingProcessCreation.RoleBaseSendMail( public async Task RemoveOfferSubscriptionAsync(Guid subscriptionId, OfferTypeId offerTypeId, string basePortalAddress) { - var offerSubscriptionsRepository = _portalRepositories.GetInstance(); - var offerSubscriptionDetails = await offerSubscriptionsRepository.GetOfferDetailsAndCheckProviderCompany(subscriptionId, _identityData.CompanyId, offerTypeId) ?? throw new NotFoundException($"Subscription {subscriptionId} does not exist."); + var offerSubscriptionDetails = await _portalRepositories.GetInstance() + .GetOfferDetailsAndCheckProviderCompany(subscriptionId, _identityData.CompanyId, offerTypeId) ?? throw new NotFoundException($"Subscription {subscriptionId} does not exist."); var offerId = offerSubscriptionDetails.OfferId; if (string.IsNullOrEmpty(offerSubscriptionDetails.OfferName)) @@ -117,12 +117,12 @@ public async Task RemoveOfferSubscriptionAsync(Guid subscriptionId, OfferT { throw new ForbiddenException("Only the providing company can decline the subscription request."); } - if (offerSubscriptionDetails.Status == OfferSubscriptionStatusId.INACTIVE) + if (offerSubscriptionDetails.Status != OfferSubscriptionStatusId.PENDING) { - throw new ConflictException($"Subscription of {offerSubscriptionDetails.OfferName} is already {offerSubscriptionDetails.Status}"); + throw new ConflictException($"Subscription of {offerSubscriptionDetails.OfferName} should be in {OfferSubscriptionStatusId.PENDING} state."); } - var offerSubscription = offerSubscriptionsRepository.DeleteOfferSubscription(subscriptionId, offerId, offerSubscriptionDetails.CompanyId, offerSubscriptionDetails.Status, offerSubscriptionDetails.RequesterId); + var offerSubscription = _portalRepositories.Remove(new OfferSubscription(subscriptionId, offerId, offerSubscriptionDetails.CompanyId, offerSubscriptionDetails.Status, offerSubscriptionDetails.RequesterId, DateTimeOffset.UtcNow)); SendNotificationsToRequester(offerId, offerTypeId, basePortalAddress, offerSubscriptionDetails); await _portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None); diff --git a/src/portalbackend/PortalBackend.DBAccess/Repositories/IOfferSubscriptionsRepository.cs b/src/portalbackend/PortalBackend.DBAccess/Repositories/IOfferSubscriptionsRepository.cs index fcde556a3d..2b569b793b 100644 --- a/src/portalbackend/PortalBackend.DBAccess/Repositories/IOfferSubscriptionsRepository.cs +++ b/src/portalbackend/PortalBackend.DBAccess/Repositories/IOfferSubscriptionsRepository.cs @@ -39,16 +39,6 @@ public interface IOfferSubscriptionsRepository /// id of the creator OfferSubscription CreateOfferSubscription(Guid offerId, Guid companyId, OfferSubscriptionStatusId offerSubscriptionStatusId, Guid requesterId); - /// - /// Deletes the given company assigned offer's subscription from the database - /// - /// Id of the assigned offer - /// Id of the company - /// id of the offer subscription - /// id of the user that requested the subscription of the offer - /// id of the creator - OfferSubscription DeleteOfferSubscription(Guid subscriptionId, Guid offerId, Guid companyId, OfferSubscriptionStatusId offerSubscriptionStatusId, Guid requesterId); - /// /// Gets the provided offer subscription statuses for the user and given company /// diff --git a/src/portalbackend/PortalBackend.DBAccess/Repositories/OfferSubscriptionsRepository.cs b/src/portalbackend/PortalBackend.DBAccess/Repositories/OfferSubscriptionsRepository.cs index 7f94d47835..0880e5bac7 100644 --- a/src/portalbackend/PortalBackend.DBAccess/Repositories/OfferSubscriptionsRepository.cs +++ b/src/portalbackend/PortalBackend.DBAccess/Repositories/OfferSubscriptionsRepository.cs @@ -37,10 +37,6 @@ public class OfferSubscriptionsRepository(PortalDbContext dbContext) : IOfferSub public OfferSubscription CreateOfferSubscription(Guid offerId, Guid companyId, OfferSubscriptionStatusId offerSubscriptionStatusId, Guid requesterId) => dbContext.OfferSubscriptions.Add(new OfferSubscription(Guid.NewGuid(), offerId, companyId, offerSubscriptionStatusId, requesterId, DateTimeOffset.UtcNow)).Entity; - /// - public OfferSubscription DeleteOfferSubscription(Guid subscriptionId, Guid offerId, Guid companyId, OfferSubscriptionStatusId offerSubscriptionStatusId, Guid requesterId) => - dbContext.OfferSubscriptions.Remove(new OfferSubscription(subscriptionId, offerId, companyId, offerSubscriptionStatusId, requesterId, DateTimeOffset.UtcNow)).Entity; - /// public Func?>> GetOwnCompanyProvidedOfferSubscriptionStatusesUntrackedAsync(Guid userCompanyId, OfferTypeId offerTypeId, SubscriptionStatusSorting? sorting, IEnumerable statusIds, Guid? offerId, string? companyName) => (skip, take) => Pagination.CreateSourceQueryAsync( diff --git a/src/portalbackend/PortalBackend.Migrations/Migrations/20241121163647_1140_Decline-Offer-Subscription.Designer.cs b/src/portalbackend/PortalBackend.Migrations/Migrations/20241121163647_1140_Decline-Offer-Subscription.Designer.cs index 243161d301..fc03805f52 100644 --- a/src/portalbackend/PortalBackend.Migrations/Migrations/20241121163647_1140_Decline-Offer-Subscription.Designer.cs +++ b/src/portalbackend/PortalBackend.Migrations/Migrations/20241121163647_1140_Decline-Offer-Subscription.Designer.cs @@ -1,4 +1,23 @@ -// +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +// using System; using System.Text.Json; using Microsoft.EntityFrameworkCore; diff --git a/src/portalbackend/PortalBackend.Migrations/Migrations/20241121163647_1140_Decline-Offer-Subscription.cs b/src/portalbackend/PortalBackend.Migrations/Migrations/20241121163647_1140_Decline-Offer-Subscription.cs index bd5dd3d1b0..4ecf136bd4 100644 --- a/src/portalbackend/PortalBackend.Migrations/Migrations/20241121163647_1140_Decline-Offer-Subscription.cs +++ b/src/portalbackend/PortalBackend.Migrations/Migrations/20241121163647_1140_Decline-Offer-Subscription.cs @@ -1,4 +1,23 @@ -using Microsoft.EntityFrameworkCore.Migrations; +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +using Microsoft.EntityFrameworkCore.Migrations; #nullable disable diff --git a/src/portalbackend/PortalBackend.Migrations/Migrations/PortalDbContextModelSnapshot.cs b/src/portalbackend/PortalBackend.Migrations/Migrations/PortalDbContextModelSnapshot.cs index 66b4aa0d15..91baf95f2d 100644 --- a/src/portalbackend/PortalBackend.Migrations/Migrations/PortalDbContextModelSnapshot.cs +++ b/src/portalbackend/PortalBackend.Migrations/Migrations/PortalDbContextModelSnapshot.cs @@ -1,3 +1,22 @@ +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + // using System; using System.Text.Json; diff --git a/tests/marketplace/Offers.Library.Tests/Service/OfferSubscriptionServiceTests.cs b/tests/marketplace/Offers.Library.Tests/Service/OfferSubscriptionServiceTests.cs index 8f8e798c72..86e18a4c69 100644 --- a/tests/marketplace/Offers.Library.Tests/Service/OfferSubscriptionServiceTests.cs +++ b/tests/marketplace/Offers.Library.Tests/Service/OfferSubscriptionServiceTests.cs @@ -493,8 +493,7 @@ public async Task RemoveOfferSubscription_ReturnsExpected(OfferTypeId offerTypeI var subscription = new OfferSubscription(); A.CallTo(() => _offerSubscriptionsRepository.GetOfferDetailsAndCheckProviderCompany(A._, A._, offerTypeId)) .Returns(offerSubscriptionDetails); - A.CallTo(() => _offerSubscriptionsRepository.DeleteOfferSubscription(A._, A._, A._, A._, A._)) - .Returns(subscription); + A.CallTo(() => _portalRepositories.Remove(A._)); // Act await _sut.RemoveOfferSubscriptionAsync(_validSubscriptionId, offerTypeId, BasePortalUrl); @@ -502,7 +501,7 @@ public async Task RemoveOfferSubscription_ReturnsExpected(OfferTypeId offerTypeI // Assert A.CallTo(() => _offerSubscriptionsRepository.GetOfferDetailsAndCheckProviderCompany(_validSubscriptionId, _identity.CompanyId, offerTypeId)) .MustHaveHappenedOnceExactly(); - A.CallTo(() => _offerSubscriptionsRepository.DeleteOfferSubscription(_validSubscriptionId, offerSubscriptionDetails.OfferId, offerSubscriptionDetails.CompanyId, offerSubscriptionDetails.Status, offerSubscriptionDetails.RequesterId)) + A.CallTo(() => _portalRepositories.Remove(A._)) .MustHaveHappenedOnceExactly(); A.CallTo(() => _notificationRepository.CreateNotification(offerSubscriptionDetails.RequesterId, offerTypeId == OfferTypeId.SERVICE @@ -517,6 +516,7 @@ public async Task RemoveOfferSubscription_ReturnsExpected(OfferTypeId offerTypeI && x["url"] == BasePortalUrl && x["requesterName"] == string.Format("{0} {1}", offerSubscriptionDetails.RequesterFirstname, offerSubscriptionDetails.RequesterLastname)) )).MustHaveHappenedOnceExactly(); + A.CallTo(() => _portalRepositories.SaveAsync()).MustHaveHappenedOnceExactly(); } [Theory] @@ -538,7 +538,7 @@ public async Task RemoveOfferSubscription_NoOfferDetails_ThrowsNotFoundException var ex = await Assert.ThrowsAsync(Action); A.CallTo(() => _offerSubscriptionsRepository.GetOfferDetailsAndCheckProviderCompany(_validSubscriptionId, _identity.CompanyId, offerTypeId)) .MustHaveHappenedOnceExactly(); - A.CallTo(() => _offerSubscriptionsRepository.DeleteOfferSubscription(_validSubscriptionId, A._, A._, A._, A._)) + A.CallTo(() => _portalRepositories.Remove(A._)) .MustNotHaveHappened(); A.CallTo(() => _notificationRepository.CreateNotification(A._, A._, false, A>._)) .MustNotHaveHappened(); @@ -566,7 +566,7 @@ public async Task RemoveOfferSubscription_NotProviderCompany_ThrowsForbiddenExce var ex = await Assert.ThrowsAsync(Action); A.CallTo(() => _offerSubscriptionsRepository.GetOfferDetailsAndCheckProviderCompany(_validSubscriptionId, _identity.CompanyId, offerTypeId)) .MustHaveHappenedOnceExactly(); - A.CallTo(() => _offerSubscriptionsRepository.DeleteOfferSubscription(_validSubscriptionId, A._, A._, A._, A._)) + A.CallTo(() => _portalRepositories.Remove(A._)) .MustNotHaveHappened(); A.CallTo(() => _notificationRepository.CreateNotification(A._, A._, false, A>._)) .MustNotHaveHappened(); @@ -595,13 +595,13 @@ public async Task RemoveOfferSubscription_AlreadyInActiveSubscription_ThrowsConf var ex = await Assert.ThrowsAsync(Action); A.CallTo(() => _offerSubscriptionsRepository.GetOfferDetailsAndCheckProviderCompany(_validSubscriptionId, _identity.CompanyId, offerTypeId)) .MustHaveHappenedOnceExactly(); - A.CallTo(() => _offerSubscriptionsRepository.DeleteOfferSubscription(_validSubscriptionId, A._, A._, A._, A._)) + A.CallTo(() => _portalRepositories.Remove(A._)) .MustNotHaveHappened(); A.CallTo(() => _notificationRepository.CreateNotification(A._, A._, false, A>._)) .MustNotHaveHappened(); A.CallTo(() => _mailingProcessCreation.CreateMailProcess(A._, A._, A>._)) .MustNotHaveHappened(); - ex.Message.Should().Be($"Subscription of {offerSubscriptionDetails.OfferName} is already {offerSubscriptionDetails.Status}"); + ex.Message.Should().Be($"Subscription of {offerSubscriptionDetails.OfferName} should be in {OfferSubscriptionStatusId.PENDING} state."); } [Theory] @@ -620,8 +620,8 @@ public async Task RemoveOfferSubscription_NoSubscriptionDetails_ThrowsNotFoundEx var ex = await Assert.ThrowsAsync(Action); A.CallTo(() => _offerSubscriptionsRepository.GetOfferDetailsAndCheckProviderCompany(_validSubscriptionId, _identity.CompanyId, offerTypeId)) .MustHaveHappenedOnceExactly(); - A.CallTo(() => _offerSubscriptionsRepository.DeleteOfferSubscription(_validSubscriptionId, A._, A._, A._, A._)) - .MustNotHaveHappened(); + A.CallTo(() => _portalRepositories.Remove(A._)). + MustNotHaveHappened(); A.CallTo(() => _notificationRepository.CreateNotification(A._, A._, false, A>._)) .MustNotHaveHappened(); A.CallTo(() => _mailingProcessCreation.CreateMailProcess(A._, A._, A>._)) diff --git a/tests/portalbackend/PortalBackend.DBAccess.Tests/OfferSubscriptionRepositoryTest.cs b/tests/portalbackend/PortalBackend.DBAccess.Tests/OfferSubscriptionRepositoryTest.cs index b20bfd0aa7..6c31d92871 100644 --- a/tests/portalbackend/PortalBackend.DBAccess.Tests/OfferSubscriptionRepositoryTest.cs +++ b/tests/portalbackend/PortalBackend.DBAccess.Tests/OfferSubscriptionRepositoryTest.cs @@ -1156,15 +1156,17 @@ public async Task CreateOfferSubscription_ReturnsExpectedResult() public async Task DeleteOfferSubscription_ReturnsExpectedResult() { // Arrange - var (sut, context) = await CreateSut(); + var context = await _dbTestDbFixture.GetPortalDbContext(); + var sut = new PortalRepositories(context); // Act - var results = sut.DeleteOfferSubscription( + var results = sut.Remove(new OfferSubscription( new Guid("eb98bdf5-14e1-4feb-a954-453eac0b93cd"), new Guid("ac1cf001-7fbc-1f2f-817f-bce0572c0007"), new Guid("2dc4249f-b5ca-4d42-bef1-7a7a950a4f87"), OfferSubscriptionStatusId.PENDING, - new Guid("0dcd8209-85e2-4073-b130-ac094fb47106")); + new Guid("0dcd8209-85e2-4073-b130-ac094fb47106"), + DateTimeOffset.UtcNow)); // Assert var changeTracker = context.ChangeTracker;