Skip to content

Conversation

@Luoq1-Xu
Copy link

Fixes #13119

Cause
Previously, admins could not approve an account request if any account with the same email existed, including student accounts. This was due to the UpdateAccountRequestAction class calling the getAccountsForEmailWithTransactionmethod from sqlLogic, which gets all accounts (regardless of if those were student-only or instructor accounts) linked to a particular email. This was too restrictive, as only instructor accounts in the same institute should block approval.

Outline of Solution

  • The approval logic now only prevents approval if there is an existing instructor account with the same email in the same institute as the account request.
  • Introduced a new method isInstructorWithEmailInInstitute(email, institute) into the Logic class, which searches for instructors with the given email and verifies if it belongs to any course under the specified institute.
  • Admins can now approve instructor account requests even if a student account with the same email exists, as long as there is no conflicting instructor account in the same institute.

Luoq1-Xu added 8 commits July 13, 2025 20:12
Previously, admin could not approve an account request if any account
with the same email existed, including student accounts. This was too
restrictive, as only instructor accounts in the same institute should
block approval.

This commit updates the approval logic to only prevent approval if there
is an existing instructor account with the same email in the same
institute as the account request. The check now uses
`logic.isInstructorWithEmailInInstitute(email, institute)`, which
searches for instructors with the given email and verifies if any belong
to a course under the specified institute.

This allows admins to approve instructor account requests even if a
student account with the same email exists, as long as there is no
conflicting instructor account in the same institute.
- Add getInstructorsForEmail method in InstructorsDb to retrieve all
instructors with a specific email address.
- Add corresponding forwarding method in Logic.
- Refactor InstructorsLogic.isExistingInstructorWithEmailInInstitute to
use getInstructorsForEmail instead of searchInstructorsInWholeSystem,
reducing dependency on search service.
- Update error message in UpdateAccountRequestAction to clarify
duplicate instructor account condition.
- Update UpdateAccountRequestActionIT test
- Fix checkstyle errors
- Update the error message in UpdateAccountRequestAction and related
tests to be more descriptive, mentioning that an instructor account
with the same email under the same institute already exists.
Adding a trivial comment to trigger a new CI workflow run. This is to
re-verify the failing integration test (Job ID: 46799151757).
@damithc damithc requested a review from Copilot July 31, 2025 13:38
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes an issue where admins couldn't create instructor accounts when a student account already existed with the same email address. The solution refines the validation logic to only prevent instructor account creation when there's an existing instructor account with the same email in the same institute, rather than blocking for any account type.

  • Replaced broad email conflict check with institute-specific instructor validation
  • Added new methods to check for existing instructors by email within a specific institute
  • Updated error messaging to be more specific about the conflict type

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
UpdateAccountRequestAction.java Updated approval logic to check for instructor conflicts only within the same institute
InstructorsDb.java Added database layer method to retrieve instructors by email
InstructorsLogic.java Added business logic to check for existing instructors by email in a specific institute
Logic.java Added public API method for instructor existence validation
UpdateAccountRequestActionIT.java Updated integration tests to verify the new behavior and error messages

@damithc damithc requested review from DhiraPT and mingyang143 July 31, 2025 13:39
Copy link
Contributor

@DhiraPT DhiraPT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR. The code changes look correct, minimal, and aligned with architecture and business rules. However, the PR currently lacks an integration test that specifically reproduces the original bug (student → instructor email reuse) to prove the fix. Adding that test is critical to ensure the fix addresses issue #13119 and to prevent regressions. Please add this test to UpdateAccountRequestActionIT.

- standardized isExistingInstructorWithEmailInInstitute and
isInstructorWithEmailInInstitute to existsInstructorWithEmailInInstitute
for better clarity
- fixed a few grammatical issues in comments
@Luoq1-Xu
Copy link
Author

Hi @DhiraPT, thanks for getting back! I have made the changes and added the relevant integration test to UpdateAccountRequestActionIT.

Copy link
Contributor

@DhiraPT DhiraPT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Please import the entities first before using them (I found this in several places).

Comment on lines 441 to 453
public boolean existsInstructorWithEmailInInstitute(String email, String institute) {
assert email != null;
assert institute != null;

List<InstructorAttributes> instructors = getInstructorsForEmail(email);
for (InstructorAttributes instructor : instructors) {
if (institute.equals(coursesLogic.getCourseInstitute(instructor.getCourseId()))) {
return true;
}
}
return false;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getCourseInstitute triggers a Datastore read. If an instructor belongs to 10 courses, this results in 11 queries (1 fetch for instructors + 10 fetches for courses). Would be better to fetch all courses in a single batch query using coursesLogic.getCourses(List<String> courseIds) to avoid the N+1 problem.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion! It does sound like a better solution. I have added a new getCourses method to do this batch fetching

______TS("email with existing account throws exception");
______TS("email with existing student account (only) under same institute should approve successfully");
Account studentAccount = logic.createAccountWithTransaction(getTypicalStudentAccount());
teammates.storage.sqlentity.Course studentCourse = getTypicalStudentCourse();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import Course first

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have added the necessary imports :)

Luoq1-Xu and others added 3 commits January 31, 2026 12:48
- ensure entities used are properly imported
- use batch fetching for courses in InstructorsLogic
to avoid cascading reads
@mingyuancode mingyuancode requested a review from DhiraPT February 2, 2026 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Admin: Unable to create an instructor account if the same email was used for a student account

3 participants