From 862ade757716ec066a1c40e0c2ba4a987ccce9bf Mon Sep 17 00:00:00 2001 From: Pouya Saadeghi Date: Tue, 13 Aug 2024 01:48:47 +0300 Subject: [PATCH] . --- create_discount.js | 61 ++++++++++++++++++++-------------------------- functions.js | 40 ++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 34 deletions(-) create mode 100644 functions.js diff --git a/create_discount.js b/create_discount.js index b52f807..3a1b1e3 100644 --- a/create_discount.js +++ b/create_discount.js @@ -1,42 +1,38 @@ import { writeFileSync } from "bun:fs"; +import { + getRandomValueWithChance, + generateDiscountCode, + getISODateTime, +} from "./functions.js"; -const chance_to_run = 40 / 100; -const discountPercentages = [5, 10, 15, 20, 25]; -const discountHours = [1, 2, 3, 4]; - -const getRandomItemOfArray = (arr) => { - const randomIndex = Math.floor(Math.random() * arr.length); - return arr[randomIndex]; -}; - -// Function to generate a random unique string -function generateDiscountCode(length) { - const characters = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789"; - let result = ""; - for (let i = 0; i < length; i++) { - result += characters.charAt(Math.floor(Math.random() * characters.length)); - } - return result; -} -function getISODateTime(hoursFromNow) { - const date = new Date(); - date.setHours(date.getHours() + hoursFromNow); - return date.toISOString(); -} - -const discountCode = generateDiscountCode(10); // Generate a 10-character discount code - +const chanceToRun = 50 / 100; +const discountPercentages = [ + { value: 5, chance: 40 }, + { value: 10, chance: 30 }, + { value: 15, chance: 20 }, + { value: 20, chance: 7 }, + { value: 25, chance: 2 }, + { value: 30, chance: 1 }, +]; +const discountHours = [ + { value: 1, chance: 40 }, + { value: 2, chance: 30 }, + { value: 3, chance: 20 }, + { value: 4, chance: 8 }, + { value: 5, chance: 2 }, +]; const apiKey = process.env.LEMONSQUEEZY_API_KEY; const url = "https://api.lemonsqueezy.com/v1/discounts"; + const data = { data: { type: "discounts", attributes: { name: "Limited time discount code!", - code: discountCode, - amount: getRandomItemOfArray(discountPercentages), + code: `LTD${generateDiscountCode(9)}`, + amount: getRandomValueWithChance(discountPercentages), amount_type: "percent", - expires_at: getISODateTime(getRandomItemOfArray(discountHours)), + expires_at: getISODateTime(getRandomValueWithChance(discountHours)), }, relationships: { store: { @@ -49,7 +45,7 @@ const data = { }, }; -if (Math.random() < chance_to_run) { +if (Math.random() < chanceToRun) { fetch(url, { method: "POST", headers: { @@ -62,11 +58,8 @@ if (Math.random() < chance_to_run) { .then((response) => response.json()) .then((json) => { if (json.data?.id) { - // Save the response in a JSON file writeFileSync("api/discount.json", JSON.stringify(json, null, 2)); - console.log( - "Discount code created successfully and response saved to api/discount.json", - ); + console.log("Discount code created successfully JSON"); } else { console.error("Failed to create discount code:", json); } diff --git a/functions.js b/functions.js new file mode 100644 index 0000000..e98bc81 --- /dev/null +++ b/functions.js @@ -0,0 +1,40 @@ +export const getRandomItemOfArray = (arr) => { + const randomIndex = Math.floor(Math.random() * arr.length); + return arr[randomIndex]; +}; + +export const getRandomValueWithChance = (obj) => { + const cumulativeChances = []; + let cumulativeSum = 0; + + // Calculate cumulative chances + for (const item of obj) { + cumulativeSum += item.chance; + cumulativeChances.push({ value: item.value, cumulative: cumulativeSum }); + } + + // Generate a random number between 0 and the sum of all chances + const randomNum = Math.random() * cumulativeSum; + + // Determine which value corresponds to the generated random number + for (const item of cumulativeChances) { + if (randomNum <= item.cumulative) { + return item.value; + } + } +}; + +export const generateDiscountCode = (length) => { + const characters = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789"; + let result = ""; + for (let i = 0; i < length; i++) { + result += characters.charAt(Math.floor(Math.random() * characters.length)); + } + return result; +}; + +export const getISODateTime = (hoursFromNow) => { + const date = new Date(); + date.setHours(date.getHours() + hoursFromNow); + return date.toISOString(); +};