Time unification
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import express from 'express';
|
||||
import { DateTime } from 'luxon';
|
||||
import { getDbConnection } from '../db/connection.js';
|
||||
import { BUSINESS_TZ, businessNow, toMySqlBound } from '../../../shared/business-time/index.js';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -55,20 +56,20 @@ const DEFAULTS = {
|
||||
pointDollarValue: DEFAULT_POINT_DOLLAR_VALUE,
|
||||
};
|
||||
|
||||
// Parse date inputs in BUSINESS time (date-only strings = business dates;
|
||||
// zoned instants keep their instant). Never zoneless — that was Berlin.
|
||||
function parseDate(value, fallback) {
|
||||
if (!value) {
|
||||
return fallback;
|
||||
}
|
||||
const parsed = DateTime.fromISO(value);
|
||||
const parsed = DateTime.fromISO(value, { zone: BUSINESS_TZ });
|
||||
if (!parsed.isValid) {
|
||||
return fallback;
|
||||
}
|
||||
return parsed;
|
||||
}
|
||||
|
||||
function formatDateForSql(dt) {
|
||||
return dt.toFormat('yyyy-LL-dd HH:mm:ss');
|
||||
}
|
||||
const formatDateForSql = (dt) => toMySqlBound(dt);
|
||||
|
||||
router.get('/promos', async (req, res) => {
|
||||
let connection;
|
||||
@@ -78,11 +79,12 @@ router.get('/promos', async (req, res) => {
|
||||
const releaseConnection = release;
|
||||
|
||||
const { startDate, endDate } = req.query || {};
|
||||
const now = DateTime.now().endOf('day');
|
||||
// Half-open business-day range [start, dayAfterEnd) in Chicago time.
|
||||
const now = businessNow();
|
||||
const defaultStart = now.minus({ years: 3 }).startOf('day');
|
||||
|
||||
const parsedStart = startDate ? parseDate(startDate, defaultStart).startOf('day') : defaultStart;
|
||||
const parsedEnd = endDate ? parseDate(endDate, now).endOf('day') : now;
|
||||
const parsedEnd = (endDate ? parseDate(endDate, now) : now).startOf('day').plus({ days: 1 });
|
||||
|
||||
const rangeStart = parsedStart <= parsedEnd ? parsedStart : parsedEnd;
|
||||
const rangeEnd = parsedEnd >= parsedStart ? parsedEnd : parsedStart;
|
||||
@@ -110,7 +112,7 @@ router.get('/promos', async (req, res) => {
|
||||
) u ON u.discount_code = p.promo_id
|
||||
WHERE p.date_start IS NOT NULL
|
||||
AND p.date_end IS NOT NULL
|
||||
AND NOT (p.date_end < ? OR p.date_start > ?)
|
||||
AND NOT (p.date_end < ? OR p.date_start >= ?)
|
||||
AND p.store = 1
|
||||
AND p.date_start >= '2010-01-01'
|
||||
ORDER BY p.promo_id DESC
|
||||
@@ -343,10 +345,11 @@ router.post('/simulate', async (req, res) => {
|
||||
pointsConfig = {}
|
||||
} = req.body || {};
|
||||
|
||||
const endDefault = DateTime.now();
|
||||
// Half-open business-day bounds [startOfDay, dayAfterEnd) in Chicago time.
|
||||
const endDefault = businessNow();
|
||||
const startDefault = endDefault.minus({ months: 6 });
|
||||
const startDt = parseDate(dateRange.start, startDefault).startOf('day');
|
||||
const endDt = parseDate(dateRange.end, endDefault).endOf('day');
|
||||
const endDt = parseDate(dateRange.end, endDefault).startOf('day').plus({ days: 1 });
|
||||
|
||||
const shipCountry = filters.shipCountry || 'US';
|
||||
const promoIds = Array.from(
|
||||
@@ -463,7 +466,7 @@ router.post('/simulate', async (req, res) => {
|
||||
AND o.order_status >= 20
|
||||
AND o.ship_method_selected <> 'holdit'
|
||||
AND o.ship_country = ?
|
||||
AND o.date_placed BETWEEN ? AND ?
|
||||
AND o.date_placed >= ? AND o.date_placed < ?
|
||||
${promoExistsClause}
|
||||
`;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user