11import { getenv } from "@qnaplus/dotenv" ;
2- import { getAllSeasonQuestions , getMetadata , upload } from "@qnaplus/store" ;
2+ import { Question } from "@qnaplus/scraper" ;
3+ import { getAllQuestions , getAllSeasonQuestions , getMetadata , upload } from "@qnaplus/store" ;
34import { trycatch } from "@qnaplus/utils" ;
45import type { Logger } from "pino" ;
56
7+ const update = async ( logger : Logger , data : Question [ ] , key : string ) => {
8+ const json = JSON . stringify ( data ) ;
9+ const buffer = Buffer . from ( json , "utf-8" ) ;
10+ const [ uploadError ] = await trycatch ( ( ) => upload ( key , buffer , logger ) ) ;
11+ if ( uploadError ) {
12+ logger ?. error ( { error : uploadError } , "Error while updating storage json" ) ;
13+ return ;
14+ }
15+ logger . info ( "Successfully completed storage update." ) ;
16+ }
17+
618export const updateStorage = async ( _logger : Logger ) => {
719 const logger = _logger ?. child ( { label : "doStorageUpdate" } ) ;
820 logger . info ( "Starting storage update." ) ;
9- const [ questionsError , questions ] = await getAllSeasonQuestions ( ) ;
21+
22+ // push all questions
23+ const [ questionsError , questions ] = await getAllQuestions ( ) ;
1024 if ( questionsError ) {
1125 logger ?. error (
1226 { error : questionsError } ,
27+ "An error occurred while retreiving all questions from database." ,
28+ ) ;
29+ return ;
30+ }
31+ await update ( logger , questions , `questions-${ getenv ( "NODE_ENV" ) } .json` ) ;
32+
33+ // push current season questions
34+ const [ seasonQuestionsError , seasonQuestions ] = await getAllSeasonQuestions ( ) ;
35+ if ( seasonQuestionsError ) {
36+ logger ?. error (
37+ { error : seasonQuestionsError } ,
1338 "An error occurred while retreiving season questions from database." ,
1439 ) ;
1540 return ;
@@ -22,13 +47,7 @@ export const updateStorage = async (_logger: Logger) => {
2247 ) ;
2348 return ;
2449 }
25- const json = JSON . stringify ( questions ) ;
26- const buffer = Buffer . from ( json , "utf-8" ) ;
27- const key = `questions-${ getenv ( "NODE_ENV" ) } .json` ; // TODO: break out by season
28- const [ uploadError ] = await trycatch ( ( ) => upload ( key , buffer , logger ) ) ;
29- if ( uploadError ) {
30- logger ?. error ( { error : uploadError } , "Error while updating storage json" ) ;
31- return ;
32- }
50+ await update ( logger , seasonQuestions , `questions-${ getenv ( "NODE_ENV" ) } -${ meta . currentSeason } .json` ) ;
51+
3352 logger . info ( "Successfully completed storage update." ) ;
3453} ;
0 commit comments