This commit is contained in:
insvrgent
2025-02-05 12:43:36 +07:00
parent c7deaf2d35
commit 556c7337eb
7 changed files with 154 additions and 121 deletions

View File

@@ -7,24 +7,31 @@ export function getAuthToken() {
return localStorage.getItem('auth');
}
// Function to create a coupon
export async function createCoupon(discountType, discountValue, discountPeriods) {
export async function createCoupon(discountType, discountValue, discountPeriods, codeExpectation) {
try {
const bodyData = {
discountType,
discountValue,
discountPeriods,
};
// Conditionally add `codeExpectation` only if it's defined
if (codeExpectation !== null && codeExpectation !== undefined) {
bodyData.couponCodeExpect = codeExpectation;
}
const response = await fetch(`${API_BASE_URL}/coupon/create`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${getAuthToken()}`,
},
body: JSON.stringify({
discountType,
discountValue,
discountPeriods,
}),
body: JSON.stringify(bodyData),
});
if (response.ok) {
const data = await response.json();
return { success: true, couponCode: data.coupon.code };
return { success: true, coupon: data.coupon };
} else {
return { success: false, message: 'Failed to create coupon.' };
}
@@ -33,6 +40,7 @@ export async function createCoupon(discountType, discountValue, discountPeriods)
return { success: false, message: 'Error creating coupon.' };
}
}
// Function to check the validity of the coupon code
export async function checkCoupon(couponCode) {
try {