Skip to content

Commit

Permalink
Merge pull request #63 from Fam-Story/fix/62-update-logic
Browse files Browse the repository at this point in the history
Fix: update 로직 수정 (#62)
  • Loading branch information
synoti21 authored Nov 30, 2023
2 parents c9a5963 + cf40d53 commit 93e783e
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/common/api/response-created.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const CustomApiCreatedResponse = (
ApiResponse({ status: 401, description: '인증되지 않은 요청입니다.' }),
ApiResponse({ status: 403, description: '접근 권한이 없습니다.' }),
ApiResponse({ status: 404, description: '리소스를 찾을 수 없습니다.' }),
ApiResponse({ status: 409, description: '이미 존재하는 리소스입니다.' }),
ApiResponse({ status: 500, description: '서버 내부 오류가 발생했습니다.' }),
);
};
1 change: 1 addition & 0 deletions src/common/api/response-ok.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const CustomApiOKResponse = (model: any, apiDescription: string) => {
ApiResponse({ status: 401, description: '인증되지 않은 요청입니다.' }),
ApiResponse({ status: 403, description: '접근 권한이 없습니다.' }),
ApiResponse({ status: 404, description: '리소스를 찾을 수 없습니다.' }),
ApiResponse({ status: 409, description: '이미 존재하는 리소스입니다.' }),
ApiResponse({ status: 500, description: '서버 내부 오류가 발생했습니다.' }),
);
};
7 changes: 5 additions & 2 deletions src/domain/family/family.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ export class FamilyService {

//가족 정보 업데이트
async updateFamily(updateFamilyDto: UpdateFamilyDto): Promise<void> {
await this.validateFamily(updateFamilyDto.familyId);
await this.familyRepository.save(updateFamilyDto);
const family = await this.validateFamily(updateFamilyDto.familyId);
await this.familyRepository.update(
{ id: family.id },
{ ...updateFamilyDto },
);
}

async validateFamily(familyId: number) {
Expand Down
6 changes: 4 additions & 2 deletions src/domain/family_member/family-member.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ export class FamilyMemberService {
const familyMember = await this.validateFamilyMember(
updateFamilyMemberDto.familyMemberId,
);
familyMember.role = updateFamilyMemberDto.role;
await this.familyMemberRepository.save(familyMember);
await this.familyMemberRepository.update(
{ id: familyMember.id },
{ ...updateFamilyMemberDto },
);
}

async deleteFamilyMember(familyMemberId: number) {
Expand Down
9 changes: 3 additions & 6 deletions src/domain/family_schedule/family-schedule.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,10 @@ export class FamilyScheduleService {
updateFamilyScheduleDto.familyScheduleId,
);
await this.validateFamily(updateFamilyScheduleDto.familyId);
familySchedule.scheduleDate = new Date(
updateFamilyScheduleDto.scheduleYear,
updateFamilyScheduleDto.scheduleMonth - 1,
updateFamilyScheduleDto.scheduleDay,
await this.familyScheduleRepository.update(
{ id: familySchedule.id },
{ ...updateFamilyScheduleDto },
);
familySchedule.scheduleName = updateFamilyScheduleDto.scheduleName;
await this.familyScheduleRepository.save(familySchedule);
}

async deleteFamilySchedule(familyScheduleId: number) {
Expand Down
4 changes: 1 addition & 3 deletions src/domain/post/post.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ export class PostService {
async updatePost(updatePostDto: UpdatePostDto) {
const post = await this.validatePost(updatePostDto.postId);
await this.validateFamilyMember(updatePostDto.srcMemberId);
post.title = updatePostDto.title;
post.context = updatePostDto.context;

await this.postRepository.save(post);
await this.postRepository.update({ id: post.id }, { ...updatePostDto });
}

async findPostListByFamilyId(familyId: number): Promise<ResponsePostDto[]> {
Expand Down
7 changes: 5 additions & 2 deletions src/domain/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ export class UserService {
userId: number,
updateUserDto: UpdateUserDto,
): Promise<void> {
await this.validateUser(userId);
await this.userRepository.save(updateUserDto);
const user = await this.validateUser(userId);
if (updateUserDto.password !== undefined) {
updateUserDto.password = await this.hashPassword(updateUserDto.password);
}
await this.userRepository.update({ id: user.id }, { ...updateUserDto });
}

//이메일을 통한 중복 검사
Expand Down
4 changes: 2 additions & 2 deletions src/test/service/family-member.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ describe('FamilyMemberService', () => {
user.id = 1;
familyMember.setId(1);

jest.spyOn(familyMemberRepository, 'save').mockResolvedValue(familyMember);
jest.spyOn(familyMemberRepository, 'update').mockResolvedValue(familyMember);
jest.spyOn(userRepository, 'findOne').mockResolvedValue(user);
jest.spyOn(familyRepository, 'findOne').mockResolvedValue(family);
jest.spyOn(familyMemberRepository, 'findOne').mockResolvedValue(family);

await familyMemberService.updateFamilyMember(updateFamilyMemberDto);
expect(familyMemberRepository.save).toBeCalled();
expect(familyMemberRepository.update).toBeCalled();
});

it('should delete family-member', async () => {
Expand Down
5 changes: 3 additions & 2 deletions src/test/service/family-schedule.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('FamilyScheduleService', () => {
findOne: jest.fn(),
save: jest.fn(),
delete: jest.fn(),
update: jest.fn(),
});

const family = Family.createFamily('testFamily', 'testKeyCode');
Expand Down Expand Up @@ -98,12 +99,12 @@ describe('FamilyScheduleService', () => {
.spyOn(familyScheduleRepository, 'findOne')
.mockResolvedValue(familySchedule);
jest
.spyOn(familyScheduleRepository, 'save')
.spyOn(familyScheduleRepository, 'update')
.mockResolvedValue(familySchedule);

await familyScheduleService.updateFamilySchedule(updateFamilyScheduleDto);

expect(familyScheduleRepository.save).toBeCalledTimes(1);
expect(familyScheduleRepository.update).toBeCalledTimes(1);
});

it('should delete family schedule', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/test/service/family.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ describe('FamilyService', () => {
familyName: 'testUpdated',
};
const family = Family.createFamily('test', familyKeyCode);
jest.spyOn(familyRepository, 'save').mockResolvedValue(1);
jest.spyOn(familyRepository, 'update').mockResolvedValue(1);
jest.spyOn(familyRepository, 'findOne').mockResolvedValue(family);

await familyService.updateFamily(updateFamilyDto);

expect(familyRepository.save).toBeCalledTimes(1);
expect(familyRepository.update).toBeCalledTimes(1);
expect(familyRepository.findOne).toBeCalledTimes(1);
});
});
5 changes: 3 additions & 2 deletions src/test/service/post.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('PostService', () => {
findOne: jest.fn(),
save: jest.fn(),
delete: jest.fn(),
update: jest.fn(),
});

let postService: PostService;
Expand Down Expand Up @@ -96,14 +97,14 @@ describe('PostService', () => {
};

jest.spyOn(postRepository, 'findOne').mockResolvedValue(post);
jest.spyOn(postRepository, 'save').mockResolvedValue(post);
jest.spyOn(postRepository, 'update').mockResolvedValue(post);
jest
.spyOn(familyMemberRepository, 'findOne')
.mockResolvedValue(familyMember);

await postService.updatePost(updatePostDto);

expect(postRepository.save).toHaveBeenCalled();
expect(postRepository.update).toHaveBeenCalled();
});

it('should delete post', async () => {
Expand Down
9 changes: 6 additions & 3 deletions src/test/service/user.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('UserService', () => {
findOne: jest.fn(),
save: jest.fn(),
delete: jest.fn(),
update: jest.fn(),
});

let userService: UserService;
Expand Down Expand Up @@ -65,7 +66,7 @@ describe('UserService', () => {
});

describe('updateUser', () => {
it('should update user', async () => {
it('should update user and should crypt password', async () => {
const user: User = User.createUser(
'[email protected]',
'test',
Expand All @@ -84,11 +85,13 @@ describe('UserService', () => {
gender: 1,
};
jest.spyOn(userRepository, 'findOne').mockResolvedValue(user);
jest.spyOn(userRepository, 'save').mockResolvedValue(1);
jest.spyOn(userRepository, 'update').mockResolvedValue(1);
jest.spyOn(userService, 'hashPassword').mockResolvedValue('@@#');

await userService.updateUser(1, updateUserDto);
expect(userRepository.findOne).toHaveBeenCalled();
expect(userRepository.save).toHaveBeenCalled();
expect(userRepository.update).toHaveBeenCalled();
expect(userService.hashPassword).toHaveBeenCalled();
});

it('should delete user', async () => {
Expand Down

0 comments on commit 93e783e

Please sign in to comment.