Skip to content

Commit

Permalink
Create account request rejection endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
xenosf committed Apr 5, 2024
1 parent 5779d2f commit bd08202
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/main/java/teammates/common/util/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public final class Const {

public static final String MISSING_RESPONSE_TEXT = "No Response";

public static final String ACCOUNT_REQUEST_NOT_FOUND = "Account request with id = %s not found";

// These constants are used as variable values to mean that the variable is in a 'special' state.

public static final int INT_UNINITIALIZED = -9999;
Expand Down Expand Up @@ -337,6 +339,7 @@ public static class ResourceURIs {
public static final String ACCOUNT_REQUEST = URI_PREFIX + "/account/request";
public static final String ACCOUNT_REQUESTS = URI_PREFIX + "/account/requests";
public static final String ACCOUNT_REQUEST_RESET = ACCOUNT_REQUEST + "/reset";
public static final String ACCOUNT_REQUEST_REJECTION = ACCOUNT_REQUEST + "/rejection";
public static final String ACCOUNTS = URI_PREFIX + "/accounts";
public static final String RESPONSE_COMMENT = URI_PREFIX + "/responsecomment";
public static final String COURSE = URI_PREFIX + "/course";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public enum ResourceEndpoints {
ACCOUNT_REQUEST(ResourceURIs.ACCOUNT_REQUEST),
ACCOUNT_REQUESTS(ResourceURIs.ACCOUNT_REQUESTS),
ACCOUNT_REQUEST_RESET(ResourceURIs.ACCOUNT_REQUEST_RESET),
ACCOUNT_REQUEST_REJECT(ResourceURIs.ACCOUNT_REQUEST_REJECTION),
ACCOUNTS(ResourceURIs.ACCOUNTS),
RESPONSE_COMMENT(ResourceURIs.RESPONSE_COMMENT),
COURSE(ResourceURIs.COURSE),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package teammates.ui.request;

import javax.annotation.Nullable;

import teammates.common.datatransfer.AccountRequestStatus;
import teammates.common.util.SanitizationHelper;

/**
* The request reasonBody for rejecting an account request.
*/
public class AccountRequestRejectionRequest extends BasicRequest {
@Nullable
private String reasonTitle;

@Nullable
private String reasonBody;

public AccountRequestRejectionRequest(String reasonTitle, String reasonBody) {
this.reasonTitle = SanitizationHelper.sanitizeTitle(reasonTitle);
this.reasonBody = SanitizationHelper.sanitizeForRichText(reasonBody);
}

@Override
public void validate() throws InvalidHttpRequestBodyException {
// No validation
}

public String getReasonTitle() {
return this.reasonTitle;
}

public String getReasonBody() {
return this.reasonBody;
}

}
1 change: 1 addition & 0 deletions src/main/java/teammates/ui/webapi/ActionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public final class ActionFactory {
map(ResourceURIs.ACCOUNT_REQUEST, PUT, UpdateAccountRequestAction.class);
map(ResourceURIs.ACCOUNT_REQUESTS, GET, GetAccountRequestsAction.class);
map(ResourceURIs.ACCOUNT_REQUEST_RESET, PUT, ResetAccountRequestAction.class);
map(ResourceURIs.ACCOUNT_REQUEST_REJECTION, PUT, RejectAccountRequestAction.class);
map(ResourceURIs.ACCOUNTS, GET, GetAccountsAction.class);
map(ResourceURIs.COURSE, GET, GetCourseAction.class);
map(ResourceURIs.COURSE, DELETE, DeleteCourseAction.class);
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/teammates/ui/webapi/RejectAccountRequestAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package teammates.ui.webapi;

import java.util.UUID;

import teammates.common.datatransfer.AccountRequestStatus;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.common.util.Const;
import teammates.storage.sqlentity.AccountRequest;
import teammates.ui.output.AccountRequestData;
import teammates.ui.request.AccountRequestRejectionRequest;
import teammates.ui.request.InvalidHttpRequestBodyException;

/**
* Rejects an account request.
*/
public class RejectAccountRequestAction extends AdminOnlyAction {

@Override
public JsonResult execute() throws InvalidOperationException, InvalidHttpRequestBodyException {
String id = getNonNullRequestParamValue(Const.ParamsNames.ACCOUNT_REQUEST_ID);
UUID accountRequestId;

try {
accountRequestId = UUID.fromString(id);
} catch (IllegalArgumentException e) {
throw new InvalidHttpParameterException(e.getMessage(), e);
}

AccountRequest accountRequest = sqlLogic.getAccountRequest(accountRequestId);

if (accountRequest == null) {
String errorMessage = String.format(Const.ACCOUNT_REQUEST_NOT_FOUND, accountRequestId.toString());
throw new EntityNotFoundException(errorMessage);
}

AccountRequestRejectionRequest accountRequestRejectionRequest =
getAndValidateRequestBody(AccountRequestRejectionRequest.class);

try {
accountRequest.setStatus(AccountRequestStatus.REJECTED);
accountRequest = sqlLogic.updateAccountRequest(accountRequest);
} catch (InvalidParametersException e) {
throw new InvalidHttpRequestBodyException(e);
} catch (EntityDoesNotExistException e) {
throw new EntityNotFoundException(e);
}

if (accountRequestRejectionRequest.getReasonBody() != null) {
// TODO: generate rejection email from reason title and reason body
}

return new JsonResult(new AccountRequestData(accountRequest));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
public class UpdateAccountRequestAction extends AdminOnlyAction {

static final String ACCOUNT_REQUEST_NOT_FOUND = "Account request with id = %s not found";

@Override
public JsonResult execute() throws InvalidOperationException, InvalidHttpRequestBodyException {
String id = getNonNullRequestParamValue(Const.ParamsNames.ACCOUNT_REQUEST_ID);
Expand All @@ -33,7 +31,7 @@ public JsonResult execute() throws InvalidOperationException, InvalidHttpRequest
AccountRequest accountRequest = sqlLogic.getAccountRequest(accountRequestId);

if (accountRequest == null) {
String errorMessage = String.format(ACCOUNT_REQUEST_NOT_FOUND, accountRequestId.toString());
String errorMessage = String.format(Const.ACCOUNT_REQUEST_NOT_FOUND, accountRequestId.toString());
throw new EntityNotFoundException(errorMessage);
}

Expand Down

0 comments on commit bd08202

Please sign in to comment.