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

Delete account route #331

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
9 changes: 8 additions & 1 deletion api/controllers/UserController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
JsonController, Params, Get, Post, Patch, UseBefore, UploadedFile, Body,
JsonController, Params, Get, Post, Patch, UseBefore, UploadedFile, Body, Delete,
} from 'routing-controllers';
import { UserModel } from '../../models/UserModel';
import UserAccountService from '../../services/UserAccountService';
Expand All @@ -14,6 +14,7 @@ import {
GetUserResponse,
GetCurrentUserResponse,
PatchUserResponse,
DeleteUserResponse,
} from '../../types';
import { UuidParam } from '../validators/GenericRequests';
import { PatchUserRequest } from '../validators/UserControllerRequests';
Expand Down Expand Up @@ -77,4 +78,10 @@ export class UserController {
const patchedUser = await this.userAccountService.update(user, patchUserRequest.user);
return { error: null, user: patchedUser.getFullUserProfile() };
}

@Delete()
async deleteAccount(@AuthenticatedUser() user: UserModel): Promise<DeleteUserResponse> {
await this.userAccountService.delete(user);
return { error: null };
}
}
22 changes: 21 additions & 1 deletion repositories/UserRepository.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { EntityRepository, In } from 'typeorm';
import * as bcrypt from 'bcrypt';
import FactoryUtils from 'tests/data/FactoryUtils';
import { Activity } from '../types/internal';
import { UserModel } from '../models/UserModel';
import { Uuid } from '../types';
import { UserAccessType, UserState, Uuid } from '../types';
import { BaseRepository } from './BaseRepository';

@EntityRepository(UserModel)
Expand Down Expand Up @@ -82,4 +83,23 @@ export class UserRepository extends BaseRepository<UserModel> {
})
.execute();
}

public async deleteUser(user: UserModel) {
const clearedSensitiveFields: Partial<UserModel> = {
email: `deleted-user-${FactoryUtils.randomHexString()}@ucsd.edu`,
profilePicture: null,
firstName: 'Deleted',
lastName: 'User',
graduationYear: 0,
major: 'Deleted',
points: 0,
Comment on lines +90 to +95
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: let's make the string values here all uppercased so that visually it's easier to distinguish deleted users and active users in e.g. a list of orders, a database table, etc.

credits: 0,
lastLogin: new Date(0),
bio: null,
accessType: UserAccessType.RESTRICTED,
state: UserState.BLOCKED,
Copy link
Collaborator

Choose a reason for hiding this comment

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

re: my earlier comment about whether we should use a new DELETED state if we plan on implementing separate BLOCKED state functionality later. I think it makes more sense to have a DELETED state because otherwise, a user could actually reach this state without even deleting their account (e.g. they fill "DELETED" as their first name, "USER" as last name, "DELETED" as major, their email as following the template above, and if they end up getting blocked by an admin that sets their access type and state to the above (again, only if the blocked functionality is implemented)). So I think it could be useful to have a state DELETED with the only state transition to it being if a user actually specifies to delete their account.

};
const deletedUser = { ...user, ...clearedSensitiveFields };
return this.repository.save(deletedUser);
}
}
4 changes: 4 additions & 0 deletions services/UserAccountService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ export default class UserAccountService {
});
}

public async delete(user: UserModel): Promise<UserModel> {
return this.transactions.readWrite(async (txn) => Repositories.user(txn).deleteUser(user));
}

public async updateProfilePicture(user: UserModel, profilePicture: string): Promise<UserModel> {
return this.transactions.readWrite(async (txn) => Repositories
.user(txn)
Expand Down
33 changes: 33 additions & 0 deletions tests/user.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ControllerFactory } from './controllers';
import { DatabaseConnection, PortalState, UserFactory } from './data';

beforeAll(async () => {
await DatabaseConnection.connect();
});

beforeEach(async () => {
await DatabaseConnection.clear();
});

afterAll(async () => {
await DatabaseConnection.clear();
await DatabaseConnection.close();
});

describe('Delete user account', () => {
test('Deleted user account cannot log in', async () => {
const conn = await DatabaseConnection.get();
const account = UserFactory.fake();

await new PortalState().createUsers(account).write();
const userController = await ControllerFactory.user(conn);

const deletedUserResponse = await userController.deleteAccount(account);

expect(deletedUserResponse).toStrictEqual({ error: null });
});
test('Deleted user account is not viewable to other members', async () => {
});
test('Deleted user account is still counted for event attendance', async () => {
});
});
2 changes: 2 additions & 0 deletions types/ApiResponses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ export interface PatchUserResponse extends ApiResponse {
user: PrivateProfile;
}

export interface DeleteUserResponse extends ApiResponse {}

export interface GetFeedbackResponse extends ApiResponse {
feedback: PublicFeedback[];
}
Expand Down