Skip to content

Commit

Permalink
Submit all rides in current month instead of all rides anywhere in th…
Browse files Browse the repository at this point in the history
…e past
  • Loading branch information
rce committed May 16, 2021
1 parent 49ff6da commit 371636a
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as puppeteer from "puppeteer"
import {fetchRentals, Rental} from "./hsl"
import {submitKaupunkifillariStats, Submission} from "./kilometrikisa"
import {DateTime} from "luxon";
import {DateTime, Interval} from "luxon";

const HSL_USERNAME = requireEnv("HSL_USERNAME")
const HSL_PASSWORD = requireEnv("HSL_PASSWORD")
Expand All @@ -17,20 +17,25 @@ async function main() {

try {
const page = await browser.pages().then(([first, ...rest]) => first)
const rentals = await fetchRentals(page, HSL_USERNAME, HSL_PASSWORD)
const submissions = groupRentalsIntoDailyKilometrikisaSubmissions(rentalsBeforeToday(rentals))
const rentalsThisMonth = rentalsInThisMonth(await fetchRentals(page, HSL_USERNAME, HSL_PASSWORD))
const submissions = groupRentalsIntoDailyKilometrikisaSubmissions(rentalsThisMonth)
await submitKaupunkifillariStats(page, submissions, KILOMETRIKISA_USERNAME, KILOMETRIKISA_PASSWORD)
} finally {
await browser.close()
}
}

function rentalsInThisMonth(rentals: ReadonlyArray<Rental>): Rental[] {
const now = DateTime.now()
const interval = Interval.fromDateTimes(now.startOf('month'), now.endOf('month'))
return rentals.filter(r => interval.contains(r.date))
}

function rentalsBeforeToday(rentals: ReadonlyArray<Rental>): Rental[] {
const today = DateTime.now().startOf("day")
return rentals.filter(r => r.date < today)
}


function groupRentalsIntoDailyKilometrikisaSubmissions(rentals: ReadonlyArray<Rental>): Map<string, Submission> {
const grouped = groupBy(rentals, r => r.date.toFormat("yyyy-MM-dd"))
return mapValues(grouped, sumRentalsToSubmission)
Expand Down

0 comments on commit 371636a

Please sign in to comment.