Skip to content
Snippets Groups Projects
Commit 643a2483 authored by Jens Christian Aanestad's avatar Jens Christian Aanestad
Browse files

Merge branch 'fix/accounts-can-belong-to-many-users' into 'master'

Fix/accounts can belong to many users

See merge request !90
parents 759b5f70 8aec9386
No related branches found
No related tags found
1 merge request!90Fix/accounts can belong to many users
Pipeline #284711 passed
Showing
with 24 additions and 90 deletions
......@@ -8,10 +8,8 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Email;
import lombok.extern.slf4j.Slf4j;
import no.ntnu.idi.stud.savingsapp.bank.model.Account;
import no.ntnu.idi.stud.savingsapp.dto.user.*;
import no.ntnu.idi.stud.savingsapp.exception.user.PermissionDeniedException;
import no.ntnu.idi.stud.savingsapp.model.BankAccountType;
import no.ntnu.idi.stud.savingsapp.model.configuration.ChallengeType;
import no.ntnu.idi.stud.savingsapp.model.configuration.Commitment;
import no.ntnu.idi.stud.savingsapp.model.configuration.Experience;
......@@ -129,6 +127,12 @@ public class UserController {
if(updateDTO.getBannerImage() != null) {
user.setBannerImage(updateDTO.getBannerImage());
}
if(updateDTO.getCheckingAccountBBAN() != null) {
user.setCheckingAccountBBAN(updateDTO.getCheckingAccountBBAN());
}
if (updateDTO.getSavingsAccountBBAN() != null) {
user.setSavingsAccountBBAN(updateDTO.getSavingsAccountBBAN());
}
if (updateDTO.getConfiguration() != null) {
if (updateDTO.getConfiguration().getCommitment() != null) {
user.getConfiguration().setCommitment(Commitment.valueOf(updateDTO.getConfiguration().getCommitment()));
......@@ -203,24 +207,6 @@ public class UserController {
log.info("[UserController:confirmPasswordReset] initiated password reset, token: {}", resetDTO.getToken());
}
@Operation(summary = "Update a user's bank account", description = "Changes either a user's "
+ "checking account or savings account")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "")
})
@PatchMapping(value = "/update-account")
public Account selectBankAccount(
@AuthenticationPrincipal AuthIdentity identity,
@RequestBody @Valid BankAccountDTO bankAccountDTO) {
BankAccountType accountType = modelMapper.map(bankAccountDTO.getBankAccountType(),
BankAccountType.class);
log.info("[UserController:selectBankAccount], bankAccountBban: {}", bankAccountDTO.getBban());
return userService.selectBankAccount(
accountType,
bankAccountDTO.getBban(),
identity.getId());
}
@Operation(summary = "Search for users by name and filter", description = "Returns a list of users whose names contain the specified search term and match the filter.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "Successfully retrieved list of users")
......
......@@ -26,6 +26,10 @@ public final class SignUpRequest {
@Password
private String password;
private Long checkingAccountBBAN;
private Long savingsAccountBBAN;
@Valid
@NotNull(message = "Configuration is required")
private ConfigurationDTO configuration;
......
package no.ntnu.idi.stud.savingsapp.dto.user;
import lombok.Data;
import no.ntnu.idi.stud.savingsapp.model.BankAccountType;
import no.ntnu.idi.stud.savingsapp.validation.Enumerator;
@Data
public class BankAccountDTO {
private Long bban;
@Enumerator(value = BankAccountType.class)
private String bankAccountType;
}
......@@ -16,8 +16,8 @@ public class UserDTO {
private Timestamp createdAt;
private String role;
private String subscriptionLevel;
private BankAccountResponseDTO checkingAccount;
private BankAccountResponseDTO savingsAccount;
private Long checkingAccountBBAN;
private Long savingsAccountBBAN;
private PointDTO point;
private StreakDTO streak;
}
......@@ -22,6 +22,10 @@ public final class UserUpdateDTO {
private Long bannerImage;
private Long savingsAccountBBAN;
private Long checkingAccountBBAN;
@Valid
private ConfigurationDTO configuration;
}
package no.ntnu.idi.stud.savingsapp.model;
/**
* Represents a type of account that a user can have.
*/
public enum BankAccountType {
SAVING_ACCOUNT,
CHECKING_ACCOUNT
}
......@@ -19,7 +19,6 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import no.ntnu.idi.stud.savingsapp.bank.model.Account;
import no.ntnu.idi.stud.savingsapp.model.configuration.Configuration;
import no.ntnu.idi.stud.savingsapp.model.store.Item;
......@@ -66,13 +65,11 @@ public class User implements UserDetails{
@Column(name = "banner_image")
private Long bannerImage;
@OneToOne
@JoinColumn(name = "checking_account_id")
private Account checkingAccount;
@Column(name = "checking_account_bban")
private Long checkingAccountBBAN;
@OneToOne
@JoinColumn(name = "savings_account_id")
private Account savingsAccount;
@Column(name = "savings_account_bban")
private Long savingsAccountBBAN;
@NonNull
@Column(name = "password")
......
......@@ -2,7 +2,6 @@ package no.ntnu.idi.stud.savingsapp.repository;
import java.util.List;
import java.util.Optional;
import no.ntnu.idi.stud.savingsapp.bank.model.Account;
import no.ntnu.idi.stud.savingsapp.model.user.SubscriptionLevel;
import no.ntnu.idi.stud.savingsapp.model.user.User;
import org.springframework.data.jpa.repository.JpaRepository;
......@@ -161,7 +160,7 @@ public interface UserRepository extends JpaRepository<User, Long> {
"WHERE user_id = :userId ) " +
// Get user attributes from ranked_users
"SELECT ru.user_id, ru.created_at, ru.email, ru.first_name, ru.last_name, ru.password, ru.role,"
+ " ru.point_id, ru.streak_id, ru.checking_account_id, ru.savings_account_id, ru"
+ " ru.point_id, ru.streak_id, ru.checking_account_bban, ru.savings_account_bban, ru"
+ ".configuration_id, ru.profile_image, ru.banner_image, ru.subscription_level, ru"
+ ".bankid_sub " +
"FROM ranked_users ru, user_rank ur " +
......@@ -184,7 +183,7 @@ public interface UserRepository extends JpaRepository<User, Long> {
"WHERE user_id = :userId ) " +
// Get user attributes from ranked_users
"SELECT ru.user_id, ru.created_at, ru.email, ru.first_name, ru.last_name, ru.password, ru.role,"
+ " ru.point_id, ru.streak_id, ru.checking_account_id, ru.savings_account_id, ru"
+ " ru.point_id, ru.streak_id, ru.checking_account_bban, ru.savings_account_bban, ru"
+ ".configuration_id, ru.profile_image, ru.banner_image, ru.subscription_level, ru.bankid_sub"
+ " " +
"FROM ranked_users ru, user_rank ur " +
......@@ -207,7 +206,7 @@ public interface UserRepository extends JpaRepository<User, Long> {
"WHERE user_id = :userId ) " +
// Get user attributes from ranked_users
"SELECT ru.user_id, ru.created_at, ru.email, ru.first_name, ru.last_name, ru.password, ru.role,"
+ " ru.point_id, ru.streak_id, ru.checking_account_id, ru.savings_account_id, ru"
+ " ru.point_id, ru.streak_id, ru.checking_account_bban, ru.savings_account_bban, ru"
+ ".configuration_id, ru.profile_image, ru.banner_image, ru.subscription_level, ru.bankid_sub"
+ " " +
"FROM ranked_users ru, user_rank ur " +
......@@ -264,8 +263,6 @@ public interface UserRepository extends JpaRepository<User, Long> {
nativeQuery = true)
long findUserRankByHighestEverStreak(@Param("userId") Long userId);
@Query("UPDATE User u SET u.savingsAccount = :account WHERE u = :user")
void updateSavingsAccount(@Param("user") User user,@Param("account") Account account);
@Query(value = "SELECT * FROM user u WHERE CONCAT(u.first_name, ' ', u.last_name) LIKE %:searchTerm%", nativeQuery = true)
List<User> findUsersByName(@Param("searchTerm") String searchTerm);
......
package no.ntnu.idi.stud.savingsapp.service;
import no.ntnu.idi.stud.savingsapp.bank.model.Account;
import no.ntnu.idi.stud.savingsapp.model.BankAccountType;
import no.ntnu.idi.stud.savingsapp.model.user.Feedback;
import no.ntnu.idi.stud.savingsapp.model.user.SearchFilter;
import no.ntnu.idi.stud.savingsapp.model.user.SubscriptionLevel;
......@@ -97,15 +96,6 @@ public interface UserService {
*/
void confirmPasswordReset(String token, String password);
/**
* Select which bank account the user has selected as either savings- or checking account.
*
* @param bankAccountType The type of account, can either be a savings account or a checking
* account.
* @param bban The Basic Bank Account Number, specifying the account.
*/
Account selectBankAccount(BankAccountType bankAccountType, Long bban, Long userId);
/**
* Retrieves a list of {@link User} objects representing the friends of the specified user.
*
......
......@@ -4,14 +4,12 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.mail.MessagingException;
import lombok.extern.slf4j.Slf4j;
import no.ntnu.idi.stud.savingsapp.SparestiApplication;
import no.ntnu.idi.stud.savingsapp.bank.model.Account;
import no.ntnu.idi.stud.savingsapp.bank.service.AccountService;
import no.ntnu.idi.stud.savingsapp.exception.auth.InvalidCredentialsException;
import no.ntnu.idi.stud.savingsapp.exception.user.EmailAlreadyExistsException;
import no.ntnu.idi.stud.savingsapp.exception.user.InvalidPasswordResetTokenException;
import no.ntnu.idi.stud.savingsapp.exception.user.UserException;
import no.ntnu.idi.stud.savingsapp.exception.user.UserNotFoundException;
import no.ntnu.idi.stud.savingsapp.model.BankAccountType;
import no.ntnu.idi.stud.savingsapp.model.user.Feedback;
import no.ntnu.idi.stud.savingsapp.model.user.Friend;
import no.ntnu.idi.stud.savingsapp.model.user.PasswordResetToken;
......@@ -39,7 +37,6 @@ import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
......@@ -53,7 +50,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.springframework.web.server.ResponseStatusException;
import java.util.Collections;
/**
......@@ -346,22 +342,6 @@ public class UserServiceImpl implements UserService {
}
}
@Override
public Account selectBankAccount(BankAccountType bankAccountType, Long bban, Long userId) {
User user = findById(userId);
Account account = accountService.getAccountByBban(bban);
if (bankAccountType == BankAccountType.SAVING_ACCOUNT) {
user.setSavingsAccount(account);
}
else if (bankAccountType == BankAccountType.CHECKING_ACCOUNT){
user.setCheckingAccount(account);
}else {
log.error("[UserServiceImpl:selectBankAccount] account type is not supported: {}", bankAccountType);
throw new ResponseStatusException(HttpStatusCode.valueOf(400), "Account type not supported");
}
update(user);
return account;
}
/**
* Retrieves a list of {@link User} objects representing the friends of the specified user.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment