44 Commits

Author SHA1 Message Date
zet 1b42808efa s 2024-12-16 23:02:54 +01:00
zet cd9752090f fix nullpointer 2024-12-16 22:59:05 +01:00
zet 04df2ee1aa fix 2024-12-15 23:47:09 +01:00
zet 2b6e038a75 fix 2024-12-11 15:43:54 +01:00
zet a0589243f2 f 2024-12-06 23:56:11 +01:00
zet c94d4015eb aa 2024-12-06 23:53:06 +01:00
zet 823ac56d8e account order 2024-12-06 23:35:28 +01:00
zet 346236ec09 col12 2024-12-02 20:20:11 +01:00
zet fa210d1e71 375 2024-12-02 20:08:17 +01:00
zet 959808ab63 ver5 2024-12-02 20:02:55 +01:00
zet 08c263f3df aa 2024-12-02 19:32:11 +01:00
zet 88c4c2ce70 transaction fix 2024-12-02 19:20:03 +01:00
zet 4819d318b8 transactions for counter 2024-12-01 15:12:39 +01:00
zet 22e52ea46c transactions 2024-12-01 15:00:08 +01:00
zet 313821ef7b afds 2024-11-30 13:17:23 +01:00
zet 620e615e72 currentEventCalculation 2024-11-30 13:11:55 +01:00
zet 5de13e8a2b a 2024-11-30 11:26:02 +01:00
zet a75ae7d971 ROLE_COUNTER 2024-11-30 11:12:55 +01:00
zet afc46e3c41 ROLE_COUNTER 2024-11-30 11:02:45 +01:00
zet 5d6a0ae3e1 key 2024-11-30 10:48:38 +01:00
zet 7076174e11 token 170days 2024-11-30 10:46:37 +01:00
zet 176861ccca event 2601 2024-11-25 23:09:45 +01:00
zet 3aa4688471 colors 2024-11-19 20:19:19 +01:00
zet bc77eec906 d 2024-11-19 17:22:25 +01:00
zet e79cab687c fixy 2024-11-19 16:39:46 +01:00
zet b06597f36e opposite transaction 2024-11-19 15:39:27 +01:00
zet f63cb0b946 aaa 2024-11-19 13:56:27 +01:00
zet 7676b3f0d2 vertical account names 2024-11-19 13:21:44 +01:00
zet 7b07f0861a s 2024-11-19 12:58:08 +01:00
zet 44fe789f5d f 2024-11-19 12:10:35 +01:00
zet 2372d8bd56 fix 2024-11-19 11:54:09 +01:00
zet 5e75b53d11 transaction edit 2024-11-19 11:49:37 +01:00
zet c56a7e6dc4 170 dni 2024-11-18 20:56:12 +01:00
zet 35b10bc873 fixy 2024-11-18 20:47:48 +01:00
zet 770791302c Prod deploy 18 listopada 2024-11-18 20:38:53 +01:00
zet a6731fcdfc aa 2024-11-18 17:20:51 +01:00
zet a59d438dbb authentication 2024-11-18 16:26:53 +01:00
zet 576c5155d9 a 2024-11-18 12:49:25 +01:00
zet 14b5142394 a 2024-11-18 11:07:16 +01:00
zet 11e38ea299 Merge branch 'Branch_db744d63' into ver5
new TransactionItem.java fields
2024-11-17 16:05:11 +01:00
zet b8bb77e807 new fields in TransactionItem.java active and relationship to registration 2024-11-17 16:03:09 +01:00
zet 4719cd3e9a a 2024-11-17 15:37:47 +01:00
zet d41c876258 a 2024-11-16 14:41:12 +01:00
zet 6a57f63784 a 2024-11-15 19:48:31 +01:00
70 changed files with 2103 additions and 225 deletions
+7
View File
@@ -40,6 +40,13 @@
"relationshipName": "event",
"relationshipSide": "right",
"relationshipType": "many-to-one"
},
{
"otherEntityField": "name",
"otherEntityName": "userAccount",
"relationshipName": "userAccount",
"relationshipSide": "left",
"relationshipType": "many-to-one"
}
],
"searchEngine": "no"
+13
View File
@@ -11,6 +11,11 @@
"fieldName": "comment",
"fieldType": "String",
"fieldValidateRules": []
},
{
"fieldName": "locked",
"fieldType": "Boolean",
"fieldValidateRules": []
}
],
"name": "TransactionItem",
@@ -39,6 +44,14 @@
"relationshipName": "event",
"relationshipSide": "left",
"relationshipType": "many-to-one"
},
{
"otherEntityField": "playerName",
"otherEntityName": "registration",
"otherEntityRelationshipName": "transactionItem",
"relationshipName": "registration",
"relationshipSide": "left",
"relationshipType": "many-to-one"
}
],
"searchEngine": "no",
+1 -1
View File
@@ -18,7 +18,7 @@
"entities": ["Charge", "Event", "Registration", "Transaction", "UserAccount", "TransactionItem"],
"feignClient": null,
"jhipsterVersion": "8.7.2",
"lastLiquibaseTimestamp": 1731510658000,
"lastLiquibaseTimestamp": 1731505842000,
"messageBroker": false,
"microfrontend": null,
"microfrontends": [],
+2 -1
View File
@@ -89,13 +89,14 @@
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>
<dependencies>
<dependency>
<groupId>tech.jhipster</groupId>
<artifactId>jhipster-framework</artifactId>
@@ -3,15 +3,25 @@ package com.sasiedzi.event.config;
import static org.springframework.security.config.Customizer.withDefaults;
import static org.springframework.security.oauth2.core.oidc.StandardClaimNames.PREFERRED_USERNAME;
import com.sasiedzi.event.domain.CurrentUserHolder;
import com.sasiedzi.event.repository.UserRepository;
import com.sasiedzi.event.security.*;
import com.sasiedzi.event.security.SecurityUtils;
import com.sasiedzi.event.security.oauth2.AudienceValidator;
import com.sasiedzi.event.security.oauth2.CustomClaimConverter;
import com.sasiedzi.event.service.EventService;
import com.sasiedzi.event.service.UserService;
import com.sasiedzi.event.service.dto.AdminUserDTO;
import com.sasiedzi.event.web.filter.SpaWebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
@@ -21,8 +31,15 @@ import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer.FrameOptionsConfig;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.oauth2.client.oidc.userinfo.OidcUserRequest;
import org.springframework.security.oauth2.client.oidc.userinfo.OidcUserService;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
@@ -35,11 +52,14 @@ import org.springframework.security.oauth2.core.oidc.user.OidcUserAuthority;
import org.springframework.security.oauth2.jwt.*;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.RememberMeServices;
import org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import org.springframework.security.web.csrf.*;
import org.springframework.security.web.header.writers.ReferrerPolicyHeaderWriter;
import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher;
import org.springframework.util.StringUtils;
import org.springframework.web.context.annotation.RequestScope;
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
import tech.jhipster.config.JHipsterProperties;
import tech.jhipster.web.filter.CookieCsrfFilter;
@@ -82,27 +102,141 @@ public class SecurityConfiguration {
.authorizeHttpRequests(authz ->
// prettier-ignore
authz
.requestMatchers(mvc.pattern("/index.html"), mvc.pattern("/*.js"), mvc.pattern("/*.txt"), mvc.pattern("/*.json"), mvc.pattern("/*.map"), mvc.pattern("/*.css")).permitAll()
.requestMatchers(mvc.pattern("/*.ico"), mvc.pattern("/*.png"), mvc.pattern("/*.svg"), mvc.pattern("/*.webapp")).permitAll()
.requestMatchers(mvc.pattern("/assets/**")).permitAll()
.requestMatchers(mvc.pattern("/swagger-ui/**")).permitAll()
.requestMatchers(mvc.pattern("/api/authenticate")).permitAll()
.requestMatchers(mvc.pattern("/api/auth-info")).permitAll()
// .requestMatchers(mvc.pattern("/index.html"), mvc.pattern("/*.js"), mvc.pattern("/*.txt"), mvc.pattern("/*.json"), mvc.pattern("/*.map"), mvc.pattern("/*.css")).permitAll()
// .requestMatchers(mvc.pattern("/*.ico"), mvc.pattern("/*.png"), mvc.pattern("/*.svg"), mvc.pattern("/*.webapp")).permitAll()
// .requestMatchers(mvc.pattern("/assets/**")).permitAll()
// .requestMatchers(mvc.pattern("/swagger-ui/**")).permitAll()
// .requestMatchers(mvc.pattern("/api/authenticate")).permitAll()
// .requestMatchers(mvc.pattern("/api/auth-info")).permitAll()
.requestMatchers(mvc.pattern("/logout")).permitAll()
.requestMatchers(mvc.pattern("/api/admin/**")).hasAuthority(AuthoritiesConstants.ADMIN)
.requestMatchers(mvc.pattern("/api/**")).authenticated()
// .requestMatchers(mvc.pattern("/api/**")).authenticated()
.requestMatchers(mvc.pattern("/**")).authenticated()
.requestMatchers(mvc.pattern("/v3/api-docs/**")).hasAuthority(AuthoritiesConstants.ADMIN)
.requestMatchers(mvc.pattern("/management/health")).permitAll()
.requestMatchers(mvc.pattern("/management/health/**")).permitAll()
.requestMatchers(mvc.pattern("/management/info")).permitAll()
.requestMatchers(mvc.pattern("/management/prometheus")).permitAll()
// .requestMatchers(mvc.pattern("/management/health")).permitAll()
// .requestMatchers(mvc.pattern("/management/health/**")).permitAll()
// .requestMatchers(mvc.pattern("/management/info")).permitAll()
// .requestMatchers(mvc.pattern("/management/prometheus")).permitAll()
.requestMatchers(mvc.pattern("/management/**")).hasAuthority(AuthoritiesConstants.ADMIN)
)
.oauth2Login(oauth2 -> oauth2.loginPage("/").userInfoEndpoint(userInfo -> userInfo.oidcUserService(this.oidcUserService())))
.rememberMe(
rememberMe -> rememberMe.rememberMeServices(rememberMeServices()) // Klucz do szyfrowania tokenu
)
.oauth2Login(oauth2 ->
oauth2
.defaultSuccessUrl("/", true)
/*.loginPage("/")*/.userInfoEndpoint(userInfo -> userInfo.oidcUserService(this.oidcUserService()))
)
.oauth2ResourceServer(oauth2 -> oauth2.jwt(jwt -> jwt.jwtAuthenticationConverter(authenticationConverter())))
.oauth2Client(withDefaults());
return http.build();
}
@Bean
public RememberMeServices rememberMeServices() {
TokenBasedRememberMeServices rememberMeServices = new TokenBasedRememberMeServices(
"remember-me-cookie-key32342ff",
userDetailsService()
);
rememberMeServices.setTokenValiditySeconds(60 * 60 * 24 * 170);
rememberMeServices.setAlwaysRemember(true);
// Ustawienie czasu ważności tokenu na 170 dni
return rememberMeServices;
}
@Autowired
UserRepository userRepository;
@Bean
public UserDetailsService userDetailsService() {
// Jeśli korzystasz z in-memory użytkowników lub innego źródła, skonfiguruj tutaj.
return new UserDetailsService() {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
String login = extractUsername(username);
// String[] authorities = extractGrantedAuthorities(username);
Map<String, Object> attributes = extractUserAttributes(username);
Optional<com.sasiedzi.event.domain.User> one = userRepository.findOneByLogin(login);
if (one.isPresent()) {
// return new User(one.get().getLogin(), "somepassword", one.get().getAuthorities().stream().map(authority-> new SimpleGrantedAuthority(authority.getName())).collect(Collectors.toSet()));
// return new User(login, "somepassword", Arrays.stream(authorities).map(authority-> new SimpleGrantedAuthority(authority)).collect(Collectors.toSet()));
return new User(username, "somepassword", extractRoles(username));
} else {
throw new UsernameNotFoundException("User not found");
}
}
private static String extractUsername(String input) {
Pattern pattern = Pattern.compile("Name:\\s+\\[([^]]+)]");
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
return matcher.group(1);
}
return null;
}
private static String[] extractGrantedAuthorities(String input) {
Pattern pattern = Pattern.compile("Granted Authorities:\\s+\\[([^]]+)]");
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
return matcher.group(1).split(", ");
}
return new String[0];
}
private static Set<GrantedAuthority> extractRoles(String input) {
Set<GrantedAuthority> roles = new HashSet<>();
Pattern pattern = Pattern.compile("https://www\\.jhipster\\.tech/roles=\\[([^]]+)]");
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
String rolesString = matcher.group(1);
String[] rolesArray = rolesString.split(",\\s*");
for (String role : rolesArray) {
roles.add(new SimpleGrantedAuthority(role));
}
}
return roles;
}
private static Map<String, Object> extractUserAttributes(String input) {
Map<String, Object> attributes = new HashMap<>();
Pattern pattern = Pattern.compile("User Attributes:\\s+\\[\\{([^}]+)\\}\\]");
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
String attributesString = matcher.group(1);
String[] attributePairs = attributesString.split(",\\s*");
for (String pair : attributePairs) {
String[] keyValue = pair.split("=");
if (keyValue.length == 2) {
String key = keyValue[0];
String value = keyValue[1];
// Tutaj wartość jest zawsze String, ale można by to rozszerzyć by obsługiwało różne typy
attributes.put(key, value);
}
}
}
return attributes;
}
};
}
@Bean
@RequestScope
public CurrentUserHolder currentUser(UserService userService, EventService eventService) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication instanceof AbstractAuthenticationToken) {
AbstractAuthenticationToken authToken = (AbstractAuthenticationToken) authentication;
AdminUserDTO userFromAuthentication = userService.getUserFromAuthentication(authToken);
return new CurrentUserHolder(
userFromAuthentication,
eventService.getOrCreateUserAccountForLogin(userFromAuthentication.getLogin()),
authentication
);
}
return new CurrentUserHolder();
}
@Bean
MvcRequestMatcher.Builder mvc(HandlerMappingIntrospector introspector) {
return new MvcRequestMatcher.Builder(introspector);
@@ -42,7 +42,7 @@ public class Charge implements Serializable {
private Event event;
@ManyToOne(fetch = FetchType.LAZY)
@JsonIgnoreProperties(value = { "user", "event" }, allowSetters = true)
@JsonIgnoreProperties(value = { "user", "event", "transactionItems" }, allowSetters = true)
private Registration registration;
@ManyToOne(fetch = FetchType.LAZY)
@@ -0,0 +1,41 @@
package com.sasiedzi.event.domain;
import com.sasiedzi.event.service.dto.AdminUserDTO;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.RequestScope;
@Component
@RequestScope
public class CurrentUserHolder {
AdminUserDTO adminUser;
UserAccount userAccount;
Authentication authentication;
public CurrentUserHolder() {}
public AdminUserDTO getAdminUser() {
return adminUser;
}
public UserAccount getUserAccount() {
return userAccount;
}
public Authentication getAuthentication() {
return authentication;
}
public String getLogin() {
if (adminUser == null) return null;
return adminUser.getLogin();
}
public CurrentUserHolder(AdminUserDTO adminUser, UserAccount userAccount, Authentication authentication) {
this.adminUser = adminUser;
this.userAccount = userAccount;
this.authentication = authentication;
}
}
@@ -1,11 +1,13 @@
package com.sasiedzi.event.domain;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.persistence.*;
import jakarta.validation.constraints.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.HashSet;
import java.util.Set;
@@ -44,11 +46,11 @@ public class Event implements Serializable {
private String comment;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "event")
@JsonIgnoreProperties(value = { "event" }, allowSetters = true)
@JsonIgnoreProperties(value = { "event", "transactionItems" }, allowSetters = true)
private Set<Registration> registrations = new HashSet<>();
@OneToMany(fetch = FetchType.LAZY, mappedBy = "event")
@JsonIgnoreProperties(value = { "event", "transactionItems" }, allowSetters = true)
@JsonIgnoreProperties(value = { "event" }, allowSetters = true)
private Set<Transaction> transactions = new HashSet<>();
// jhipster-needle-entity-add-field - JHipster will add fields here
@@ -92,6 +94,89 @@ public class Event implements Serializable {
this.date = date;
}
// @Transient
// private Boolean editable;
@JsonProperty("editable")
public Boolean getEditable() {
if (date == null) return false;
LocalDate currentDate = LocalDate.now();
long daysBetween = ChronoUnit.DAYS.between(this.date, currentDate);
return daysBetween <= 7;
}
@Transient
private Long chargeTransactionId;
@JsonProperty("chargeTransactionId")
public Long getChargeTransactionId() {
return chargeTransactionId;
}
public void setChargeTransactionId(Long chargeTransactionId) {
this.chargeTransactionId = chargeTransactionId;
}
// public void setEditable(Boolean editable) {
// this.editable = editable;
// }
@Transient
private Boolean paid;
@JsonProperty("paid")
public Boolean getPaid() {
return paid;
}
public void setPaid(Boolean paid) {
this.paid = paid;
}
@Transient
private Boolean charged;
@JsonProperty("charged")
public Boolean getCharged() {
return charged;
}
public void setCharged(Boolean charged) {
this.charged = charged;
}
@Transient
private Boolean current;
@JsonProperty("current")
public Boolean getCurrent() {
return current;
}
public void setCurrent(Boolean current) {
this.current = current;
}
@JsonProperty("active")
public Boolean getActive() {
if (date == null) return false;
LocalDate currentDate = LocalDate.now();
long daysBetween = ChronoUnit.DAYS.between(this.date, currentDate);
return daysBetween <= 0;
}
// @Transient
// private Boolean deletable;
@JsonProperty("deletable")
public Boolean getDeletable() {
return getEditable();
}
// public void setDeletable(Boolean deletable) {
// this.deletable = deletable;
// }
public Integer getPlayersLimit() {
return this.playersLimit;
}
@@ -5,6 +5,8 @@ import jakarta.persistence.*;
import jakarta.validation.constraints.*;
import java.io.Serializable;
import java.time.ZonedDateTime;
import java.util.HashSet;
import java.util.Set;
/**
* A Registration.
@@ -44,6 +46,14 @@ public class Registration implements Serializable {
@JsonIgnoreProperties(value = { "registrations", "transactions" }, allowSetters = true)
private Event event;
@ManyToOne(fetch = FetchType.EAGER)
@JsonIgnoreProperties(value = { "users", "transactionItems" }, allowSetters = true)
private UserAccount userAccount;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "registration")
@JsonIgnoreProperties(value = { "userAccount", "transaction", "event", "registration" }, allowSetters = true)
private Set<TransactionItem> transactionItems = new HashSet<>();
// jhipster-needle-entity-add-field - JHipster will add fields here
public Long getId() {
@@ -137,6 +147,50 @@ public class Registration implements Serializable {
return this;
}
public UserAccount getUserAccount() {
return this.userAccount;
}
public void setUserAccount(UserAccount userAccount) {
this.userAccount = userAccount;
}
public Registration userAccount(UserAccount userAccount) {
this.setUserAccount(userAccount);
return this;
}
public Set<TransactionItem> getTransactionItems() {
return this.transactionItems;
}
public void setTransactionItems(Set<TransactionItem> transactionItems) {
if (this.transactionItems != null) {
this.transactionItems.forEach(i -> i.setRegistration(null));
}
if (transactionItems != null) {
transactionItems.forEach(i -> i.setRegistration(this));
}
this.transactionItems = transactionItems;
}
public Registration transactionItems(Set<TransactionItem> transactionItems) {
this.setTransactionItems(transactionItems);
return this;
}
public Registration addTransactionItem(TransactionItem transactionItem) {
this.transactionItems.add(transactionItem);
transactionItem.setRegistration(this);
return this;
}
public Registration removeTransactionItem(TransactionItem transactionItem) {
this.transactionItems.remove(transactionItem);
transactionItem.setRegistration(null);
return this;
}
// jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here
@Override
@@ -1,12 +1,14 @@
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;
import java.time.LocalDate;
import java.util.HashSet;
import java.util.Set;
import java.time.temporal.ChronoUnit;
import java.util.*;
import org.hibernate.annotations.SortComparator;
/**
* A Transaction.
@@ -39,8 +41,22 @@ public class Transaction implements Serializable {
private Event event;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "transaction", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonIgnoreProperties(value = { "userAccount", "transaction", "event" }, allowSetters = true)
private Set<TransactionItem> transactionItems = new HashSet<>();
@JsonIgnoreProperties(value = { "transaction", "event", "registration" }, allowSetters = true)
@SortComparator(TransactionItemComparator.class)
private SortedSet<TransactionItem> transactionItems = Transaction.getEmptyTransactionItemSet();
@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
@@ -66,6 +82,33 @@ public class Transaction implements Serializable {
return this;
}
// @Transient
// private Boolean editable;
@JsonProperty("editable")
public Boolean getEditable() {
if (date == null) return false;
LocalDate currentDate = LocalDate.now();
long daysBetween = ChronoUnit.DAYS.between(this.date, currentDate);
return daysBetween <= 7;
}
// public void setEditable(Boolean editable) {
// this.editable = editable;
// }
// @Transient
// private Boolean deletable;
@JsonProperty("deletable")
public Boolean getDeletable() {
return getEditable();
}
// public void setDeletable(Boolean deletable) {
// this.deletable = deletable;
// }
public void setType(TransactionType type) {
this.type = type;
}
@@ -109,11 +152,11 @@ public class Transaction implements Serializable {
return this;
}
public Set<TransactionItem> getTransactionItems() {
public SortedSet<TransactionItem> getTransactionItems() {
return this.transactionItems;
}
public void setTransactionItems(Set<TransactionItem> transactionItems) {
public void setTransactionItems(SortedSet<TransactionItem> transactionItems) {
if (this.transactionItems != null) {
this.transactionItems.forEach(i -> i.setTransaction(null));
}
@@ -123,7 +166,7 @@ public class Transaction implements Serializable {
this.transactionItems = transactionItems;
}
public Transaction transactionItems(Set<TransactionItem> transactionItems) {
public Transaction transactionItems(SortedSet<TransactionItem> transactionItems) {
this.setTransactionItems(transactionItems);
return this;
}
@@ -169,4 +212,8 @@ public class Transaction implements Serializable {
", comment='" + getComment() + "'" +
"}";
}
public static SortedSet<TransactionItem> getEmptyTransactionItemSet() {
return new TreeSet<>(new TransactionItemComparator());
}
}
@@ -11,7 +11,7 @@ import java.math.BigDecimal;
@Entity
@Table(name = "transaction_item")
@SuppressWarnings("common-java:DuplicatedBlocks")
public class TransactionItem implements Serializable {
public class TransactionItem implements Serializable, Comparable<TransactionItem> {
private static final long serialVersionUID = 1L;
@@ -27,7 +27,10 @@ public class TransactionItem implements Serializable {
@Column(name = "comment")
private String comment;
@ManyToOne(fetch = FetchType.LAZY)
@Column(name = "locked")
private Boolean locked;
@ManyToOne(fetch = FetchType.EAGER)
@JsonIgnoreProperties(value = { "users", "transactionItems" }, allowSetters = true)
private UserAccount userAccount;
@@ -39,6 +42,10 @@ public class TransactionItem implements Serializable {
@JsonIgnoreProperties(value = { "registrations", "transactions" }, allowSetters = true)
private Event event;
@ManyToOne(fetch = FetchType.LAZY)
@JsonIgnoreProperties(value = { "user", "event", "transactionItems" }, allowSetters = true)
private Registration registration;
// jhipster-needle-entity-add-field - JHipster will add fields here
public Long getId() {
@@ -80,6 +87,19 @@ public class TransactionItem implements Serializable {
this.comment = comment;
}
public Boolean getLocked() {
return this.locked;
}
public TransactionItem locked(Boolean locked) {
this.setLocked(locked);
return this;
}
public void setLocked(Boolean locked) {
this.locked = locked;
}
public UserAccount getUserAccount() {
return this.userAccount;
}
@@ -119,6 +139,19 @@ public class TransactionItem implements Serializable {
return this;
}
public Registration getRegistration() {
return this.registration;
}
public void setRegistration(Registration registration) {
this.registration = registration;
}
public TransactionItem registration(Registration registration) {
this.setRegistration(registration);
return this;
}
// jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here
@Override
@@ -145,6 +178,12 @@ public class TransactionItem implements Serializable {
"id=" + getId() +
", amount=" + getAmount() +
", comment='" + getComment() + "'" +
", locked='" + getLocked() + "'" +
"}";
}
@Override
public int compareTo(TransactionItem o) {
return TransactionItemComparator.compare2(this, o);
}
}
@@ -0,0 +1,46 @@
package com.sasiedzi.event.domain;
import java.util.Comparator;
import java.util.Objects;
public class TransactionItemComparator implements Comparator<TransactionItem> {
private static final UserAccountComparator userAccountComparator = new UserAccountComparator();
public static int compare2(TransactionItem item1, TransactionItem item2) {
// Sprawdzamy, czy którykolwiek z obiektów jest null
if (item1 == null && item2 == null) return 0;
if (item1 == null) return -1;
if (item2 == null) return 1;
if (item1.getId() == null && item2.getId() == null) return -1;
if (Objects.equals(item1.getId(), item2.getId())) return 0;
// Teraz porównujemy pola userAccount
return compareUserAccounts(item1.getUserAccount(), item2.getUserAccount());
}
@Override
public int compare(TransactionItem item1, TransactionItem item2) {
return compare2(item1, item2);
}
private static int compareUserAccounts(UserAccount ua1, UserAccount ua2) {
// Używamy naszego komparatora UserAccount
int compare = userAccountComparator.compare(ua1, ua2);
if (compare == 0) {
// Porównanie id, uwzględniając null
if (ua1.getId() == null && ua2.getId() == null) {
return -1; // Jeśli oba id są null, to nie są równe, ale nie wiemy jak je uporządkować, więc zwracamy -1 (lub 1, zależnie od preferencji)
} else if (ua1.getId() == null) {
return -1; // Pusty ID jest "mniejszy" niż niepusty
} else if (ua2.getId() == null) {
return 1; // Pusty ID jest "mniejszy" niż niepusty
} else {
// Oba ID nie są null, więc porównujemy je normalnie
return -1;
}
} else {
return compare;
}
}
}
@@ -1,8 +1,11 @@
package com.sasiedzi.event.domain;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import jakarta.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
@@ -34,7 +37,7 @@ public class UserAccount implements Serializable {
private Set<User> users = new HashSet<>();
@OneToMany(fetch = FetchType.LAZY, mappedBy = "userAccount")
@JsonIgnoreProperties(value = { "userAccount", "transaction", "event" }, allowSetters = true)
@JsonIgnoreProperties(value = { "userAccount", "transaction", "event", "registration" }, allowSetters = true)
private Set<TransactionItem> transactionItems = new HashSet<>();
// jhipster-needle-entity-add-field - JHipster will add fields here
@@ -43,6 +46,15 @@ public class UserAccount implements Serializable {
return this.id;
}
@Transient
// @JsonSerialize
BigDecimal balance = BigDecimal.ZERO;
@JsonProperty("balance")
public BigDecimal getBalance() {
return balance;
}
public UserAccount id(Long id) {
this.setId(id);
return this;
@@ -88,6 +100,10 @@ public class UserAccount implements Serializable {
return this;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
public Set<TransactionItem> getTransactionItems() {
return this.transactionItems;
}
@@ -0,0 +1,61 @@
package com.sasiedzi.event.domain;
import com.sasiedzi.event.service.Account;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Objects;
public class UserAccountComparator implements Comparator<UserAccount> {
String[] specialAccounts;
public UserAccountComparator() {
ArrayList<String> specialAccountList = new ArrayList<String>();
specialAccountList.addAll(Arrays.stream(Account.values()).map(Enum::toString).toList());
specialAccountList.add("Tomasz Taraszewski");
this.specialAccounts = specialAccountList.toArray(new String[0]);
}
@Override
public int compare(UserAccount ua1, UserAccount ua2) {
// Sprawdzamy, czy obiekty nie są null
if (ua1 == null && ua2 == null) return 0;
if (ua1 == null) return -1;
if (ua2 == null) return 1;
if (ua1.getId() == null && ua2.getId() == null) return -1;
if (Objects.equals(ua1.getId(), ua2.getId())) return 0;
String name1 = ua1.getName();
String name2 = ua2.getName();
// Sprawdzamy, czy oba są null
if (name1 == null && name2 == null) return 0;
if (name1 == null) return -1;
if (name2 == null) return 1;
// Najpierw sprawdzamy, czy nazwy znajdują się w liście specjalnych nazw
int index1 = findSpecialNameIndex(name1);
int index2 = findSpecialNameIndex(name2);
// Jeśli obie nazwy są specjalne, porównujemy ich indeksy
if (index1 != -1 && index2 != -1) {
return Integer.compare(index1, index2);
}
// Jeśli tylko jedna nazwa jest specjalna, ona jest "mniejsza"
if (index1 != -1) return -1;
if (index2 != -1) return 1;
// Jeśli żadna z nazw nie jest specjalna, porównujemy alfabetycznie
return name1.compareTo(name2);
}
private int findSpecialNameIndex(String name) {
for (int i = 0; i < specialAccounts.length; i++) {
if (specialAccounts[i].equalsIgnoreCase(name)) {
return i; // Zwracamy indeks specjalnej nazwy
}
}
return -1; // Nazwa nie jest specjalna
}
}
@@ -1,7 +1,10 @@
package com.sasiedzi.event.repository;
import com.sasiedzi.event.domain.Charge;
import com.sasiedzi.event.domain.Event;
import java.util.List;
import org.springframework.data.jpa.repository.*;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
/**
@@ -9,4 +12,7 @@ import org.springframework.stereotype.Repository;
*/
@SuppressWarnings("unused")
@Repository
public interface EventRepository extends JpaRepository<Event, Long> {}
public interface EventRepository extends JpaRepository<Event, Long> {
@Query("select event from Event event left join fetch event.transactions where event.id =:id")
Event findByIdWithTransactions(@Param("id") Long id);
}
@@ -27,16 +27,21 @@ public interface TransactionItemRepository extends JpaRepository<TransactionItem
}
@Query(
value = "select transactionItem from TransactionItem transactionItem left join fetch transactionItem.event",
value = "select transactionItem from TransactionItem transactionItem left join fetch transactionItem.event left join fetch transactionItem.registration",
countQuery = "select count(transactionItem) from TransactionItem transactionItem"
)
Page<TransactionItem> findAllWithToOneRelationships(Pageable pageable);
@Query("select transactionItem from TransactionItem transactionItem left join fetch transactionItem.event")
@Query(
"select transactionItem from TransactionItem transactionItem left join fetch transactionItem.event left join fetch transactionItem.registration"
)
List<TransactionItem> findAllWithToOneRelationships();
@Query(
"select transactionItem from TransactionItem transactionItem left join fetch transactionItem.event where transactionItem.id =:id"
"select transactionItem from TransactionItem transactionItem left join fetch transactionItem.event left join fetch transactionItem.registration where transactionItem.id =:id"
)
Optional<TransactionItem> findOneWithToOneRelationships(@Param("id") Long id);
@Query("select transactionItem from TransactionItem transactionItem where transactionItem.userAccount.id =:accountId")
List<TransactionItem> getTransactionItemsForAccountId(@Param("accountId") Long accountId);
}
@@ -35,7 +35,9 @@ public interface TransactionRepository extends JpaRepository<Transaction, Long>
@Query("select transaction from Transaction transaction left join fetch transaction.event")
List<Transaction> findAllWithToOneRelationships();
@Query("select transaction from Transaction transaction left join fetch transaction.event where transaction.id =:id")
@Query(
"select transaction from Transaction transaction left join fetch transaction.event left join fetch transaction.transactionItems where transaction.id =:id"
)
Optional<Transaction> findOneWithToOneRelationships(@Param("id") Long id);
@Query(
@@ -1,5 +1,6 @@
package com.sasiedzi.event.repository;
import com.sasiedzi.event.domain.Charge;
import com.sasiedzi.event.domain.UserAccount;
import java.util.List;
import java.util.Optional;
@@ -27,4 +28,10 @@ public interface UserAccountRepository extends UserAccountRepositoryWithBagRelat
default Page<UserAccount> findAllWithEagerRelationships(Pageable pageable) {
return this.fetchBagRelationships(this.findAll(pageable));
}
@Query("select userAccount from UserAccount userAccount join fetch userAccount.users")
// List<UserAccount> findByUserLogin(String login);
List<UserAccount> findAllFetchAccounts();
List<UserAccount> findByName(String name);
}
@@ -9,6 +9,8 @@ public final class AuthoritiesConstants {
public static final String USER = "ROLE_USER";
public static final String COUNTER = "ROLE_COUNTER";
public static final String ANONYMOUS = "ROLE_ANONYMOUS";
private AuthoritiesConstants() {}
@@ -0,0 +1,12 @@
package com.sasiedzi.event.service;
public enum Account {
Boisko,
Skarbiec,
Counter {
@Override
public String toString() {
return "Subkonto Tomka";
}
},
}
@@ -2,20 +2,25 @@ package com.sasiedzi.event.service;
import com.sasiedzi.event.domain.*;
import com.sasiedzi.event.domain.enumeration.TransactionType;
import com.sasiedzi.event.repository.EventRepository;
import com.sasiedzi.event.repository.TransactionRepository;
import com.sasiedzi.event.repository.UserAccountRepository;
import com.sasiedzi.event.web.rest.TransactionResource;
import com.sasiedzi.event.repository.*;
import com.sasiedzi.event.service.dto.AdminUserDTO;
import com.sasiedzi.event.web.rest.AccountResource;
import jakarta.validation.Valid;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.RememberMeAuthenticationToken;
import org.springframework.security.core.AuthenticatedPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -30,6 +35,15 @@ public class EventService {
private final EventRepository eventRepository;
@Autowired
private UserService userService;
@Autowired
private UserRepository userRepository;
@Autowired
private AccountResource accountResource;
public EventService(EventRepository eventRepository) {
this.eventRepository = eventRepository;
}
@@ -112,6 +126,25 @@ public class EventService {
return eventRepository.findById(id);
}
@Transactional(readOnly = true)
public Event getEventForRegistration(Long id) {
LOG.debug("Request to get Event : {}", id);
Event event = eventRepository.findByIdWithTransactions(id);
event.setCurrent(id.equals(getCurrentEventId()));
event.setPaid(event.getTransactions().stream().anyMatch(t -> TransactionType.FIELDPAYMENT.equals(t.getType())));
event.setCharged(event.getTransactions().stream().anyMatch(t -> TransactionType.MATCH.equals(t.getType())));
event.setChargeTransactionId(
event
.getTransactions()
.stream()
.filter(t -> TransactionType.MATCH.equals(t.getType()))
.map(Transaction::getId)
.findFirst()
.orElse(null)
);
return event;
}
/**
* Delete the event by id.
*
@@ -128,7 +161,8 @@ public class EventService {
@Autowired
UserAccountRepository userAccountRepository;
public Optional<Event> settle(@Valid Event event) {
public Optional<Event> settle(@Valid Event eventFromUI) {
Event event = eventRepository.getReferenceById(eventFromUI.getId());
Long eventId = event.getId();
List<Transaction> allTransactions = transactionRepository
.findAllByEventId(eventId)
@@ -142,7 +176,7 @@ public class EventService {
transaction.setEvent(event);
transaction.setType(TransactionType.MATCH);
transaction.setDate(LocalDate.now());
transaction.setTransactionItems(new HashSet<>());
transaction.setTransactionItems(Transaction.getEmptyTransactionItemSet());
transactionRepository.save(transaction);
} else {
transaction = allTransactions.get(0);
@@ -153,8 +187,6 @@ public class EventService {
transactionRepository.save(transaction);
}
if (event.getRegistrations() != null && !event.getRegistrations().isEmpty()) {
String fieldServiceAccountName = "Boisko";
String vaultAccountName = "Skarbiec";
List<UserAccount> accounts = userAccountRepository.findAll();
Map<String, UserAccount> accountsByLogin = new HashMap<>();
accounts.forEach(acc -> {
@@ -167,46 +199,50 @@ public class EventService {
Map<String, UserAccount> accountsByAccountName = accounts
.stream()
.collect(Collectors.toMap(UserAccount::getName, Function.identity(), (existing, replacement) -> existing));
if (!accountsByAccountName.containsKey(fieldServiceAccountName)) {
if (!accountsByAccountName.containsKey(Account.Boisko.name())) {
UserAccount entity = new UserAccount();
entity.setName(fieldServiceAccountName);
entity.setName(Account.Boisko.name());
entity = userAccountRepository.save(entity);
accountsByAccountName.put(fieldServiceAccountName, entity);
accountsByAccountName.put(Account.Boisko.name(), entity);
}
if (!accountsByAccountName.containsKey(vaultAccountName)) {
if (!accountsByAccountName.containsKey(Account.Skarbiec.name())) {
UserAccount entity = new UserAccount();
entity.setName(vaultAccountName);
entity.setName(Account.Skarbiec.name());
entity = userAccountRepository.save(entity);
accountsByAccountName.put(vaultAccountName, entity);
accountsByAccountName.put(Account.Skarbiec.name(), entity);
}
List<UserAccount> accountsToCharge = new ArrayList<>();
event
List<TransactionItem> itemsToCharge = new ArrayList<>();
List<Registration> registrationsToCharge = event
.getRegistrations()
.forEach(registration -> {
.stream()
.filter(registration -> !Boolean.FALSE.equals(registration.getActive()))
.sorted(Comparator.comparing(Registration::getDateTime))
.limit(event.getPlayersLimit() == null ? 10000 : event.getPlayersLimit()) // Ograniczamy do pierwszych n elementów
.toList();
registrationsToCharge.forEach(registration -> {
String login = registration.getUser().getLogin();
UserAccount userAccount = accountsByLogin.get(login);
if (userAccount == null) {
userAccount = new UserAccount();
userAccount.setName(registration.getUser().getName());
userAccount.getUsers().add(registration.getUser());
userAccount = userAccountRepository.save(userAccount);
userAccount = createNewAccountForLogin(login);
accountsByLogin.put(login, userAccount);
}
accountsToCharge.add(userAccount);
TransactionItem transactionForPlayer = new TransactionItem();
transactionForPlayer.setRegistration(registration);
transactionForPlayer.setUserAccount(userAccount);
itemsToCharge.add(transactionForPlayer);
});
TransactionItem fieldServiceItem = new TransactionItem();
fieldServiceItem.setEvent(event);
fieldServiceItem.setAmount(event.getCost());
fieldServiceItem.setUserAccount(accountsByAccountName.get(fieldServiceAccountName));
fieldServiceItem.setAmount(new BigDecimal(375));
fieldServiceItem.setUserAccount(accountsByAccountName.get(Account.Boisko.name()));
fieldServiceItem.setTransaction(transaction);
transaction.getTransactionItems().add(fieldServiceItem);
accountsToCharge.forEach(userAccount -> {
TransactionItem playerCharge = new TransactionItem();
playerCharge.setEvent(event);
playerCharge.setAmount(event.getCost().divide(BigDecimal.valueOf(-accountsToCharge.size()), 2, RoundingMode.HALF_UP));
playerCharge.setUserAccount(userAccount);
playerCharge.setTransaction(transaction);
transaction.getTransactionItems().add(playerCharge);
itemsToCharge.forEach(transactionForPlayer -> {
transactionForPlayer.setEvent(event);
transactionForPlayer.setAmount(event.getCost().divide(BigDecimal.valueOf(-itemsToCharge.size()), 2, RoundingMode.HALF_UP));
transactionForPlayer.setTransaction(transaction);
transaction.getTransactionItems().add(transactionForPlayer);
});
BigDecimal vaultValue = transaction
.getTransactionItems()
@@ -218,7 +254,7 @@ public class EventService {
TransactionItem vaultItem = new TransactionItem();
vaultItem.setEvent(event);
vaultItem.setAmount(vaultValue);
vaultItem.setUserAccount(accountsByAccountName.get(vaultAccountName));
vaultItem.setUserAccount(accountsByAccountName.get(Account.Skarbiec.name()));
vaultItem.setTransaction(transaction);
transaction.getTransactionItems().add(vaultItem);
}
@@ -227,4 +263,111 @@ public class EventService {
return eventRepository.findById(event.getId());
}
@Autowired
AdminUserDTO currentUser;
// public String getCurrentAuthenticatedUserAsDTO() {
// if (SecurityContextHolder.getContext().getAuthentication() == null) {
// return null;
// }
// if (SecurityContextHolder.getContext() == null) {
// return null;
// }
// Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
// if (principal instanceof AuthenticatedPrincipal) {
// return extractUsername((AuthenticatedPrincipal) principal).getName();
// // return userService.getUserFromAuthentication((AbstractAuthenticationToken) principal);
// } else if (principal instanceof UserDetails) {
// return asdf((UserDetails) principal).getUsername();
// // return userService.getUserFromAuthentication((AbstractAuthenticationToken) principal);
// } else {
// throw new RuntimeException("User could not be found");
// }
// }
private static String extractUsername(String input) {
Pattern pattern = Pattern.compile("Name:\\s+\\[([^]]+)]");
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
return matcher.group(1);
}
return null;
}
public UserAccount createNewAccountForLogin(String login) {
UserAccount entity = new UserAccount();
Optional<User> oneByLogin = userRepository.findOneByLogin(login);
if (oneByLogin.isPresent()) {
entity.setName(oneByLogin.get().getName());
entity.getUsers().add(oneByLogin.get());
entity = userAccountRepository.save(entity);
return entity;
} else {
throw new RuntimeException("user not found");
}
}
public UserAccount getOrCreateUserAccountForLogin(String username) {
if (username != null) {
// List<UserAccount> userAccounts = userAccountRepository.findByUserLogin(currentAuthenticatedUserAsDTO.getLogin());
List<UserAccount> userAccounts = userAccountRepository
.findAllFetchAccounts()
.stream()
.filter(accountResource -> {
return accountResource.getUsers().stream().anyMatch(user -> user.getLogin().equals(username));
})
.toList();
if (userAccounts.isEmpty()) {
return createNewAccountForLogin(username);
} else {
UserAccount userAccount = userAccounts.get(0);
List<TransactionItem> transactionItemsForAccountId = transactionItemRepository.getTransactionItemsForAccountId(
userAccount.getId()
);
BigDecimal totalBalance = transactionItemsForAccountId
.stream()
.map(TransactionItem::getAmount)
.reduce(BigDecimal.ZERO, BigDecimal::add);
userAccount.setBalance(totalBalance);
return userAccount;
}
} else {
UserAccount userAccount = new UserAccount();
userAccount.setName("Unauthenticated User");
return userAccount;
}
}
public Long getCurrentEventId() {
List<Event> 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;
@Autowired
RegistrationRepository registrationRepository;
}
@@ -0,0 +1,255 @@
package com.sasiedzi.event.service;
import com.sasiedzi.event.domain.*;
import com.sasiedzi.event.domain.enumeration.TransactionType;
import com.sasiedzi.event.repository.TransactionItemRepository;
import com.sasiedzi.event.repository.TransactionRepository;
import com.sasiedzi.event.repository.UserAccountRepository;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
@Service
@Transactional
public class TransactionService {
@Autowired
TransactionRepository transactionRepository;
@Autowired
UserAccountRepository userAccountRepository;
@Autowired
private TransactionItemRepository transactionItemRepository;
@Autowired
private EventService eventService;
public Transaction save(Transaction transaction) {
SortedSet<TransactionItem> items = transaction.getTransactionItems();
String beneficiaryAccountName = transaction.getBeneficiary() == null
? Account.Skarbiec.toString()
: transaction.getBeneficiary().getName();
Set<TransactionItem> beneficiaryItems = items
.stream()
.filter(item -> beneficiaryAccountName.equals(item.getUserAccount().getName()))
.collect(Collectors.toSet());
Set<TransactionItem> nonBeneficiaryItems = items
.stream()
.filter(item -> !beneficiaryAccountName.equals(item.getUserAccount().getName()))
.collect(Collectors.toSet());
BigDecimal beneficiaryValue = nonBeneficiaryItems
.stream()
.map(TransactionItem::getAmount)
.map(BigDecimal::negate)
.reduce(BigDecimal.ZERO, BigDecimal::add);
boolean beneficiaryItemIsNeeded = beneficiaryValue.compareTo(BigDecimal.ZERO) != 0;
HashSet<TransactionItem> itemsToBeRemoved = new HashSet<>();
if (beneficiaryItemIsNeeded) {
if (beneficiaryItems.isEmpty()) {
TransactionItem transactionItem = new TransactionItem();
UserAccount beneficiaryAccount = userAccountRepository
.findAll()
.stream()
.filter(item -> beneficiaryAccountName.equals(item.getName()))
.findFirst()
.orElse(null);
transactionItem.setUserAccount(beneficiaryAccount);
transactionItem.setAmount(beneficiaryValue);
transactionItem.setTransaction(transaction);
transaction.getTransactionItems().add(transactionItem);
} else {
TransactionItem first = null;
for (TransactionItem item : beneficiaryItems) {
if (first == null) {
first = item;
} else {
itemsToBeRemoved.add(item);
}
}
first.setAmount(beneficiaryValue);
}
} else {
itemsToBeRemoved.addAll(beneficiaryItems);
}
itemsToBeRemoved.forEach(transactionItem -> {
transaction.getTransactionItems().remove(transactionItem);
});
if (transaction.getDate() == null) transaction.setDate(LocalDate.now());
return transactionRepository.save(transaction);
}
public Transaction createOpposite(Long id) {
Transaction existing = transactionRepository.findOneWithEagerRelationships(id).orElseGet(Transaction::new);
Transaction newTransaction = new Transaction();
newTransaction.setType(TransactionType.INTERNALTRANSFER);
newTransaction.setEvent(existing.getEvent());
for (TransactionItem item : existing.getTransactionItems()) {
TransactionItem newItem = new TransactionItem();
newItem.setAmount(item.getAmount().negate());
newItem.setTransaction(newTransaction);
newItem.setUserAccount(item.getUserAccount());
newItem.setEvent(item.getEvent());
newItem.setRegistration(item.getRegistration());
newItem.setComment(item.getComment());
newTransaction.getTransactionItems().add(newItem);
}
return newTransaction;
}
public Transaction createPayments(Long id) {
Transaction existing = transactionRepository.findOneWithEagerRelationships(id).orElseGet(Transaction::new);
Transaction newTransaction = new Transaction();
newTransaction.setType(TransactionType.INTERNALTRANSFER);
setBeneficiary(newTransaction);
newTransaction.setEvent(existing.getEvent());
calculateBalances(existing.getTransactionItems().stream().map(TransactionItem::getUserAccount).toList());
Set<Long> processedAccounts = new HashSet<>();
for (TransactionItem item : existing.getTransactionItems()) {
BigDecimal accountBalance = item.getUserAccount().getBalance();
if (accountBalance == null) {
accountBalance = BigDecimal.ZERO;
}
if (
accountBalance.compareTo(BigDecimal.ZERO) < 0 &&
!Account.Skarbiec.toString().equals(item.getUserAccount().getName()) &&
!Account.Boisko.toString().equals(item.getUserAccount().getName()) &&
!Account.Counter.toString().equals(item.getUserAccount().getName()) &&
!processedAccounts.contains(item.getUserAccount().getId())
) {
TransactionItem newItem = new TransactionItem();
newItem.setAmount(accountBalance.negate());
newItem.setTransaction(newTransaction);
newItem.setUserAccount(item.getUserAccount());
newItem.setEvent(item.getEvent());
newItem.setRegistration(item.getRegistration());
newItem.setComment(item.getComment());
newTransaction.getTransactionItems().add(newItem);
processedAccounts.add(item.getUserAccount().getId());
}
}
return newTransaction;
}
public Transaction createFieldPayment(Long eventId) {
Transaction newTransaction = new Transaction();
newTransaction.setType(TransactionType.FIELDPAYMENT);
setBeneficiary(newTransaction);
Event event = null;
if (eventId != null) {
event = eventService.findOne(eventId).orElse(null);
newTransaction.setEvent(event);
}
TransactionItem newItem = new TransactionItem();
newItem.setAmount(new BigDecimal("375"));
newItem.setTransaction(newTransaction);
newItem.setUserAccount(getAccount(Account.Counter));
newItem.setEvent(event);
newTransaction.getTransactionItems().add(newItem);
return newTransaction;
}
private void calculateBalances(List<UserAccount> userAccounts) {
Map<Long, List<TransactionItem>> itemsByUserAccountId = transactionItemRepository
.findAll()
.stream()
.collect(Collectors.groupingBy(item -> item.getUserAccount().getId()));
for (UserAccount userAccount : userAccounts) {
List<TransactionItem> itemsForAccount = itemsByUserAccountId.get(userAccount.getId());
BigDecimal balance = itemsForAccount.stream().map(TransactionItem::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
userAccount.setBalance(balance);
}
}
public Transaction get(Long id) {
Transaction transaction = transactionRepository.findOneWithEagerRelationships(id).orElse(null);
setBeneficiary(transaction);
return transaction;
}
private void setBeneficiary(Transaction transaction) {
switch (transaction.getType()) {
case INTERNALTRANSFER -> {
transaction.setBeneficiary(getAccount(Account.Counter));
}
case MATCH, PURCHASE -> {
transaction.setBeneficiary(getAccount(Account.Skarbiec));
}
case FIELDPAYMENT -> {
transaction.setBeneficiary(getAccount(Account.Boisko));
}
}
}
public UserAccount getAccount(Account account) {
return userAccountRepository.findByName(account.toString()).stream().min(Comparator.comparing(UserAccount::getId)).orElse(null);
}
public List<TransactionDTO> getAllTransactions() {
// LOG.debug("REST request to get all Transactions");
// if (eagerload) {
// return transactionRepository.findAllWithEagerRelationships();
// } else {
// return transactionRepository.findAll();
// }
List<Transaction> all = transactionRepository.findAll();
Map<UserAccount, TransactionItem> accounts = new HashMap<>();
Map<Pair<UserAccount, Transaction>, BigDecimal> items = new LinkedHashMap<>();
all.forEach(transaction -> {
transaction
.getTransactionItems()
.forEach(transactionItem -> {
accounts.put(transactionItem.getUserAccount(), transactionItem);
});
});
all.forEach(transaction -> {
transaction
.getTransactionItems()
.forEach(transactionItem -> {
items.put(
Pair.of(transactionItem.getUserAccount(), transaction),
add(items.get(Pair.of(transactionItem.getUserAccount(), transaction)), transactionItem.getAmount())
);
});
});
List<UserAccount> accountsSorted = accounts.keySet().stream().sorted(new UserAccountComparator()).toList();
List<TransactionDTO> out = new ArrayList<>();
for (Transaction transaction : all.stream().sorted(Comparator.comparing(Transaction::getDate)).toList()) {
TransactionDTO transactionDTO = new TransactionDTO(
transaction.getId(),
transaction.getType(),
transaction.getDate(),
transaction.getComment(),
transaction.getEvent()
);
for (UserAccount userAccount : accountsSorted) {
BigDecimal amount = items.get(Pair.of(userAccount, transaction));
TransactionItem item = new TransactionItem();
item.setUserAccount(userAccount);
transactionDTO.getItems().add(item);
item.setAmount(amount);
}
out.add(transactionDTO);
}
return out;
}
BigDecimal add(BigDecimal val, BigDecimal val2) {
if (val == null) return val2;
return val.add(val2);
}
}
@@ -8,14 +8,20 @@ import com.sasiedzi.event.repository.UserRepository;
import com.sasiedzi.event.security.SecurityUtils;
import com.sasiedzi.event.service.dto.AdminUserDTO;
import com.sasiedzi.event.service.dto.UserDTO;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.authentication.RememberMeAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
@@ -129,6 +135,48 @@ public class UserService {
return user;
}
private static Map<String, Object> extractUserAttributes(String input) {
Map<String, Object> attributes = new HashMap<>();
Pattern pattern = Pattern.compile("User Attributes:\\s+\\[\\{([^}]+)\\}\\]");
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
String attributesString = matcher.group(1);
String[] attributePairs = attributesString.split(",\\s*");
for (String pair : attributePairs) {
String[] keyValue = pair.split("=");
if (keyValue.length == 2) {
String key = keyValue[0];
String value = keyValue[1];
// Konwersja wartości "true" i "false" na typ Boolean
if ("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value)) {
attributes.put(key, Boolean.parseBoolean(value));
} else {
// Próba sparsowania jako Instant
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX");
Instant instant = Instant.from(formatter.parse(value));
attributes.put(key, instant);
} catch (Exception e) {
// Jeśli nie jest datą, zostawiamy jako String
attributes.put(key, value);
}
}
}
}
}
return attributes;
}
// private static String extractUsername(String input) {
// Pattern pattern = Pattern.compile("Name:\\s+\\[([^]]+)]");
// Matcher matcher = pattern.matcher(input);
// if (matcher.find()) {
// return matcher.group(1);
// }
// return null;
// }
/**
* Returns the user from an OAuth 2.0 login or resource server with JWT.
* Synchronizes the user in the local repository.
@@ -143,6 +191,10 @@ public class UserService {
attributes = ((OAuth2AuthenticationToken) authToken).getPrincipal().getAttributes();
} else if (authToken instanceof JwtAuthenticationToken) {
attributes = ((JwtAuthenticationToken) authToken).getTokenAttributes();
} else if (authToken instanceof RememberMeAuthenticationToken) {
attributes = extractUserAttributes(authToken.getName());
// attributes = new HashMap<>();
// attributes = ((RememberMeAuthenticationToken) authToken).getTokenAttributes();
} else {
throw new IllegalArgumentException("AuthenticationToken is not OAuth2 or JWT!");
}
@@ -8,10 +8,14 @@ import java.io.Serializable;
import java.time.Instant;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.RequestScope;
/**
* A DTO representing a user, with his authorities.
*/
@Component
@RequestScope
public class AdminUserDTO implements Serializable {
private static final long serialVersionUID = 1L;
@@ -1,6 +1,7 @@
package com.sasiedzi.event.service.dto;
import com.sasiedzi.event.domain.User;
import jakarta.persistence.Transient;
import java.io.Serializable;
import java.util.Objects;
@@ -1,10 +1,12 @@
package com.sasiedzi.event.web.rest;
import com.sasiedzi.event.domain.CurrentUserHolder;
import com.sasiedzi.event.service.UserService;
import com.sasiedzi.event.service.dto.AdminUserDTO;
import java.security.Principal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.web.bind.annotation.GetMapping;
@@ -44,13 +46,16 @@ public class AccountResource {
*/
@GetMapping("/account")
public AdminUserDTO getAccount(Principal principal) {
if (principal instanceof AbstractAuthenticationToken) {
return userService.getUserFromAuthentication((AbstractAuthenticationToken) principal);
if (currentUser != null) {
return currentUser.getAdminUser();
} else {
throw new AccountResourceException("User could not be found");
}
}
@Autowired
CurrentUserHolder currentUser;
/**
* {@code GET /authenticate} : check if the user is authenticated, and return its login.
*
@@ -2,6 +2,7 @@ package com.sasiedzi.event.web.rest;
import com.sasiedzi.event.domain.Charge;
import com.sasiedzi.event.repository.ChargeRepository;
import com.sasiedzi.event.security.AuthoritiesConstants;
import com.sasiedzi.event.web.rest.errors.BadRequestAlertException;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
@@ -14,6 +15,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import tech.jhipster.web.util.HeaderUtil;
@@ -25,6 +27,7 @@ import tech.jhipster.web.util.ResponseUtil;
@RestController
@RequestMapping("/api/charges")
@Transactional
@Secured({ AuthoritiesConstants.ADMIN })
public class ChargeResource {
private static final Logger LOG = LoggerFactory.getLogger(ChargeResource.class);
@@ -2,6 +2,7 @@ package com.sasiedzi.event.web.rest;
import com.sasiedzi.event.domain.Event;
import com.sasiedzi.event.repository.EventRepository;
import com.sasiedzi.event.security.AuthoritiesConstants;
import com.sasiedzi.event.service.EventService;
import com.sasiedzi.event.web.rest.errors.BadRequestAlertException;
import jakarta.validation.Valid;
@@ -15,6 +16,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;
import tech.jhipster.web.util.HeaderUtil;
import tech.jhipster.web.util.ResponseUtil;
@@ -50,6 +52,7 @@ public class EventResource {
* @throws URISyntaxException if the Location URI syntax is incorrect.
*/
@PostMapping("")
@Secured({ AuthoritiesConstants.ADMIN, AuthoritiesConstants.COUNTER })
public ResponseEntity<Event> createEvent(@Valid @RequestBody Event event) throws URISyntaxException {
LOG.debug("REST request to save Event : {}", event);
if (event.getId() != null) {
@@ -62,6 +65,7 @@ public class EventResource {
}
@PostMapping("/{id}/settle")
@Secured({ AuthoritiesConstants.ADMIN, AuthoritiesConstants.COUNTER })
public ResponseEntity<Optional<Event>> settleEvent(@RequestBody Optional<Event> event) throws URISyntaxException {
event = eventService.settle(event.orElse(null));
return ResponseEntity.ok()
@@ -80,6 +84,7 @@ public class EventResource {
* @throws URISyntaxException if the Location URI syntax is incorrect.
*/
@PutMapping("/{id}")
@Secured({ AuthoritiesConstants.ADMIN, AuthoritiesConstants.COUNTER })
public ResponseEntity<Event> updateEvent(@PathVariable(value = "id", required = false) final Long id, @Valid @RequestBody Event event)
throws URISyntaxException {
LOG.debug("REST request to update Event : {}, {}", id, event);
@@ -111,6 +116,7 @@ public class EventResource {
* or with status {@code 500 (Internal Server Error)} if the event couldn't be updated.
* @throws URISyntaxException if the Location URI syntax is incorrect.
*/
@Secured({ AuthoritiesConstants.ADMIN, AuthoritiesConstants.COUNTER })
@PatchMapping(value = "/{id}", consumes = { "application/json", "application/merge-patch+json" })
public ResponseEntity<Event> partialUpdateEvent(
@PathVariable(value = "id", required = false) final Long id,
@@ -154,10 +160,9 @@ public class EventResource {
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the event, or with status {@code 404 (Not Found)}.
*/
@GetMapping("/{id}")
public ResponseEntity<Event> getEvent(@PathVariable("id") Long id) {
public Event getEvent(@PathVariable("id") Long id) {
LOG.debug("REST request to get Event : {}", id);
Optional<Event> event = eventService.findOne(id);
return ResponseUtil.wrapOrNotFound(event);
return eventService.getEventForRegistration(id);
}
/**
@@ -167,6 +172,7 @@ public class EventResource {
* @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
*/
@DeleteMapping("/{id}")
@Secured({ AuthoritiesConstants.ADMIN })
public ResponseEntity<Void> deleteEvent(@PathVariable("id") Long id) {
LOG.debug("REST request to delete Event : {}", id);
eventService.delete(id);
@@ -1,13 +1,21 @@
package com.sasiedzi.event.web.rest;
import com.sasiedzi.event.domain.CurrentUserHolder;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.core.oidc.OidcIdToken;
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser;
import org.springframework.security.oauth2.core.user.DefaultOAuth2User;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -23,24 +31,47 @@ public class LogoutResource {
this.registration = registrations.findByRegistrationId("oidc");
}
@Autowired
CurrentUserHolder currentUser;
/**
* {@code POST /api/logout} : logout the current user.
*
* @param request the {@link HttpServletRequest}.
* @param idToken the ID token.
* @param principal principal with the ID token.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and a body with a global logout URL.
*/
@PostMapping("/api/logout")
public ResponseEntity<?> logout(HttpServletRequest request, @AuthenticationPrincipal(expression = "idToken") OidcIdToken idToken) {
public ResponseEntity<?> logout(HttpServletRequest request, HttpServletResponse response) {
OidcIdToken idToken = null;
Authentication authentication = currentUser.getAuthentication();
StringBuilder logoutUrl = new StringBuilder();
String originUrl = request.getHeader(HttpHeaders.ORIGIN);
if (authentication != null && authentication.getPrincipal() instanceof DefaultOAuth2User) {
idToken = ((DefaultOidcUser) authentication.getPrincipal()).getIdToken();
logoutUrl.append(this.registration.getProviderDetails().getConfigurationMetadata().get("end_session_endpoint").toString());
String originUrl = request.getHeader(HttpHeaders.ORIGIN);
if (idToken != null) {
logoutUrl.append("?id_token_hint=").append(idToken.getTokenValue()).append("&post_logout_redirect_uri=").append(originUrl);
}
} else {
logoutUrl.append(originUrl);
}
new SecurityContextLogoutHandler().logout(request, response, null);
// Ręczne usuwanie ciasteczka 'remember-me'
Cookie cookie = new Cookie("remember-me", null);
cookie.setPath("/");
cookie.setMaxAge(0);
response.addCookie(cookie);
HttpSession existingSession = request.getSession();
if (existingSession != null) {
existingSession.invalidate();
}
request.getSession().invalidate();
return ResponseEntity.ok().body(Map.of("logoutUrl", logoutUrl.toString()));
}
}
@@ -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());
}
}
@@ -1,9 +1,11 @@
package com.sasiedzi.event.web.rest;
import com.sasiedzi.event.domain.CurrentUserHolder;
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;
@@ -20,6 +22,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
@@ -49,11 +52,21 @@ public class RegistrationResource {
@Autowired
private UserRepository userRepository;
@Autowired
private EventService eventService;
public RegistrationResource(RegistrationRepository registrationRepository, UserRepository userRepository) {
this.registrationRepository = registrationRepository;
this.userRepository = userRepository;
}
private Long getCurrentEventId() {
return eventService.getCurrentEventId();
}
@Autowired
CurrentUserHolder currentUser;
/**
* {@code POST /registrations} : Create a new registration.
*
@@ -64,6 +77,12 @@ public class RegistrationResource {
@PostMapping("")
public ResponseEntity<Registration> createRegistration(@Valid @RequestBody Registration registration, Principal principal)
throws URISyntaxException {
if (
!currentUser.getAdminUser().getAuthorities().contains("ROLE_ADMIN") &&
!registration.getEvent().getId().equals(getCurrentEventId())
) {
throw new AccessDeniedException("Registration is closed for this event");
}
LOG.debug("REST request to save Registration : {}", registration);
AdminUserDTO userFromAuthentication;
if (principal instanceof AbstractAuthenticationToken) {
@@ -97,6 +116,12 @@ public class RegistrationResource {
@PathVariable(value = "id", required = false) final Long id,
@Valid @RequestBody Registration registration
) throws URISyntaxException {
if (
!currentUser.getAdminUser().getAuthorities().contains("ROLE_ADMIN") &&
!registration.getEvent().getId().equals(getCurrentEventId())
) {
throw new AccessDeniedException("Registration is closed for this event");
}
LOG.debug("REST request to update Registration : {}, {}", id, registration);
if (registration.getId() == null) {
throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
@@ -131,6 +156,12 @@ public class RegistrationResource {
@PathVariable(value = "id", required = false) final Long id,
@NotNull @RequestBody Registration registration
) throws URISyntaxException {
if (
!currentUser.getAdminUser().getAuthorities().contains("ROLE_ADMIN") &&
!registration.getEvent().getId().equals(getCurrentEventId())
) {
throw new AccessDeniedException("Registration is closed for this event");
}
LOG.debug("REST request to partial update Registration partially : {}, {}", id, registration);
if (registration.getId() == null) {
throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
@@ -208,8 +239,16 @@ public class RegistrationResource {
*/
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteRegistration(@PathVariable("id") Long id) {
Registration registration = registrationRepository.findById(id).get();
if (
!currentUser.getAdminUser().getAuthorities().contains("ROLE_ADMIN") &&
!registration.getEvent().getId().equals(getCurrentEventId())
) {
throw new AccessDeniedException("Registration is closed for this event");
}
LOG.debug("REST request to delete Registration : {}", id);
registrationRepository.deleteById(id);
registration.setActive(Boolean.FALSE);
registrationRepository.save(registration);
return ResponseEntity.noContent()
.headers(HeaderUtil.createEntityDeletionAlert(applicationName, false, ENTITY_NAME, id.toString()))
.build();
@@ -2,6 +2,7 @@ package com.sasiedzi.event.web.rest;
import com.sasiedzi.event.domain.TransactionItem;
import com.sasiedzi.event.repository.TransactionItemRepository;
import com.sasiedzi.event.security.AuthoritiesConstants;
import com.sasiedzi.event.web.rest.errors.BadRequestAlertException;
import java.net.URI;
import java.net.URISyntaxException;
@@ -12,6 +13,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import tech.jhipster.web.util.HeaderUtil;
@@ -23,6 +25,7 @@ import tech.jhipster.web.util.ResponseUtil;
@RestController
@RequestMapping("/api/transaction-items")
@Transactional
@Secured({ AuthoritiesConstants.ADMIN })
public class TransactionItemResource {
private static final Logger LOG = LoggerFactory.getLogger(TransactionItemResource.class);
@@ -127,6 +130,9 @@ public class TransactionItemResource {
if (transactionItem.getComment() != null) {
existingTransactionItem.setComment(transactionItem.getComment());
}
if (transactionItem.getLocked() != null) {
existingTransactionItem.setLocked(transactionItem.getLocked());
}
return existingTransactionItem;
})
@@ -2,6 +2,8 @@ package com.sasiedzi.event.web.rest;
import com.sasiedzi.event.domain.*;
import com.sasiedzi.event.repository.TransactionRepository;
import com.sasiedzi.event.security.AuthoritiesConstants;
import com.sasiedzi.event.service.TransactionService;
import com.sasiedzi.event.web.rest.errors.BadRequestAlertException;
import java.math.BigDecimal;
import java.net.URI;
@@ -10,8 +12,10 @@ import java.util.*;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import tech.jhipster.web.util.HeaderUtil;
@@ -46,12 +50,13 @@ public class TransactionResource {
* @throws URISyntaxException if the Location URI syntax is incorrect.
*/
@PostMapping("")
@Secured({ AuthoritiesConstants.ADMIN, AuthoritiesConstants.COUNTER })
public ResponseEntity<Transaction> createTransaction(@RequestBody Transaction transaction) throws URISyntaxException {
LOG.debug("REST request to save Transaction : {}", transaction);
if (transaction.getId() != null) {
throw new BadRequestAlertException("A new transaction cannot already have an ID", ENTITY_NAME, "idexists");
}
transaction = transactionRepository.save(transaction);
transaction = transactionService.save(transaction);
return ResponseEntity.created(new URI("/api/transactions/" + transaction.getId()))
.headers(HeaderUtil.createEntityCreationAlert(applicationName, false, ENTITY_NAME, transaction.getId().toString()))
.body(transaction);
@@ -67,6 +72,7 @@ public class TransactionResource {
* or with status {@code 500 (Internal Server Error)} if the transaction couldn't be updated.
* @throws URISyntaxException if the Location URI syntax is incorrect.
*/
@Secured({ AuthoritiesConstants.ADMIN, AuthoritiesConstants.COUNTER })
@PutMapping("/{id}")
public ResponseEntity<Transaction> updateTransaction(
@PathVariable(value = "id", required = false) final Long id,
@@ -84,7 +90,7 @@ public class TransactionResource {
throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
}
transaction = transactionRepository.save(transaction);
transaction = transactionService.save(transaction);
return ResponseEntity.ok()
.headers(HeaderUtil.createEntityUpdateAlert(applicationName, false, ENTITY_NAME, transaction.getId().toString()))
.body(transaction);
@@ -101,6 +107,7 @@ public class TransactionResource {
* or with status {@code 500 (Internal Server Error)} if the transaction couldn't be updated.
* @throws URISyntaxException if the Location URI syntax is incorrect.
*/
@Secured({ AuthoritiesConstants.ADMIN, AuthoritiesConstants.COUNTER })
@PatchMapping(value = "/{id}", consumes = { "application/json", "application/merge-patch+json" })
public ResponseEntity<Transaction> partialUpdateTransaction(
@PathVariable(value = "id", required = false) final Long id,
@@ -141,6 +148,18 @@ public class TransactionResource {
);
}
@GetMapping("/forAccount/{userAccountId}")
public List<TransactionDTO> getAllTransactionsForAccountId(
@PathVariable(value = "userAccountId", required = false) final Long userAccountId,
@RequestParam(name = "eagerload", required = false, defaultValue = "true") boolean eagerload
) {
List<TransactionDTO> allTransactions = getAllTransactions(true);
if (userAccountId != null) {
// allTransactions.removeFirst();
}
return allTransactions;
}
/**
* {@code GET /transactions} : get all the transactions.
*
@@ -151,64 +170,7 @@ public class TransactionResource {
public List<TransactionDTO> getAllTransactions(
@RequestParam(name = "eagerload", required = false, defaultValue = "true") boolean eagerload
) {
// LOG.debug("REST request to get all Transactions");
// if (eagerload) {
// return transactionRepository.findAllWithEagerRelationships();
// } else {
// return transactionRepository.findAll();
// }
List<Transaction> all = transactionRepository.findAll();
Map<UserAccount, TransactionItem> accounts = new HashMap<>();
Map<Pair<UserAccount, Transaction>, BigDecimal> items = new HashMap<>();
all.forEach(transaction -> {
transaction
.getTransactionItems()
.forEach(transactionItem -> {
accounts.put(transactionItem.getUserAccount(), transactionItem);
});
});
all.forEach(transaction -> {
transaction
.getTransactionItems()
.forEach(transactionItem -> {
items.put(
Pair.of(transactionItem.getUserAccount(), transaction),
add(items.get(Pair.of(transactionItem.getUserAccount(), transaction)), transactionItem.getAmount())
);
});
});
List<UserAccount> accountsSorted = accounts
.keySet()
.stream()
.sorted(Comparator.comparing(UserAccount::getName).thenComparing(Comparator.comparing(UserAccount::getName)))
.toList();
List<TransactionDTO> out = new ArrayList<>();
for (Transaction transaction : all.stream().sorted(Comparator.comparing(Transaction::getDate)).toList()) {
TransactionDTO transactionDTO = new TransactionDTO(
transaction.getId(),
transaction.getType(),
transaction.getDate(),
transaction.getComment(),
transaction.getEvent()
);
for (UserAccount userAccount : accountsSorted) {
BigDecimal amount = items.get(Pair.of(userAccount, transaction));
TransactionItem item = new TransactionItem();
item.setUserAccount(userAccount);
transactionDTO.getItems().add(item);
item.setAmount(amount);
}
out.add(transactionDTO);
}
return out;
}
BigDecimal add(BigDecimal val, BigDecimal val2) {
if (val == null) return val2;
return val.add(val2);
return transactionService.getAllTransactions();
}
// private Comparator<UserAccount> getMyAccountFirstComparator (final String myAccountName) {
@@ -252,10 +214,54 @@ public class TransactionResource {
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the transaction, or with status {@code 404 (Not Found)}.
*/
@GetMapping("/{id}")
public ResponseEntity<Transaction> getTransaction(@PathVariable("id") Long id) {
public Transaction getTransaction(@PathVariable("id") Long id) {
LOG.debug("REST request to get Transaction : {}", id);
Optional<Transaction> transaction = transactionRepository.findOneWithEagerRelationships(id);
return ResponseUtil.wrapOrNotFound(transaction);
return transactionService.get(id);
// Optional<Transaction> transaction = transactionRepository.findOneWithEagerRelationships(id);
// // if (transaction.isPresent()) {
// // transaction.get().getTransactionItems().stream().sorted();
// // transaction.get().getTransactionItems().sort(Comparator.comparing(item -> item.getUserAccount().getName()));
// // }
// return ResponseUtil.wrapOrNotFound(transaction);
}
@Autowired
TransactionService transactionService;
/**
* {@code GET /transactions/:id} : get the "id" transaction.
*
* @param id the id of the transaction to retrieve.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the transaction, or with status {@code 404 (Not Found)}.
*/
@GetMapping("/payments/{id}")
public Transaction createPayments(@PathVariable("id") Long id) {
LOG.debug("REST request to get Transaction : {}", id);
return transactionService.createPayments(id);
}
/**
* {@code GET /transactions/:id} : get the "id" transaction.
*
* @param id the id of the transaction to retrieve.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the transaction, or with status {@code 404 (Not Found)}.
*/
@GetMapping("/field-payment/{id}")
public Transaction getTransactionPayments(@PathVariable("id") Long id) {
LOG.debug("REST request to get Transaction : {}", id);
return transactionService.createFieldPayment(id);
}
/**
* {@code GET /transactions/:id} : get the "id" transaction.
*
* @param id the id of the transaction to retrieve.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the transaction, or with status {@code 404 (Not Found)}.
*/
@GetMapping("/opposing/{id}")
public Transaction getTransactionOpposite(@PathVariable("id") Long id) {
LOG.debug("REST request to get Transaction : {}", id);
return transactionService.createOpposite(id);
}
/**
@@ -265,6 +271,7 @@ public class TransactionResource {
* @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
*/
@DeleteMapping("/{id}")
@Secured({ AuthoritiesConstants.ADMIN })
public ResponseEntity<Void> deleteTransaction(@PathVariable("id") Long id) {
LOG.debug("REST request to delete Transaction : {}", id);
transactionRepository.deleteById(id);
@@ -1,7 +1,12 @@
package com.sasiedzi.event.web.rest;
import com.sasiedzi.event.domain.CurrentUserHolder;
import com.sasiedzi.event.domain.User;
import com.sasiedzi.event.domain.UserAccount;
import com.sasiedzi.event.repository.UserAccountRepository;
import com.sasiedzi.event.security.AuthoritiesConstants;
import com.sasiedzi.event.service.EventService;
import com.sasiedzi.event.service.UserService;
import com.sasiedzi.event.web.rest.errors.BadRequestAlertException;
import java.net.URI;
import java.net.URISyntaxException;
@@ -10,8 +15,12 @@ import java.util.Objects;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import tech.jhipster.web.util.HeaderUtil;
@@ -46,6 +55,7 @@ public class UserAccountResource {
* @throws URISyntaxException if the Location URI syntax is incorrect.
*/
@PostMapping("")
@Secured({ AuthoritiesConstants.ADMIN })
public ResponseEntity<UserAccount> createUserAccount(@RequestBody UserAccount userAccount) throws URISyntaxException {
LOG.debug("REST request to save UserAccount : {}", userAccount);
if (userAccount.getId() != null) {
@@ -67,6 +77,7 @@ public class UserAccountResource {
* or with status {@code 500 (Internal Server Error)} if the userAccount couldn't be updated.
* @throws URISyntaxException if the Location URI syntax is incorrect.
*/
@Secured({ AuthoritiesConstants.ADMIN })
@PutMapping("/{id}")
public ResponseEntity<UserAccount> updateUserAccount(
@PathVariable(value = "id", required = false) final Long id,
@@ -101,6 +112,7 @@ public class UserAccountResource {
* or with status {@code 500 (Internal Server Error)} if the userAccount couldn't be updated.
* @throws URISyntaxException if the Location URI syntax is incorrect.
*/
@Secured({ AuthoritiesConstants.ADMIN })
@PatchMapping(value = "/{id}", consumes = { "application/json", "application/merge-patch+json" })
public ResponseEntity<UserAccount> partialUpdateUserAccount(
@PathVariable(value = "id", required = false) final Long id,
@@ -166,6 +178,14 @@ public class UserAccountResource {
return ResponseUtil.wrapOrNotFound(userAccount);
}
@GetMapping("/currentUser")
public UserAccount getCurrentUserAccount() {
return currentUser.getUserAccount();
}
@Autowired
CurrentUserHolder currentUser;
/**
* {@code DELETE /user-accounts/:id} : delete the "id" userAccount.
*
@@ -173,6 +193,7 @@ public class UserAccountResource {
* @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
*/
@DeleteMapping("/{id}")
@Secured({ AuthoritiesConstants.ADMIN })
public ResponseEntity<Void> deleteUserAccount(@PathVariable("id") Long id) {
LOG.debug("REST request to delete UserAccount : {}", id);
userAccountRepository.deleteById(id);
@@ -180,4 +201,7 @@ public class UserAccountResource {
.headers(HeaderUtil.createEntityDeletionAlert(applicationName, false, ENTITY_NAME, id.toString()))
.build();
}
@Autowired
EventService eventService;
}
@@ -2,6 +2,7 @@ package com.sasiedzi.event.web.rest.errors;
import static org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation;
import com.sasiedzi.event.SasiedziApp;
import jakarta.servlet.http.HttpServletRequest;
import java.net.URI;
import java.util.Arrays;
@@ -9,7 +10,10 @@ import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.dao.ConcurrencyFailureException;
@@ -57,9 +61,16 @@ public class ExceptionTranslator extends ResponseEntityExceptionHandler {
this.env = env;
}
private static final Logger LOG = LoggerFactory.getLogger(ExceptionTranslator.class);
@ExceptionHandler
public ResponseEntity<Object> handleAnyException(Throwable ex, NativeWebRequest request) {
ProblemDetailWithCause pdCause = wrapAndCustomizeProblem(ex, request);
if (pdCause.getStatus() == HttpStatus.INTERNAL_SERVER_ERROR.value() && !env.matchesProfiles("dev")) {
// If profile is dev, then Logging Aspect will handle logging.
// In all other profiles no error is logged :-( No idea why that should be a good decision.
LOG.error("Internal Server error happened", ex);
}
return handleExceptionInternal((Exception) ex, pdCause, buildHeaders(ex), HttpStatusCode.valueOf(pdCause.getStatus()), request);
}
@@ -20,6 +20,9 @@
<column name="comment" type="varchar(255)">
<constraints nullable="true" />
</column>
<column name="locked" type="boolean">
<constraints nullable="true" />
</column>
<column name="user_account_id" type="bigint">
<constraints nullable="true" />
</column>
@@ -29,6 +32,9 @@
<column name="event_id" type="bigint">
<constraints nullable="true" />
</column>
<column name="registration_id" type="bigint">
<constraints nullable="true" />
</column>
<!-- jhipster-needle-liquibase-add-column - JHipster will add columns here -->
</createTable>
</changeSet>
@@ -52,6 +58,7 @@
<column name="id" type="numeric"/>
<column name="amount" type="numeric"/>
<column name="comment" type="string"/>
<column name="locked" type="boolean"/>
<!-- jhipster-needle-liquibase-add-loadcolumn - JHipster (and/or extensions) can add load columns here -->
</loadData>
</changeSet>
@@ -30,5 +30,12 @@
referencedColumnNames="id"
referencedTableName="event"
/>
<addForeignKeyConstraint baseColumnNames="registration_id"
baseTableName="transaction_item"
constraintName="fk_transaction_item__registration_id"
referencedColumnNames="id"
referencedTableName="registration"
/>
</changeSet>
</databaseChangeLog>
@@ -0,0 +1,18 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
<changeSet author="Zet (generated)" id="1731926141078-7">
<addColumn tableName="registration">
<column name="user_account_id" type="bigint">
<constraints nullable="true" />
</column>
</addColumn>
</changeSet>
<changeSet author="Zet (generated)" id="1731926141078-8">
<addForeignKeyConstraint baseColumnNames="user_account_id"
baseTableName="registration"
constraintName="fk_registration__user_account_id"
referencedColumnNames="id"
referencedTableName="user_account"
/>
</changeSet>
</databaseChangeLog>
@@ -1,2 +1,2 @@
id;amount;comment
id;amount;comment;locked
1 id amount comment locked
2
@@ -25,4 +25,5 @@
<include file="config/liquibase/changelog/20241113151058_added_entity_constraints_TransactionItem.xml" relativeToChangelogFile="false"/>
<!-- jhipster-needle-liquibase-add-constraints-changelog - JHipster will add liquibase constraints changelogs here -->
<!-- jhipster-needle-liquibase-add-incremental-changelog - JHipster will add incremental liquibase changelogs here -->
<include file="config/liquibase/changelog/20241118103414_changelog.xml" relativeToChangelogFile="false"/>
</databaseChangeLog>
@@ -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) {
@@ -1,6 +1,6 @@
<template>
<div id="footer" class="footer">
<p>This is your footer</p>
<p>Coś nie działa? napisz na FB Sąsiedzi</p>
</div>
</template>
@@ -1,10 +1,13 @@
import { type Ref, computed, defineComponent, inject, ref } from 'vue';
import { useRouter } from 'vue-router';
import { useRoute, useRouter } from 'vue-router';
import type LoginService from '@/account/login.service';
import type AccountService from '@/account/account.service';
import EntitiesMenu from '@/entities/entities-menu.vue';
import { useStore } from '@/store';
import UserAccountService from '@/entities/user-account/user-account.service';
import { useAlertService } from '@/shared/alert/alert.service';
import type { IUserAccount } from '@/shared/model/user-account.model';
export default defineComponent({
compatConfig: { MODE: 3 },
@@ -27,6 +30,21 @@ export default defineComponent({
const inProduction = computed(() => store.activeProfiles.indexOf('prod') > -1);
const authenticated = computed(() => store.authenticated);
const userAccountService = inject('userAccountService', () => new UserAccountService());
const userAccount: Ref<IUserAccount> = ref({});
const retrieveUserAccount = async () => {
try {
const res = await userAccountService().getCurrentUser();
userAccount.value = res;
} catch (error) {
userAccount.value = {};
}
};
console.log('Asdf');
retrieveUserAccount();
console.log('Asdf2');
const openLogin = () => {
loginService.login();
};
@@ -53,12 +71,14 @@ export default defineComponent({
subIsActive,
accountService,
openLogin,
userAccount,
version,
currentLanguage,
hasAnyAuthorityValues,
openAPIEnabled,
inProduction,
authenticated,
retrieveUserAccount,
};
},
methods: {
@@ -1,8 +1,19 @@
<template>
<b-navbar data-cy="navbar" toggleable="md" type="dark" class="jh-navbar">
<b-navbar-brand class="logo" b-link to="/">
<span class="logo-img"></span>
<span class="navbar-title">Sasiedzi</span> <span class="navbar-version">{{ version }}</span>
<!-- <span class="logo-img"></span>-->
<!-- <span class="navbar-title">Sasiedzi</span>-->
<!-- <span class="navbar-version">{{ // version }}</span>-->
<!-- <span class="navbar-version"><span class="bold">PLN {{ userAccount.name}}</span></span>-->
<router-link :to="{ name: 'MyTransaction', params: { userAccountId: userAccount.id } }">
<!-- <router-link :to="{ name: 'TransactionView' }">-->
<span class="navbar-title"> Saldo: {{ userAccount.balance }} PLN </span>
</router-link>
<button class="btn" @click="retrieveUserAccount">
<font-awesome-icon :icon="['fas', 'sync']" />
</button>
</b-navbar-brand>
<b-navbar-toggle
right
@@ -94,7 +105,8 @@
<template #button-content>
<span class="navbar-dropdown-menu">
<font-awesome-icon icon="user" />
<span class="no-bold">Account</span>
<!-- <span class="no-bold">Stan konta {{userAccount.balance}} PLN</span>-->
<span class="no-bold">{{ userAccount.name }}</span>
</span>
</template>
<b-dropdown-item data-cy="logout" v-if="authenticated" @click="logout()" id="logout" active-class="active">
@@ -10,16 +10,19 @@ 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({
compatConfig: { MODE: 3 },
name: 'EventDetails',
setup() {
console.log('jestem tu');
const eventService = inject('eventService', () => new EventService());
const alertService = inject('alertService', () => useAlertService(), true);
const { formatDateShort } = useDateFormat();
const dataUtils = useDataUtils();
const isCurrentEvent = ref(false);
const registrationService = inject('registrationService', () => new RegistrationService());
const accountService = inject<AccountService>('accountService');
@@ -31,9 +34,18 @@ export default defineComponent({
const event: Ref<IEvent> = ref({});
const sortedAndIndexedRegistrations: Ref<IRegistration[]> = 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 == currentEventId;
event.value = res;
// sortedAndIndexedRegistrations.value = res.registrations;
sortedAndIndexedRegistrations.value = res.registrations.sort(
@@ -44,8 +56,11 @@ export default defineComponent({
}
};
console.log('asdfasd' + route.meta?.eventId);
if (route.params?.eventId) {
retrieveEvent(route.params.eventId as string);
} else if (route.meta?.eventId) {
retrieveEvent(route.meta.eventId as string);
}
// const sortedAndIndexedRegistrations = () => {
@@ -108,6 +123,7 @@ export default defineComponent({
accountService,
eventService,
event,
isCurrentEvent,
...dataUtils,
formatDateShort,
previousState,
@@ -1,6 +1,6 @@
<template>
<div class="row justify-content-center">
<div class="col-8">
<div class="col-12">
<div v-if="event">
<!-- <h2 class="jh-entity-heading" data-cy="eventDetailsHeading"><span>Event</span> {{ event.id }}</h2>-->
<dl class="row jh-entity-details">
@@ -35,14 +35,27 @@
<span>{{ event.comment }}</span>
</dd>
</dl>
<router-link :to="{ name: 'RegistrationCreateForEvent', params: { eventId: event.id } }" custom v-slot="{ navigate }">
<router-link
:to="{ name: 'RegistrationCreateForEvent', params: { eventId: event.id } }"
custom
v-slot="{ navigate }"
v-if="event.active"
>
<button @click="navigate" class="btn btn-primary">
<font-awesome-icon icon="plus"></font-awesome-icon>&nbsp;<span>Dołącz do wydarzenia</span>
</button>
</router-link>
<button class="btn btn-primary float-right" v-if="hasAnyAuthority('ROLE_ADMIN')" @click="settle()">
<font-awesome-icon icon="sync"></font-awesome-icon> <span>Rozlicz wydarzenie</span>
</button>
<!-- <router-link-->
<!-- :to="{ name: 'TransactionFieldPayment', params: { paymentForFieldByEventId: event.id } }"-->
<!-- custom-->
<!-- v-slot="{ navigate }"-->
<!-- v-if="hasAnyAuthority(['ROLE_ADMIN', 'ROLE_COUNTER'])"-->
<!-- >-->
<!-- <button @click="navigate" class="btn btn-primary">-->
<!-- <font-awesome-icon icon="plus"></font-awesome-icon>&nbsp;<span>Opłać boisko</span>-->
<!-- </button>-->
<!-- </router-link>-->
<div class="table-responsive" v-if="event.registrations && event.registrations.length > 0">
<table class="table table-striped" aria-describedby="event.registrations">
<thead>
@@ -61,15 +74,22 @@
<!-- Display row number or empty if not active -->
{{ getRegistrationIndex(index) }}
</td>
<td>{{ formatDateShort(registration.dateTime) || '' }}</td>
<td>
{{ registration.playerName }}
<span :class="{ strikethrough: registration.active === false }">{{ formatDateShort(registration.dateTime) || '' }}</span>
</td>
<td>
<span :class="{ strikethrough: registration.active === false }">{{ registration.playerName }}</span>
<b-button
@click="prepareRemove(registration)"
variant="danger"
class="btn btn-sm"
data-cy="entityDeleteButton"
v-if="(registration.id && registration.user?.id == currentUserId) || hasAnyAuthority('ROLE_ADMIN')"
v-if="
((registration.id && registration.user?.id == currentUserId && isCurrentEvent) ||
hasAnyAuthority(['ROLE_ADMIN', 'ROLE_COUNTER'])) &&
registration.active !== false &&
(event.active || (event.editable && hasAnyAuthority(['ROLE_COUNTER'])) || hasAnyAuthority(['ROLE_ADMIN']))
"
v-b-modal.removeEntity
>
<font-awesome-icon icon="times"></font-awesome-icon>
@@ -141,6 +161,72 @@
</div>
</div>
</div>
<button
class="btn btn-primary float-right"
v-if="(event.editable && hasAnyAuthority(['ROLE_COUNTER'])) || hasAnyAuthority(['ROLE_ADMIN'])"
@click="settle()"
>
<font-awesome-icon icon="sync"></font-awesome-icon> <span v-if="!event.charged">Rozlicz wydarzenie</span
><span v-if="event.charged">Przelicz ponownie zobowiązania</span>
</button>
<router-link
v-if="event.id && event.paid == false"
:to="{ name: 'TransactionFieldPayment', params: { paymentForFieldByEventId: event.id } }"
custom
v-slot="{ navigate }"
><!-- <router-link
v-if="transaction.id"
:to="{ name: 'TransactionCreateOpposite', params: { opposingTransactionId: transaction.id } }"
custom
v-slot="{ navigate }"
>-->
<button @click="navigate" class="btn btn-primary" v-if="hasAnyAuthority(['ROLE_ADMIN', 'ROLE_COUNTER'])">
<font-awesome-icon icon="pencil-alt"></font-awesome-icon>&nbsp;<span>Dodaj płatność za boisko</span>
</button>
</router-link>
<router-link
v-if="event.charged && event.editable"
:to="{ name: 'TransactionPaymentsForTransaction', params: { paymentsForTransactionId: event.chargeTransactionId } }"
custom
v-slot="{ navigate }"
><!-- <router-link
v-if="transaction.id"
:to="{ name: 'TransactionCreateOpposite', params: { opposingTransactionId: transaction.id } }"
custom
v-slot="{ navigate }"
>-->
<button @click="navigate" class="btn btn-primary" v-if="hasAnyAuthority(['ROLE_ADMIN', 'ROLE_COUNTER'])">
<font-awesome-icon icon="pencil-alt"></font-awesome-icon>&nbsp;<span>Dodaj wpłaty na konto</span>
</button>
</router-link>
<div class="table-responsive" v-if="event.transactions && event.transactions.length > 0">
Rozliczenie:
<table class="table table-striped" aria-describedby="transactions">
<thead>
<tr>
<!-- <th/>-->
<!-- <th/>-->
<!-- <th/>-->
<th />
</tr>
</thead>
<tbody>
<tr v-for="transaction in event.transactions" :key="transaction.id" data-cy="entityTable">
<td>
<router-link :to="{ name: 'TransactionView', params: { transactionId: transaction.id } }"
>{{ transaction.date }} {{ transaction.type }}
</router-link>
<div v-if="transaction.comment">{{ transaction.comment }}</div>
</td>
<!-- <td>{{ transaction.type }}</td>-->
<!-- <td>{{ transaction.date }}</td>-->
<!-- <td>{{ transaction.comment }}</td>-->
</tr>
</tbody>
</table>
</div>
</template>
<script lang="ts" src="./event-details.component.ts"></script>
+2 -2
View File
@@ -6,7 +6,7 @@
<button class="btn btn-info mr-2" @click="handleSyncList" :disabled="isFetching">
<font-awesome-icon icon="sync" :spin="isFetching"></font-awesome-icon> <span>Odśwież listę</span>
</button>
<router-link :to="{ name: 'EventCreate' }" custom v-slot="{ navigate }" v-if="hasAnyAuthority('ROLE_ADMIN')">
<router-link :to="{ name: 'EventCreate' }" custom v-slot="{ navigate }" v-if="hasAnyAuthority(['ROLE_ADMIN', 'ROLE_COUNTER'])">
<button
@click="navigate"
id="jh-create-entity"
@@ -58,7 +58,7 @@
:to="{ name: 'EventEdit', params: { eventId: event.id } }"
custom
v-slot="{ navigate }"
v-if="hasAnyAuthority('ROLE_ADMIN')"
v-if="hasAnyAuthority(['ROLE_ADMIN', 'ROLE_COUNTER'])"
>
<button @click="navigate" class="btn btn-primary btn-sm edit" data-cy="entityEditButton">
<font-awesome-icon icon="pencil-alt"></font-awesome-icon>
@@ -108,6 +108,7 @@ export default defineComponent({
registration.value = res;
// Assuming you can retrieve event details here
eventName.value = '' + registration.value.event?.name; // Set this from the event details
currentUserFullName.value = '' + registration.value.user?.firstName + ' ' + registration.value.user?.lastName;
} catch (error) {
alertService.showHttpError(error.response);
}
@@ -11,7 +11,8 @@ export default defineComponent({
setup() {
const transactionService = inject('transactionService', () => new TransactionService());
const alertService = inject('alertService', () => useAlertService(), true);
const hasAnyAuthorityValues: Ref<any> = ref({});
const accountService = inject<AccountService>('accountService');
const route = useRoute();
const router = useRouter();
@@ -34,8 +35,19 @@ export default defineComponent({
return {
alertService,
transaction,
accountService,
hasAnyAuthorityValues,
previousState,
};
},
methods: {
hasAnyAuthority(authorities: any): boolean {
this.accountService.hasAnyAuthorityAndCheckAuth(authorities).then(value => {
if (this.hasAnyAuthorityValues[authorities] !== value) {
this.hasAnyAuthorityValues = { ...this.hasAnyAuthorityValues, [authorities]: value };
}
});
return this.hasAnyAuthorityValues[authorities] ?? false;
},
},
});
@@ -1,6 +1,6 @@
<template>
<div class="row justify-content-center">
<div class="col-8">
<div class="col-12">
<div v-if="transaction">
<h2 class="jh-entity-heading" data-cy="transactionDetailsHeading"><span>Transaction</span> {{ transaction.id }}</h2>
<dl class="row jh-entity-details">
@@ -40,10 +40,68 @@
custom
v-slot="{ navigate }"
>
<button @click="navigate" class="btn btn-primary">
<button @click="navigate" class="btn btn-primary" v-if="hasAnyAuthority(['ROLE_ADMIN', 'ROLE_COUNTER'])">
<font-awesome-icon icon="pencil-alt"></font-awesome-icon>&nbsp;<span>Edit</span>
</button>
</router-link>
<router-link
v-if="transaction.id && transaction.type == 'MATCH'"
:to="{ name: 'TransactionPaymentsForTransaction', params: { paymentsForTransactionId: transaction.id } }"
custom
v-slot="{ navigate }"
><!-- <router-link
v-if="transaction.id"
:to="{ name: 'TransactionCreateOpposite', params: { opposingTransactionId: transaction.id } }"
custom
v-slot="{ navigate }"
>-->
<button @click="navigate" class="btn btn-primary" v-if="hasAnyAuthority(['ROLE_ADMIN', 'ROLE_COUNTER'])">
<font-awesome-icon icon="pencil-alt"></font-awesome-icon>&nbsp;<span>Dodaj wpłaty na konto</span>
</button>
</router-link>
<!-- :to="{ name: 'TransactionPaymentsForTransaction', params: { paymentsForTransactionId: transaction.id } }"-->
<!-- <router-link-->
<!-- :to="{ name: 'TransactionPaymentsForTransaction', params: { transactionId: transaction.id } }"-->
<!-- custom-->
<!-- v-slot="{ navigate }"-->
<!-- v-if="hasAnyAuthority(['ROLE_ADMIN', 'ROLE_COUNTER'])"-->
<!-- >-->
<!-- <button @click="navigate" class="btn btn-primary">-->
<!-- <font-awesome-icon icon="plus"></font-awesome-icon>&nbsp;<span>Dodaj wpłaty na konto</span>-->
<!-- </button>-->
<!-- </router-link>-->
<!-- <router-link :to="{ name: 'TransactionCreateOpposite', params: { opposingTransactionId: transaction.id } }">Create opposite transaction</router-link>-->
</div>
<div class="form-group">
<div class="table-responsive">
<table class="table table-striped" aria-describedby="event.registrations">
<thead>
<tr>
<th scope="col"><span>#</span></th>
<th scope="col"><span>Account</span></th>
<th scope="col"><span>Amount</span></th>
<th scope="col"><span>Comment</span></th>
</tr>
</thead>
<tbody>
<tr v-for="(item, index) in transaction.transactionItems" :key="index" class="transaction-item">
<td>
{{ index + 1 }}
</td>
<td>
{{ item.userAccount?.name }}
</td>
<td>
{{ item.amount }}
</td>
<td>
{{ item.comment }}
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
@@ -10,6 +10,8 @@ import EventService from '@/entities/event/event.service';
import { type IEvent } from '@/shared/model/event.model';
import { type ITransaction, Transaction } from '@/shared/model/transaction.model';
import { TransactionType } from '@/shared/model/enumerations/transaction-type.model';
import { type IUserAccount } from '@/shared/model/user-account.model';
import UserAccountService from '@/entities/user-account/user-account.service';
export default defineComponent({
compatConfig: { MODE: 3 },
@@ -20,7 +22,11 @@ export default defineComponent({
const transaction: Ref<ITransaction> = ref(new Transaction());
transaction.value.transactionItems = [];
const dictUserAccounts = ref([]) as Ref<IUserAccount[]>;
const eventService = inject('eventService', () => new EventService());
const userAccountService = inject('userAccountService', () => new UserAccountService());
const events: Ref<IEvent[]> = ref([]);
const transactionTypeValues: Ref<string[]> = ref(Object.keys(TransactionType));
@@ -32,18 +38,92 @@ export default defineComponent({
const previousState = () => router.go(-1);
const retrieveTransaction = async transactionId => {
const retrieveTransaction = async (transactionId, opposing) => {
try {
const res = await transactionService().find(transactionId);
if (dictUserAccounts.value.length == 0) {
const res2 = await userAccountService().retrieve();
dictUserAccounts.value = res2.data;
}
const res = await transactionService().find(transactionId, opposing);
transaction.value = res;
transaction.value.transactionItems.forEach((item, index) => {
const matchingAccount = dictUserAccounts.value.find(account => account.id === item.userAccount.id);
transaction.value.transactionItems[index].userAccount = matchingAccount || null;
});
} catch (error) {
alertService.showHttpError(error.response);
}
};
if (route.params?.transactionId) {
retrieveTransaction(route.params.transactionId);
const retrieveTransactionForPayments = async transactionId => {
try {
if (dictUserAccounts.value.length == 0) {
const res2 = await userAccountService().retrieve();
dictUserAccounts.value = res2.data;
}
const res = await transactionService().payments(transactionId);
transaction.value = res;
transaction.value.transactionItems.forEach((item, index) => {
const matchingAccount = dictUserAccounts.value.find(account => account.id === item.userAccount.id);
transaction.value.transactionItems[index].userAccount = matchingAccount || null;
});
} catch (error) {
alertService.showHttpError(error.response);
}
};
const retrieveTransactionForFieldPayment = async eventId => {
try {
if (dictUserAccounts.value.length == 0) {
const res2 = await userAccountService().retrieve();
dictUserAccounts.value = res2.data;
}
const res = await transactionService().fieldPayment(eventId);
transaction.value = res;
transaction.value.transactionItems.forEach((item, index) => {
const matchingAccount = dictUserAccounts.value.find(account => account.id === item.userAccount.id);
transaction.value.transactionItems[index].userAccount = matchingAccount || null;
});
} catch (error) {
alertService.showHttpError(error.response);
}
};
const retrieveUserAccounts = async () => {
if (dictUserAccounts.value.length == 0) {
try {
const res = await userAccountService().retrieve();
console.log('got accounts' + res);
dictUserAccounts.value = res.data;
} catch (error) {
alertService.showHttpError(error.response);
}
}
};
retrieveUserAccounts();
if (route.params?.transactionId) {
console.log('a1');
retrieveTransaction(route.params.transactionId, false);
}
if (route.params?.opposingTransactionId) {
console.log('a2');
console.log('getting opposite transaction for ' + route.params?.opposingTransactionId);
retrieveTransaction(route.params.opposingTransactionId, true);
}
if (route.params?.paymentsForTransactionId) {
console.log('a3');
retrieveTransactionForPayments(route.params.paymentsForTransactionId);
}
if (route.params?.paymentForFieldByEventId) {
console.log('a4');
retrieveTransactionForFieldPayment(route.params.paymentForFieldByEventId);
}
console.log('a5');
const initRelationships = () => {
eventService()
@@ -61,10 +141,36 @@ export default defineComponent({
date: {},
comment: {},
event: {},
// transactionItems: {
// $each: {
// amount: { required: validations.required, minValue: validations.minValue(0.01) },
// comment: {},
// locked: {},
// userAccount: {},
// },
// },
};
const v$ = useVuelidate(validationRules, transaction as any);
v$.value.$validate();
const addTransactionItem = () => {
transaction.value.transactionItems.push({
amount: 0,
comment: '',
locked: false,
userAccount: null,
});
};
const removeTransactionItem = (index: number) => {
transaction.value.transactionItems.splice(index, 1);
};
const calculateUnsettledBalance = (): string => {
const balance = transaction.value.transactionItems.reduce((sum, item) => sum + item.amount, 0);
return balance.toFixed(2);
};
return {
transactionService,
alertService,
@@ -75,9 +181,12 @@ export default defineComponent({
currentLanguage,
events,
v$,
dictUserAccounts,
addTransactionItem,
removeTransactionItem,
calculateUnsettledBalance,
};
},
created(): void {},
methods: {
save(): void {
this.isSaving = true;
@@ -1,6 +1,6 @@
<template>
<div class="row justify-content-center">
<div class="col-8">
<div class="col-12">
<form name="editForm" novalidate @submit.prevent="save()">
<h2 id="sasiedziApp.transaction.home.createOrEditLabel" data-cy="TransactionCreateUpdateHeading">Create or edit a Transaction</h2>
<div>
@@ -76,6 +76,55 @@
</option>
</select>
</div>
<div class="form-group">
<label>Destination account</label>
<select v-model="transaction.beneficiary" class="form-control" v-if="dictUserAccounts.length">
<option v-for="account in dictUserAccounts" :key="account.id" :value="account">{{ account.name }}</option>
</select>
<label>Unsettled Balance:</label>
<input type="text" class="form-control" :value="calculateUnsettledBalance()" readonly />
</div>
<div class="form-group">
<button type="button" @click="addTransactionItem" class="btn btn-success">Add Transaction Item</button>
<div class="table-responsive">
<table class="table table-striped" aria-describedby="event.registrations">
<thead>
<tr>
<th scope="col"><span></span></th>
<!-- <th scope="col"><span>Locked</span></th>-->
<th scope="col"><span>Account</span></th>
<th scope="col"><span>Amount</span></th>
<th scope="col"><span>Comment</span></th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<tr v-for="(item, index) in transaction.transactionItems" :key="index" class="transaction-item">
<td>
{{ item.id }}
</td>
<!-- <td>-->
<!-- <input type="checkbox" v-model="item.locked" class="form-check-input" />-->
<!-- </td>-->
<td>
<select v-model="item.userAccount" class="form-control" v-if="dictUserAccounts.length">
<option v-for="account in dictUserAccounts" :key="account.id" :value="account">{{ account.name }}</option>
</select>
</td>
<td>
<input type="number" v-model="item.amount" step="0.01" class="form-control" />
</td>
<td>
<input type="text" v-model="item.comment" class="form-control" placeholder="Comment" />
</td>
<td>
<button type="button" @click="removeTransactionItem(index)" class="btn btn-danger">Remove</button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div>
<button type="button" id="cancel-save" data-cy="entityCreateCancelButton" class="btn btn-secondary" @click="previousState()">
@@ -3,25 +3,39 @@ import { type Ref, defineComponent, inject, onMounted, ref } from 'vue';
import TransactionService from './transaction.service';
import { type ITransaction } from '@/shared/model/transaction.model';
import { useAlertService } from '@/shared/alert/alert.service';
import type AccountService from '@/account/account.service';
export default defineComponent({
compatConfig: { MODE: 3 },
name: 'Transaction',
setup() {
props: {
userAccountId: {
type: [Number, String],
required: false,
default: null,
},
},
setup(props) {
const transactionService = inject('transactionService', () => new TransactionService());
const alertService = inject('alertService', () => useAlertService(), true);
const transactions: Ref<ITransaction[]> = ref([]);
const hasAnyAuthorityValues: Ref<any> = ref({});
const accountService = inject<AccountService>('accountService');
const isFetching = ref(false);
const sums: Ref<number[]> = ref([]);
const clear = () => {};
// console.log(props.userAccountId);
const retrieveTransactions = async () => {
isFetching.value = true;
try {
// console.log(props);
const res = await transactionService().retrieve();
// const res = await transactionService().retrieveForAccount(props.userAccountId);
transactions.value = res.data;
sums.value = sumAmountsPerColumn(transactions.value);
} catch (err) {
alertService.showHttpError(err.response);
} finally {
@@ -59,17 +73,50 @@ export default defineComponent({
}
};
function sumAmountsPerColumn(transactions: ITransaction[]): number[] {
// Jeżeli nie ma transakcji, zwracamy pustą tablicę
if (transactions.length === 0) return [];
// Liczba kolumn to liczba transactionItems w pierwszej transakcji
const columnCount = transactions[0].items.length;
// Inicjalizacja tablicy sum z zerami dla każdej kolumny
const sums = new Array(columnCount).fill(0);
// Iteracja przez każdą transakcję
for (const transaction of transactions) {
for (let i = 0; i < columnCount; i++) {
// Dodajemy amount z każdego transactionItem do odpowiedniej kolumny
sums[i] += transaction.items[i].amount;
}
}
// Zaokrąglanie każdej sumy do dwóch miejsc po przecinku
return sums.map(sum => Number(sum.toFixed(2)));
}
return {
transactions,
handleSyncList,
isFetching,
retrieveTransactions,
clear,
sums,
removeId,
accountService,
hasAnyAuthorityValues,
removeEntity,
prepareRemove,
closeDialog,
removeTransaction,
};
},
methods: {
hasAnyAuthority(authorities: any): boolean {
this.accountService.hasAnyAuthorityAndCheckAuth(authorities).then(value => {
if (this.hasAnyAuthorityValues[authorities] !== value) {
this.hasAnyAuthorityValues = { ...this.hasAnyAuthorityValues, [authorities]: value };
}
});
return this.hasAnyAuthorityValues[authorities] ?? false;
},
},
});
@@ -5,10 +5,42 @@ import { type ITransaction } from '@/shared/model/transaction.model';
const baseApiUrl = 'api/transactions';
export default class TransactionService {
public find(id: number): Promise<ITransaction> {
public find(id: number, opposing: boolean): Promise<ITransaction> {
return new Promise<ITransaction>((resolve, reject) => {
let url = `${baseApiUrl}/${id}`;
if (opposing) {
url = `${baseApiUrl}/opposing/${id}`;
}
axios
.get(`${baseApiUrl}/${id}`)
.get(url)
.then(res => {
resolve(res.data);
})
.catch(err => {
reject(err);
});
});
}
public fieldPayment(eventId: number): Promise<ITransaction> {
return new Promise<ITransaction>((resolve, reject) => {
let url = `${baseApiUrl}/field-payment/${eventId}`;
axios
.get(url)
.then(res => {
resolve(res.data);
})
.catch(err => {
reject(err);
});
});
}
public payments(transactionId: number): Promise<ITransaction> {
return new Promise<ITransaction>((resolve, reject) => {
let url = `${baseApiUrl}/payments/${transactionId}`;
axios
.get(url)
.then(res => {
resolve(res.data);
})
@@ -30,6 +62,19 @@ export default class TransactionService {
});
});
}
//
// public retrieveForAccount(userAccountId: any): Promise<any> {
// return new Promise<any>((resolve, reject) => {
// axios
// .get(`${baseApiUrl}/forAccount/${userAccountId}`)
// .then(res => {
// resolve(res);
// })
// .catch(err => {
// reject(err);
// });
// });
// }
public delete(id: number): Promise<any> {
return new Promise<any>((resolve, reject) => {
@@ -6,7 +6,7 @@
<button class="btn btn-info mr-2" @click="handleSyncList" :disabled="isFetching">
<font-awesome-icon icon="sync" :spin="isFetching"></font-awesome-icon> <span>Refresh list</span>
</button>
<router-link :to="{ name: 'TransactionCreate' }" custom v-slot="{ navigate }">
<router-link :to="{ name: 'TransactionCreate' }" custom v-slot="{ navigate }" v-if="hasAnyAuthority(['ROLE_ADMIN'])">
<button
@click="navigate"
id="jh-create-entity"
@@ -27,14 +27,38 @@
<table class="table table-striped" aria-describedby="transactions">
<thead>
<tr>
<th scope="row"><span>ID</span></th>
<th scope="row"><span>Type</span></th>
<th scope="row"><span>Date</span></th>
<th scope="row"><span>Comment</span></th>
<!-- <th/>-->
<!-- <th/>-->
<!-- <th/>-->
<th />
<th />
<th scope="row"><span>Event</span></th>
<th scope="row" v-for="item in transactions[0].items">
<span>{{ item.userAccount?.name }}</span>
<th class="rotate" scope="row" v-for="item in transactions[0].items">
<div class="rotate">{{ item.userAccount?.name }}</div>
</th>
<th scope="row"></th>
</tr>
<tr>
<!-- <th/>-->
<!-- <th/>-->
<!-- <th/>-->
<th />
<th>Saldo</th>
<th v-for="amount in sums" class="border-left" :class="{ inplus: amount > 0, inminus: amount < 0 }">
<span class="bold">{{ amount }}</span>
</th>
</tr>
<tr>
<!-- <th scope="row"><span>ID</span></th>-->
<!-- <th scope="row"><span>Type</span></th>-->
<th scope="row"><span>Date</span></th>
<!-- <th scope="row"><span>Comment</span></th>-->
<th scope="row"><span></span></th>
<!-- <th scope="row"><span>Transactions:</span></th>-->
<th scope="row" v-for="item in transactions[0].items" class="border-left">
<span></span>
<!-- <span>{{ item.userAccount?.name }}</span>-->
</th>
<th scope="row"></th>
</tr>
@@ -42,11 +66,14 @@
<tbody>
<tr v-for="transaction in transactions" :key="transaction.id" data-cy="entityTable">
<td>
<router-link :to="{ name: 'TransactionView', params: { transactionId: transaction.id } }">{{ transaction.id }}</router-link>
<router-link :to="{ name: 'TransactionView', params: { transactionId: transaction.id } }"
>{{ transaction.date }} {{ transaction.type }}
</router-link>
<div v-if="transaction.comment">{{ transaction.comment }}</div>
</td>
<td>{{ transaction.type }}</td>
<td>{{ transaction.date }}</td>
<td>{{ transaction.comment }}</td>
<!-- <td>{{ transaction.type }}</td>-->
<!-- <td>{{ transaction.date }}</td>-->
<!-- <td>{{ transaction.comment }}</td>-->
<td>
<div v-if="transaction.event">
<router-link :to="{ name: 'EventView', params: { eventId: transaction.event.id } }">{{
@@ -54,7 +81,7 @@
}}</router-link>
</div>
</td>
<td v-for="item in transaction.items">{{ item.amount }}</td>
<td v-for="item in transaction.items" class="border-left">{{ item.amount }}</td>
<td class="text-right">
<div class="btn-group">
<router-link :to="{ name: 'TransactionView', params: { transactionId: transaction.id } }" custom v-slot="{ navigate }">
@@ -63,7 +90,12 @@
<span class="d-none d-md-inline">View</span>
</button>
</router-link>
<router-link :to="{ name: 'TransactionEdit', params: { transactionId: transaction.id } }" custom v-slot="{ navigate }">
<router-link
:to="{ name: 'TransactionEdit', params: { transactionId: transaction.id } }"
custom
v-slot="{ navigate }"
v-if="(transaction.editable && hasAnyAuthority(['ROLE_COUNTER'])) || hasAnyAuthority(['ROLE_ADMIN'])"
>
<button @click="navigate" class="btn btn-primary btn-sm edit" data-cy="entityEditButton">
<font-awesome-icon icon="pencil-alt"></font-awesome-icon>
<span class="d-none d-md-inline">Edit</span>
@@ -74,6 +106,7 @@
variant="danger"
class="btn btn-sm"
data-cy="entityDeleteButton"
v-if="hasAnyAuthority('ROLE_ADMIN')"
v-b-modal.removeEntity
>
<font-awesome-icon icon="times"></font-awesome-icon>
@@ -18,6 +18,19 @@ export default class UserAccountService {
});
}
public getCurrentUser(): Promise<IUserAccount> {
return new Promise<IUserAccount>((resolve, reject) => {
axios
.get(`${baseApiUrl}/currentUser`)
.then(res => {
resolve(res.data);
})
.catch(err => {
reject(err);
});
});
}
public retrieve(): Promise<any> {
return new Promise<any>((resolve, reject) => {
axios
+43 -13
View File
@@ -33,25 +33,25 @@ export default {
path: 'charge',
name: 'Charge',
component: Charge,
meta: { authorities: [Authority.USER] },
meta: { authorities: [Authority.ADMIN] },
},
{
path: 'charge/new',
name: 'ChargeCreate',
component: ChargeUpdate,
meta: { authorities: [Authority.USER] },
meta: { authorities: [Authority.ADMIN] },
},
{
path: 'charge/:chargeId/edit',
name: 'ChargeEdit',
component: ChargeUpdate,
meta: { authorities: [Authority.USER] },
meta: { authorities: [Authority.ADMIN] },
},
{
path: 'charge/:chargeId/view',
name: 'ChargeView',
component: ChargeDetails,
meta: { authorities: [Authority.USER] },
meta: { authorities: [Authority.ADMIN] },
},
{
path: 'event',
@@ -63,13 +63,13 @@ export default {
path: 'event/new',
name: 'EventCreate',
component: EventUpdate,
meta: { authorities: [Authority.USER] },
meta: { authorities: [Authority.ADMIN, Authority.COUNTER] },
},
{
path: 'event/:eventId/edit',
name: 'EventEdit',
component: EventUpdate,
meta: { authorities: [Authority.USER] },
meta: { authorities: [Authority.ADMIN, Authority.COUNTER] },
},
{
path: 'event/:eventId/view',
@@ -94,7 +94,7 @@ export default {
path: 'registration/:registrationId/edit',
name: 'RegistrationEdit',
component: RegistrationUpdate,
meta: { authorities: [Authority.USER] },
meta: { authorities: [Authority.ADMIN] },
},
{
path: 'registration/:registrationId/view',
@@ -108,17 +108,47 @@ export default {
component: Transaction,
meta: { authorities: [Authority.USER] },
},
{
path: 'my-transaction/:userAccountId?',
name: 'MyTransaction',
component: Transaction,
meta: { authorities: [Authority.USER] },
},
{
path: 'transaction/new',
name: 'TransactionCreate',
component: TransactionUpdate,
meta: { authorities: [Authority.USER] },
meta: { authorities: [Authority.ADMIN, Authority.COUNTER] },
},
{
path: 'transaction/opposing/:opposingTransactionId/new',
name: 'TransactionCreateOpposite',
component: TransactionUpdate,
meta: { authorities: [Authority.ADMIN, Authority.COUNTER] },
},
{
path: 'transaction/field-payment/:paymentForFieldByEventId/new',
name: 'TransactionFieldPayment',
component: TransactionUpdate,
meta: { authorities: [Authority.ADMIN, Authority.COUNTER] },
},
{
path: 'transaction/payments/:paymentsForTransactionId/new',
name: 'TransactionPaymentsForTransaction',
component: TransactionUpdate,
meta: { authorities: [Authority.ADMIN, Authority.COUNTER] },
},
{
path: 'transaction/opposing/:opposingTransactionId/new',
name: 'TransactionCreateOpposite',
component: TransactionUpdate,
meta: { authorities: [Authority.ADMIN, Authority.COUNTER] },
},
{
path: 'transaction/:transactionId/edit',
name: 'TransactionEdit',
component: TransactionUpdate,
meta: { authorities: [Authority.USER] },
meta: { authorities: [Authority.ADMIN, Authority.COUNTER] },
},
{
path: 'transaction/:transactionId/view',
@@ -130,25 +160,25 @@ export default {
path: 'user-account',
name: 'UserAccount',
component: UserAccount,
meta: { authorities: [Authority.USER] },
meta: { authorities: [Authority.ADMIN] },
},
{
path: 'user-account/new',
name: 'UserAccountCreate',
component: UserAccountUpdate,
meta: { authorities: [Authority.USER] },
meta: { authorities: [Authority.ADMIN] },
},
{
path: 'user-account/:userAccountId/edit',
name: 'UserAccountEdit',
component: UserAccountUpdate,
meta: { authorities: [Authority.USER] },
meta: { authorities: [Authority.ADMIN] },
},
{
path: 'user-account/:userAccountId/view',
name: 'UserAccountView',
component: UserAccountDetails,
meta: { authorities: [Authority.USER] },
meta: { authorities: [Authority.ADMIN] },
},
// jhipster-needle-add-entity-to-router - JHipster will add entities to the router here
],
+11 -4
View File
@@ -5,6 +5,10 @@ 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');
export const createRouter = () =>
createVueRouter({
@@ -12,7 +16,9 @@ export const createRouter = () =>
routes: [
{
path: '/',
redirect: '/event/1551/view',
name: 'CurrentEventView',
component: EventDetails,
meta: { authorities: [Authority.USER], eventId: 'useCurrentEventId' },
},
{
path: '/forbidden',
@@ -22,9 +28,10 @@ export const createRouter = () =>
},
{
path: '/not-found',
name: 'NotFound',
component: Error,
meta: { error404: true },
redirect: '/',
// name: 'NotFound',
// component: Error,
// meta: { error404: true },
},
...admin,
entities,
@@ -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;
},
},
});
@@ -1,4 +1,5 @@
import type { IRegistration } from '@/shared/model/registration.model';
import type { ITransaction } from '@/shared/model/transaction.model';
export interface IEvent {
id?: number;
@@ -8,6 +9,14 @@ export interface IEvent {
cost?: number | null;
comment?: string | null;
registrations?: IRegistration[];
transactions?: ITransaction[];
editable?: boolean | false;
deletable?: boolean | false;
charged?: boolean | false;
paid?: boolean | false;
active?: boolean | false;
current?: boolean | false;
chargeTransactionId?: number | null;
}
export class Event implements IEvent {
@@ -19,5 +28,13 @@ export class Event implements IEvent {
public cost?: number | null,
public comment?: string | null,
public registrations?: IRegistration[],
public transactions?: ITransaction[],
public editable?: boolean | false,
public deletable?: boolean | false,
public charged?: boolean | false,
public paid?: boolean | false,
public active?: boolean | false,
public current?: boolean | false,
public chargeTransactionId?: number | null,
) {}
}
@@ -8,7 +8,11 @@ export interface ITransaction {
date?: Date | null;
comment?: string | null;
event?: IEvent | null;
transactionItems?: ITransactionItem[] | [];
items?: ITransactionItem[] | [];
beneficiary?: IUserAccount | null;
editable?: boolean | false;
deletable?: boolean | false;
}
export class Transaction implements ITransaction {
@@ -19,6 +23,9 @@ export class Transaction implements ITransaction {
public comment?: string | null,
public event?: IEvent | null,
public items?: ITransactionItem[] | [],
public beneficiary?: IUserAccount | null,
public editable?: boolean | false,
public deletable?: boolean | false,
) {}
}
@@ -4,6 +4,7 @@ export interface IUserAccount {
id?: number;
name?: string | null;
users?: IUser[] | null;
balance?: number | 0;
}
export class UserAccount implements IUserAccount {
@@ -11,5 +12,6 @@ export class UserAccount implements IUserAccount {
public id?: number,
public name?: string | null,
public users?: IUser[] | null,
public balance?: number | 0,
) {}
}
@@ -1,4 +1,5 @@
export enum Authority {
ADMIN = 'ROLE_ADMIN',
USER = 'ROLE_USER',
COUNTER = 'ROLE_COUNTER',
}
+31
View File
@@ -150,3 +150,34 @@
-webkit-animation-delay: 0s;
animation-delay: 0s;
}
th.rotate {
/*height:10px;*/
height: 180px;
/*width:50px;*/
white-space: nowrap;
position: relative;
border: 1px;
}
th.rotate > div {
transform: rotate(290deg);
position: absolute;
left: 0;
right: 0;
top: 126px;
margin: auto;
}
.strikethrough {
text-decoration: line-through;
color: lightgray;
}
.inplus {
background-color: lightgreen;
}
.inminus {
background-color: red;
}
@@ -65,6 +65,7 @@ public class RegistrationAsserts {
public static void assertRegistrationUpdatableRelationshipsEquals(Registration expected, Registration actual) {
assertThat(expected)
.as("Verify Registration relationships")
.satisfies(e -> assertThat(e.getEvent()).as("check event").isEqualTo(actual.getEvent()));
.satisfies(e -> assertThat(e.getEvent()).as("check event").isEqualTo(actual.getEvent()))
.satisfies(e -> assertThat(e.getUserAccount()).as("check userAccount").isEqualTo(actual.getUserAccount()));
}
}
@@ -2,9 +2,13 @@ package com.sasiedzi.event.domain;
import static com.sasiedzi.event.domain.EventTestSamples.*;
import static com.sasiedzi.event.domain.RegistrationTestSamples.*;
import static com.sasiedzi.event.domain.TransactionItemTestSamples.*;
import static com.sasiedzi.event.domain.UserAccountTestSamples.*;
import static org.assertj.core.api.Assertions.assertThat;
import com.sasiedzi.event.web.rest.TestUtil;
import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.api.Test;
class RegistrationTest {
@@ -34,4 +38,38 @@ class RegistrationTest {
registration.event(null);
assertThat(registration.getEvent()).isNull();
}
@Test
void userAccountTest() {
Registration registration = getRegistrationRandomSampleGenerator();
UserAccount userAccountBack = getUserAccountRandomSampleGenerator();
registration.setUserAccount(userAccountBack);
assertThat(registration.getUserAccount()).isEqualTo(userAccountBack);
registration.userAccount(null);
assertThat(registration.getUserAccount()).isNull();
}
@Test
void transactionItemTest() {
Registration registration = getRegistrationRandomSampleGenerator();
TransactionItem transactionItemBack = getTransactionItemRandomSampleGenerator();
registration.addTransactionItem(transactionItemBack);
assertThat(registration.getTransactionItems()).containsOnly(transactionItemBack);
assertThat(transactionItemBack.getRegistration()).isEqualTo(registration);
registration.removeTransactionItem(transactionItemBack);
assertThat(registration.getTransactionItems()).doesNotContain(transactionItemBack);
assertThat(transactionItemBack.getRegistration()).isNull();
registration.transactionItems(new HashSet<>(Set.of(transactionItemBack)));
assertThat(registration.getTransactionItems()).containsOnly(transactionItemBack);
assertThat(transactionItemBack.getRegistration()).isEqualTo(registration);
registration.setTransactionItems(new HashSet<>());
assertThat(registration.getTransactionItems()).doesNotContain(transactionItemBack);
assertThat(transactionItemBack.getRegistration()).isNull();
}
}
@@ -49,7 +49,8 @@ public class TransactionItemAsserts {
assertThat(expected)
.as("Verify TransactionItem relevant properties")
.satisfies(e -> assertThat(e.getAmount()).as("check amount").usingComparator(bigDecimalCompareTo).isEqualTo(actual.getAmount()))
.satisfies(e -> assertThat(e.getComment()).as("check comment").isEqualTo(actual.getComment()));
.satisfies(e -> assertThat(e.getComment()).as("check comment").isEqualTo(actual.getComment()))
.satisfies(e -> assertThat(e.getLocked()).as("check locked").isEqualTo(actual.getLocked()));
}
/**
@@ -63,6 +64,7 @@ public class TransactionItemAsserts {
.as("Verify TransactionItem relationships")
.satisfies(e -> assertThat(e.getUserAccount()).as("check userAccount").isEqualTo(actual.getUserAccount()))
.satisfies(e -> assertThat(e.getTransaction()).as("check transaction").isEqualTo(actual.getTransaction()))
.satisfies(e -> assertThat(e.getEvent()).as("check event").isEqualTo(actual.getEvent()));
.satisfies(e -> assertThat(e.getEvent()).as("check event").isEqualTo(actual.getEvent()))
.satisfies(e -> assertThat(e.getRegistration()).as("check registration").isEqualTo(actual.getRegistration()));
}
}
@@ -1,6 +1,7 @@
package com.sasiedzi.event.domain;
import static com.sasiedzi.event.domain.EventTestSamples.*;
import static com.sasiedzi.event.domain.RegistrationTestSamples.*;
import static com.sasiedzi.event.domain.TransactionItemTestSamples.*;
import static com.sasiedzi.event.domain.TransactionTestSamples.*;
import static com.sasiedzi.event.domain.UserAccountTestSamples.*;
@@ -60,4 +61,16 @@ class TransactionItemTest {
transactionItem.event(null);
assertThat(transactionItem.getEvent()).isNull();
}
@Test
void registrationTest() {
TransactionItem transactionItem = getTransactionItemRandomSampleGenerator();
Registration registrationBack = getRegistrationRandomSampleGenerator();
transactionItem.setRegistration(registrationBack);
assertThat(transactionItem.getRegistration()).isEqualTo(registrationBack);
transactionItem.registration(null);
assertThat(transactionItem.getRegistration()).isNull();
}
}
@@ -51,11 +51,11 @@ class TransactionTest {
assertThat(transaction.getTransactionItems()).doesNotContain(transactionItemBack);
assertThat(transactionItemBack.getTransaction()).isNull();
transaction.transactionItems(new HashSet<>(Set.of(transactionItemBack)));
transaction.transactionItems(Transaction.getEmptyTransactionItemSet());
assertThat(transaction.getTransactionItems()).containsOnly(transactionItemBack);
assertThat(transactionItemBack.getTransaction()).isEqualTo(transaction);
transaction.setTransactionItems(new HashSet<>());
transaction.setTransactionItems(Transaction.getEmptyTransactionItemSet());
assertThat(transaction.getTransactionItems()).doesNotContain(transactionItemBack);
assertThat(transactionItemBack.getTransaction()).isNull();
}
@@ -49,6 +49,9 @@ class TransactionItemResourceIT {
private static final String DEFAULT_COMMENT = "AAAAAAAAAA";
private static final String UPDATED_COMMENT = "BBBBBBBBBB";
private static final Boolean DEFAULT_LOCKED = false;
private static final Boolean UPDATED_LOCKED = true;
private static final String ENTITY_API_URL = "/api/transaction-items";
private static final String ENTITY_API_URL_ID = ENTITY_API_URL + "/{id}";
@@ -81,7 +84,7 @@ class TransactionItemResourceIT {
* if they test an entity which requires the current entity.
*/
public static TransactionItem createEntity() {
return new TransactionItem().amount(DEFAULT_AMOUNT).comment(DEFAULT_COMMENT);
return new TransactionItem().amount(DEFAULT_AMOUNT).comment(DEFAULT_COMMENT).locked(DEFAULT_LOCKED);
}
/**
@@ -91,7 +94,7 @@ class TransactionItemResourceIT {
* if they test an entity which requires the current entity.
*/
public static TransactionItem createUpdatedEntity() {
return new TransactionItem().amount(UPDATED_AMOUNT).comment(UPDATED_COMMENT);
return new TransactionItem().amount(UPDATED_AMOUNT).comment(UPDATED_COMMENT).locked(UPDATED_LOCKED);
}
@BeforeEach
@@ -163,7 +166,8 @@ class TransactionItemResourceIT {
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$.[*].id").value(hasItem(transactionItem.getId().intValue())))
.andExpect(jsonPath("$.[*].amount").value(hasItem(sameNumber(DEFAULT_AMOUNT))))
.andExpect(jsonPath("$.[*].comment").value(hasItem(DEFAULT_COMMENT)));
.andExpect(jsonPath("$.[*].comment").value(hasItem(DEFAULT_COMMENT)))
.andExpect(jsonPath("$.[*].locked").value(hasItem(DEFAULT_LOCKED.booleanValue())));
}
@SuppressWarnings({ "unchecked" })
@@ -196,7 +200,8 @@ class TransactionItemResourceIT {
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$.id").value(transactionItem.getId().intValue()))
.andExpect(jsonPath("$.amount").value(sameNumber(DEFAULT_AMOUNT)))
.andExpect(jsonPath("$.comment").value(DEFAULT_COMMENT));
.andExpect(jsonPath("$.comment").value(DEFAULT_COMMENT))
.andExpect(jsonPath("$.locked").value(DEFAULT_LOCKED.booleanValue()));
}
@Test
@@ -218,7 +223,7 @@ class TransactionItemResourceIT {
TransactionItem updatedTransactionItem = transactionItemRepository.findById(transactionItem.getId()).orElseThrow();
// Disconnect from session so that the updates on updatedTransactionItem are not directly saved in db
em.detach(updatedTransactionItem);
updatedTransactionItem.amount(UPDATED_AMOUNT).comment(UPDATED_COMMENT);
updatedTransactionItem.amount(UPDATED_AMOUNT).comment(UPDATED_COMMENT).locked(UPDATED_LOCKED);
restTransactionItemMockMvc
.perform(
@@ -303,7 +308,7 @@ class TransactionItemResourceIT {
TransactionItem partialUpdatedTransactionItem = new TransactionItem();
partialUpdatedTransactionItem.setId(transactionItem.getId());
partialUpdatedTransactionItem.comment(UPDATED_COMMENT);
partialUpdatedTransactionItem.amount(UPDATED_AMOUNT).comment(UPDATED_COMMENT);
restTransactionItemMockMvc
.perform(
@@ -335,7 +340,7 @@ class TransactionItemResourceIT {
TransactionItem partialUpdatedTransactionItem = new TransactionItem();
partialUpdatedTransactionItem.setId(transactionItem.getId());
partialUpdatedTransactionItem.amount(UPDATED_AMOUNT).comment(UPDATED_COMMENT);
partialUpdatedTransactionItem.amount(UPDATED_AMOUNT).comment(UPDATED_COMMENT).locked(UPDATED_LOCKED);
restTransactionItemMockMvc
.perform(