Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
luccasmaso committed Jan 30, 2023
1 parent e2d5955 commit 17dcc16
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
37 changes: 20 additions & 17 deletions lib/Data/Results.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DynamoDBClient, ScanCommand } from '@aws-sdk/client-dynamodb'
import { Result } from './Types'
import { DynamoDBClient, ScanCommand, ScanOutput } from '@aws-sdk/client-dynamodb'
import { Participant, Result, Status } from './Types'

export default class Results {
client?: DynamoDBClient
Expand All @@ -9,8 +9,8 @@ export default class Results {
region: "us-east-1",
credentials: {
accessKeyId: `${process.env.ACCESS_KEY}`,
secretAccessKey: `${process.env.SECRET_KEY}`,
},
secretAccessKey: `${process.env.SECRET_KEY}`
}
})
}

Expand All @@ -20,22 +20,25 @@ export default class Results {
TableName: "enquete-bbb-results"
}))

const results = data.Items?.map((element) => {
return {
id: element.id.S,
totalVotes: parseInt(element.totalVotes.N || '0'),
status: element.status.S,
participants: element.participants.L?.map(participant => ({
name: participant.M?.name.S,
votesPercentage: parseFloat(participant.M?.votesPercentage.N || '0')
})),
}
}) || []
const results = this.mapItems(data.Items)

results
return results
} catch (error) {
return undefined
}
}


mapItems(items?: ScanOutput["Items"]): Result[] {
return items?.map<Result>((element) => {
return {
id: parseInt(element.id.S!),
status: element.status.S as Status,
totalVotes: parseInt(element.totalVotes.N || '0'),
participants: element.participants.L?.map<Participant>(participant => ({
name: participant.M?.name.S!,
votesPercentage: parseFloat(participant.M?.votesPercentage.N || '0')
})) || [],
}
}) || []
}
}
5 changes: 3 additions & 2 deletions lib/Data/Types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
export type Participant = {
name: string,
image: string,
votesPercentage: number
}

export type Status = 'active'|'inactive'

export type Result = {
id: number,
status: 'active'|'inactive',
status: Status,
totalVotes: number,
participants: Participant[],
}

1 comment on commit 17dcc16

@vercel
Copy link

@vercel vercel bot commented on 17dcc16 Jan 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.