99 getProject ,
1010 getSession ,
1111 imessageRedirectUrl ,
12+ listSpectrumUsers ,
1213 provisionImessage ,
1314 regenerateProjectSecret ,
1415 togglePlatform ,
@@ -77,6 +78,14 @@ export async function POST(req: Request) {
7778 openaiKey ?: unknown ;
7879 userPhone ?: unknown ;
7980 } ;
81+ console . log (
82+ "[provision] body received: openaiKey=" ,
83+ typeof body . openaiKey === "string"
84+ ? `present(len=${ body . openaiKey . length } )`
85+ : typeof body . openaiKey ,
86+ "userPhone=" ,
87+ typeof body . userPhone === "string" ? `"${ body . userPhone } "` : typeof body . userPhone ,
88+ ) ;
8089 if ( typeof body . userPhone === "string" && body . userPhone . trim ( ) . length > 0 ) {
8190 userPhone = body . userPhone . trim ( ) ;
8291 }
@@ -113,6 +122,17 @@ export async function POST(req: Request) {
113122 const ownerPhone =
114123 userPhone ?? ( typeof session . user . phoneNumber === "string" ? session . user . phoneNumber : null ) ;
115124
125+ if ( ! ownerPhone ) {
126+ return NextResponse . json (
127+ {
128+ error :
129+ "We need your phone number to assign you a Spectrum iMessage bot. Add it and continue." ,
130+ reason : "phone_required" ,
131+ } ,
132+ { status : 422 } ,
133+ ) ;
134+ }
135+
116136 const db = getDb ( ) ;
117137
118138 const existing = await db
@@ -150,17 +170,6 @@ export async function POST(req: Request) {
150170 `[provision] dashboard id=${ project . id } cloud spectrumProjectId=${ details . spectrumProjectId ?? "(missing — using dashboard id)" } ` ,
151171 ) ;
152172
153- if ( ! ownerPhone ) {
154- return NextResponse . json (
155- {
156- error :
157- "We need your phone number to assign you a Spectrum iMessage bot. Add it and continue." ,
158- reason : "phone_required" ,
159- } ,
160- { status : 422 } ,
161- ) ;
162- }
163-
164173 const fullName = session . user . name ?. trim ( ) ?? "" ;
165174 const [ firstName , ...rest ] = fullName . split ( / \s + / ) ;
166175 const userResp = await createSpectrumUser ( bearer , project . id , {
@@ -172,29 +181,46 @@ export async function POST(req: Request) {
172181 } ) ;
173182 console . log ( "[provision] create-spectrum-user response:" , JSON . stringify ( userResp ) ) ;
174183
175- let line = await provisionImessage ( bearer , cloudProjectId , projectSecret ) . catch (
176- ( err : unknown ) => {
184+ let line : { id : string ; phoneNumber : string } | null = null ;
185+
186+ const fromUser = pickPhoneFrom ( userResp , ownerPhone ) ;
187+ if ( fromUser ) {
188+ line = { id : userResp . user ?. id ?? `${ cloudProjectId } :imessage` , phoneNumber : fromUser } ;
189+ console . log ( `[provision] picked bot number from user-add response: ${ fromUser } ` ) ;
190+ }
191+
192+ if ( ! line ) {
193+ try {
194+ line = await provisionImessage ( bearer , cloudProjectId , projectSecret ) ;
195+ console . log ( `[provision] picked bot number from provisionImessage: ${ line . phoneNumber } ` ) ;
196+ } catch ( err ) {
177197 console . warn (
178- "[provision] provisionImessage fallback after user-add :" ,
198+ "[provision] provisionImessage didn't yield a number :" ,
179199 err instanceof Error ? err . message : err ,
180200 ) ;
181- return null ;
182- } ,
183- ) ;
201+ }
202+ }
184203
185204 if ( ! line ) {
186- const fromUser = pickPhoneFrom ( userResp , ownerPhone ) ;
187- if ( fromUser ) {
188- line = { id : userResp . user ?. id ?? `${ cloudProjectId } :imessage` , phoneNumber : fromUser } ;
189- } else {
190- throw new SpectrumError (
191- "Couldn't read the assigned iMessage number from Spectrum's response." ,
192- 500 ,
193- userResp ,
194- ) ;
205+ const usersList = await listSpectrumUsers ( bearer , project . id ) ;
206+ if ( usersList ) {
207+ console . log ( "[provision] users list response:" , JSON . stringify ( usersList ) . slice ( 0 , 1500 ) ) ;
208+ const fromList = pickPhoneFrom ( usersList , ownerPhone ) ;
209+ if ( fromList ) {
210+ line = { id : `${ cloudProjectId } :imessage` , phoneNumber : fromList } ;
211+ console . log ( `[provision] picked bot number from users list: ${ fromList } ` ) ;
212+ }
195213 }
196214 }
197215
216+ if ( ! line ) {
217+ throw new SpectrumError (
218+ "Couldn't read the assigned iMessage number from Spectrum. Set SPECTRUM_SHARED_IMESSAGE_NUMBER to the shared inbound number, or check the Spectrum dashboard." ,
219+ 500 ,
220+ { userResp } ,
221+ ) ;
222+ }
223+
198224 const projectSecretBlob = encrypt ( projectSecret ) ;
199225 const openaiBlob = openaiKey ? encrypt ( openaiKey ) : null ;
200226
0 commit comments