Skip to content

Commit

Permalink
Fix tests, rename email method
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanDing26 committed Mar 25, 2024
1 parent 726f143 commit fb902e9
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
8 changes: 4 additions & 4 deletions api/controllers/AdminController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
CreateMilestoneResponse,
CreateBonusResponse,
UploadBannerResponse,
GetAllEmailsResponse,
GetAllNamesEmailsResponse,
SubmitAttendanceForUsersResponse,
ModifyUserAccessLevelResponse,
GetAllUserAccessLevelsResponse,
Expand Down Expand Up @@ -41,10 +41,10 @@ export class AdminController {
}

@Get('/email')
async getAllEmails(@AuthenticatedUser() user: UserModel): Promise<GetAllEmailsResponse> {
async getAllNamesEmails(@AuthenticatedUser() user: UserModel): Promise<GetAllNamesEmailsResponse> {
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')
Expand Down
7 changes: 4 additions & 3 deletions repositories/UserRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ export class UserRepository extends BaseRepository<UserModel> {
return this.repository.findOne({ accessCode });
}

public async getAllEmails(): Promise<string[]> {
const emailsRaw = await this.repository
public async getAllNamesEmails(): Promise<string[]> {
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<string> {
Expand Down
5 changes: 3 additions & 2 deletions services/UserAccountService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
UserPatches,
UserState,
PrivateProfile,
GetAllNamesEmailsResponse,
} from '../types';
import { UserRepository } from '../repositories/UserRepository';
import { UserModel } from '../models/UserModel';
Expand Down Expand Up @@ -167,10 +168,10 @@ export default class UserAccountService {
});
}

public async getAllEmails(): Promise<string[]> {
public async getAllNamesEmails(): Promise<string[]> {
return this.transactions.readOnly(async (txn) => Repositories
.user(txn)
.getAllEmails());
.getAllNamesEmails());
}

/**
Expand Down
9 changes: 5 additions & 4 deletions tests/admin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})`]);
});
});

Expand Down
4 changes: 2 additions & 2 deletions types/ApiResponses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit fb902e9

Please sign in to comment.