@@ -172,6 +172,51 @@ export class ShopService {
172172 }
173173
174174 async purchaseItem ( userId : number , itemId : number , variantId ?: number ) {
175+ console . log ( `[Shop Purchase] Starting purchase for userId: ${ userId } , itemId: ${ itemId } , variantId: ${ variantId || 'none' } ` ) ;
176+
177+ const user = await this . prisma . user . findUnique ( {
178+ where : { userId } ,
179+ select : { email : true } ,
180+ } ) ;
181+
182+ if ( ! user || ! user . email ) {
183+ console . error ( `[Shop Purchase] User email not found for userId: ${ userId } ` ) ;
184+ throw new BadRequestException ( 'User email not found' ) ;
185+ }
186+
187+ console . log ( `[Shop Purchase] Checking verification status for user: ${ userId } , email: ${ user . email } ` ) ;
188+ const externalApiBaseUrl = this . configService . get < string > ( 'EXTERNAL_VERIFICATION_API_URL' , 'https://identity.hackclub.com/api/external' ) ;
189+ const checkUrl = `${ externalApiBaseUrl } /check?email=${ encodeURIComponent ( user . email ) } ` ;
190+ console . log ( `[Shop Purchase] Verification API URL: ${ checkUrl } ` ) ;
191+
192+ try {
193+ const verificationResponse = await fetch ( checkUrl ) ;
194+ console . log ( `[Shop Purchase] Verification API response status: ${ verificationResponse . status } ` ) ;
195+
196+ if ( ! verificationResponse . ok ) {
197+ const errorText = await verificationResponse . text ( ) . catch ( ( ) => 'Unable to read response' ) ;
198+ console . error ( `[Shop Purchase] Verification API returned non-OK status: ${ verificationResponse . status } , response: ${ errorText } ` ) ;
199+ throw new BadRequestException ( 'Failed to verify eligibility. Please try again later.' ) ;
200+ }
201+
202+ const verificationData = await verificationResponse . json ( ) ;
203+ console . log ( `[Shop Purchase] Verification API response data:` , JSON . stringify ( verificationData ) ) ;
204+
205+ if ( verificationData . result !== 'verified_eligible' ) {
206+ console . warn ( `[Shop Purchase] User ${ userId } (${ user . email } ) is not verified_eligible. Result: ${ verificationData . result } ` ) ;
207+ throw new BadRequestException ( 'You must be verified eligible to purchase items from the shop' ) ;
208+ }
209+
210+ console . log ( `[Shop Purchase] User ${ userId } (${ user . email } ) verification check passed` ) ;
211+ } catch ( error ) {
212+ if ( error instanceof BadRequestException ) {
213+ console . log ( `[Shop Purchase] Verification check failed for user ${ userId } : ${ error . message } ` ) ;
214+ throw error ;
215+ }
216+ console . error ( `[Shop Purchase] Error checking verification status for user ${ userId } :` , error ) ;
217+ throw new BadRequestException ( 'Failed to verify eligibility. Please try again later.' ) ;
218+ }
219+
175220 const item = await this . prisma . shopItem . findUnique ( {
176221 where : { itemId } ,
177222 include : {
@@ -231,6 +276,7 @@ export class ShopService {
231276 ) ;
232277 }
233278
279+ console . log ( `[Shop Purchase] Creating transaction for userId: ${ userId } , itemId: ${ itemId } , cost: ${ cost } ` ) ;
234280 const transaction = await this . prisma . transaction . create ( {
235281 data : {
236282 userId,
@@ -245,7 +291,9 @@ export class ShopService {
245291 } ,
246292 } ) ;
247293
294+ console . log ( `[Shop Purchase] Transaction created successfully: transactionId ${ transaction . transactionId } ` ) ;
248295 const newBalance = await this . getUserBalance ( userId ) ;
296+ console . log ( `[Shop Purchase] New balance for userId ${ userId } : ${ newBalance . balance } hours` ) ;
249297
250298 let specialAction : string | null = null ;
251299
@@ -298,6 +346,7 @@ export class ShopService {
298346 }
299347 }
300348
349+ console . log ( `[Shop Purchase] Purchase completed successfully for userId: ${ userId } , transactionId: ${ transaction . transactionId } , specialAction: ${ specialAction || 'none' } ` ) ;
301350 return {
302351 transaction,
303352 newBalance,
0 commit comments