Skip to content

Commit

Permalink
Update task and list limits
Browse files Browse the repository at this point in the history
  • Loading branch information
bobalazek committed Jan 26, 2024
1 parent 7c48674 commit 0281290
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ export class TasksStatisticsController {
}

@UseGuards(AuthenticatedGuard)
@Get('date-count-map')
async dateCountMap(@Req() req: Request) {
const data = await tasksStatisticsManager.getDateCountMap(req.user);
@Get('tasks-created')
async tasksCreated(@Req() req: Request) {
const from = req.query.from ? new Date(req.query.from as string) : undefined;
const to = req.query.to ? new Date(req.query.to as string) : undefined;

const data = await tasksStatisticsManager.getTasksCreated(req.user, from, to);

return {
success: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export class TasksController {
const tasksCount = await tasksManager.countByListId(list.id);
if (tasksCount >= tasksMaxPerListCount) {
throw new Error(
`You have reached the maximum number of tasks per list (${tasksMaxPerListCount}).`
`You have reached the maximum number of tasks for that list (${tasksMaxPerListCount}).`
);
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/database-services/src/features/auth/UsersManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ export class UsersManager {
// TODO: once we have plans, we need to adjust the limits depending on that

return {
tasksMaxPerListCount: 50,
tasksMaxPerListCount: 10,
listsMaxPerUserCount: 10,
tagsMaxPerUserCount: 20,
tagsMaxPerUserCount: 10,
calendarsMaxPerUserCount: 10,
calendarsMaxEventsPerCalendarCount: 500,
calendarsMaxEventsPerCalendarCount: 100,
calendarsMaxUserCalendarsPerUserCount: 5,
notesMaxPerUserCount: 100,
notesMaxPerUserCount: 10,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { format, startOfMonth, startOfWeek } from 'date-fns';
import { and, between, count, eq, gte, isNull, lte, SQL, sql } from 'drizzle-orm';

import { getDatabase, tasks, User } from '@moaitime/database-core';
import { StatisticsDateCountData, StatisticsTasksBasicData } from '@moaitime/shared-common';
import {
padDataForRangeMap,
StatisticsDateCountData,
StatisticsTasksBasicData,
} from '@moaitime/shared-common';

export class TasksStatisticsManager {
async getBasics(user: User): Promise<StatisticsTasksBasicData> {
Expand All @@ -19,7 +23,7 @@ export class TasksStatisticsManager {
const todayString = format(today, 'yyyy-MM-dd');
const yesterdayString = format(yesterday, 'yyyy-MM-dd');

const rows = await this.getDateCountMap(user, startOfThisMonth);
const rows = await this.getTasksCreated(user, startOfThisMonth);
for (const date in rows) {
const count = rows[date];
const dateObject = new Date(date);
Expand Down Expand Up @@ -49,7 +53,7 @@ export class TasksStatisticsManager {
};
}

async getDateCountMap(user: User, from?: Date, to?: Date): Promise<StatisticsDateCountData> {
async getTasksCreated(user: User, from?: Date, to?: Date): Promise<StatisticsDateCountData> {
let where = and(eq(tasks.userId, user.id), isNull(tasks.deletedAt));

if (from && to) {
Expand All @@ -73,6 +77,10 @@ export class TasksStatisticsManager {
result[format(row.date, 'yyyy-MM-dd')] = row.count;
}

if (from && to) {
return padDataForRangeMap(result, from, to);
}

return result;
}
}
Expand Down
23 changes: 22 additions & 1 deletion packages/shared-common/src/Helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { timeZonesNames } from '@vvo/tzdb';
import { endOfDay, startOfDay } from 'date-fns';
import { addDays, endOfDay, startOfDay } from 'date-fns';
import { format, utcToZonedTime, zonedTimeToUtc } from 'date-fns-tz';

import { StatisticsDateCountData } from '@moaitime/shared-common';

// General
export const sleep = (milliseconds: number): Promise<unknown> => {
return new Promise((resolve) => {
Expand Down Expand Up @@ -77,6 +79,25 @@ export const getTimeDifferenceInSeconds = (start: Date, end: Date) => {
return Math.floor((start.getTime() - end.getTime()) / 1000);
};

export const padDataForRangeMap = (data: StatisticsDateCountData, from: Date, to: Date) => {
const map: StatisticsDateCountData = {};
const range: Date[] = [];

let date = from;
while (date <= to) {
range.push(date);
date = addDays(date, 1);
}

for (const date of range) {
const key = format(date, 'yyyy-MM-dd');

map[key] = data[key] || 0;
}

return map;
};

// UUID
// Could have used the UUID library, but one of our other dependencies requires a lower version,
// which we are then unable to use
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ErrorAlert } from '../../../core/components/ErrorAlert';
import { Loader } from '../../../core/components/Loader';
import { useStatisticsCalendarQuery } from '../../hooks/StatisticsHooks';
import { useCalendarStatisticsQuery } from '../../hooks/StatisticsHooks';
import StatisticsCard from '../statistics-card/StatisticsCard';

const StatisticsCalendarTabContent = () => {
const { isLoading, error, data } = useStatisticsCalendarQuery();
const { isLoading, error, data } = useCalendarStatisticsQuery();

if (isLoading) {
return <Loader />;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ErrorAlert } from '../../../core/components/ErrorAlert';
import { Loader } from '../../../core/components/Loader';
import { useStatisticsFocusQuery } from '../../hooks/StatisticsHooks';
import { useFocusStatisticsQuery } from '../../hooks/StatisticsHooks';
import StatisticsCard from '../statistics-card/StatisticsCard';

const StatisticsFocusTabContent = () => {
const { isLoading, error, data } = useStatisticsFocusQuery();
const { isLoading, error, data } = useFocusStatisticsQuery();

if (isLoading) {
return <Loader />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import {

import { ErrorAlert } from '../../../core/components/ErrorAlert';
import { Loader } from '../../../core/components/Loader';
import { useStatisticsGeneralQuery } from '../../hooks/StatisticsHooks';
import { useGeneralStatisticsQuery } from '../../hooks/StatisticsHooks';
import StatisticsCard from '../statistics-card/StatisticsCard';

const StatisticsGeneralTabContent = () => {
const { isLoading, error, data } = useStatisticsGeneralQuery();
const { isLoading, error, data } = useGeneralStatisticsQuery();

if (isLoading) {
return <Loader />;
Expand All @@ -26,7 +26,7 @@ const StatisticsGeneralTabContent = () => {
}

return (
<div className="grid grid-cols-4 gap-4">
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4">
<StatisticsCard
title="Tasks"
value={data.tasksCountTotal}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ErrorAlert } from '../../../core/components/ErrorAlert';
import { Loader } from '../../../core/components/Loader';
import { useStatisticsMoodQuery } from '../../hooks/StatisticsHooks';
import { useMoodStatisticsQuery } from '../../hooks/StatisticsHooks';
import StatisticsCard from '../statistics-card/StatisticsCard';

const StatisticsMoodTabContent = () => {
const { isLoading, error, data } = useStatisticsMoodQuery();
const { isLoading, error, data } = useMoodStatisticsQuery();

if (isLoading) {
return <Loader />;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ErrorAlert } from '../../../core/components/ErrorAlert';
import { Loader } from '../../../core/components/Loader';
import { useStatisticsNotesQuery } from '../../hooks/StatisticsHooks';
import { useNotesStatisticsQuery } from '../../hooks/StatisticsHooks';
import StatisticsCard from '../statistics-card/StatisticsCard';

const StatisticsNotesTabContent = () => {
const { isLoading, error, data } = useStatisticsNotesQuery();
const { isLoading, error, data } = useNotesStatisticsQuery();

if (isLoading) {
return <Loader />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ import StatisticsTasksTabContentDateCountMap from './tasks/StatisticsTasksTabCon

export default function StatisticsTasksTabContent() {
const to = new Date();
const from = subDays(to, 28);
const from = subDays(to, 14);

return (
<div>
<h3 className="mb-2 text-2xl font-bold">Tasks</h3>
<div className="space-y-4">
<StatisticsTasksTabContentBasics />
<StatisticsTasksTabContentDateCountMap from={from} to={to} />
</div>
<div className="space-y-4">
<StatisticsTasksTabContentBasics />
<StatisticsTasksTabContentDateCountMap from={from} to={to} />
</div>
);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ErrorAlert } from '../../../../core/components/ErrorAlert';
import { Loader } from '../../../../core/components/Loader';
import { useStatisticsTasksQuery } from '../../../hooks/StatisticsHooks';
import { useTasksStatisticsQuery } from '../../../hooks/StatisticsHooks';
import StatisticsCard from '../../statistics-card/StatisticsCard';

export default function StatisticsTasksTabContentBasics() {
const { isLoading, error, data } = useStatisticsTasksQuery();
const { isLoading, error, data } = useTasksStatisticsQuery();

if (isLoading) {
return <Loader />;
Expand All @@ -16,6 +16,7 @@ export default function StatisticsTasksTabContentBasics() {

return (
<div>
<h3 className="mb-2 text-2xl font-bold">Tasks</h3>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4">
<StatisticsCard
title="Created Today"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import Chart from 'react-apexcharts';

import { ErrorAlert } from '../../../../core/components/ErrorAlert';
import { Loader } from '../../../../core/components/Loader';
import { useStatisticsTasksDateCountMapQuery } from '../../../hooks/StatisticsHooks';
import { padDataForRangeMap } from '../../../utils/StatisticsHelpers';
import { useTasksStatisticsTasksCreatedQuery } from '../../../hooks/StatisticsHooks';

export default function StatisticsTasksTabContentDateCountMap({
from,
Expand All @@ -12,7 +11,7 @@ export default function StatisticsTasksTabContentDateCountMap({
from: Date;
to: Date;
}) {
const { isLoading, error, data } = useStatisticsTasksDateCountMapQuery();
const { isLoading, error, data } = useTasksStatisticsTasksCreatedQuery({ from, to });

if (isLoading) {
return <Loader />;
Expand All @@ -22,34 +21,36 @@ export default function StatisticsTasksTabContentDateCountMap({
return <ErrorAlert error={error} />;
}

const finalData = padDataForRangeMap(data, from, to);
const dates = Object.keys(finalData).map((date) => new Date(date).toLocaleDateString());
const counts = Object.values(finalData);
const dates = Object.keys(data).map((date) => new Date(date).toLocaleDateString());
const counts = Object.values(data);

return (
<Chart
options={{
chart: {
id: 'tasks-chart',
toolbar: {
show: false,
<div>
<h4 className="mb-2 text-lg font-bold">Tasks Created by Day</h4>
<Chart
options={{
chart: {
id: 'tasks-chart',
toolbar: {
show: false,
},
},
},
xaxis: {
categories: dates,
},
dataLabels: {
enabled: false,
},
}}
series={[
{
name: 'Tasks',
data: counts,
},
]}
type="bar"
height={350}
/>
xaxis: {
categories: dates,
},
dataLabels: {
enabled: false,
},
}}
series={[
{
name: 'Tasks',
data: counts,
},
]}
type="bar"
height={350}
/>
</div>
);
}
Loading

0 comments on commit 0281290

Please sign in to comment.