From a2352e1eaa2f98903448aa23641380416141a441 Mon Sep 17 00:00:00 2001 From: Markus Thielker Date: Mon, 11 Mar 2024 02:45:35 +0100 Subject: [PATCH] N-FIN-8: add scope type --- src/lib/types/scope.ts | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/lib/types/scope.ts diff --git a/src/lib/types/scope.ts b/src/lib/types/scope.ts new file mode 100644 index 0000000..a5f589a --- /dev/null +++ b/src/lib/types/scope.ts @@ -0,0 +1,48 @@ +export enum ScopeType { + ThisMonth = 'This month', + LastMonth = 'Last month', + ThisYear = 'This year', + LastYear = 'Last year', +} + +export class Scope { + + public type: ScopeType; + public start: Date; + public end: Date; + + private constructor(type: ScopeType, start: Date, end: Date) { + this.type = type; + this.start = start; + this.end = end; + } + + static of(type: ScopeType): Scope { + + let start: Date; + let end: Date; + + const today = new Date(); + + switch (type) { + case ScopeType.ThisMonth: + start = new Date(today.getFullYear(), today.getMonth(), 1, 0, 0, 0, 0); + end = new Date(today.getFullYear(), today.getMonth() + 1, 0, 24, 0, 0, -1); + break; + case ScopeType.LastMonth: + start = new Date(today.getFullYear(), today.getMonth() - 1, 1, 0, 0, 0, 0); + end = new Date(today.getFullYear(), today.getMonth(), 0, 24, 0, 0, -1); + break; + case ScopeType.ThisYear: + start = new Date(today.getFullYear(), 0, 1, 0, 0, 0, 0); + end = new Date(today.getFullYear(), 11, 31, 24, 0, 0, -1); + break; + case ScopeType.LastYear: + start = new Date(today.getFullYear() - 1, 0, 1, 0, 0, 0, 0); + end = new Date(today.getFullYear() - 1, 11, 31, 24, 0, 0, -1); + break; + } + + return new Scope(type, start, end); + } +}