cosmos-db-repositories
Version:
cosmos-db repositories
194 lines (193 loc) • 7.02 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const luxon_1 = require("luxon");
const time_utils_1 = __importDefault(require("./time.utils"));
// Default time zone from environment variables or fallback to "Europe/Berlin"
const DEFAULT_ZONE = process.env.DEFAULT_ZONE || 'Europe/Berlin';
const parse = (time, zone = DEFAULT_ZONE) => {
if (!time) {
return null;
}
switch (time.toLowerCase()) {
case 'yesterday':
return buildYesterdayRange(zone);
case 'today':
return buildAllDayRange(zone);
case 'tomorrow':
return buildTomorrowRange(zone);
case 'this week':
return buildAllWeekRange(zone);
case 'last week':
return buildLastWeekRange(zone);
case 'next week':
return buildNextWeekRange(zone);
case 'this month':
return buildAllMonthRange(zone);
case 'last month':
return buildLastMonthRange(zone);
case 'next month':
return buildNextMonthRange(zone);
case 'this quarter':
return buildThisQuarterRange(zone);
case 'next quarter':
return buildNextQuarterRange(zone);
case 'last quarter':
return buildLastQuarterRange(zone);
case '1 quarter':
return build1QuarterRange(zone);
case '2 quarter':
return build2QuarterRange(zone);
case '3 quarter':
return build3QuarterRange(zone);
case '4 quarter':
return build4QuarterRange(zone);
case 'all':
return buildRange(luxon_1.DateTime.now().setZone(zone).minus({ year: 10 }).startOf('day'), luxon_1.DateTime.now().setZone(zone).plus({ year: 10 }).endOf('day'));
}
return time.split(',')
.map(it => it.trim())
.map(it => time_utils_1.default.toUTC(it, zone))
.map(it => it.toISOString());
};
const buildYesterdayRange = (zone) => {
const now = luxon_1.DateTime.now().setZone(zone);
const start = now.startOf('day').minus({ day: 1 });
const end = start.endOf('day');
return buildRange(start, end);
};
const buildAllDayRange = (zone) => {
const now = luxon_1.DateTime.now().setZone(zone);
const start = now.startOf('day');
const end = start.endOf('day');
return buildRange(start, end);
};
const buildTomorrowRange = (zone) => {
const now = luxon_1.DateTime.now().setZone(zone);
const start = now.plus({ days: 1 }).startOf('day');
const end = start.endOf('day');
return buildRange(start, end);
};
const buildAllWeekRange = (zone) => {
const now = luxon_1.DateTime.now().setZone(zone);
const start = now.startOf('week');
const end = start.endOf('week');
return buildRange(start, end);
};
const buildLastWeekRange = (zone) => {
const now = luxon_1.DateTime.now().setZone(zone);
const start = now.minus({ month: 1 }).startOf('week');
const end = start.endOf('week');
return buildRange(start, end);
};
const buildNextWeekRange = (zone) => {
const now = luxon_1.DateTime.now().setZone(zone);
const start = now.plus({ month: 1 }).startOf('week');
const end = start.endOf('week');
return buildRange(start, end);
};
const buildAllMonthRange = (zone) => {
const now = luxon_1.DateTime.now().setZone(zone);
const start = now.startOf('month');
const end = start.endOf('month');
return buildRange(start, end);
};
const buildNextMonthRange = (zone) => {
const now = luxon_1.DateTime.now().setZone(zone);
const start = now.plus({ month: 1 }).startOf('month');
const end = start.endOf('month');
return buildRange(start, end);
};
const buildLastMonthRange = (zone) => {
const now = luxon_1.DateTime.now().setZone(zone);
const start = now.minus({ month: 1 }).startOf('month');
const end = start.endOf('month');
return buildRange(start, end);
};
const buildThisQuarterRange = (zone) => {
const now = luxon_1.DateTime.now().setZone(zone);
const quarter = now.quarter;
switch (quarter) {
case 1:
return build1QuarterRange(zone);
case 2:
return build2QuarterRange(zone);
case 3:
return build3QuarterRange(zone);
case 4:
return build4QuarterRange(zone);
}
throw new Error('Unsupported quarter index: ' + quarter);
};
const buildLastQuarterRange = (zone) => {
const now = luxon_1.DateTime.now().setZone(zone);
const quarter = now.quarter;
switch (quarter) {
case 1:
return buildLastQuarterFromLastYearRange(zone);
case 2:
return build1QuarterRange(zone);
case 3:
return build2QuarterRange(zone);
case 4:
return build3QuarterRange(zone);
}
throw new Error('Unsupported quarter index: ' + quarter);
};
const buildNextQuarterRange = (zone) => {
const now = luxon_1.DateTime.now().setZone(zone);
const quarter = now.quarter;
switch (quarter) {
case 1:
return build2QuarterRange(zone);
case 2:
return build3QuarterRange(zone);
case 3:
return build4QuarterRange(zone);
case 4:
return buildNextQuarterFromNextYearRange(zone);
}
throw new Error('Unsupported quarter index: ' + quarter);
};
const buildLastQuarterFromLastYearRange = (zone) => {
const now = luxon_1.DateTime.now().setZone(zone);
const start = now.endOf('year').minus({ year: 1 });
const end = start.plus({ month: 3 });
return buildRange(start, end);
};
const buildNextQuarterFromNextYearRange = (zone) => {
const now = luxon_1.DateTime.now().setZone(zone);
const start = now.endOf('year');
const end = start.plus({ month: 3 });
return buildRange(start, end);
};
const build1QuarterRange = (zone) => {
const now = luxon_1.DateTime.now().setZone(zone);
const start = now.startOf('year');
const end = start.plus({ month: 3 });
return buildRange(start, end);
};
const build2QuarterRange = (zone) => {
const now = luxon_1.DateTime.now().setZone(zone);
const start = now.startOf('year').plus({ month: 3 });
const end = start.plus({ month: 3 });
return buildRange(start, end);
};
const build3QuarterRange = (zone) => {
const now = luxon_1.DateTime.now().setZone(zone);
const start = now.startOf('year').plus({ month: 6 });
const end = start.plus({ month: 3 });
return buildRange(start, end);
};
const build4QuarterRange = (zone) => {
const now = luxon_1.DateTime.now().setZone(zone);
const start = now.startOf('year').plus({ month: 9 });
const end = start.plus({ month: 3 });
return buildRange(start, end);
};
const buildRange = (start, end) => {
return [start.toJSDate().toISOString(), end.toJSDate().toISOString()];
};
exports.default = { parse };