From 2b8ea4865f52033d95f38e9303e05098b4c37b60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Meuter?= Date: Sun, 21 Jan 2024 13:03:59 +0100 Subject: [PATCH] Feature/Add `wtd` and `mtd` as possible values for date range This commit adds support for the following date ranges - 'wtd': week-to-date (from the start of the week) - 'mtd': month-to-date (from the start of the month) fixes #2900 --- .../api/src/app/portfolio/portfolio.service.ts | 18 ++++++++++++++++-- .../src/app/user/update-user-setting.dto.ts | 2 +- libs/common/src/lib/types/date-range.type.ts | 2 +- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index 359ec0079..e6077904f 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -73,7 +73,9 @@ import { min, parseISO, set, - setDayOfYear, + startOfWeek, + startOfMonth, + startOfYear, subDays, subYears } from 'date-fns'; @@ -1652,7 +1654,19 @@ export class PortfolioService { case 'ytd': portfolioStart = max([ portfolioStart, - setDayOfYear(new Date().setHours(0, 0, 0, 0), 1) + startOfYear(new Date().setHours(0, 0, 0, 0)) + ]); + break; + case 'mtd': + portfolioStart = max([ + portfolioStart, + startOfMonth(new Date().setHours(0, 0, 0, 0)) + ]); + break; + case 'wtd': + portfolioStart = max([ + portfolioStart, + startOfWeek(new Date().setHours(0, 0, 0, 0), { weekStartsOn: 1 }) ]); break; case '1y': diff --git a/apps/api/src/app/user/update-user-setting.dto.ts b/apps/api/src/app/user/update-user-setting.dto.ts index f618e42b1..b04f9a494 100644 --- a/apps/api/src/app/user/update-user-setting.dto.ts +++ b/apps/api/src/app/user/update-user-setting.dto.ts @@ -30,7 +30,7 @@ export class UpdateUserSettingDto { @IsOptional() colorScheme?: ColorScheme; - @IsIn(['1d', '1y', '5y', 'max', 'ytd']) + @IsIn(['1d', '1y', '5y', 'max', 'mtd', 'wtd', 'ytd']) @IsOptional() dateRange?: DateRange; diff --git a/libs/common/src/lib/types/date-range.type.ts b/libs/common/src/lib/types/date-range.type.ts index afee7b100..41aa877de 100644 --- a/libs/common/src/lib/types/date-range.type.ts +++ b/libs/common/src/lib/types/date-range.type.ts @@ -1 +1 @@ -export type DateRange = '1d' | '1y' | '5y' | 'max' | 'ytd'; +export type DateRange = '1d' | '1y' | '5y' | 'max' | 'mtd' | 'wtd' | 'ytd';