Tons of Stuff
This commit is contained in:
@@ -4,23 +4,44 @@ import { PriceRepository } from '../../database/repository';
|
||||
const repository = new PriceRepository();
|
||||
|
||||
export const priceRoutes: FastifyPluginAsync = async (server) => {
|
||||
// GET /api/prices/latest - Get all current listings
|
||||
// GET /api/prices/latest - Get all current listings with lowest price
|
||||
server.get('/latest', async (request, reply) => {
|
||||
try {
|
||||
const prices = await repository.getLatestPrices();
|
||||
|
||||
if (prices.length === 0) {
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
timestamp: new Date(),
|
||||
lowestPrice: 0,
|
||||
platform: '',
|
||||
sellerName: '',
|
||||
allPrices: []
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const lowestPrice = prices.reduce((min: any, p: any) =>
|
||||
Number(p.usdPerMillion) < Number(min.usdPerMillion) ? p : min
|
||||
);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: prices.map((p: any) => ({
|
||||
id: p.id,
|
||||
timestamp: p.timestamp,
|
||||
vendor: p.vendor,
|
||||
seller: p.sellerName,
|
||||
usdPrice: p.usdPrice.toString(),
|
||||
auecAmount: p.auecAmount.toString(),
|
||||
usdPerMillion: p.usdPerMillion.toString(),
|
||||
deliveryTime: p.deliveryTime,
|
||||
url: p.url,
|
||||
})),
|
||||
data: {
|
||||
timestamp: new Date(),
|
||||
lowestPrice: Number(lowestPrice.usdPerMillion),
|
||||
platform: lowestPrice.vendor,
|
||||
sellerName: lowestPrice.sellerName,
|
||||
allPrices: prices.map((p: any) => ({
|
||||
id: p.id,
|
||||
platform: p.vendor,
|
||||
sellerName: p.sellerName,
|
||||
pricePerMillion: Number(p.usdPerMillion),
|
||||
timestamp: p.timestamp,
|
||||
url: p.url
|
||||
}))
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
reply.code(500);
|
||||
|
||||
Reference in New Issue
Block a user