UNPKG

@rsc-labs/medusa-store-analytics

Version:
79 lines (78 loc) 3.12 kB
"use strict"; /* * Copyright 2024 RSC-Labs, https://rsoftcon.com/ * * MIT License * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.getQueryEndDate = exports.getTruncateFunction = exports.calculateResolution = exports.DateResolutionType = void 0; var DateResolutionType; (function (DateResolutionType) { DateResolutionType["Day"] = "day"; DateResolutionType["Month"] = "month"; })(DateResolutionType = exports.DateResolutionType || (exports.DateResolutionType = {})); function calculateResolution(date, toDate) { if (!date) return DateResolutionType.Month; const weekAgoTruncated = new Date(new Date(Date.now() - 604800000).setHours(0, 0, 0, 0)); const monthAgoTruncated = new Date(new Date(new Date().setMonth(new Date().getMonth() - 1)).setHours(0, 0, 0, 0)); const yearAgoTruncated = new Date(new Date(new Date().setFullYear(new Date().getFullYear() - 1)).setHours(0, 0, 0, 0)); if (toDate) { const diffTime = toDate.getTime() - date.getTime(); const weekTime = 604800000; const monthTime = weekTime * 4; const twoMonthsTime = monthTime * 2; if (diffTime <= twoMonthsTime) { return DateResolutionType.Day; } const yearTime = monthTime * 12; if (diffTime < yearTime) { return DateResolutionType.Month; } } if (date.getTime() >= weekAgoTruncated.getTime()) { return DateResolutionType.Day; } if (date.getTime() >= monthAgoTruncated.getTime()) { return DateResolutionType.Day; } if (date.getTime() > yearAgoTruncated.getTime()) { return DateResolutionType.Month; } return DateResolutionType.Month; } exports.calculateResolution = calculateResolution; function getTruncateFunction(dateResolution) { if (dateResolution == DateResolutionType.Day) { return (date) => new Date(new Date(date).setHours(0, 0, 0, 0)); } else { return (date) => new Date(new Date(new Date(date).setDate(0)).setHours(0, 0, 0, 0)); } } exports.getTruncateFunction = getTruncateFunction; /** * Calculates the end date for a date range query. * - If 'to' is provided (from date picker), adds 1 day to include the full selected day * - If 'to' is undefined, returns the current date/time * * @param to - Optional end date from date picker (at midnight) * @returns Date object for use in query with < operator */ function getQueryEndDate(to) { if (to) { // Date picker date at midnight - add 1 day to include the full day const nextDay = new Date(to); nextDay.setDate(nextDay.getDate() + 1); return nextDay; } // No date provided - use current time return new Date(Date.now()); } exports.getQueryEndDate = getQueryEndDate;