colouring-montreal/app/src/api/services/dataExtract.ts

65 lines
1.6 KiB
TypeScript
Raw Normal View History

2019-08-29 12:53:48 -04:00
import path from 'path';
import db from '../../db';
interface DataExtractRow {
extract_id: number;
extracted_on: Date;
extract_path: string;
}
interface DataExtract {
extract_id: number;
extracted_on: Date;
download_path: string;
}
2019-09-09 10:17:24 -04:00
async function listDataExtracts(): Promise<DataExtract[]> {
2019-08-29 12:53:48 -04:00
try {
2019-10-01 09:46:28 -04:00
const extractRecords = await db.manyOrNone<DataExtractRow>(
2019-08-29 12:53:48 -04:00
`SELECT
extract_id, extracted_on, extract_path
FROM bulk_extracts`
);
return extractRecords.map(getDataExtractFromRow);
} catch (err) {
console.error('Error:', err);
return undefined;
}
}
2019-09-09 10:17:24 -04:00
async function getDataExtractById(extractId: number): Promise<DataExtract> {
2019-08-29 12:53:48 -04:00
try {
const extractRecord = await db.one<DataExtractRow>(
`SELECT
extract_id, extracted_on, extract_path
FROM bulk_extracts
WHERE extract_id = $1
`, [extractId]);
return getDataExtractFromRow(extractRecord);
} catch (err) {
console.error('Error:', err);
return undefined;
}
}
function getDataExtractFromRow(er: DataExtractRow): DataExtract {
return {
extract_id: er.extract_id,
extracted_on: er.extracted_on,
download_path: getDownloadLinkForExtract(er)
2019-11-07 03:13:30 -05:00
};
2019-08-29 12:53:48 -04:00
}
function getDownloadLinkForExtract(extract: DataExtractRow): string {
const file_name = path.basename(extract.extract_path);
2019-11-07 03:13:30 -05:00
return `/downloads/${file_name}`; // TODO: potentially move base path to env var
2019-08-29 12:53:48 -04:00
}
2019-09-09 10:17:24 -04:00
export {
listDataExtracts,
getDataExtractById
};