@@ -4,9 +4,9 @@ import { emitResult, emitBare } from "bailian-cli-runtime";
44const API = {
55 loginInfo : "zeldaEasy.cornerstone-portal.cs-console.loginInfo" ,
66 initSpace : "zeldaEasy.bailian-dash-workspace.space.initSpace" ,
7- queryBuyResult : "zeldaEasy.broadscope- bailian.bill.queryBuyPostpaidResult" ,
8- commodityOrderInfo : "zeldaEasy.broadscope- bailian.bill.postpaidCommodityOrderInfo" ,
9- buyCommodity : "zeldaEasy.broadscope- bailian.bill.buyPostpaidCommodity" ,
7+ queryBuyResult : "zeldaEasy.bailian-commerce .bill.queryBuyPostpaidResult" ,
8+ commodityOrderInfo : "zeldaEasy.bailian-commerce .bill.postpaidCommodityOrderInfo" ,
9+ buyCommodity : "zeldaEasy.bailian-commerce .bill.buyPostpaidCommodity" ,
1010} as const ;
1111
1212const POLL_INTERVAL_MS = 1000 ;
@@ -16,10 +16,13 @@ function sleep(ms: number): Promise<void> {
1616 return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
1717}
1818
19+ function extractData ( resp : any ) : any {
20+ return resp ?. data ?. DataV2 ?. data ?. data ;
21+ }
22+
1923interface CommodityItem {
20- commodity ?: string ;
24+ commodityCode ?: string ;
2125 status ?: number ;
22- startDate ?: string ;
2326}
2427
2528export default defineCommand ( {
@@ -36,9 +39,21 @@ export default defineCommand({
3639 emitResult (
3740 {
3841 apis : [
39- { step : 1 , api : API . loginInfo , description : "Check login & workspace status" } ,
40- { step : 2 , api : API . initSpace , description : "Initialize workspace (if needed)" } ,
41- { step : 3 , api : API . queryBuyResult , description : "Query postpaid order status" } ,
42+ {
43+ step : 1 ,
44+ api : API . loginInfo ,
45+ description : "Check login & workspace status" ,
46+ } ,
47+ {
48+ step : 2 ,
49+ api : API . initSpace ,
50+ description : "Initialize workspace (if needed)" ,
51+ } ,
52+ {
53+ step : 3 ,
54+ api : API . queryBuyResult ,
55+ description : "Query postpaid order status" ,
56+ } ,
4257 {
4358 step : 4 ,
4459 api : API . commodityOrderInfo ,
@@ -59,15 +74,21 @@ export default defineCommand({
5974 const verbose = settings . verbose ;
6075 const callApi = async ( api : string ) => {
6176 if ( verbose ) process . stderr . write ( `> ${ api } \n` ) ;
62- const resp = await ctx . client . console < any > ( api , { } ) ;
63- if ( verbose ) process . stderr . write ( `< ${ JSON . stringify ( resp ) } \n` ) ;
64- return resp ;
77+ try {
78+ const resp = await ctx . client . console < any > ( api , { } ) ;
79+ if ( verbose ) process . stderr . write ( `< ${ JSON . stringify ( resp ) } \n` ) ;
80+ return resp ;
81+ } catch ( err ) {
82+ if ( verbose ) process . stderr . write ( `< ERROR: ${ err instanceof Error ? err . message : err } \n` ) ;
83+ throw err ;
84+ }
6585 } ;
6686
6787 // Step 1: Check login info
6888 emitBare ( "Checking workspace status..." ) ;
69- const loginInfo = await callApi ( API . loginInfo ) ;
70- const spaceInited = loginInfo ?. spaceInited === true ;
89+ const loginResp = await callApi ( API . loginInfo ) ;
90+ const loginData = extractData ( loginResp ) ;
91+ const spaceInited = loginData ?. spaceInited === true ;
7192
7293 // Step 2: Init space if needed
7394 if ( ! spaceInited ) {
@@ -90,7 +111,8 @@ async function ensureCommoditiesActive(call: ApiCall, format: "text" | "json"):
90111 let buyResult : string | undefined ;
91112 for ( let i = 0 ; i < MAX_POLL_ATTEMPTS ; i ++ ) {
92113 const resp = await call ( API . queryBuyResult ) ;
93- buyResult = resp ?. result ;
114+ const data = extractData ( resp ) ;
115+ buyResult = typeof data === "string" ? data : data ?. result ;
94116 if ( buyResult !== "buying" ) break ;
95117 if ( i === 0 ) emitBare ( "Service activation in progress, polling..." ) ;
96118 await sleep ( POLL_INTERVAL_MS ) ;
@@ -108,43 +130,48 @@ async function ensureCommoditiesActive(call: ApiCall, format: "text" | "json"):
108130 await checkAndActivateCommodities ( call , format ) ;
109131}
110132
133+ function extractCommodities ( resp : any ) : CommodityItem [ ] {
134+ const data = extractData ( resp ) ;
135+ return Array . isArray ( data ) ? data : [ ] ;
136+ }
137+
111138async function checkAndActivateCommodities ( call : ApiCall , format : "text" | "json" ) : Promise < void > {
112139 const resp = await call ( API . commodityOrderInfo ) ;
113- const items : CommodityItem [ ] = resp ?. result ?? [ ] ;
140+ const items = extractCommodities ( resp ) ;
114141
115142 const overdue = items . filter ( ( c ) => c . status === 11 ) ;
116143 if ( overdue . length > 0 ) {
117144 emitBare ( "Warning: Some services are overdue:" ) ;
118- for ( const c of overdue ) emitBare ( ` - ${ c . commodity } ` ) ;
145+ for ( const c of overdue ) emitBare ( ` - ${ c . commodityCode } ` ) ;
119146 }
120147
121148 const notActivated = items . filter ( ( c ) => c . status === 1 ) ;
122149 if ( notActivated . length > 0 ) {
123- emitBare ( " Activating postpaid services..." ) ;
150+ emitBare ( ` Activating ${ notActivated . length } postpaid services...` ) ;
124151 await call ( API . buyCommodity ) ;
125152 await pollCommoditiesUntilActive ( call , format ) ;
126153 return ;
127154 }
128155
129156 const active = items . filter ( ( c ) => c . status === 10 ) ;
130- emitResult ( { status : "ready" , activeServices : active . map ( ( c ) => c . commodity ) } , format ) ;
157+ emitResult ( { status : "ready" , activeServices : active . map ( ( c ) => c . commodityCode ) } , format ) ;
131158}
132159
133160async function pollCommoditiesUntilActive ( call : ApiCall , format : "text" | "json" ) : Promise < void > {
134161 emitBare ( "Waiting for services to activate..." ) ;
135162 for ( let i = 0 ; i < MAX_POLL_ATTEMPTS ; i ++ ) {
136163 const resp = await call ( API . commodityOrderInfo ) ;
137- const items : CommodityItem [ ] = resp ?. result ?? [ ] ;
164+ const items = extractCommodities ( resp ) ;
138165
139166 const pending = items . filter ( ( c ) => c . status !== 10 && c . status !== 11 ) ;
140167 if ( pending . length === 0 ) {
141168 const overdue = items . filter ( ( c ) => c . status === 11 ) ;
142169 if ( overdue . length > 0 ) {
143170 emitBare ( "Warning: Some services are overdue:" ) ;
144- for ( const c of overdue ) emitBare ( ` - ${ c . commodity } ` ) ;
171+ for ( const c of overdue ) emitBare ( ` - ${ c . commodityCode } ` ) ;
145172 }
146173 const active = items . filter ( ( c ) => c . status === 10 ) ;
147- emitResult ( { status : "ready" , activeServices : active . map ( ( c ) => c . commodity ) } , format ) ;
174+ emitResult ( { status : "ready" , activeServices : active . map ( ( c ) => c . commodityCode ) } , format ) ;
148175 return ;
149176 }
150177 await sleep ( POLL_INTERVAL_MS ) ;
0 commit comments