@@ -17,19 +17,45 @@ export class ReportsService {
1717 private readonly tableName : string ;
1818
1919 constructor ( private configService : ConfigService ) {
20- this . dynamoClient = new DynamoDBClient ( {
21- region : this . configService . get < string > ( 'AWS_REGION' , 'us-east-1' ) ,
22- } ) ;
23- this . tableName = this . configService . get < string > ( 'DYNAMODB_TABLE_NAME' , 'reports' ) ;
20+ const region = this . configService . get < string > ( 'AWS_REGION' , 'us-east-1' ) ;
21+
22+ try {
23+ this . dynamoClient = new DynamoDBClient ( {
24+ region : this . configService . get < string > ( 'AWS_REGION' , 'us-east-1' )
25+ } ) ;
26+ } catch ( error : unknown ) {
27+ console . error ( 'DynamoDB Client Config:' , JSON . stringify ( error , null , 2 ) ) ;
28+ const accessKeyId = this . configService . get < string > ( 'AWS_ACCESS_KEY_ID' ) ;
29+ const secretAccessKey = this . configService . get < string > ( 'AWS_SECRET_ACCESS_KEY' ) ;
30+
31+ const clientConfig : any = { region } ;
32+
33+ // Only add credentials if both values are present
34+ if ( accessKeyId && secretAccessKey ) {
35+ clientConfig . credentials = { accessKeyId, secretAccessKey } ;
36+ }
37+
38+ this . dynamoClient = new DynamoDBClient ( clientConfig ) ;
39+ }
40+
41+ this . tableName = this . configService . get < string > ( 'DYNAMODB_REPORTS_TABLE' , 'reports' ) ;
2442 }
2543
2644 async findAll ( ) : Promise < Report [ ] > {
2745 const command = new ScanCommand ( {
2846 TableName : this . tableName ,
2947 } ) ;
3048
31- const response = await this . dynamoClient . send ( command ) ;
32- return ( response . Items || [ ] ) . map ( item => unmarshall ( item ) as Report ) ;
49+ try {
50+ const response = await this . dynamoClient . send ( command ) ;
51+ return ( response . Items || [ ] ) . map ( item => unmarshall ( item ) as Report ) ;
52+ } catch ( error : unknown ) {
53+ console . error ( 'DynamoDB Error Details:' , JSON . stringify ( error , null , 2 ) ) ;
54+ if ( error instanceof Error && error . name === 'UnrecognizedClientException' ) {
55+ throw new Error ( 'Invalid AWS credentials. Please check your AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.' ) ;
56+ }
57+ throw error ;
58+ }
3359 }
3460
3561 async findLatest ( queryDto : GetReportsQueryDto ) : Promise < Report [ ] > {
0 commit comments