a
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -39,4 +39,7 @@ public interface TransactionItemRepository extends JpaRepository<TransactionItem
|
||||
"select transactionItem from TransactionItem transactionItem left join fetch transactionItem.event 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);
|
||||
}
|
||||
|
||||
@@ -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,8 @@ 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();
|
||||
}
|
||||
|
||||
@@ -2,10 +2,8 @@ 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.web.rest.AccountResource;
|
||||
import jakarta.validation.Valid;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
@@ -16,6 +14,8 @@ import java.util.stream.Collectors;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.AuthenticatedPrincipal;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@@ -30,6 +30,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;
|
||||
}
|
||||
@@ -186,10 +195,7 @@ public class EventService {
|
||||
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);
|
||||
@@ -227,4 +233,72 @@ public class EventService {
|
||||
|
||||
return eventRepository.findById(event.getId());
|
||||
}
|
||||
|
||||
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 ((AuthenticatedPrincipal) principal).getName();
|
||||
// return userService.getUserFromAuthentication((AbstractAuthenticationToken) principal);
|
||||
} else {
|
||||
throw new RuntimeException("User could not be found");
|
||||
}
|
||||
}
|
||||
|
||||
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 getOrCreateForCurrentUser() {
|
||||
// if (true) return new UserAccount();
|
||||
String username = getCurrentAuthenticatedUserAsDTO();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
TransactionItemRepository transactionItemRepository;
|
||||
}
|
||||
|
||||
@@ -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,7 +1,10 @@
|
||||
package com.sasiedzi.event.web.rest;
|
||||
|
||||
import com.sasiedzi.event.domain.User;
|
||||
import com.sasiedzi.event.domain.UserAccount;
|
||||
import com.sasiedzi.event.repository.UserAccountRepository;
|
||||
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 +13,11 @@ 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.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;
|
||||
@@ -166,6 +172,12 @@ public class UserAccountResource {
|
||||
return ResponseUtil.wrapOrNotFound(userAccount);
|
||||
}
|
||||
|
||||
@GetMapping("/currentUser")
|
||||
public UserAccount getCurrentUserAccount() {
|
||||
UserAccount userAccount = eventService.getOrCreateForCurrentUser();
|
||||
return userAccount;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code DELETE /user-accounts/:id} : delete the "id" userAccount.
|
||||
*
|
||||
@@ -180,4 +192,7 @@ public class UserAccountResource {
|
||||
.headers(HeaderUtil.createEntityDeletionAlert(applicationName, false, ENTITY_NAME, id.toString()))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
EventService eventService;
|
||||
}
|
||||
|
||||
@@ -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">
|
||||
|
||||
@@ -7,6 +7,13 @@ import { useAlertService } from '@/shared/alert/alert.service';
|
||||
export default defineComponent({
|
||||
compatConfig: { MODE: 3 },
|
||||
name: 'Transaction',
|
||||
props: {
|
||||
userAccountId: {
|
||||
type: [Number, String],
|
||||
required: false,
|
||||
default: null,
|
||||
},
|
||||
},
|
||||
setup() {
|
||||
const transactionService = inject('transactionService', () => new TransactionService());
|
||||
const alertService = inject('alertService', () => useAlertService(), true);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -108,6 +108,12 @@ 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',
|
||||
|
||||
@@ -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,
|
||||
) {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user