uomlibraryapps-card-scanner-hybrid-local
Version:
Library Node.js app for UoM card scanning (hybrid-local)
85 lines (84 loc) • 5.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tscommons_core_1 = require("tscommons-core");
const nodecommons_cli_1 = require("nodecommons-cli");
const nodecommons_rest_1 = require("nodecommons-rest");
const nodecommons_rest_2 = require("nodecommons-rest");
const nodecommons_file_1 = require("nodecommons-file");
const nodecommons_file_2 = require("nodecommons-file");
const nodecommons_file_3 = require("nodecommons-file");
const picommons_kiosk_1 = require("picommons-kiosk");
const uomlibrarycommons_card_scanner_1 = require("uomlibrarycommons-card-scanner");
class MediaApi extends picommons_kiosk_1.PiKioskMediaApi {
constructor(restServer, eventSessionModel, signupModel, detectionModel, path, mediaPath) {
super(restServer, path, mediaPath);
super.getHandler(`${path}medias/:mount[idname]/import`, async (req, _res) => {
if (!tscommons_core_1.CommonsType.hasPropertyString(req.query, 'filename'))
return nodecommons_rest_1.CommonsRestApi.badRequest('Invalid filename supplied');
req.query.filename = req.query.filename.replace(/[^-a-z0-9_.]/i, '');
const importFile = `${mediaPath}/${req.strictParams.mount}/${req.query.filename}`;
if (!nodecommons_file_1.CommonsFile.exists(importFile))
return nodecommons_rest_1.CommonsRestApi.notFound('No such file exists');
console.log(`importing from`, importFile);
try {
const imported = nodecommons_file_1.CommonsFile.readJsonFile(importFile);
if (!imported)
throw new Error('Unable to read event JSON file');
const decoded = tscommons_core_1.CommonsType.decodePropertyObject(imported);
if (!uomlibrarycommons_card_scanner_1.isIUomLibraryCardScannerEventSession(decoded))
throw new Error('Invalid event JSON file');
const existing = await eventSessionModel.getByEventAndSession(decoded.event, decoded.session);
if (existing)
return nodecommons_rest_1.CommonsRestApi.conflict('Event session already exists');
const signups = decoded.signups;
delete decoded.signups;
const eventSession = await eventSessionModel.insertFirstClass(decoded);
if (signups) {
for (const signup of signups) {
await signupModel.insertForFirstClass(eventSession, signup);
}
}
return tscommons_core_1.CommonsType.encodePropertyObject(decoded);
}
catch (e) {
if (e instanceof nodecommons_rest_2.CommonsRestOutcomeError)
throw e;
console.log(e);
return nodecommons_rest_1.CommonsRestApi.error(e.message);
}
});
super.putHandler(`${path}medias/:mount[idname]/:event[base62]/:session[base62]`, async (req, _res) => {
if (!tscommons_core_1.CommonsType.hasPropertyString(req.body, 'filename'))
throw new Error('Invalid filename supplied');
req.body.filename = req.body.filename.replace(/[^-a-z0-9_.]/i, '');
const exportFile = `${mediaPath}/${req.strictParams.mount}/${req.body.filename}`;
nodecommons_cli_1.CommonsOutput.debug(`Exporting to ${exportFile}`);
if (nodecommons_file_1.CommonsFile.exists(exportFile)) {
return nodecommons_rest_1.CommonsRestApi.conflict('File already exists');
}
try {
const eventSession = await eventSessionModel.getByEventAndSession(req.strictParams.event, req.strictParams.session);
if (!eventSession)
return nodecommons_rest_1.CommonsRestApi.notFound('No such event and session exists by those UIDs');
nodecommons_cli_1.CommonsOutput.debug(`Found event to export: ${eventSession.title}`);
const detections = (await detectionModel.listByFirstClass(eventSession))
.sort((a, b) => b.scanned.getTime() - a.scanned.getTime());
const csv = new nodecommons_file_2.CommonsCsv([
{ name: 'spotId', type: nodecommons_file_3.ECsvColumnType.STRING },
{ name: 'scanned', type: nodecommons_file_3.ECsvColumnType.DATETIME },
{ name: 'signout', type: nodecommons_file_3.ECsvColumnType.DATETIME }
], ',', '"', nodecommons_file_2.EDateFormat.DMYHI);
csv.save(detections, exportFile);
nodecommons_file_1.CommonsFile.sync();
return true;
}
catch (e) {
if (e instanceof nodecommons_rest_2.CommonsRestOutcomeError)
throw e;
console.log(e);
return nodecommons_rest_1.CommonsRestApi.error(e.message);
}
});
}
}
exports.MediaApi = MediaApi;