Merged PR 114: [MISOチーム] ユーザ追加時のAuthorIDの重複チェックがエラーの場合にAzureにアカウント追加しないよう修正する

## 概要
[Task1765: [MISOチーム] ユーザ追加時のAuthorIDの重複チェックがエラーの場合にAzureにアカウント追加しないよう修正する](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/1765)

タスク 1765: [MISOチーム] ユーザ追加時のAuthorIDの重複チェックがエラーの場合にAzureにアカウント追加しないよう修正する
・ユーザ追加時、Azureへのユーザ登録前に同一アカウント内のAuthorIDの重複チェックを追加。

## レビューポイント
処理に問題がないか。

## UIの変更
なし

## 動作確認状況
ローカルでユーザ追加APIを実行し、同一AuthorIDの際にエラーが出ることを確認。

## 補足
なし
This commit is contained in:
oura.a 2023-05-25 06:54:50 +00:00
parent 24a784b02f
commit 4ec3d7c51f
4 changed files with 95 additions and 3 deletions

View File

@ -21,6 +21,7 @@ export type UsersRepositoryMockValue = {
createNormalUser: User | Error;
findSameAccountUsers: User[] | Error;
findUserByExternalId: User | Error;
existsAuthorId: boolean | Error;
};
export type AdB2cMockValue = {
@ -188,6 +189,7 @@ export const makeUsersRepositoryMock = (value: UsersRepositoryMockValue) => {
createNormalUser,
findSameAccountUsers,
findUserByExternalId,
existsAuthorId,
} = value;
const aIdError = new authorIdError('ER_DUP_ENTRY');
@ -219,6 +221,10 @@ export const makeUsersRepositoryMock = (value: UsersRepositoryMockValue) => {
findUserByExternalId instanceof Error
? jest.fn<Promise<void>, []>().mockRejectedValue(findUserByExternalId)
: jest.fn<Promise<User>, []>().mockResolvedValue(findUserByExternalId),
existsAuthorId:
existsAuthorId instanceof Error
? jest.fn<Promise<void>, []>().mockRejectedValue(existsAuthorId)
: jest.fn<Promise<boolean>, []>().mockResolvedValue(existsAuthorId),
};
};
@ -332,5 +338,6 @@ export const makeDefaultUsersRepositoryMockValue =
createNormalUser: newUser,
findSameAccountUsers: [user1, user2],
findUserByExternalId: newUser,
existsAuthorId: false,
};
};

View File

@ -460,7 +460,46 @@ it('メールアドレスが重複している場合、エラーとなる。', a
new HttpException(makeErrorResponse('E010301'), HttpStatus.BAD_REQUEST),
);
});
it('AuthorIDが重複している場合、エラーとなる。', async () => {
it('AuthorIDが重複している場合、エラーとなる。(AuthorID重複チェックでエラー)', async () => {
const cryptoMockValue = makeDefaultCryptoMockValue();
const usersRepositoryMockValue = makeDefaultUsersRepositoryMockValue();
const adb2cParam = makeDefaultAdB2cMockValue();
const sendgridMockValue = makeDefaultSendGridlValue();
const configMockValue = makeDefaultConfigValue();
usersRepositoryMockValue.createNormalUser = new Error();
usersRepositoryMockValue.existsAuthorId = true;
const service = await makeUsersServiceMock(
cryptoMockValue,
usersRepositoryMockValue,
adb2cParam,
sendgridMockValue,
configMockValue,
);
const name = 'test_user8';
const role = 'Author';
const email = 'test8@example.co.jp';
const autoRenew = true;
const licenseAlert = true;
const notification = true;
const authorId = 'testID';
const token: AccessToken = { userId: '0001', role: '' };
await expect(
service.createUser(
token,
name,
role,
email,
autoRenew,
licenseAlert,
notification,
authorId,
),
).rejects.toEqual(
new HttpException(makeErrorResponse('E010302'), HttpStatus.BAD_REQUEST),
);
});
it('AuthorIDが重複している場合、エラーとなる。(insert失敗)', async () => {
const cryptoMockValue = makeDefaultCryptoMockValue();
const usersRepositoryMockValue = makeDefaultUsersRepositoryMockValue();
const adb2cParam = makeDefaultAdB2cMockValue();
@ -476,9 +515,9 @@ it('AuthorIDが重複している場合、エラーとなる。', async () => {
sendgridMockValue,
configMockValue,
);
const name = 'test_user8';
const name = 'test_user9';
const role = 'Author';
const email = 'test8@example.co.jp';
const email = 'test9@example.co.jp';
const autoRenew = true;
const licenseAlert = true;
const notification = true;

View File

@ -114,6 +114,28 @@ export class UsersService {
const accountId = adminUser.account_id;
//authorIdが重複していないかチェックする
if (authorId) {
let isAuthorIdDuplicated = false;
try {
isAuthorIdDuplicated = await this.usersRepository.existsAuthorId(
accountId,
authorId,
);
} catch (e) {
throw new HttpException(
makeErrorResponse('E009999'),
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
if (isAuthorIdDuplicated) {
throw new HttpException(
makeErrorResponse('E010302'),
HttpStatus.BAD_REQUEST,
);
}
}
// ランダムなパスワードを生成する
const ramdomPassword = makePassword();

View File

@ -122,6 +122,30 @@ export class UsersRepositoryService {
return user;
}
/**
* AuthorIdが既に存在するか確認する
* @param user
* @returns true false
*/
async existsAuthorId(
accountId: number,
authorId: string,
): Promise<boolean | undefined> {
const user = await this.dataSource.getRepository(User).findOne({
where: [
{
account_id: accountId,
author_id: authorId,
},
],
});
if (user) {
return true;
}
return false;
}
/**
*
* @param user