Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix organization update #1045

Merged
merged 3 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ ResponseEntity<UUID> create(@PathVariable("organizationId") int organizationId,
}

@PostMapping("/{subscriptionId}")
ResponseEntity<UUID> create(@PathVariable("organizationId") int organizationId,
@PathVariable("subscriptionId") UUID subscriptionId,
@RequestBody SubscriptionDescriptorModification subscriptionDescriptor,
Principal principal) {
ResponseEntity<UUID> update(@PathVariable("organizationId") int organizationId,
@PathVariable("subscriptionId") UUID subscriptionId,
@RequestBody SubscriptionDescriptorModification subscriptionDescriptor,
Principal principal) {
if (organizationId == subscriptionDescriptor.getOrganizationId()
&& userManager.isOwnerOfOrganization(principal.getName(), subscriptionDescriptor.getOrganizationId())
&& subscriptionId.equals(subscriptionDescriptor.getId())
Expand Down
1 change: 1 addition & 0 deletions src/main/java/alfio/manager/EventManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ public void updateEventHeader(Event original, EventModification em, String usern
Validate.isTrue(ownershipChecker.test(original.getOrganizationId()) && (sameOrganization || ownershipChecker.test(em.getOrganizationId())), "Invalid organizationId");
int eventId = original.getId();
Validate.isTrue(sameOrganization || groupRepository.countByEventId(eventId) == 0, "Cannot change organization because there is a group linked to this event.");
Validate.isTrue(sameOrganization || !subscriptionRepository.hasLinkedSubscription(eventId), "Cannot change organization because there are one or more subscriptions linked.");

boolean formatUpdated = em.getFormat() != original.getFormat();
if(em.getFormat() == EventFormat.ONLINE && formatUpdated) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/alfio/repository/SubscriptionRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ int createSubscription(@Bind("id") UUID id,
@Query("select exists (select id from subscription_event where event_id_fk = :eventId)")
boolean hasLinkedSubscription(@Bind("eventId") int eventId);

@Query("select exists (select id from subscription_event where subscription_descriptor_id_fk = :descriptorId::uuid)")
boolean hasLinkedEvents(@Bind("descriptorId") UUID subscriptionDescriptorId);

@Query("select * from subscription where id = :id for update")
Subscription findSubscriptionByIdForUpdate(@Bind("id") UUID id);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
--
-- This file is part of alf.io.
--
-- alf.io is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- alf.io is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with alf.io. If not, see <http://www.gnu.org/licenses/>.
--

-- this function propagates organization id change to event's descendant resources
create or replace function propagate_event_organization_change() returns trigger
as $$
DECLARE
subscription_links_count numeric;
group_links_count numeric;
BEGIN
if new.org_id <> old.org_id then
-- organizationId has changed.
-- We need to migrate all descendant resources to the new organization

-- first we check if there are any incompatible links in place
subscription_links_count := (select count(*) from subscription_event where event_id_fk = old.id);
if subscription_links_count > 0 then
raise 'CANNOT_TRANSFER_SUBSCRIPTION_LINK' USING DETAIL = ('{count:' || subscription_links_count || '}');
end if;

group_links_count := (select count(*) from group_link where event_id_fk = old.id);
if group_links_count > 0 then
raise 'CANNOT_TRANSFER_GROUP_LINK' USING DETAIL = ('{count:' || group_links_count || '}');
end if;

-- ticket categories / tickets
update ticket_category set organization_id_fk = new.org_id where event_id = old.id;
update ticket set organization_id_fk = new.org_id where event_id = old.id;

-- additional services
update additional_service set organization_id_fk = new.org_id where event_id_fk = old.id;
update additional_service_description set organization_id_fk = new.org_id
from additional_service ase
where additional_service_id_fk = ase.id and ase.event_id_fk = old.id;
update additional_service_item set organization_id_fk = new.org_id
from additional_service ase
where additional_service_id_fk = ase.id and ase.event_id_fk = old.id;

-- ticket reservations
update tickets_reservation set organization_id_fk = new.org_id where event_id_fk = old.id;

-- admin reservation request
update admin_reservation_request set organization_id_fk = new.org_id where event_id = old.id;

-- auditing
update auditing set organization_id_fk = new.org_id where event_id = old.id;

-- billing_document
update billing_document set organization_id_fk = new.org_id where event_id_fk = old.id;

-- configuration
update configuration_event set organization_id_fk = new.org_id where event_id_fk = old.id;
update configuration_ticket_category set organization_id_fk = new.org_id where event_id_fk = old.id;

-- messages
update email_message set organization_id_fk = new.org_id where event_id = old.id;

-- event descriptions
update event_description_text set organization_id_fk = new.org_id where event_id_fk = old.id;

-- polls
update poll set organization_id_fk = new.org_id where event_id_fk = old.id;
update poll_answer set organization_id_fk = new.org_id
from poll p
where poll_id_fk = p.id and p.event_id_fk = old.id;
update poll_option set organization_id_fk = new.org_id
from poll p
where poll_id_fk = p.id and p.event_id_fk = old.id;

-- promo code
update promo_code set organization_id_fk = new.org_id where event_id_fk = old.id;

-- transaction
update b_transaction set organization_id_fk = new.org_id
from tickets_reservation tr
where reservation_id = tr.id and tr.event_id_fk = old.id;

-- event resources
update resource_event set organization_id_fk = new.org_id where event_id_fk = old.id;

-- scan audit
update scan_audit set organization_id_fk = new.org_id where event_id_fk = old.id;

-- special price
update special_price set organization_id_fk = new.org_id
from ticket_category tc
where ticket_category_id = tc.id and tc.event_id = old.id;

-- sponsor scan
update sponsor_scan set organization_id_fk = new.org_id where event_id = old.id;

-- ticket_category_text
update ticket_category_text set organization_id_fk = new.org_id
from ticket_category tc
where ticket_category_id_fk = tc.id and tc.event_id = old.id;

-- ticket field
update ticket_field_configuration set organization_id_fk = new.org_id where event_id_fk = old.id;
update ticket_field_value set organization_id_fk = new.org_id
from ticket_field_configuration tfc
where ticket_field_configuration_id_fk = tfc.id and tfc.event_id_fk = old.id;
update ticket_field_description set organization_id_fk = new.org_id
from ticket_field_configuration tfc
where ticket_field_configuration_id_fk = tfc.id and tfc.event_id_fk = old.id;
update ticket_field_value set organization_id_fk = new.org_id
from ticket_field_configuration tfc
where ticket_field_configuration_id_fk = tfc.id and tfc.event_id_fk = old.id;

update waiting_queue set organization_id_fk = new.org_id where event_id = old.id;

end if;
return new;

END
$$ language plpgsql;

create trigger event_update_org_id_fk_trigger
after update on event
for each row execute procedure propagate_event_organization_change();

Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
--
-- This file is part of alf.io.
--
-- alf.io is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- alf.io is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with alf.io. If not, see <http://www.gnu.org/licenses/>.
--

-- this function propagates organization id change to event's descendant resources
create or replace function propagate_subscription_organization_change() returns trigger
as $$
DECLARE
subscription_links_count numeric;
BEGIN
if new.organization_id_fk <> old.organization_id_fk then
-- organizationId has changed.
-- We need to migrate all descendant resources to the new organization

-- first we check if there are any incompatible links in place
subscription_links_count := (select count(*) from subscription_event where subscription_descriptor_id_fk = old.id);
if subscription_links_count > 0 then
raise 'CANNOT_TRANSFER_SUBSCRIPTION_LINK' USING DETAIL = ('{count:' || subscription_links_count || '}');
end if;

-- subscription
update subscription set organization_id_fk = new.organization_id_fk where subscription_descriptor_fk = old.id;

-- ticket reservations
update tickets_reservation set organization_id_fk = new.organization_id_fk
from subscription s
where s.reservation_id_fk = tickets_reservation.id and s.subscription_descriptor_fk = old.id;

-- auditing
update auditing set organization_id_fk = new.organization_id_fk
from subscription s
where s.reservation_id_fk = auditing.reservation_id and s.subscription_descriptor_fk = old.id;

-- billing_document
update billing_document set organization_id_fk = new.organization_id_fk
from subscription s
where s.reservation_id_fk = billing_document.reservation_id_fk and s.subscription_descriptor_fk = old.id;

-- messages
update email_message set organization_id_fk = new.organization_id_fk
from subscription s
where s.reservation_id_fk = email_message.reservation_id and s.subscription_descriptor_fk = old.id;

-- promo code
update promo_code set organization_id_fk = new.organization_id_fk
from tickets_reservation tr
join subscription s on tr.id = s.reservation_id_fk
where tr.promo_code_id_fk = promo_code.id and s.subscription_descriptor_fk = old.id;

-- transaction
update b_transaction set organization_id_fk = new.organization_id_fk
from tickets_reservation tr
join subscription s on tr.id = s.reservation_id_fk
where tr.id = b_transaction.reservation_id and s.subscription_descriptor_fk = old.id;

end if;
return new;

END
$$ language plpgsql;

create trigger subscription_update_org_id_fk_trigger
after update on subscription_descriptor
for each row execute procedure propagate_subscription_organization_change();

Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import alfio.repository.system.ConfigurationRepository;
import alfio.repository.user.OrganizationRepository;
import alfio.test.util.IntegrationTestUtil;
import alfio.util.BaseIntegrationTest;
import alfio.util.ClockProvider;
import alfio.util.PinGenerator;
import org.apache.commons.lang3.time.DateUtils;
Expand All @@ -48,6 +49,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -89,12 +91,15 @@ class PollApiControllerIntegrationTest {
private TicketRepository ticketRepository;
@Autowired
private EventDeleterRepository eventDeleterRepository;
@Autowired
private NamedParameterJdbcTemplate jdbcTemplate;

private Event event;
private Long pollId;
private Ticket ticket;
private Long firstOptionId;
private Long secondOptionId;
private String username;


@BeforeEach
Expand All @@ -108,7 +113,7 @@ void init() {
DESCRIPTION, BigDecimal.ZERO, false, "", false, null, null, null, null, null, 0, null, null, AlfioMetadata.empty())
);
Pair<Event, String> eventAndUser = initEvent(categories, organizationRepository, userManager, eventManager, eventRepository);

username = eventAndUser.getRight();
event = eventAndUser.getKey();
var rowCountAndKey = pollRepository.insert(Map.of("en", "test poll"), null, List.of(), 0, event.getId(), event.getOrganizationId());
pollId = rowCountAndKey.getKey();
Expand All @@ -128,6 +133,7 @@ void init() {

@AfterEach
void deleteAll() {
BaseIntegrationTest.testTransferEventToAnotherOrg(event.getId(), event.getOrganizationId(), username, jdbcTemplate);
eventDeleterRepository.deleteAllForEvent(event.getId());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import alfio.repository.system.ConfigurationRepository;
import alfio.repository.user.OrganizationRepository;
import alfio.repository.user.UserRepository;
import alfio.util.BaseIntegrationTest;
import alfio.util.ClockProvider;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -161,4 +162,10 @@ private ReservationFlowContext createContext() {
public void hybridEvent() throws Exception {
super.testBasicFlow(this::createContext);
}

@Override
protected void performAdditionalTests(ReservationFlowContext context) {
var event = context.event;
BaseIntegrationTest.testTransferEventToAnotherOrg(event.getId(), event.getOrganizationId(), context.userId, jdbcTemplate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import alfio.repository.system.ConfigurationRepository;
import alfio.repository.user.OrganizationRepository;
import alfio.repository.user.UserRepository;
import alfio.util.BaseIntegrationTest;
import alfio.util.ClockProvider;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -161,4 +162,10 @@ private ReservationFlowContext createContext() {
public void onlineEvent() throws Exception {
super.testBasicFlow(this::createContext);
}

@Override
protected void performAdditionalTests(ReservationFlowContext context) {
var event = context.event;
BaseIntegrationTest.testTransferEventToAnotherOrg(event.getId(), event.getOrganizationId(), context.userId, jdbcTemplate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import alfio.repository.system.ConfigurationRepository;
import alfio.repository.user.OrganizationRepository;
import alfio.repository.user.UserRepository;
import alfio.util.BaseIntegrationTest;
import alfio.util.ClockProvider;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -190,5 +191,8 @@ protected void performAdditionalTests(ReservationFlowContext reservationFlowCont
assertNotNull(reservationsResponse.getBody());
assertFalse(reservationsResponse.getBody().isEmpty());
assertEquals(1, reservationsResponse.getBody().size());

var event = reservationFlowContext.event;
BaseIntegrationTest.testTransferEventToAnotherOrg(event.getId(), event.getOrganizationId(), reservationFlowContext.userId, jdbcTemplate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import alfio.repository.system.ConfigurationRepository;
import alfio.repository.user.OrganizationRepository;
import alfio.repository.user.UserRepository;
import alfio.util.BaseIntegrationTest;
import alfio.util.ClockProvider;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -166,4 +167,10 @@ private ReservationFlowContext createContext() {
public void inPersonEvent() throws Exception {
super.testBasicFlow(this::createContext);
}

@Override
protected void performAdditionalTests(ReservationFlowContext context) {
var event = context.event;
BaseIntegrationTest.testTransferEventToAnotherOrg(event.getId(), event.getOrganizationId(), context.userId, jdbcTemplate);
}
}
Loading