Fix promo codes

This commit is contained in:
2025-09-25 14:51:34 -04:00
parent a161f4533d
commit 4ea3a4aec3
3 changed files with 48 additions and 7 deletions

View File

@@ -174,7 +174,25 @@ router.post('/simulate', async (req, res) => {
const endDt = parseDate(dateRange.end, endDefault).endOf('day');
const shipCountry = filters.shipCountry || 'US';
const promoIds = Array.isArray(filters.promoIds) ? filters.promoIds.filter(Boolean) : [];
const rawPromoFilters = [
...(Array.isArray(filters.promoIds) ? filters.promoIds : []),
...(Array.isArray(filters.promoCodes) ? filters.promoCodes : []),
];
const promoCodes = Array.from(
new Set(
rawPromoFilters
.map((value) => {
if (typeof value === 'string') {
return value.trim();
}
if (typeof value === 'number') {
return String(value);
}
return '';
})
.filter((value) => value.length > 0)
)
);
const config = {
merchantFeePercent: typeof merchantFeePercent === 'number' ? merchantFeePercent : DEFAULTS.merchantFeePercent,
@@ -222,13 +240,17 @@ router.post('/simulate', async (req, res) => {
formatDateForSql(startDt),
formatDateForSql(endDt)
];
const promoJoin = promoIds.length > 0
const promoJoin = promoCodes.length > 0
? 'JOIN order_discounts od ON od.order_id = o.order_id AND od.discount_active = 1 AND od.discount_type = 10'
: '';
if (promoIds.length > 0) {
params.push(promoIds);
let promoFilterClause = '';
if (promoCodes.length > 0) {
const placeholders = promoCodes.map(() => '?').join(',');
promoFilterClause = `AND od.discount_code IN (${placeholders})`;
params.push(...promoCodes);
}
params.push(formatDateForSql(startDt), formatDateForSql(endDt));
const filteredOrdersQuery = `
@@ -249,7 +271,7 @@ router.post('/simulate', async (req, res) => {
AND o.ship_method_selected <> 'holdit'
AND o.ship_country = ?
AND o.date_placed BETWEEN ? AND ?
${promoIds.length > 0 ? 'AND od.discount_code IN (?)' : ''}
${promoFilterClause}
`;
const bucketQuery = `

View File

@@ -124,6 +124,13 @@ export function DiscountSimulator() {
const promosLoading = !promosQuery.data && promosQuery.isLoading;
const selectedPromoCode = useMemo(() => {
if (selectedPromoId == null) {
return undefined;
}
return promosQuery.data?.find((promo) => promo.id === selectedPromoId)?.code || undefined;
}, [promosQuery.data, selectedPromoId]);
const createPayload = useCallback((): DiscountSimulationRequest => {
const { from, to } = ensureDateRange(dateRange);
@@ -135,6 +142,7 @@ export function DiscountSimulator() {
filters: {
shipCountry: 'US',
promoIds: selectedPromoId ? [selectedPromoId] : undefined,
promoCodes: selectedPromoCode ? [selectedPromoCode] : undefined,
},
productPromo,
shippingPromo,
@@ -151,7 +159,17 @@ export function DiscountSimulator() {
pointDollarValue: pointsConfig.pointDollarValue,
},
};
}, [dateRange, selectedPromoId, productPromo, shippingPromo, shippingTiers, merchantFeePercent, fixedCostPerOrder, pointsConfig]);
}, [
dateRange,
selectedPromoId,
selectedPromoCode,
productPromo,
shippingPromo,
shippingTiers,
merchantFeePercent,
fixedCostPerOrder,
pointsConfig,
]);
const simulationMutation = useMutation<
DiscountSimulationResponse,

View File

@@ -72,7 +72,8 @@ export interface DiscountSimulationRequest {
};
filters: {
shipCountry?: string;
promoIds?: number[];
promoIds?: Array<number | string>;
promoCodes?: string[];
};
productPromo: {
type: DiscountPromoType;