Skip to content

Commit

Permalink
fix: Stats data querying
Browse files Browse the repository at this point in the history
  • Loading branch information
letehaha committed Feb 4, 2024
1 parent 1d1ce0c commit 9fac762
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 82 deletions.
28 changes: 1 addition & 27 deletions src/services/stats/get-balance-history-for-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,7 @@ import { GenericSequelizeModelAttributes } from '@common/types';
import { connection } from '@models/index';
import * as Balances from '@models/Balances.model';
import * as Accounts from '@models/Accounts.model';

interface DateQuery {
// yyyy-mm-dd
from?: string;
// yyyy-mm-dd
to?: string;
}

const getWhereConditionForTime = ({ from, to }: DateQuery) => {
const where: { date?: Record<symbol, Date[] | Date> } = {};

if (from && to) {
where.date = {
[Op.between]: [new Date(from), new Date(to)],
};
} else if (from) {
where.date = {
[Op.gte]: new Date(from),
};
} else if (to) {
where.date = {
[Op.lte]: new Date(to),
};
}

return where;
};
import { getWhereConditionForTime } from './utils';

/**
* Retrieves the balances for the requested account for a user within a specified date range.
Expand Down
28 changes: 1 addition & 27 deletions src/services/stats/get-balance-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,7 @@ import { GenericSequelizeModelAttributes } from '@common/types';
import { connection } from '@models/index';
import * as Balances from '@models/Balances.model';
import * as Accounts from '@models/Accounts.model';

interface DateQuery {
// yyyy-mm-dd
from?: string;
// yyyy-mm-dd
to?: string;
}

const getWhereConditionForTime = ({ from, to }: DateQuery) => {
const where: { date?: Record<symbol, Date[] | Date> } = {};

if (from && to) {
where.date = {
[Op.between]: [new Date(from), new Date(to)],
};
} else if (from) {
where.date = {
[Op.gte]: new Date(from),
};
} else if (to) {
where.date = {
[Op.lte]: new Date(to),
};
}

return where;
};
import { getWhereConditionForTime } from './utils';

/**
* Retrieves the balances for all the accounts for a user within a specified date range.
Expand Down
29 changes: 1 addition & 28 deletions src/services/stats/get-expenses-history.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Op } from 'sequelize';
import {
TransactionModel,
TRANSACTION_TYPES,
Expand All @@ -9,33 +8,7 @@ import { GenericSequelizeModelAttributes } from '@common/types';

import { connection } from '@models/index';
import * as Transactions from '@models/Transactions.model';

interface DateQuery {
// yyyy-mm-dd
from?: string;
// yyyy-mm-dd
to?: string;
}

const getWhereConditionForTime = ({ from, to }: DateQuery) => {
const where: { time?: Record<symbol, Date[] | Date> } = {};

if (from && to) {
where.time = {
[Op.between]: [new Date(from), new Date(to)],
};
} else if (from) {
where.time = {
[Op.gte]: new Date(from),
};
} else if (to) {
where.time = {
[Op.lte]: new Date(to),
};
}

return where;
};
import { getWhereConditionForTime } from './utils';

export type GetExpensesHistoryResponseSchema = Pick<
TransactionModel,
Expand Down
29 changes: 29 additions & 0 deletions src/services/stats/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { endOfDay } from "date-fns";
import { Op } from "sequelize";

interface DateQuery {
// yyyy-mm-dd
from?: string;
// yyyy-mm-dd
to?: string;
}

export const getWhereConditionForTime = ({ from, to }: DateQuery) => {
const where: { time?: Record<symbol, Date[] | Date> } = {};

if (from && to) {
where.time = {
[Op.between]: [new Date(from), endOfDay(new Date(to))],
};
} else if (from) {
where.time = {
[Op.gte]: new Date(from),
};
} else if (to) {
where.time = {
[Op.lte]: new Date(to),
};
}

return where;
};

0 comments on commit 9fac762

Please sign in to comment.