<commit_before>import { startOfMonth, addMonths, setMonth, setYear } from 'date-fns';
import groupBy from 'lodash/groupBy';
import { DatabaseTransactionConnectionType } from 'slonik';

import { formatDate, combineJoinedEntryRows } from './shared';
import config from '~api/config';
import { selectEntry, selectAllEntries, selectOldNetWorth } from '~api/queries';
import { Entry } from '~api/types';

export async function fetchById(
  db: DatabaseTransactionConnectionType,
  uid: number,
  netWorthId: number,
): Promise<Entry> {
  const entryRows = await selectEntry(db, uid, netWorthId);
  return combineJoinedEntryRows(entryRows);
}
export async function fetchAll(
  db: DatabaseTransactionConnectionType,
  uid: number,
  oldDateEnd: Date,
): Promise<Entry[]> {
  const allRows = await selectAllEntries(db, uid, formatDate(oldDateEnd));
  const groupedRows = groupBy(allRows, 'id');

  return Object.values(groupedRows)
    .map(combineJoinedEntryRows)
    .sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime());
}

export async function fetchOld(
  db: DatabaseTransactionConnectionType,
  uid: number,
  startDate: Date,
  oldDateEnd: Date,
): Promise<{
  old: number[];
  oldOptions: number[];
}> {
  const rows = await selectOldNetWorth(db, uid, formatDate(startDate), formatDate(oldDateEnd));

  const old = rows.map(({ value }) => value);
  const oldOptions = rows.map(({ option_value }) => option_value);

  return { old, oldOptions };
}

export async function readNetWorthEntry(
  db: DatabaseTransactionConnectionType,
  uid: number,
  netWorthId: number,
): Promise<Entry> {
  const entry = await fetchById(db, uid, netWorthId);
  return entry;
}

export async function readAllNetWorthEntries(
  db: DatabaseTransactionConnectionType,
  uid: number,
): Promise<{
  items: Entry[];
  old: number[];
  oldOptions: number[];
}> {
  const { numLast, startYear, startMonth } = config.data.overview;

  const oldDateEnd = startOfMonth(addMonths(new Date(), -numLast));
  const startDate = startOfMonth(setMonth(setYear(new Date(), startYear), startMonth));

  const [items, { old, oldOptions }] = await Promise.all([
    fetchAll(db, uid, oldDateEnd),
    fetchOld(db, uid, startDate, oldDateEnd),
  ]);

  return { items, old, oldOptions };
}
<commit_msg>fix: correct start month on net worth endpoint<commit_after>import { startOfMonth, addMonths, setMonth, setYear } from 'date-fns';
import groupBy from 'lodash/groupBy';
import { DatabaseTransactionConnectionType } from 'slonik';

import { formatDate, combineJoinedEntryRows } from './shared';
import config from '~api/config';
import { selectEntry, selectAllEntries, selectOldNetWorth } from '~api/queries';
import { Entry } from '~api/types';

export async function fetchById(
  db: DatabaseTransactionConnectionType,
  uid: number,
  netWorthId: number,
): Promise<Entry> {
  const entryRows = await selectEntry(db, uid, netWorthId);
  return combineJoinedEntryRows(entryRows);
}
export async function fetchAll(
  db: DatabaseTransactionConnectionType,
  uid: number,
  oldDateEnd: Date,
): Promise<Entry[]> {
  const allRows = await selectAllEntries(db, uid, formatDate(oldDateEnd));
  const groupedRows = groupBy(allRows, 'id');

  return Object.values(groupedRows)
    .map(combineJoinedEntryRows)
    .sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime());
}

export async function fetchOld(
  db: DatabaseTransactionConnectionType,
  uid: number,
  startDate: Date,
  oldDateEnd: Date,
): Promise<{
  old: number[];
  oldOptions: number[];
}> {
  const rows = await selectOldNetWorth(db, uid, formatDate(startDate), formatDate(oldDateEnd));

  const old = rows.map(({ value }) => value);
  const oldOptions = rows.map(({ option_value }) => option_value);

  return { old, oldOptions };
}

export async function readNetWorthEntry(
  db: DatabaseTransactionConnectionType,
  uid: number,
  netWorthId: number,
): Promise<Entry> {
  const entry = await fetchById(db, uid, netWorthId);
  return entry;
}

export async function readAllNetWorthEntries(
  db: DatabaseTransactionConnectionType,
  uid: number,
): Promise<{
  items: Entry[];
  old: number[];
  oldOptions: number[];
}> {
  const { numLast, startYear, startMonth } = config.data.overview;

  const oldDateEnd = startOfMonth(addMonths(new Date(), -numLast));
  const startDate = startOfMonth(setMonth(setYear(new Date(), startYear), startMonth - 1));

  const [items, { old, oldOptions }] = await Promise.all([
    fetchAll(db, uid, oldDateEnd),
    fetchOld(db, uid, startDate, oldDateEnd),
  ]);

  return { items, old, oldOptions };
}

{'fraction_non_alphanumeric': 0.1290462741690202, 'fraction_numerical': 0.00021724961981316532, 'mean_word_length': 5.263945578231293, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5509447', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1440, 'n_words': 382}