import mongoConnect from "./mongoose" ; import { Scraper } from "@democracy-deutschland/scapacra" ; import { NamedPollDeputyScraper } from "@democracy-deutschland/scapacra-bt" ; import { NamedPollModel , setCronStart , setCronSuccess , setCronError , } from "@democracy-deutschland/bundestagio-common" ; const CRON_NAME = "NamedPollsDeputies" ; const start = async ( ) => { const startDate = new Date ( ) ; await setCronStart ( { name : CRON_NAME , startDate } ) ; try { await Scraper . scrape ( new NamedPollDeputyScraper ( ) , async ( dataPackage ) => { process . stdout . write ( "." ) ; const namedPoll = { webId : dataPackage . data . id } ; const deputies = dataPackage . data . votes . deputies . reduce ( ( accumulator , deputy ) => { if ( ! deputy . id ) { return accumulator ; } const dep = deputy ; dep . webId = dep . id ; delete dep . id ; return [ ... accumulator , dep ] ; } , [ ] ) ; const existingNamedPoll = await NamedPollModel . findOne ( { webId : namedPoll . webId , } ) ; if ( ! existingNamedPoll || ! existingNamedPoll . votes || ! ( JSON . stringify ( existingNamedPoll . votes . deputies ) === JSON . stringify ( deputies ) ) ) { namedPoll [ "votes.deputies" ] = deputies ; } await NamedPollModel . findOneAndUpdate ( { webId : namedPoll . webId } , { $set : namedPoll } , { upsert : true } ) ; return ; } ) ; } catch ( error ) { await setCronError ( { name : CRON_NAME , error : JSON . stringify ( error ) } ) ; throw error ; } await setCronSuccess ( { name : CRON_NAME , successStartDate : startDate } ) ; } ; ( async ( ) => { console . info ( "START" ) ; console . info ( "process.env" , process . env . DB_URL ) ; if ( ! process . env . DB_URL ) { throw new Error ( "you have to set environment variable: DB_URL" ) ; } await mongoConnect ( ) ; console . log ( "procedures" , await NamedPollModel . countDocuments ( { } ) ) ; await start ( ) . catch ( ( ) => process . exit ( 1 ) ) ; process . exit ( 0 ) ; } ) ( ) ;