|
| 1 | +import { Injectable, NotFoundException } from '@nestjs/common'; |
| 2 | +import { ConfigService } from '@nestjs/config'; |
| 3 | +import { |
| 4 | + DynamoDBClient, |
| 5 | + ScanCommand, |
| 6 | + GetItemCommand, |
| 7 | + UpdateItemCommand, |
| 8 | + QueryCommand |
| 9 | +} from '@aws-sdk/client-dynamodb'; |
| 10 | +import { marshall, unmarshall } from '@aws-sdk/util-dynamodb'; |
| 11 | +import { Report, ReportStatus } from './models/report.model'; |
| 12 | +import { GetReportsQueryDto } from './dto/get-reports.dto'; |
| 13 | +import { UpdateReportStatusDto } from './dto/update-report-status.dto'; |
| 14 | + |
| 15 | +@Injectable() |
| 16 | +export class ReportsService { |
| 17 | + private readonly dynamoClient: DynamoDBClient; |
| 18 | + private readonly tableName: string; |
| 19 | + |
| 20 | + constructor(private configService: ConfigService) { |
| 21 | + this.dynamoClient = new DynamoDBClient({ |
| 22 | + region: this.configService.get<string>('AWS_REGION', 'us-east-1'), |
| 23 | + }); |
| 24 | + this.tableName = this.configService.get<string>('DYNAMODB_TABLE_NAME', 'reports'); |
| 25 | + } |
| 26 | + |
| 27 | + async findAll(): Promise<Report[]> { |
| 28 | + const command = new ScanCommand({ |
| 29 | + TableName: this.tableName, |
| 30 | + }); |
| 31 | + |
| 32 | + const response = await this.dynamoClient.send(command); |
| 33 | + return (response.Items || []).map(item => unmarshall(item) as Report); |
| 34 | + } |
| 35 | + |
| 36 | + async findLatest(queryDto: GetReportsQueryDto): Promise<Report[]> { |
| 37 | + const limit = queryDto.limit || 10; |
| 38 | + |
| 39 | + const command = new ScanCommand({ |
| 40 | + TableName: this.tableName, |
| 41 | + Limit: limit, |
| 42 | + ScanIndexForward: false, // This will sort in descending order |
| 43 | + }); |
| 44 | + |
| 45 | + const response = await this.dynamoClient.send(command); |
| 46 | + const reports = (response.Items || []).map(item => unmarshall(item) as Report); |
| 47 | + |
| 48 | + // Sort by createdAt in descending order |
| 49 | + return reports.sort((a, b) => |
| 50 | + new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime() |
| 51 | + ).slice(0, limit); |
| 52 | + } |
| 53 | + |
| 54 | + async findOne(id: string): Promise<Report> { |
| 55 | + const command = new GetItemCommand({ |
| 56 | + TableName: this.tableName, |
| 57 | + Key: marshall({ id }), |
| 58 | + }); |
| 59 | + |
| 60 | + const response = await this.dynamoClient.send(command); |
| 61 | + |
| 62 | + if (!response.Item) { |
| 63 | + throw new NotFoundException(`Report with ID ${id} not found`); |
| 64 | + } |
| 65 | + |
| 66 | + return unmarshall(response.Item) as Report; |
| 67 | + } |
| 68 | + |
| 69 | + async updateStatus(id: string, updateDto: UpdateReportStatusDto): Promise<Report> { |
| 70 | + // First check if the report exists |
| 71 | + await this.findOne(id); |
| 72 | + |
| 73 | + const command = new UpdateItemCommand({ |
| 74 | + TableName: this.tableName, |
| 75 | + Key: marshall({ id }), |
| 76 | + UpdateExpression: 'SET #status = :status, updatedAt = :updatedAt', |
| 77 | + ExpressionAttributeNames: { |
| 78 | + '#status': 'status', |
| 79 | + }, |
| 80 | + ExpressionAttributeValues: marshall({ |
| 81 | + ':status': updateDto.status, |
| 82 | + ':updatedAt': new Date().toISOString(), |
| 83 | + }), |
| 84 | + ReturnValues: 'ALL_NEW', |
| 85 | + }); |
| 86 | + |
| 87 | + const response = await this.dynamoClient.send(command); |
| 88 | + return unmarshall(response.Attributes) as Report; |
| 89 | + } |
| 90 | +} |
0 commit comments