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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
GetAllNamesAndEmailsResponse,
SubmitAttendanceForUsersResponse,
ModifyUserAccessLevelResponse,
GetAllUserAccessLevelsResponse,
Expand Down Expand Up @@ -41,10 +41,10 @@ export class AdminController {
}

@Get('/email')
async getAllEmails(@AuthenticatedUser() user: UserModel): Promise<GetAllEmailsResponse> {
async getAllNamesAndEmails(@AuthenticatedUser() user: UserModel): Promise<GetAllNamesAndEmailsResponse> {
if (!PermissionsService.canSeeAllUserEmails(user)) throw new ForbiddenError();
const emails = await this.userAccountService.getAllEmails();
return { error: null, emails };
const namesAndEmails = await this.userAccountService.getAllNamesAndEmails();
return { error: null, namesAndEmails };
}

@Post('/milestone')
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@acmucsd/membership-portal",
"version": "3.6.0",
"version": "3.6.1",
"description": "REST API for ACM UCSD's membership portal.",
"main": "index.d.ts",
"files": [
Expand Down
14 changes: 9 additions & 5 deletions repositories/UserRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EntityRepository, In } from 'typeorm';
import * as bcrypt from 'bcrypt';
import { Activity } from '../types/internal';
import { UserModel } from '../models/UserModel';
import { Uuid } from '../types';
import { Uuid, NameAndEmail } from '../types';
import { BaseRepository } from './BaseRepository';

@EntityRepository(UserModel)
Expand Down Expand Up @@ -50,12 +50,16 @@ export class UserRepository extends BaseRepository<UserModel> {
return this.repository.findOne({ accessCode });
}

public async getAllEmails(): Promise<string[]> {
const emailsRaw = await this.repository
public async getAllNamesAndEmails(): Promise<NameAndEmail[]> {
const namesAndEmailsRaw = await this.repository
.createQueryBuilder()
.select('email')
.select(['email', 'UserModel.firstName', 'UserModel.lastName'])
.getRawMany();
return emailsRaw.map((emailRaw) => emailRaw.email);
const namesAndEmailsFormatted: NameAndEmail[] = namesAndEmailsRaw.map((nameAndEmailRaw) => ({ firstName:
nameAndEmailRaw.UserModel_firstName,
lastName: nameAndEmailRaw.UserModel_lastName,
email: nameAndEmailRaw.email }));
return namesAndEmailsFormatted;
}

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 @@ -20,6 +20,7 @@ import {
UserPatches,
UserState,
PrivateProfile,
NameAndEmail,
} from '../types';
import { UserRepository } from '../repositories/UserRepository';
import { UserModel } from '../models/UserModel';
Expand Down Expand Up @@ -181,10 +182,10 @@ export default class UserAccountService {
});
}

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

/**
Expand Down
15 changes: 10 additions & 5 deletions tests/admin.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BadRequestError, ForbiddenError } from 'routing-controllers';
import { In } from 'typeorm';
import { ActivityScope, ActivityType, SubmitAttendanceForUsersRequest, UserAccessType } from '../types';
import { ActivityScope, ActivityType, SubmitAttendanceForUsersRequest, UserAccessType, NameAndEmail } from '../types';
import { ControllerFactory } from './controllers';
import { DatabaseConnection, EventFactory, PortalState, UserFactory } from './data';
import { UserModel } from '../models/UserModel';
Expand Down Expand Up @@ -131,19 +131,24 @@ 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 namesAndEmails: NameAndEmail[] = users.map((user) => ({ firstName: user.firstName,
lastName: user.lastName,
email:
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).getAllNamesAndEmails(admin);
const expected: NameAndEmail = { firstName: admin.firstName, lastName: admin.lastName, email: admin.email };
expect(expect.arrayContaining(response.namesAndEmails)).toEqual([...namesAndEmails,
expected]);
});
});

Expand Down
10 changes: 7 additions & 3 deletions types/ApiResponses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
import { MerchItemOptionMetadata, Uuid } from '.';

// RESPONSE TYPES

export interface CustomErrorBody {
name: string;
message: string;
Expand All @@ -31,8 +30,8 @@ export interface UploadBannerResponse extends ApiResponse {
banner: string;
}

export interface GetAllEmailsResponse extends ApiResponse {
emails: string[];
export interface GetAllNamesAndEmailsResponse extends ApiResponse {
namesAndEmails: NameAndEmail[];
}

export interface SubmitAttendanceForUsersResponse extends ApiResponse {
Expand Down Expand Up @@ -325,6 +324,11 @@ export interface FulfillMerchOrderResponse extends ApiResponse {
}

// USER
export interface NameAndEmail {
firstName: string;
lastName: string;
email: string;
}

export interface PublicActivity {
type: ActivityType,
Expand Down
Loading