Fix promo codes
This commit is contained in:
@@ -174,7 +174,25 @@ router.post('/simulate', async (req, res) => {
|
|||||||
const endDt = parseDate(dateRange.end, endDefault).endOf('day');
|
const endDt = parseDate(dateRange.end, endDefault).endOf('day');
|
||||||
|
|
||||||
const shipCountry = filters.shipCountry || 'US';
|
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 = {
|
const config = {
|
||||||
merchantFeePercent: typeof merchantFeePercent === 'number' ? merchantFeePercent : DEFAULTS.merchantFeePercent,
|
merchantFeePercent: typeof merchantFeePercent === 'number' ? merchantFeePercent : DEFAULTS.merchantFeePercent,
|
||||||
@@ -222,13 +240,17 @@ router.post('/simulate', async (req, res) => {
|
|||||||
formatDateForSql(startDt),
|
formatDateForSql(startDt),
|
||||||
formatDateForSql(endDt)
|
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'
|
? '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) {
|
let promoFilterClause = '';
|
||||||
params.push(promoIds);
|
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));
|
params.push(formatDateForSql(startDt), formatDateForSql(endDt));
|
||||||
|
|
||||||
const filteredOrdersQuery = `
|
const filteredOrdersQuery = `
|
||||||
@@ -249,7 +271,7 @@ router.post('/simulate', async (req, res) => {
|
|||||||
AND o.ship_method_selected <> 'holdit'
|
AND o.ship_method_selected <> 'holdit'
|
||||||
AND o.ship_country = ?
|
AND o.ship_country = ?
|
||||||
AND o.date_placed BETWEEN ? AND ?
|
AND o.date_placed BETWEEN ? AND ?
|
||||||
${promoIds.length > 0 ? 'AND od.discount_code IN (?)' : ''}
|
${promoFilterClause}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const bucketQuery = `
|
const bucketQuery = `
|
||||||
|
|||||||
@@ -124,6 +124,13 @@ export function DiscountSimulator() {
|
|||||||
|
|
||||||
const promosLoading = !promosQuery.data && promosQuery.isLoading;
|
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 createPayload = useCallback((): DiscountSimulationRequest => {
|
||||||
const { from, to } = ensureDateRange(dateRange);
|
const { from, to } = ensureDateRange(dateRange);
|
||||||
|
|
||||||
@@ -135,6 +142,7 @@ export function DiscountSimulator() {
|
|||||||
filters: {
|
filters: {
|
||||||
shipCountry: 'US',
|
shipCountry: 'US',
|
||||||
promoIds: selectedPromoId ? [selectedPromoId] : undefined,
|
promoIds: selectedPromoId ? [selectedPromoId] : undefined,
|
||||||
|
promoCodes: selectedPromoCode ? [selectedPromoCode] : undefined,
|
||||||
},
|
},
|
||||||
productPromo,
|
productPromo,
|
||||||
shippingPromo,
|
shippingPromo,
|
||||||
@@ -151,7 +159,17 @@ export function DiscountSimulator() {
|
|||||||
pointDollarValue: pointsConfig.pointDollarValue,
|
pointDollarValue: pointsConfig.pointDollarValue,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}, [dateRange, selectedPromoId, productPromo, shippingPromo, shippingTiers, merchantFeePercent, fixedCostPerOrder, pointsConfig]);
|
}, [
|
||||||
|
dateRange,
|
||||||
|
selectedPromoId,
|
||||||
|
selectedPromoCode,
|
||||||
|
productPromo,
|
||||||
|
shippingPromo,
|
||||||
|
shippingTiers,
|
||||||
|
merchantFeePercent,
|
||||||
|
fixedCostPerOrder,
|
||||||
|
pointsConfig,
|
||||||
|
]);
|
||||||
|
|
||||||
const simulationMutation = useMutation<
|
const simulationMutation = useMutation<
|
||||||
DiscountSimulationResponse,
|
DiscountSimulationResponse,
|
||||||
|
|||||||
@@ -72,7 +72,8 @@ export interface DiscountSimulationRequest {
|
|||||||
};
|
};
|
||||||
filters: {
|
filters: {
|
||||||
shipCountry?: string;
|
shipCountry?: string;
|
||||||
promoIds?: number[];
|
promoIds?: Array<number | string>;
|
||||||
|
promoCodes?: string[];
|
||||||
};
|
};
|
||||||
productPromo: {
|
productPromo: {
|
||||||
type: DiscountPromoType;
|
type: DiscountPromoType;
|
||||||
|
|||||||
Reference in New Issue
Block a user