diff --git a/dictation_server/src/features/accounts/accounts.service.spec.ts b/dictation_server/src/features/accounts/accounts.service.spec.ts index 7335dee..baefc94 100644 --- a/dictation_server/src/features/accounts/accounts.service.spec.ts +++ b/dictation_server/src/features/accounts/accounts.service.spec.ts @@ -3920,7 +3920,7 @@ describe('deleteWorktype', () => { source = null; }); - it('WorktypeIDを削除できる', async () => { + it('WorktypeIDを削除できること', async () => { const module = await makeTestingModule(source); // 第五階層のアカウント作成 const { account, admin } = await makeTestAccount(source, { tier: 5 }); @@ -3966,6 +3966,51 @@ describe('deleteWorktype', () => { } }); + it('指定されたWorktypeIDがアカウントのActiveWorktypeIDの場合、削除できること', async () => { + const module = await makeTestingModule(source); + // 第五階層のアカウント作成 + const { account, admin } = await makeTestAccount(source, { tier: 5 }); + + const service = module.get(AccountsService); + const context = makeContext(admin.external_id); + + const { id: worktypeId1 } = await createWorktype( + source, + account.id, + 'worktype1', + 'description1', + true, + ); + await createOptionItems(source, worktypeId1); + + // 作成したデータを確認 + { + const worktypes = await getWorktypes(source, account.id); + const optionItems = await getOptionItems(source); + const accounts = await getAccounts(source); + + expect(worktypes.length).toBe(1); + expect(worktypes[0].id).toBe(worktypeId1); + expect(worktypes[0].custom_worktype_id).toBe('worktype1'); + expect(optionItems.length).toBe(10); + expect(accounts.length).toBe(1); + expect(accounts[0].active_worktype_id).toBe(worktypeId1); + } + + await service.deleteWorktype(context, admin.external_id, worktypeId1); + + //実行結果を確認 + { + const worktypes = await getWorktypes(source, account.id); + const optionItems = await getOptionItems(source); + const accounts = await getAccounts(source); + expect(worktypes.length).toBe(0); + expect(optionItems.length).toBe(0); + expect(accounts.length).toBe(1); + expect(accounts[0].active_worktype_id).toBe(null); + } + }); + it('指定したWorktypeIDが登録されていない場合、400エラーとなること', async () => { const module = await makeTestingModule(source); // 第五階層のアカウント作成 diff --git a/dictation_server/src/repositories/worktypes/worktypes.repository.service.ts b/dictation_server/src/repositories/worktypes/worktypes.repository.service.ts index 7fef32e..88f3bd3 100644 --- a/dictation_server/src/repositories/worktypes/worktypes.repository.service.ts +++ b/dictation_server/src/repositories/worktypes/worktypes.repository.service.ts @@ -176,6 +176,19 @@ export class WorktypesRepositoryService { throw new WorktypeIdNotFoundError(`Worktype is not found. id: ${id}`); } + // アカウントのActiveWorktypeIDが削除対象のワークタイプIDの場合はActiveWorktypeIDをnullに更新 + const accountRepo = entityManager.getRepository(Account); + const account = await accountRepo.findOne({ + where: { id: accountId }, + }); + + if (account?.active_worktype_id === id) { + await accountRepo.update( + { id: accountId }, + { active_worktype_id: null }, + ); + } + // ワークタイプがワークフローに紐づいている場合はエラー const workflowRepo = entityManager.getRepository(Workflow); const workflows = await workflowRepo.find({