Skip to content

Commit 288156f

Browse files
committed
feat: implement updateProfile method in UserService and corresponding tests
1 parent a766f69 commit 288156f

File tree

3 files changed

+85
-4
lines changed

3 files changed

+85
-4
lines changed

server/src/user/user.controller.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { Controller, Inject, Get, Patch, Body, Query } from '@nestjs/common';
2-
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
3-
import { GetRequestToken, validateUser } from '@server/GetRequestUser';
1+
import { Body, Controller, Get, Inject, Patch, Query } from '@nestjs/common';
2+
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
3+
44
import { PageQueryDTO } from '@shared/validation/common/dto/PageQuery.dto';
55
import { GetUser } from '@shared/validation/user/dto/GetUser.dto';
66
import { UpdateUsernameDto } from '@shared/validation/user/dto/UpdateUsername.dto';
77
import { UpdateUserProfileDto } from '@shared/validation/user/dto/UpdateUserProfile.dto';
88

9+
import { GetRequestToken, validateUser } from '@server/GetRequestUser';
10+
911
import { UserDocument } from './entity/user.entity';
1012
import { UserService } from './user.service';
1113

server/src/user/user.service.spec.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Model } from 'mongoose';
88

99
import { User, UserDocument } from './entity/user.entity';
1010
import { UserService } from './user.service';
11+
import { UpdateUserProfileDto } from '@shared/validation/user/dto/UpdateUserProfile.dto';
1112

1213
const mockUserModel = {
1314
create: jest.fn(),
@@ -18,6 +19,7 @@ const mockUserModel = {
1819
exec: jest.fn(),
1920
select: jest.fn(),
2021
countDocuments: jest.fn(),
22+
findOneAndUpdate: jest.fn(),
2123
};
2224

2325
describe('UserService', () => {
@@ -332,6 +334,7 @@ describe('UserService', () => {
332334
username: 'testuser',
333335
save: jest.fn().mockReturnThis(),
334336
} as unknown as UserDocument;
337+
335338
const body = { username: 'newuser' };
336339

337340
jest.spyOn(service, 'usernameExists').mockResolvedValue(false);
@@ -358,4 +361,72 @@ describe('UserService', () => {
358361
);
359362
});
360363
});
364+
365+
describe('updateProfile', () => {
366+
it('should update the user profile successfully', async () => {
367+
const user = {
368+
_id: 'userId',
369+
description: 'old description',
370+
socialLinks: {},
371+
username: 'oldUsername',
372+
} as unknown as UserDocument;
373+
374+
const body: UpdateUserProfileDto = {
375+
description: 'new description',
376+
socialLinks: { github: 'https://github.com/newuser' },
377+
username: 'newUsername',
378+
};
379+
380+
const updatedUser = {
381+
...user,
382+
...body,
383+
};
384+
385+
jest
386+
.spyOn(userModel, 'findOneAndUpdate')
387+
.mockResolvedValue(updatedUser as any);
388+
389+
const result = await service.updateProfile(user, body);
390+
391+
expect(result).toEqual(updatedUser);
392+
393+
expect(userModel.findOneAndUpdate).toHaveBeenCalledWith(
394+
{ _id: user._id },
395+
user,
396+
{ new: true },
397+
);
398+
});
399+
400+
it('should update only provided fields', async () => {
401+
const user = {
402+
_id: 'userId',
403+
description: 'old description',
404+
socialLinks: {},
405+
username: 'oldUsername',
406+
} as unknown as UserDocument;
407+
408+
const body: UpdateUserProfileDto = {
409+
description: 'new description',
410+
};
411+
412+
const updatedUser = {
413+
...user,
414+
description: 'new description',
415+
};
416+
417+
jest
418+
.spyOn(userModel, 'findOneAndUpdate')
419+
.mockResolvedValue(updatedUser as any);
420+
421+
const result = await service.updateProfile(user, body);
422+
423+
expect(result).toEqual(updatedUser);
424+
425+
expect(userModel.findOneAndUpdate).toHaveBeenCalledWith(
426+
{ _id: user._id },
427+
user,
428+
{ new: true },
429+
);
430+
});
431+
});
361432
});

server/src/user/user.service.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ export class UserService {
151151
}
152152

153153
public async updateProfile(user: UserDocument, body: UpdateUserProfileDto) {
154-
throw new Error('Method not implemented.');
154+
const { description, socialLinks, username } = body;
155+
156+
if (description) user.description = description;
157+
if (socialLinks) user.socialLinks = socialLinks;
158+
if (username) user.username = username;
159+
160+
return await this.userModel.findOneAndUpdate({ _id: user._id }, user, {
161+
new: true,
162+
});
155163
}
156164
}

0 commit comments

Comments
 (0)