From 620e615e725fa6b8e39fc27762b2612a530b343e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awek=20Zatorski?= Date: Sat, 30 Nov 2024 13:11:55 +0100 Subject: [PATCH] currentEventCalculation --- .../sasiedzi/event/domain/Transaction.java | 14 +++++++++++ .../sasiedzi/event/service/EventService.java | 23 +++++++++++++++++++ .../event/web/rest/MyInfoContributor.java | 19 +++++++++++++++ .../event/web/rest/RegistrationResource.java | 20 ++++++++++++---- .../webapp/app/account/account.service.ts | 1 + .../entities/event/event-details.component.ts | 11 ++++++++- src/main/webapp/app/router/index.ts | 3 ++- .../app/shared/config/store/account-store.ts | 5 ++++ 8 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/sasiedzi/event/web/rest/MyInfoContributor.java diff --git a/src/main/java/com/sasiedzi/event/domain/Transaction.java b/src/main/java/com/sasiedzi/event/domain/Transaction.java index e38a23d..90ea5f3 100644 --- a/src/main/java/com/sasiedzi/event/domain/Transaction.java +++ b/src/main/java/com/sasiedzi/event/domain/Transaction.java @@ -1,6 +1,7 @@ package com.sasiedzi.event.domain; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; import com.sasiedzi.event.domain.enumeration.TransactionType; import jakarta.persistence.*; import java.io.Serializable; @@ -42,6 +43,19 @@ public class Transaction implements Serializable { @JsonIgnoreProperties(value = { "transaction", "event", "registration" }, allowSetters = true) private Set transactionItems = new HashSet<>(); + @Transient + private UserAccount beneficiary; + + @JsonProperty("beneficiary") + public UserAccount getBeneficiary() { + return beneficiary; + } + + @JsonProperty("beneficiary") + public void setBeneficiary(UserAccount beneficiary) { + this.beneficiary = beneficiary; + } + // jhipster-needle-entity-add-field - JHipster will add fields here public Long getId() { diff --git a/src/main/java/com/sasiedzi/event/service/EventService.java b/src/main/java/com/sasiedzi/event/service/EventService.java index ac93bc0..4ca820b 100644 --- a/src/main/java/com/sasiedzi/event/service/EventService.java +++ b/src/main/java/com/sasiedzi/event/service/EventService.java @@ -323,6 +323,29 @@ public class EventService { } } + public Long getCurrentEventId() { + List allEvents = eventRepository.findAll(); + Event currentEvent = allEvents + .stream() + .filter(event -> event.getDate() != null && event.getDate().isAfter(LocalDate.now())) + .min(Comparator.comparing(Event::getDate)) + .orElse(null); + if (currentEvent == null) { + Event latestEvent = allEvents + .stream() + .filter(event -> event.getDate() != null) + .max(Comparator.comparing(Event::getDate)) + .orElse(null); + if (latestEvent != null) { + return latestEvent.getId(); + } else { + return -1L; + } + } else { + return currentEvent.getId(); + } + } + @Autowired TransactionItemRepository transactionItemRepository; diff --git a/src/main/java/com/sasiedzi/event/web/rest/MyInfoContributor.java b/src/main/java/com/sasiedzi/event/web/rest/MyInfoContributor.java new file mode 100644 index 0000000..eb815fe --- /dev/null +++ b/src/main/java/com/sasiedzi/event/web/rest/MyInfoContributor.java @@ -0,0 +1,19 @@ +package com.sasiedzi.event.web.rest; + +import com.sasiedzi.event.service.EventService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.actuate.info.Info; +import org.springframework.boot.actuate.info.InfoContributor; +import org.springframework.stereotype.Component; + +@Component +public class MyInfoContributor implements InfoContributor { + + @Autowired + EventService eventService; + + @Override + public void contribute(Info.Builder builder) { + builder.withDetail("currentEventId", eventService.getCurrentEventId()); + } +} diff --git a/src/main/java/com/sasiedzi/event/web/rest/RegistrationResource.java b/src/main/java/com/sasiedzi/event/web/rest/RegistrationResource.java index c1eb98c..3eb233c 100644 --- a/src/main/java/com/sasiedzi/event/web/rest/RegistrationResource.java +++ b/src/main/java/com/sasiedzi/event/web/rest/RegistrationResource.java @@ -5,6 +5,7 @@ import com.sasiedzi.event.domain.Registration; import com.sasiedzi.event.domain.User; import com.sasiedzi.event.repository.RegistrationRepository; import com.sasiedzi.event.repository.UserRepository; +import com.sasiedzi.event.service.EventService; import com.sasiedzi.event.service.UserService; import com.sasiedzi.event.service.dto.AdminUserDTO; import com.sasiedzi.event.web.rest.errors.BadRequestAlertException; @@ -51,12 +52,17 @@ public class RegistrationResource { @Autowired private UserRepository userRepository; + @Autowired + private EventService eventService; + public RegistrationResource(RegistrationRepository registrationRepository, UserRepository userRepository) { this.registrationRepository = registrationRepository; this.userRepository = userRepository; } - private static Long currentEventId = 2601L; + private Long getCurrentEventId() { + return eventService.getCurrentEventId(); + } @Autowired CurrentUserHolder currentUser; @@ -72,7 +78,8 @@ public class RegistrationResource { public ResponseEntity createRegistration(@Valid @RequestBody Registration registration, Principal principal) throws URISyntaxException { if ( - !currentUser.getAdminUser().getAuthorities().contains("ROLE_ADMIN") && !registration.getEvent().getId().equals(currentEventId) + !currentUser.getAdminUser().getAuthorities().contains("ROLE_ADMIN") && + !registration.getEvent().getId().equals(getCurrentEventId()) ) { throw new AccessDeniedException("Registration is closed for this event"); } @@ -110,7 +117,8 @@ public class RegistrationResource { @Valid @RequestBody Registration registration ) throws URISyntaxException { if ( - !currentUser.getAdminUser().getAuthorities().contains("ROLE_ADMIN") && !registration.getEvent().getId().equals(currentEventId) + !currentUser.getAdminUser().getAuthorities().contains("ROLE_ADMIN") && + !registration.getEvent().getId().equals(getCurrentEventId()) ) { throw new AccessDeniedException("Registration is closed for this event"); } @@ -149,7 +157,8 @@ public class RegistrationResource { @NotNull @RequestBody Registration registration ) throws URISyntaxException { if ( - !currentUser.getAdminUser().getAuthorities().contains("ROLE_ADMIN") && !registration.getEvent().getId().equals(currentEventId) + !currentUser.getAdminUser().getAuthorities().contains("ROLE_ADMIN") && + !registration.getEvent().getId().equals(getCurrentEventId()) ) { throw new AccessDeniedException("Registration is closed for this event"); } @@ -232,7 +241,8 @@ public class RegistrationResource { public ResponseEntity deleteRegistration(@PathVariable("id") Long id) { Registration registration = registrationRepository.findById(id).get(); if ( - !currentUser.getAdminUser().getAuthorities().contains("ROLE_ADMIN") && !registration.getEvent().getId().equals(currentEventId) + !currentUser.getAdminUser().getAuthorities().contains("ROLE_ADMIN") && + !registration.getEvent().getId().equals(getCurrentEventId()) ) { throw new AccessDeniedException("Registration is closed for this event"); } diff --git a/src/main/webapp/app/account/account.service.ts b/src/main/webapp/app/account/account.service.ts index 0a914cd..974e6e0 100644 --- a/src/main/webapp/app/account/account.service.ts +++ b/src/main/webapp/app/account/account.service.ts @@ -19,6 +19,7 @@ export default class AccountService { if (res.data && res.data.activeProfiles) { this.store.setRibbonOnProfiles(res.data['display-ribbon-on-profiles']); this.store.setActiveProfiles(res.data.activeProfiles); + this.store.setCurrentEventId(res.data.currentEventId); } return true; } catch (error) { diff --git a/src/main/webapp/app/entities/event/event-details.component.ts b/src/main/webapp/app/entities/event/event-details.component.ts index 4e09461..abe9c56 100644 --- a/src/main/webapp/app/entities/event/event-details.component.ts +++ b/src/main/webapp/app/entities/event/event-details.component.ts @@ -10,6 +10,7 @@ import type { IRegistration } from '@/shared/model/registration.model'; import RegistrationService from '@/entities/registration/registration.service'; import UserService from '@/entities/user/user.service'; import type AccountService from '@/account/account.service'; +import { useStore } from '@/store'; // import type EventService from '@/account/account.service'; export default defineComponent({ @@ -33,10 +34,18 @@ export default defineComponent({ const event: Ref = ref({}); const sortedAndIndexedRegistrations: Ref = ref([]); + let store = useStore(); + const retrieveEvent = async (eventId: string) => { try { + console.log('event' + eventId); + let currentEventId = '' + store.currentEventId; + if (eventId == 'useCurrentEventId') { + eventId = currentEventId; + console.log('event2' + eventId); + } const res = await eventService().find(eventId); - isCurrentEvent.value = eventId == '2601'; + isCurrentEvent.value = eventId == currentEventId; event.value = res; // sortedAndIndexedRegistrations.value = res.registrations; sortedAndIndexedRegistrations.value = res.registrations.sort( diff --git a/src/main/webapp/app/router/index.ts b/src/main/webapp/app/router/index.ts index f1a03ac..acbdd9e 100644 --- a/src/main/webapp/app/router/index.ts +++ b/src/main/webapp/app/router/index.ts @@ -5,6 +5,7 @@ const Error = () => import('@/core/error/error.vue'); import admin from '@/router/admin'; import entities from '@/router/entities'; import pages from '@/router/pages'; +// import { useStore } from '@/store'; import { Authority } from '@/shared/security/authority'; const EventDetails = () => import('@/entities/event/event-details.vue'); const Event = () => import('@/entities/event/event.vue'); @@ -17,7 +18,7 @@ export const createRouter = () => path: '/', name: 'CurrentEventView', component: EventDetails, - meta: { authorities: [Authority.USER], eventId: '2601' }, + meta: { authorities: [Authority.USER], eventId: 'useCurrentEventId' }, }, { path: '/forbidden', diff --git a/src/main/webapp/app/shared/config/store/account-store.ts b/src/main/webapp/app/shared/config/store/account-store.ts index 8b87b84..c0074f8 100644 --- a/src/main/webapp/app/shared/config/store/account-store.ts +++ b/src/main/webapp/app/shared/config/store/account-store.ts @@ -7,6 +7,7 @@ export interface AccountStateStorable { profilesLoaded: boolean; ribbonOnProfiles: string; activeProfiles: string; + currentEventId: number; } export const defaultAccountState: AccountStateStorable = { @@ -16,6 +17,7 @@ export const defaultAccountState: AccountStateStorable = { profilesLoaded: false, ribbonOnProfiles: '', activeProfiles: '', + currentEventId: 0, }; export const useAccountStore = defineStore('main', { @@ -46,5 +48,8 @@ export const useAccountStore = defineStore('main', { setRibbonOnProfiles(ribbon) { this.ribbonOnProfiles = ribbon; }, + setCurrentEventId(currentEventId) { + this.currentEventId = currentEventId; + }, }, });