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

feat: Patch형태의 업데이트 API에서 변경하는 프로퍼티가 없을경우 업데이트가 실패하도록 구현 #314

Merged
merged 2 commits into from
Jul 18, 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
2 changes: 2 additions & 0 deletions backend/src/project/dto/epic/EpicUpdateRequest.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import {
Length,
} from 'class-validator';
import { EpicColor } from 'src/project/entity/epic.entity';
import { AtLeastOneProperty } from 'src/project/util/validation.util';

class Epic {
@IsNotEmpty()
@IsInt()
@AtLeastOneProperty(['name', 'color'])
id: number;

@IsOptional()
Expand Down
2 changes: 2 additions & 0 deletions backend/src/project/dto/member/MemberUpdateRequest.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import {
Matches,
ValidateNested,
} from 'class-validator';
import { AtLeastOneProperty } from 'src/project/util/validation.util';
import { MemberStatus } from '../../enum/MemberStatus.enum';

class Content {
@IsInt()
@AtLeastOneProperty(['username', 'imageUrl', 'status'])
id: number;

@IsString()
Expand Down
7 changes: 7 additions & 0 deletions backend/src/project/dto/story/StoryUpdateRequest.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ import {
ValidateNested,
} from 'class-validator';
import { StoryStatus } from 'src/project/entity/story.entity';
import { AtLeastOneProperty } from 'src/project/util/validation.util';

class Story {
@IsInt()
@AtLeastOneProperty([
'epicId',
'title',
'point',
'status'
])
id: number;

@IsOptional()
Expand Down
11 changes: 10 additions & 1 deletion backend/src/project/dto/task/TaskUpdateRequest.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@ import {
ValidateNested,
} from 'class-validator';
import { TaskStatus } from 'src/project/entity/task.entity';
import { AtLeastOneProperty } from 'src/project/util/validation.util';
import { IsOneDecimalPlace } from './TaskCreateRequest.dto';

class Task {
@IsInt()
@AtLeastOneProperty([
'storyId',
'title',
'expectedTime',
'actualTime',
'assignedMemberId',
'status',
])
id: number;

@IsOptional()
Expand All @@ -28,7 +37,7 @@ class Task {
@IsOptional()
@IsOneDecimalPlace()
expectedTime?: number;

@IsOptional()
@IsOneDecimalPlace()
actualTime?: number;
Expand Down
27 changes: 26 additions & 1 deletion backend/src/project/util/validation.util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ValidationError } from 'class-validator';
import { registerDecorator, ValidationArguments, ValidationError, ValidationOptions } from 'class-validator';

export function getRecursiveErrorMsgList(errors: ValidationError[]): string[] {
return errors.reduce((acc, error) => {
Expand All @@ -11,3 +11,28 @@ export function getRecursiveErrorMsgList(errors: ValidationError[]): string[] {
return acc;
}, []);
}

export function AtLeastOneProperty(
properties: string[],
validationOptions?: ValidationOptions
) {
return function (object: Object, propertyName: string) {
registerDecorator({
name: 'atLeastOneProperty',
target: object.constructor,
propertyName: propertyName,
options: validationOptions,
constraints: [properties],
validator: {
validate(value: any, args: ValidationArguments) {
const object = args.object as any;
return properties.some((property) => object[property] !== undefined);
},
defaultMessage(args: ValidationArguments) {
const properties = args.constraints[0];
return `At least one of these properties must be provided: ${properties.join(', ')}`;
},
},
});
};
}
37 changes: 37 additions & 0 deletions backend/test/project/ws-backlog-page/ws-story.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,43 @@ describe('WS story', () => {
});
});
};

it('should return error when updated property is do not exist', async () => {
const socket = await getMemberJoinedLandingPage();
socket.emit('joinBacklog');
await initBacklog(socket);

const name = '회원';
const color = 'yellow';
let requestData: any = {
action: 'create',
content: { name, color },
};
socket.emit('epic', requestData);
const [epicId] = await Promise.all([getEpicId(socket)]);

const title = '타이틀';
const point = 2;
const status = '시작전';
requestData = {
action: 'create',
content: { title, point, status, epicId },
};
socket.emit('story', requestData);
const storyId = await getStoryId(socket);

requestData = {
action: 'update',
content: { id: storyId },
};
socket.emit('story', requestData);
await new Promise<void>((resolve) => {
socket.on('error', () => {
resolve();
});
});
socket.close();
});
});
});

Expand Down
Loading