95 lines
3.3 KiB
JavaScript
95 lines
3.3 KiB
JavaScript
import { requestJson } from './apiClient';
|
|
|
|
function normalizeItems(items) {
|
|
if (Array.isArray(items)) return items;
|
|
if (items && typeof items === 'object') return [items];
|
|
return [];
|
|
}
|
|
|
|
export const CouponService = {
|
|
async listCoupons({ page, limit, id, tenant_id, tenant_code, tenant_name, keyword, type, status, created_at_from, created_at_to, sortField, sortOrder } = {}) {
|
|
const iso = (d) => {
|
|
if (!d) return undefined;
|
|
const date = d instanceof Date ? d : new Date(d);
|
|
if (Number.isNaN(date.getTime())) return undefined;
|
|
return date.toISOString();
|
|
};
|
|
|
|
const query = {
|
|
page,
|
|
limit,
|
|
id,
|
|
tenant_id,
|
|
tenant_code,
|
|
tenant_name,
|
|
keyword,
|
|
type,
|
|
status,
|
|
created_at_from: iso(created_at_from),
|
|
created_at_to: iso(created_at_to)
|
|
};
|
|
if (sortField && sortOrder) {
|
|
if (sortOrder === 1) query.asc = sortField;
|
|
if (sortOrder === -1) query.desc = sortField;
|
|
}
|
|
|
|
const data = await requestJson('/super/v1/coupons', { query });
|
|
return {
|
|
page: data?.page ?? page ?? 1,
|
|
limit: data?.limit ?? limit ?? 10,
|
|
total: data?.total ?? 0,
|
|
items: normalizeItems(data?.items)
|
|
};
|
|
},
|
|
async createCoupon(tenantID, form = {}) {
|
|
if (!tenantID) throw new Error('tenantID is required');
|
|
return requestJson(`/super/v1/tenants/${tenantID}/coupons`, {
|
|
method: 'POST',
|
|
body: normalizeCouponForm(form)
|
|
});
|
|
},
|
|
async getCoupon(tenantID, couponID) {
|
|
if (!tenantID) throw new Error('tenantID is required');
|
|
if (!couponID) throw new Error('couponID is required');
|
|
return requestJson(`/super/v1/tenants/${tenantID}/coupons/${couponID}`);
|
|
},
|
|
async updateCoupon(tenantID, couponID, form = {}) {
|
|
if (!tenantID) throw new Error('tenantID is required');
|
|
if (!couponID) throw new Error('couponID is required');
|
|
return requestJson(`/super/v1/tenants/${tenantID}/coupons/${couponID}`, {
|
|
method: 'PUT',
|
|
body: normalizeCouponForm(form)
|
|
});
|
|
},
|
|
async grantCoupon(tenantID, couponID, userIDs = []) {
|
|
if (!tenantID) throw new Error('tenantID is required');
|
|
if (!couponID) throw new Error('couponID is required');
|
|
if (!Array.isArray(userIDs) || userIDs.length === 0) throw new Error('userIDs is required');
|
|
return requestJson(`/super/v1/tenants/${tenantID}/coupons/${couponID}/grant`, {
|
|
method: 'POST',
|
|
body: { user_ids: userIDs }
|
|
});
|
|
}
|
|
};
|
|
|
|
function normalizeCouponForm(form) {
|
|
const iso = (d) => {
|
|
if (!d) return undefined;
|
|
const date = d instanceof Date ? d : new Date(d);
|
|
if (Number.isNaN(date.getTime())) return undefined;
|
|
return date.toISOString();
|
|
};
|
|
|
|
return {
|
|
title: form.title,
|
|
description: form.description,
|
|
type: form.type,
|
|
value: form.value,
|
|
min_order_amount: form.min_order_amount,
|
|
max_discount: form.max_discount,
|
|
total_quantity: form.total_quantity,
|
|
start_at: iso(form.start_at),
|
|
end_at: iso(form.end_at)
|
|
};
|
|
}
|