import * as Promise from 'bluebird' import Table = require('cli-table') import { PresentationClient, ExportGetOptions, ExportData } from '../../../services/presentation' import { CoError } from '../../../modules/co-error' import HorizontalTable, { SortOrder } from '../../../modules/horizontal-table' export default function list (presentationClient: PresentationClient, params: ExportGetOptions, maxWidth: number, tableStyle: TableStyle): Promise { return presentationClient.getLastExports(params) .then(createTable(maxWidth, tableStyle)) .then(descendingSortByDate) .catch(handleError) } function createTable (maxWidth: number, tableStyle: TableStyle): (presentations: ExportData[]) => HorizontalTable { return function (presentations) { const head = ['ID', 'CLM', 'Presentation', 'Status', 'Updated'] const rows: string[][] = presentations.map(getTableRowsData) return new HorizontalTable({ head, rows }, maxWidth, tableStyle) } } function descendingSortByDate(table: HorizontalTable): HorizontalTable { const DATE_COLUMN_INDEX = 4 return table.sortByRow(DATE_COLUMN_INDEX, SortOrder.Descending, Date.parse) } function getTableRowsData( presentation: ExportData): string[] { return [ presentation.id, presentation.sub_type.code, presentation.presentation.name, presentation.last_build.status[presentation.last_build.status.length - 1].type, formatDate(presentation.updated_at) ] } function formatDate (date: string): string { return date ? new Date(date).toUTCString() : '' } function handleError (err: CoError): never { if(err.code === 'EMPTY_LIST'){ throw new CoError('EXPORT_EMPTY_LIST') } throw err }