Skip to content

Commit

Permalink
user me flows
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Oct 11, 2024
1 parent a0b614f commit 774db43
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 35 deletions.
14 changes: 7 additions & 7 deletions api/src/modules/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ export class UsersController {
});
}

// @TsRestHandler(c.updateMe)
// async update(@GetUser() user: User): Promise<any> {
// return tsRestHandler(c.updateMe, async () => {
// const user = await this.usersService.update(user.id, dto);
// return { body: { data: user }, status: HttpStatus.CREATED };
// });
// }
@TsRestHandler(c.updateMe)
async update(@GetUser() user: User): Promise<any> {
return tsRestHandler(c.updateMe, async ({ body }) => {
const updatedUser = await this.usersService.update(user.id, body);
return { body: { data: updatedUser }, status: HttpStatus.CREATED };
});
}

@TsRestHandler(c.deleteMe)
async deleteMe(@GetUser() user: User): Promise<any> {
Expand Down
43 changes: 31 additions & 12 deletions api/test/integration/users/users-me.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,13 @@ import { usersContract } from '@shared/contracts/users.contract';

describe('Users ME (e2e)', () => {
let testManager: TestManager;
let authToken: string;
let testUser: User;

beforeAll(async () => {
testManager = await TestManager.createTestManager();
});

beforeEach(async () => {
await testManager.clearDatabase();

const { jwtToken, user } = await testManager.setUpTestUser();
authToken = jwtToken;
testUser = user;
});

afterAll(async () => {
Expand Down Expand Up @@ -74,20 +68,21 @@ describe('Users ME (e2e)', () => {

expect(newToken).toBeDefined();
});
it('should update a user', async () => {
it('should update a user name', async () => {
const user = await createUser(testManager.getDataSource(), {
email: '[email protected]',
});

const { jwtToken } = await testManager.logUserIn(user);
const updatedUser = { email: '[email protected]' };
const newName = 'newName';
const response = await testManager
.request()
.patch('/users/' + user.id)
.send(updatedUser)
.patch(usersContract.updateMe.path)
.send({ name: newName })
.set('Authorization', `Bearer ${jwtToken}`);
expect(response.status).toBe(201);
expect(response.body.data.email).toEqual(updatedUser.email);
expect(response.body.data.id).toEqual(user.id);
expect(response.body.data.name).toEqual(newName);

// Previous token should work after updating the user's email
const userMeResponse = await testManager
Expand All @@ -96,6 +91,30 @@ describe('Users ME (e2e)', () => {
.set('Authorization', `Bearer ${jwtToken}`);

expect(userMeResponse.status).toBe(200);
expect(userMeResponse.body.data.email).toEqual(updatedUser.email);
expect(userMeResponse.body.data.name).toEqual(newName);
});

it('should delete my own user', async () => {
const users: User[] = [];
for (const n of Array(3).keys()) {
users.push(
await testManager.mocks().createUser({ email: `user${n}@test.com` }),
);
}
const user = users[0];
const { jwtToken } = await testManager.logUserIn(user);
const response = await testManager
.request()
.delete(usersContract.deleteMe.path)
.set('Authorization', `Bearer ${jwtToken}`);

expect(response.status).toBe(HttpStatus.OK);
expect(response.body).toEqual({});
const foundUser = await testManager
.getDataSource()
.getRepository(User)
.findOneBy({ id: user.id });

expect(foundUser).toBeNull();
});
});
8 changes: 0 additions & 8 deletions shared/contracts/users.contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,4 @@ export const usersContract = contract.router({
},
body: null,
},
resetPassword: {
method: "POST",
path: "/users/me/password/reset",
responses: {
200: contract.type<null>(),
},
body: PasswordSchema,
},
});
9 changes: 3 additions & 6 deletions shared/dtos/users/update-user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { IsEmail, IsNotEmpty } from 'class-validator';
import { z } from "zod";
import { UpdateUserSchema } from "@shared/schemas/users/update-user.schema";

export class UpdateUserDto {
@IsEmail()
@IsNotEmpty()
email: string;
}
export type UpdateUserDto = z.infer<typeof UpdateUserSchema>;
2 changes: 0 additions & 2 deletions shared/schemas/users/create-user.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ export const CreateUserSchema = z.object({
name: z.string().optional(),
partnerName: z.string(),
});

export type CreateUserDto = z.infer<typeof CreateUserSchema>;
5 changes: 5 additions & 0 deletions shared/schemas/users/update-user.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { z } from "zod";

export const UpdateUserSchema = z.object({
name: z.string().optional(),
});

0 comments on commit 774db43

Please sign in to comment.