Skip to content

Commit

Permalink
handle duplicate approved account request
Browse files Browse the repository at this point in the history
  • Loading branch information
domoberzin committed Apr 12, 2024
1 parent ce75a0a commit 88ba6ee
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ protected void tearDown() {
HibernateUtil.beginTransaction();
List<AccountRequest> accountRequests = logic.getAllAccountRequests();
for (AccountRequest ar : accountRequests) {
logic.deleteAccountRequest(ar.getEmail(), ar.getInstitute());
logic.deleteAccountRequest(ar.getId());
}
HibernateUtil.commitTransaction();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,20 @@ public void testExecute() throws Exception {
assertEquals(email, data.getEmail());
assertEquals(institute, data.getInstitute());
assertEquals(null, data.getComments());

______TS("email with approved account request throws exception");
logic.createAccountRequestWithTransaction("test", "[email protected]",
"institute", AccountRequestStatus.APPROVED, "comments");
accountRequest = logic.createAccountRequestWithTransaction("test", "[email protected]",
"institute", AccountRequestStatus.PENDING, "comments");
requestBody = new AccountRequestUpdateRequest(accountRequest.getName(), accountRequest.getEmail(),
accountRequest.getInstitute(), AccountRequestStatus.APPROVED, comments);
params = new String[] {Const.ParamsNames.ACCOUNT_REQUEST_ID, accountRequest.getId().toString()};

ipe = verifyInvalidOperation(requestBody, params);

assertEquals(String.format("An account request with email %s has already been approved. "
+ "Please reject or delete the account request instead.", accountRequest.getEmail()), ipe.getMessage());
}

@Override
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/teammates/sqllogic/api/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ public List<AccountRequest> getAllAccountRequests() {
return accountRequestLogic.getAllAccountRequests();
}

/**
* Get a list of account requests associated with email provided.
*/
public List<AccountRequest> getApprovedAccountRequestsForEmailWithTransaction(String email) {
return accountRequestLogic.getApprovedAccountRequestsForEmailWithTransaction(email);
}

/**
* Gets an account.
*/
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/teammates/sqllogic/core/AccountRequestsLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ public List<AccountRequest> getAllAccountRequests() {
return accountRequestDb.getAllAccountRequests();
}

/**
* Get a list of account requests associated with email provided.
*/
public List<AccountRequest> getApprovedAccountRequestsForEmailWithTransaction(String email) {
HibernateUtil.beginTransaction();
List<AccountRequest> accountRequests = accountRequestDb.getApprovedAccountRequestsForEmail(email);
HibernateUtil.commitTransaction();
return accountRequests;
}

/**
* Creates/resets the account request with the given id such that it is not registered.
*/
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/teammates/storage/sqlapi/AccountRequestsDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ public List<AccountRequest> getAllAccountRequests() {
return query.getResultList();
}

/**
* Get all Account Requests for a given {@code email}.
*/
public List<AccountRequest> getApprovedAccountRequestsForEmail(String email) {
CriteriaBuilder cb = HibernateUtil.getCriteriaBuilder();
CriteriaQuery<AccountRequest> cr = cb.createQuery(AccountRequest.class);
Root<AccountRequest> root = cr.from(AccountRequest.class);
cr.select(root).where(cb.and(cb.equal(root.get("email"), email),
cb.equal(root.get("status"), AccountRequestStatus.APPROVED)));

TypedQuery<AccountRequest> query = HibernateUtil.createQuery(cr);
return query.getResultList();
}

/**
* Get AccountRequest by {@code registrationKey} from database.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public JsonResult execute() throws InvalidOperationException, InvalidHttpRequest
accountRequest.getEmail()));
}

if (sqlLogic.getApprovedAccountRequestsForEmailWithTransaction(accountRequest.getEmail()).size() > 0) {
throw new InvalidOperationException(String.format(
"An account request with email %s has already been approved. "
+ "Please reject or delete the account request instead.",
accountRequest.getEmail()));
}

try {
// should not need to update other fields for an approval
accountRequest.setStatus(accountRequestUpdateRequest.getStatus());
Expand Down

0 comments on commit 88ba6ee

Please sign in to comment.