97 lines
2.8 KiB
TypeScript
97 lines
2.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 { AccountsApi, SearchPartner, PartnerHierarchy } from "../../../api/api";
|
|
import { Configuration } from "../../../api/configuration";
|
|
import { ErrorObject, createErrorObject } from "../../../common/errors";
|
|
|
|
export const searchPartnersAsync = createAsyncThunk<
|
|
// 正常時の戻り値の型
|
|
SearchPartner[],
|
|
// 引数
|
|
{
|
|
companyName?: string;
|
|
accountId?: number;
|
|
},
|
|
{
|
|
// rejectした時の返却値の型
|
|
rejectValue: {
|
|
error: ErrorObject;
|
|
};
|
|
}
|
|
>("licenses/searchPartners", async (args, thunkApi) => {
|
|
// apiのConfigurationを取得する
|
|
const { companyName, accountId } = args;
|
|
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 searchPartnerResponse = await accountsApi.searchPartners(
|
|
companyName,
|
|
accountId,
|
|
{
|
|
headers: { authorization: `Bearer ${accessToken}` },
|
|
}
|
|
);
|
|
return searchPartnerResponse.data.searchResult;
|
|
} catch (e) {
|
|
// e ⇒ errorObjectに変換"
|
|
const error = createErrorObject(e);
|
|
thunkApi.dispatch(
|
|
openSnackbar({
|
|
level: "error",
|
|
message: getTranslationID("common.message.internalServerError"),
|
|
})
|
|
);
|
|
return thunkApi.rejectWithValue({ error });
|
|
}
|
|
});
|
|
|
|
export const getPartnerHierarchy = createAsyncThunk<
|
|
// 正常時の戻り値の型
|
|
PartnerHierarchy[],
|
|
// 引数
|
|
{
|
|
accountId: number;
|
|
},
|
|
{
|
|
// rejectした時の返却値の型
|
|
rejectValue: {
|
|
error: ErrorObject;
|
|
};
|
|
}
|
|
>("licenses/getPartnerHierarchy", async (args, thunkApi) => {
|
|
// apiのConfigurationを取得する
|
|
const { accountId } = args;
|
|
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 partnerHierarchyResponse = await accountsApi.getPartnerHierarchy(
|
|
accountId,
|
|
{
|
|
headers: { authorization: `Bearer ${accessToken}` },
|
|
}
|
|
);
|
|
return partnerHierarchyResponse.data.accountHierarchy;
|
|
} catch (e) {
|
|
// e ⇒ errorObjectに変換"
|
|
const error = createErrorObject(e);
|
|
thunkApi.dispatch(
|
|
openSnackbar({
|
|
level: "error",
|
|
message: getTranslationID("common.message.internalServerError"),
|
|
})
|
|
);
|
|
return thunkApi.rejectWithValue({ error });
|
|
}
|
|
});
|