import { Handler } from 'aws-lambda' ; import { IssueSummary , issueSummarySort } from '../../../common' ; import { getIssueSummary } from './helpers/get-issue-summary' ; import { getOtherIssuesSummariesForEdition } from './helpers/summary' ; import { upload , FIVE_SECONDS , getBucket } from '../../utils/s3' ; import { UploadTaskOutput } from '../upload' ; import { handleAndNotify } from '../../services/task-handler' ; import { Status } from '../../services/status' ; import { sleep } from '../../utils/sleep' ; type IndexTaskInput = UploadTaskOutput ; export interface IndexTaskOutput extends UploadTaskOutput { issueSummary : IssueSummary ; index : IssueSummary [ ] ; } const handlerCurry = ( bucket , statusOnSuccess ) => handleAndNotify ( statusOnSuccess , async ( { issuePublication , issue } ) => { console . log ( `Updating index for ${ issue . name }, ${ issue . date }` ) ; await sleep ( 1000 ) ; const Bucket = getBucket ( bucket ) ; const thisIssueSummary = await getIssueSummary ( issuePublication , Bucket ) ; if ( thisIssueSummary == undefined ) { throw new Error ( 'No issue summary was generated for the current issue' ) ; } console . log ( 'thisIssueSummary:' , JSON . stringify ( thisIssueSummary ) ) ; const { edition } = issuePublication ; const otherIssuesSummariesForEdition = await getOtherIssuesSummariesForEdition ( issuePublication , edition , Bucket ) ; console . log ( 'otherIssuesSummariesForEdition:' , JSON . stringify ( otherIssuesSummariesForEdition ) ) ; console . log ( `Creating index using the new and ${ otherIssuesSummariesForEdition . length } existing issue summaries` ) ; const all = [ thisIssueSummary , ... otherIssuesSummariesForEdition ] ; const allSortedEditionsIssues = issueSummarySort ( all ) ; if ( allSortedEditionsIssues . length > 0 ) { await upload ( `${ issuePublication . edition }/issues` , allSortedEditionsIssues , Bucket , 'application/json' , FIVE_SECONDS ) ; if ( issuePublication . edition === 'daily-edition' ) { await upload ( 'issues' , allSortedEditionsIssues , Bucket , 'application/json' , FIVE_SECONDS ) ; } console . log ( 'Uploaded new issues file' ) ; } else { console . error ( 'New issues file is empty. Upload cancelled.' ) ; } return { issuePublication , index : otherIssuesSummariesForEdition , issue , issueSummary : thisIssueSummary , } ; } , getBucket ( bucket ) ) ; export const handlerProof = handlerCurry ( 'proof' , 'proofed' ) ; export const handlerPublish = handlerCurry ( 'publish' , 'published' ) ;