75 lines
2.2 KiB
TypeScript
75 lines
2.2 KiB
TypeScript
import { createEkbClient } from "../../core/auth/ekb.js";
|
|
import { getUnityClient } from "../../core/auth/easo.js";
|
|
import type { CorpConfig } from "../../core/config/types.js";
|
|
import {
|
|
listOpenApiKeys,
|
|
getOpenConfig,
|
|
listClientGoods,
|
|
searchStoreGoods,
|
|
getGoodsDetail,
|
|
} from "./api.js";
|
|
import type {
|
|
OpenApiKey,
|
|
OpenConfig,
|
|
ClientGoods,
|
|
StoreGoodsItem,
|
|
GoodsDetail,
|
|
} from "./types.js";
|
|
|
|
export async function fetchOpenApiKeys(corp: CorpConfig): Promise<OpenApiKey[]> {
|
|
const ekbClient = await createEkbClient(corp);
|
|
const corpId = corp.corpId || corp.credentials.corpId;
|
|
return listOpenApiKeys(ekbClient, corpId);
|
|
}
|
|
|
|
export async function fetchOpenConfig(corp: CorpConfig): Promise<OpenConfig> {
|
|
const ekbClient = await createEkbClient(corp);
|
|
const corpId = corp.corpId || corp.credentials.corpId;
|
|
return getOpenConfig(ekbClient, corpId);
|
|
}
|
|
|
|
export async function fetchClientGoods(
|
|
corp: CorpConfig,
|
|
options: { keyword?: string; isAll?: boolean } = {}
|
|
): Promise<ClientGoods[]> {
|
|
const { client: unityClient } = await getUnityClient(corp);
|
|
const corpId = corp.corpId || corp.credentials.corpId;
|
|
const items = await listClientGoods(unityClient, corpId, {
|
|
name: options.keyword,
|
|
isAll: options.isAll,
|
|
});
|
|
|
|
if (options.keyword && options.keyword.trim()) {
|
|
const kw = options.keyword.trim().toLowerCase();
|
|
return items.filter(
|
|
(item) =>
|
|
item.name.toLowerCase().includes(kw) ||
|
|
(item.summary && item.summary.toLowerCase().includes(kw))
|
|
);
|
|
}
|
|
|
|
return items;
|
|
}
|
|
|
|
export async function searchGoods(
|
|
corp: CorpConfig,
|
|
options: { keyword?: string; start?: number; count?: number } = {}
|
|
): Promise<{ total: number; items: StoreGoodsItem[] }> {
|
|
const { client: unityClient } = await getUnityClient(corp);
|
|
const corpId = corp.corpId || corp.credentials.corpId;
|
|
return searchStoreGoods(unityClient, corpId, {
|
|
name: options.keyword,
|
|
start: options.start,
|
|
count: options.count,
|
|
});
|
|
}
|
|
|
|
export async function fetchGoodsDetail(
|
|
corp: CorpConfig,
|
|
goodsId: string
|
|
): Promise<GoodsDetail> {
|
|
const { client: unityClient } = await getUnityClient(corp);
|
|
const corpId = corp.corpId || corp.credentials.corpId;
|
|
return getGoodsDetail(unityClient, corpId, goodsId);
|
|
}
|