Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add names to retroactive attendance form #407

Merged
merged 18 commits into from
Apr 30, 2024
Merged
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
9 changes: 5 additions & 4 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')
.select(['email', 'UserModel.firstName', 'UserModel.lastName'])
.getRawMany();
return emailsRaw.map((emailRaw) => emailRaw.email);
return namesEmailsRaw.map((nameEmailRaw) => `${nameEmailRaw.UserModel_firstName} `
nik-dange marked this conversation as resolved.
Show resolved Hide resolved
+ `${nameEmailRaw.UserModel_lastName} (${nameEmailRaw.email})`);
}

public static async generateHash(pass: string): Promise<string> {
Expand Down
4 changes: 2 additions & 2 deletions services/UserAccountService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,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
Loading