diff --git a/api/controllers/AdminController.ts b/api/controllers/AdminController.ts index 6ab76108..56107c1b 100644 --- a/api/controllers/AdminController.ts +++ b/api/controllers/AdminController.ts @@ -12,7 +12,7 @@ import { CreateMilestoneResponse, CreateBonusResponse, UploadBannerResponse, - GetAllEmailsResponse, + GetAllNamesEmailsResponse, SubmitAttendanceForUsersResponse, ModifyUserAccessLevelResponse, GetAllUserAccessLevelsResponse, @@ -41,10 +41,10 @@ export class AdminController { } @Get('/email') - async getAllEmails(@AuthenticatedUser() user: UserModel): Promise { + async getAllNamesEmails(@AuthenticatedUser() user: UserModel): Promise { if (!PermissionsService.canSeeAllUserEmails(user)) throw new ForbiddenError(); - const emails = await this.userAccountService.getAllEmails(); - return { error: null, emails }; + const namesEmails = await this.userAccountService.getAllNamesEmails(); + return { error: null, namesEmails }; } @Post('/milestone') diff --git a/repositories/UserRepository.ts b/repositories/UserRepository.ts index 6f5e6539..7e2a79a5 100644 --- a/repositories/UserRepository.ts +++ b/repositories/UserRepository.ts @@ -45,12 +45,13 @@ export class UserRepository extends BaseRepository { return this.repository.findOne({ accessCode }); } - public async getAllEmails(): Promise { - const emailsRaw = await this.repository + public async getAllNamesEmails(): Promise { + const namesEmailsRaw = await this.repository .createQueryBuilder() .select(['email', 'UserModel.firstName', 'UserModel.lastName']) .getRawMany(); - return emailsRaw.map((emailRaw) => `${emailRaw.UserModel_firstName} ${emailRaw.UserModel_lastName} - ${emailRaw.email}`); + return namesEmailsRaw.map((nameEmailRaw)=> + `${nameEmailRaw.UserModel_firstName} ${nameEmailRaw.UserModel_lastName} (${nameEmailRaw.email})`); } public static async generateHash(pass: string): Promise { diff --git a/services/UserAccountService.ts b/services/UserAccountService.ts index 3a17d175..ec3f714b 100644 --- a/services/UserAccountService.ts +++ b/services/UserAccountService.ts @@ -15,6 +15,7 @@ import { UserPatches, UserState, PrivateProfile, + GetAllNamesEmailsResponse, } from '../types'; import { UserRepository } from '../repositories/UserRepository'; import { UserModel } from '../models/UserModel'; @@ -167,10 +168,10 @@ export default class UserAccountService { }); } - public async getAllEmails(): Promise { + public async getAllNamesEmails(): Promise { return this.transactions.readOnly(async (txn) => Repositories .user(txn) - .getAllEmails()); + .getAllNamesEmails()); } /** diff --git a/tests/admin.test.ts b/tests/admin.test.ts index 021f8c6d..9e362990 100644 --- a/tests/admin.test.ts +++ b/tests/admin.test.ts @@ -131,19 +131,20 @@ describe('retroactive attendance submission', () => { }); }); -describe('email retrieval', () => { +describe('names and emails retrieval', () => { test('gets all the emails of stored users', async () => { const conn = await DatabaseConnection.get(); const users = UserFactory.create(5); - const emails = users.map((user) => user.email.toLowerCase()); + const namesEmails = users.map((user) => `${user.firstName} ${user.lastName} (${user.email.toLowerCase()})`); const admin = UserFactory.fake({ accessType: UserAccessType.ADMIN }); await new PortalState() .createUsers(...users, admin) .write(); - const response = await ControllerFactory.admin(conn).getAllEmails(admin); - expect(expect.arrayContaining(response.emails)).toEqual([...emails, admin.email]); + const response = await ControllerFactory.admin(conn).getAllNamesEmails(admin); + expect(expect.arrayContaining(response.namesEmails)).toEqual([...namesEmails, + `${admin.firstName} ${admin.lastName} (${admin.email})`]); }); }); diff --git a/types/ApiResponses.ts b/types/ApiResponses.ts index cb93f046..e342c308 100644 --- a/types/ApiResponses.ts +++ b/types/ApiResponses.ts @@ -31,8 +31,8 @@ export interface UploadBannerResponse extends ApiResponse { banner: string; } -export interface GetAllEmailsResponse extends ApiResponse { - emails: string[]; +export interface GetAllNamesEmailsResponse extends ApiResponse { + namesEmails: string[]; } export interface SubmitAttendanceForUsersResponse extends ApiResponse {