@rsc-labs/medusa-store-analytics
Version:
Get analytics data about your store
85 lines (84 loc) • 4.06 kB
JavaScript
;
/*
* 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.getLegendName = exports.getChartTooltipDate = exports.getChartDateName = exports.compareDatesBasedOnResolutionType = exports.calculateResolution = exports.ChartResolutionType = void 0;
var ChartResolutionType;
(function (ChartResolutionType) {
ChartResolutionType[ChartResolutionType["DayMonth"] = 0] = "DayMonth";
ChartResolutionType[ChartResolutionType["Month"] = 1] = "Month";
})(ChartResolutionType = exports.ChartResolutionType || (exports.ChartResolutionType = {}));
function calculateResolution(fromDate, toDate) {
if (!fromDate)
return undefined;
const calculateToDate = toDate ? new Date(toDate) : new Date(Date.now());
const diffTime = calculateToDate.getTime() - fromDate.getTime();
const weekTime = 604800000;
const monthTime = weekTime * 4;
const twoMonthsTime = monthTime * 2;
if (diffTime <= twoMonthsTime) {
return ChartResolutionType.DayMonth;
}
const yearTime = monthTime * 12;
if (diffTime < yearTime) {
return ChartResolutionType.Month;
}
return ChartResolutionType.Month;
}
exports.calculateResolution = calculateResolution;
const compareDatesBasedOnResolutionType = (date1, date2, resolutionType) => {
switch (resolutionType) {
case ChartResolutionType.DayMonth:
return new Date(new Date(date1).setHours(0, 0, 0, 0)).getTime() == new Date(new Date(date2).setHours(0, 0, 0, 0)).getTime();
case ChartResolutionType.Month:
return new Date(new Date(new Date(date1).setDate(0)).setHours(0, 0, 0, 0)).getTime() == new Date(new Date(new Date(date2).setDate(0)).setHours(0, 0, 0, 0)).getTime();
default:
return new Date(new Date(date1).setHours(0, 0, 0, 0)).getTime() == new Date(new Date(date2).setHours(0, 0, 0, 0)).getTime();
}
};
exports.compareDatesBasedOnResolutionType = compareDatesBasedOnResolutionType;
const getChartDateName = (date, resolutionType, startDate, endDate) => {
switch (resolutionType) {
case ChartResolutionType.DayMonth:
if ((0, exports.compareDatesBasedOnResolutionType)(date, startDate, resolutionType) || (0, exports.compareDatesBasedOnResolutionType)(date, endDate, resolutionType)) {
return `${date.getDate().toString()} ${getShortMonthName(date)}`;
}
return date.getDate().toString();
case ChartResolutionType.Month:
if ((0, exports.compareDatesBasedOnResolutionType)(date, startDate, resolutionType) || (0, exports.compareDatesBasedOnResolutionType)(date, endDate, resolutionType)) {
return `${getShortMonthName(date)} ${date.getFullYear().toString()}`;
}
return getShortMonthName(date);
default:
return date.getFullYear().toString();
}
};
exports.getChartDateName = getChartDateName;
const getChartTooltipDate = (date, resolutionType) => {
switch (resolutionType) {
case ChartResolutionType.DayMonth:
return `${date.getDate().toString()}-${getShortMonthName(date)}`;
case ChartResolutionType.Month:
return `${getShortMonthName(date)}-${date.getFullYear()}`;
default:
return date.getFullYear().toString();
}
};
exports.getChartTooltipDate = getChartTooltipDate;
const getLegendName = (current) => {
return current ? `Current` : `Preceding`;
};
exports.getLegendName = getLegendName;
const getShortMonthName = (date) => {
let days = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
return days[date.getMonth()];
};