Skip to content

Commit

Permalink
add: getCoursesByCode suppressNotFoundError
Browse files Browse the repository at this point in the history
  • Loading branch information
SIY1121 committed Mar 24, 2021
1 parent 81f8afd commit ebebeac
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
24 changes: 23 additions & 1 deletion __tests__/usecase/getCoursesByCode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ test('重複したid', () =>
])
).rejects.toThrow(InvalidArgumentError))

test('存在しないidの情報は帰らない', () => {
test('見つからない条件がある場合はエラーになる', () => {
return expect(
getCoursesByCodeUseCase([
{ year: 2020, code: 'FOO' },
Expand All @@ -47,6 +47,28 @@ test('存在しないidの情報は帰らない', () => {
).rejects.toThrow(NotFoundError)
})

test('NotFoundErrorを抑制', async () => {
const res = await getCoursesByCodeUseCase(
[
{ year: 2020, code: 'FOO' },
{ year: 2020, code: initialData[1].code },
],
true
)
expect(res.length).toBe(1)
})

test('NotFoundErrorを抑制', async () => {
const res = await getCoursesByCodeUseCase(
[
{ year: 2020, code: 'FOO' },
{ year: 2021, code: initialData[1].code },
],
true
)
expect(res.length).toBe(0)
})

afterAll(() => {
disconnectDatabase()
})
2 changes: 2 additions & 0 deletions protos/CourseService.proto
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ message GetCoursesByCodeRequestCondition {

message GetCoursesByCodeRequest {
repeated GetCoursesByCodeRequestCondition conditions = 1;
/* 一部の講義が見つからない場合でもエラーにせず、見つかった分だけ返す */
bool suppressNotFoundError = 2;
}

message GetCoursesByCodeResponse {
Expand Down
3 changes: 2 additions & 1 deletion src/grpc/course.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ export const courseService: GrpcServer<CourseService> = applyLogger({
try {
const conditions = request.conditions
const courses = await getCoursesByCodeUseCase(
conditions.map((cc) => ({ year: cc.year, code: cc.code }))
conditions.map((cc) => ({ year: cc.year, code: cc.code })),
request.suppressNotFoundError
)

callback(
Expand Down
6 changes: 4 additions & 2 deletions src/usecase/getCoursesByCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ type GetCoursesByCodeUseCaseProps = {
/**
* 指定された年度と科目番号から情報を取得する
* @param props 条件
* @param suppressNotFoundError 一部の講義が見つからなくてもエラーにしない
*/
export async function getCoursesByCodeUseCase(
props: GetCoursesByCodeUseCaseProps
props: GetCoursesByCodeUseCaseProps,
suppressNotFoundError: boolean = false
): Promise<Course[]> {
if (
props.length !==
Expand All @@ -26,7 +28,7 @@ export async function getCoursesByCodeUseCase(
where: props,
relations: ['recommendedGrades', 'methods', 'schedules'],
})
if (res.length !== props.length)
if (res.length !== props.length && !suppressNotFoundError)
throw new NotFoundError(
'指定されたidのコースが見つかりませんでした',
undefined,
Expand Down

0 comments on commit ebebeac

Please sign in to comment.