saito.k 5860da285e Merged PR 899: 階層構造変更API修正
## 概要
[Task4179: 階層構造変更API修正](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/4179)

- 階層構造変更API修正
  - 第四⇔第五の切り替え処理の時は国・リージョンの一致を確認しない
  - 不要なエラーコード・エラーハンドリングを削除
- テスト修正

## レビューポイント
- 修正箇所の認識は合っているか
- テストケースは足りているか

## 動作確認状況
- ローカルで確認
  - 国の違うDealerに付け替える処理を行い成功した
- 行った修正がデグレを発生させていないことを確認できるか
  - 具体的にどのような確認をしたか
    - 国の違う第四⇔第五間の付け替えのテストが成功することを確認
    - ほかのテストが成功することを確認

## 補足
- 相談、参考資料などがあれば
2024-05-17 06:49:26 +00:00

187 lines
4.8 KiB
TypeScript

import { createAsyncThunk } from "@reduxjs/toolkit";
import { getAccessToken } from "features/auth";
import type { RootState } from "../../../app/store";
import { getTranslationID } from "../../../translation";
import { openSnackbar } from "../../ui/uiSlice";
import {
Account,
AccountsApi,
GetPartnerLicensesResponse,
} from "../../../api/api";
import { Configuration } from "../../../api/configuration";
import { ErrorObject, createErrorObject } from "../../../common/errors";
export const getMyAccountAsync = createAsyncThunk<
// 正常時の戻り値の型
Account,
// 引数
void,
{
// rejectした時の返却値の型
rejectValue: {
error: ErrorObject;
};
}
>("licenses/getMyAccountAsync", async (args, thunkApi) => {
// apiのConfigurationを取得する
const { getState } = thunkApi;
const state = getState() as RootState;
const { configuration } = state.auth;
const accessToken = getAccessToken(state.auth);
const config = new Configuration(configuration);
const accountsApi = new AccountsApi(config);
try {
const getMyAccountResponse = await accountsApi.getMyAccount({
headers: { authorization: `Bearer ${accessToken}` },
});
// accountIDを返す
return getMyAccountResponse.data.account;
} catch (e) {
// e ⇒ errorObjectに変換"
const error = createErrorObject(e);
thunkApi.dispatch(
openSnackbar({
level: "error",
message: getTranslationID("common.message.internalServerError"),
})
);
return thunkApi.rejectWithValue({ error });
}
});
// サービスAPIからアカウント情報をもらう
export const getPartnerLicenseAsync = createAsyncThunk<
// 正常時の戻り値の型
GetPartnerLicensesResponse,
// 引数
{
limit: number;
offset: number;
accountId: number | undefined;
},
{
// rejectした時の返却値の型
rejectValue: {
error: ErrorObject;
};
}
>("licenses/getPartnerLicenseAsync", async (args, thunkApi) => {
// apiのConfigurationを取得する
const { getState } = thunkApi;
const state = getState() as RootState;
const { configuration } = state.auth;
const accessToken = getAccessToken(state.auth);
const config = new Configuration(configuration);
const accountsApi = new AccountsApi(config);
try {
const getMyAccountResponse = await accountsApi.getMyAccount({
headers: { authorization: `Bearer ${accessToken}` },
});
const accountId =
args.accountId ?? getMyAccountResponse.data.account.accountId;
const getPartnerLicenseResponse = await accountsApi.getPartnerLicenses(
{
limit: args.limit,
offset: args.offset,
accountId,
},
{ headers: { authorization: `Bearer ${accessToken}` } }
);
return getPartnerLicenseResponse.data;
} catch (e) {
// e ⇒ errorObjectに変換"
const error = createErrorObject(e);
thunkApi.dispatch(
openSnackbar({
level: "error",
message: getTranslationID("common.message.internalServerError"),
})
);
return thunkApi.rejectWithValue({ error });
}
});
export const switchParentAsync = createAsyncThunk<
{
/* Empty Object */
},
{
// パラメータ
to: number;
children: number[];
},
{
// rejectした時の返却値の型
rejectValue: {
error: ErrorObject;
};
}
>("accounts/switchParentAsync", async (args, thunkApi) => {
// apiのConfigurationを取得する
const { getState } = thunkApi;
const state = getState() as RootState;
const { configuration } = state.auth;
const accessToken = getAccessToken(state.auth);
const config = new Configuration(configuration);
const accountsApi = new AccountsApi(config);
const { to, children } = args;
try {
await accountsApi.switchParent(
{
to,
children,
},
{
headers: { authorization: `Bearer ${accessToken}` },
}
);
thunkApi.dispatch(
openSnackbar({
level: "info",
message: getTranslationID("common.message.success"),
})
);
return {};
} catch (e) {
// e ⇒ errorObjectに変換"
const error = createErrorObject(e);
let errorMessage = getTranslationID("common.message.internalServerError");
// TODO:エラー処理
if (error.code === "E017001") {
errorMessage = getTranslationID(
"changeOwnerPopup.message.accountNotFoundError"
);
}
if (error.code === "E017002") {
errorMessage = getTranslationID(
"changeOwnerPopup.message.hierarchyMismatchError"
);
}
if (error.code === "E017003") {
errorMessage = getTranslationID(
"changeOwnerPopup.message.regionMismatchError"
);
}
thunkApi.dispatch(
openSnackbar({
level: "error",
message: errorMessage,
})
);
return thunkApi.rejectWithValue({ error });
}
});