diff --git a/__tests__/grpc/timetable.service.test.ts b/__tests__/grpc/timetable.service.test.ts index 1e9ec09..f3b00ce 100644 --- a/__tests__/grpc/timetable.service.test.ts +++ b/__tests__/grpc/timetable.service.test.ts @@ -336,10 +336,11 @@ describe('deleteRegisteredCourse', () => { test('deleteRegisteredCourse', (done) => { const ids = [v4(), v4()] mocked(deleteRegisteredCoursesUseCase).mockImplementation(async (i) => { - expect(i).toEqual(deepContaining(ids)) + expect(i.ids).toEqual(deepContaining(ids)) + expect(i.userId).toEqual(userId) }) - client.deleteRegisteredCourses({ ids }, (err, res) => { + client.deleteRegisteredCourses({ userId, ids }, (err, res) => { expect(err).toBeNull() done() }) @@ -351,7 +352,7 @@ describe('deleteRegisteredCourse', () => { throw new NotFoundError('指定された講義は見つかりませんでした') }) - client.deleteRegisteredCourses({ ids }, (err, res) => { + client.deleteRegisteredCourses({ userId, ids }, (err, res) => { expect(err?.code).toBe(Status.NOT_FOUND) done() }) @@ -361,7 +362,7 @@ describe('deleteRegisteredCourse', () => { mocked(deleteRegisteredCoursesUseCase).mockImplementation( throwUnexpectedError ) - client.deleteRegisteredCourses({ ids }, (err, res) => { + client.deleteRegisteredCourses({ userId, ids }, (err, res) => { expect(err?.code).toBeTruthy() done() }) @@ -372,10 +373,11 @@ describe('deleteTags', () => { test('deleteTags', (done) => { const ids = [v4(), v4()] mocked(deleteTagsUseCase).mockImplementation(async (i) => { - expect(i).toEqual(deepContaining(ids)) + expect(i.ids).toEqual(deepContaining(ids)) + expect(i.userId).toEqual(userId) }) - client.deleteTags({ ids }, (err, res) => { + client.deleteTags({ userId, ids }, (err, res) => { expect(err).toBeNull() done() }) @@ -387,7 +389,7 @@ describe('deleteTags', () => { throw new NotFoundError('指定されたタグは見つかりませんでした') }) - client.deleteTags({ ids }, (err, res) => { + client.deleteTags({ userId, ids }, (err, res) => { expect(err?.code).toBe(Status.NOT_FOUND) done() }) @@ -397,7 +399,7 @@ describe('deleteTags', () => { const ids = [v4(), v4()] mocked(deleteTagsUseCase).mockImplementation(throwUnexpectedError) - client.deleteTags({ ids }, (err, res) => { + client.deleteTags({ userId, ids }, (err, res) => { expect(err?.code).toBeTruthy() done() }) diff --git a/__tests__/usecase/deleteRegisteredCourses.test.ts b/__tests__/usecase/deleteRegisteredCourses.test.ts index 81f52f6..a0ae8e0 100644 --- a/__tests__/usecase/deleteRegisteredCourses.test.ts +++ b/__tests__/usecase/deleteRegisteredCourses.test.ts @@ -60,14 +60,20 @@ beforeAll(async () => { test('既に存在するデータを正しく削除できる', () => { return expect( - deleteRegisteredCoursesUseCase(data.map((c) => c.id)) + deleteRegisteredCoursesUseCase({ userId, ids: data.map((c) => c.id) }) ).resolves.toBeUndefined() }) test('存在しないデータはエラーになる', () => { - return expect(deleteRegisteredCoursesUseCase([v4()])).rejects.toThrow( - NotFoundError - ) + return expect( + deleteRegisteredCoursesUseCase({ userId, ids: [v4()] }) + ).rejects.toThrow(NotFoundError) +}) + +test('存在しないデータはエラーになる', () => { + return expect( + deleteRegisteredCoursesUseCase({ userId: v4(), ids: data.map((c) => c.id) }) + ).rejects.toThrow(NotFoundError) }) afterAll(disconnectDatabase) diff --git a/__tests__/usecase/deleteTags.test.ts b/__tests__/usecase/deleteTags.test.ts index dd8f697..c613016 100644 --- a/__tests__/usecase/deleteTags.test.ts +++ b/__tests__/usecase/deleteTags.test.ts @@ -23,12 +23,20 @@ beforeAll(async () => { test('正しく削除できる', () => { return expect( - deleteTagsUseCase(data.map((t) => t.id)) + deleteTagsUseCase({ userId, ids: data.map((t) => t.id) }) ).resolves.toBeUndefined() }) test('存在しないデータでエラーになる', () => { - return expect(deleteTagsUseCase([v4()])).rejects.toThrow(NotFoundError) + return expect(deleteTagsUseCase({ userId, ids: [v4()] })).rejects.toThrow( + NotFoundError + ) +}) + +test('存在しないデータでエラーになる', () => { + return expect( + deleteTagsUseCase({ userId: v4(), ids: data.map((t) => t.id) }) + ).rejects.toThrow(NotFoundError) }) afterAll(disconnectDatabase) diff --git a/protos/TimetableService.proto b/protos/TimetableService.proto index 1cfd51c..8694751 100644 --- a/protos/TimetableService.proto +++ b/protos/TimetableService.proto @@ -72,12 +72,14 @@ message UpdateTagsResponse { message DeleteRegisteredCoursesRequest { - repeated string ids = 1; + string userId = 1; + repeated string ids = 2; } message DeleteRegisteredCoursesResponse {} message DeleteTagsRequest { - repeated string ids = 1; + string userId = 1; + repeated string ids = 2; } message DeleteTagsResponse {} \ No newline at end of file diff --git a/src/grpc/timetable.service.ts b/src/grpc/timetable.service.ts index 7da9ba9..f02b6bb 100644 --- a/src/grpc/timetable.service.ts +++ b/src/grpc/timetable.service.ts @@ -113,7 +113,7 @@ export const timetableService: GrpcServer = { }, async deleteRegisteredCourses({ request }, callback) { try { - await deleteRegisteredCoursesUseCase(request.ids) + await deleteRegisteredCoursesUseCase(request) callback(null, DeleteRegisteredCoursesResponse.create({})) } catch (e) { callback(toGrpcError(e)) @@ -121,7 +121,7 @@ export const timetableService: GrpcServer = { }, async deleteTags({ request }, callback) { try { - await deleteTagsUseCase(request.ids) + await deleteTagsUseCase(request) callback(null, DeleteTagsResponse.create({})) } catch (e) { callback(toGrpcError(e)) diff --git a/src/usecase/deleteRegisteredCourses.ts b/src/usecase/deleteRegisteredCourses.ts index 4b6b3cf..341f115 100644 --- a/src/usecase/deleteRegisteredCourses.ts +++ b/src/usecase/deleteRegisteredCourses.ts @@ -2,11 +2,16 @@ import { getConnection } from 'typeorm' import { RegisteredCourse } from '../database/model/registeredCourse' import { NotFoundError } from '../error' -type Input = string[] +type Input = { + userId: string + ids: string[] +} -export async function deleteRegisteredCoursesUseCase(ids: Input) { +export async function deleteRegisteredCoursesUseCase({ userId, ids }: Input) { const repository = getConnection().getRepository(RegisteredCourse) - const target = await repository.findByIds(ids) + const target = await repository.find({ + where: ids.map((id) => ({ id, userId })), + }) if (target.length !== ids.length) throw new NotFoundError( '指定された講義は見つかりませんでした', diff --git a/src/usecase/deleteTags.ts b/src/usecase/deleteTags.ts index 0d2674f..b68cc26 100644 --- a/src/usecase/deleteTags.ts +++ b/src/usecase/deleteTags.ts @@ -2,11 +2,16 @@ import { getConnection } from 'typeorm' import { Tag } from '../database/model/tag' import { NotFoundError } from '../error' -type Input = string[] +type Input = { + userId: string + ids: string[] +} -export async function deleteTagsUseCase(ids: Input) { +export async function deleteTagsUseCase({ userId, ids }: Input) { const repository = getConnection().getRepository(Tag) - const target = await repository.findByIds(ids) + const target = await repository.find({ + where: ids.map((id) => ({ id, userId })), + }) if (target.length !== ids.length) throw new NotFoundError( '指定されたタグは見つかりませんでした', diff --git a/src/usecase/updateRegisteredCourses.ts b/src/usecase/updateRegisteredCourses.ts index 755e1ea..ff9959e 100644 --- a/src/usecase/updateRegisteredCourses.ts +++ b/src/usecase/updateRegisteredCourses.ts @@ -47,7 +47,9 @@ export async function updateRegisteredCourseUseCase(courses: Input[]) { }) try { const repository = getConnection().getRepository(RegisteredCourse) - const target = await repository.findByIds(courses.map((c) => c.id)) + const target = await repository.find({ + where: courses.map(({ id, userId }) => ({ id, userId })), + }) if (target.length !== courses.length) throw new NotFoundError( '指定された講義は見つかりませんでした', diff --git a/src/usecase/updateTags.ts b/src/usecase/updateTags.ts index ca05fc6..e96c39b 100644 --- a/src/usecase/updateTags.ts +++ b/src/usecase/updateTags.ts @@ -10,7 +10,9 @@ type Input = { export async function updateTagsUseCase(tags: Input[]) { const repository = getConnection().getRepository(Tag) - const target = await repository.findByIds(tags.map((t) => t.id)) + const target = await repository.find({ + where: tags.map(({ id, userId }) => ({ id, userId })), + }) if (target.length !== tags.length) throw new NotFoundError( '指定されたタグが見つかりません',