Updated return data for public endpoint & added bulk admin endpoint

This commit is contained in:
pfych 2024-10-20 10:11:06 +11:00
parent 5ef33a9f49
commit 6a0dcce2bd
3 changed files with 129 additions and 8 deletions

View file

@ -35,6 +35,32 @@ app.post('/chart/admin', [
},
]);
app.post('/chart/admin/bulk', [
isAdmin,
async (req: RequestContext, res: Response) => {
try {
const chartData = (req.body as Chart[]).map((chart) => ({
md5: chart.md5,
sha256: chart.sha256,
resourceUri: chart.resourceUri,
parentChart: chart.parentChart,
name: chart.name,
comment: chart.comment,
}));
const charts = await Promise.all(
chartData.map((chart) => chartService.create(chart)),
);
res.json(charts.map(chartMapper));
} catch (error) {
const message = getErrorMessage(error);
console.error(`Failed to create chart ${message}`);
res.status(400).json({ error: 'Failed to create chart' });
}
},
]);
app.patch('/chart/admin', [
isAdmin,
async (req: RequestContext, res: Response) => {

View file

@ -4,7 +4,7 @@ import { RequestContext } from '../../util/request-context.type';
import { Response } from 'express';
import { getErrorMessage } from '../../util/error-message';
import { getChartsByMd5, getChartsBySha256 } from './chart.service';
import { flatten, keyBy } from 'lodash-es';
import { flatten, uniqBy } from 'lodash-es';
const app = createApp();
export const handler = createAuthenticatedHandler(app);
@ -17,22 +17,30 @@ app.post('/chart/public', [
sha256: string[];
};
/** @TODO Ask herman how to batch query */
const md5Charts = flatten(
await Promise.all(md5.map((md5) => getChartsByMd5(md5))),
);
const md5ChartsKeyedByMd5 = keyBy(md5Charts, 'md5');
console.log(md5Charts);
const md5ChartsFound = md5Charts.map((chart) => chart.md5);
const md5ChartsNotFound = md5.filter(
(md5) => !md5ChartsFound.includes(md5),
);
const sha256Charts = flatten(
await Promise.all(sha256.map((sha256) => getChartsBySha256(sha256))),
);
const sha256ChartsKeyedBySha256 = keyBy(sha256Charts, 'sha256');
const sha256ChartsFound = sha256Charts.map((chart) => chart.sha256);
const sha256ChartsNotFound = sha256.filter(
(sha256) => !sha256ChartsFound.includes(sha256),
);
res.json({
md5: md5ChartsKeyedByMd5,
sha256: sha256ChartsKeyedBySha256,
resources: uniqBy([...md5Charts, ...sha256Charts], 'resourceUri').map(
(chart) => chart.resourceUri,
),
metadata: {
success: [...md5ChartsFound, ...sha256ChartsFound],
failure: [...md5ChartsNotFound, ...sha256ChartsNotFound],
},
});
} catch (error) {
const message = getErrorMessage(error);