// import { UserTypes } from "@prisma/client"; import { addWeeks, formatISO, setDay, setHours, setMinutes, } from 'date-fns' import readline from 'readline' // Enums // export const prismaUserEnums: UserTypes[] = Object.values(UserTypes); export const getRandomBool = () => Math.random() >= 0.5 export const getRandomEnumValue = (enumValues: T[]): T => { const randomIndex = Math.floor(Math.random() * enumValues.length) return enumValues[randomIndex] } export const getRandomEnumValueByEnumType = >(enumType: T): T[keyof T] => { const enumValues = Object.keys(enumType) as (keyof T)[] const randomKey = enumValues[Math.floor(Math.random() * enumValues.length)] return enumType[randomKey] } export const getRandomDateWithinNextYear = (): Date => { // Random milliseconds within the next year const randomMilliseconds = Math.floor(Math.random() * 1000) * 60 * 60 * 24 * 7 * 52 return new Date(new Date().getTime() + randomMilliseconds) } export const getRandomDateLastSixMonths = () => { const currentDate = new Date() const sixMonthsAgo = new Date(currentDate) const oneMonthAgo = new Date(currentDate) // Set dates for 6 months ago and 1 month ago sixMonthsAgo.setMonth(currentDate.getMonth() - 6) oneMonthAgo.setMonth(currentDate.getMonth() - 1) // Generate random date within the range const randomTime = sixMonthsAgo.getTime() + Math.random() * (oneMonthAgo.getTime() - sixMonthsAgo.getTime()) // Create new date object with the random time return new Date(randomTime) } export const getRandomNumericValueByRange = ({ min, max, step, }: { min: number, max: number, step?: number }): number => { if (step && step > 0) { const range = (max - min) / step const randomStep = Math.floor(Math.random() * (range + 1)) return min + randomStep * step } return Math.floor(Math.random() * (max - min + 1)) + min } const getSpecificDay = (options: { weeksAway: number, day: string, time24Hour: number }) => { const { weeksAway, day, time24Hour } = options const daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] const dayIndex = daysOfWeek.indexOf(day) if (dayIndex === -1) { throw new Error('Invalid day. Please provide a valid day of the week.') } const specificDay = setMinutes(setHours(setDay(addWeeks(new Date(), weeksAway), dayIndex), time24Hour), 0) return { date: specificDay, iso: formatISO(specificDay), } } export const checkIfProdEnvironment = () => { const { DATABASE_URL } = process.env if (!DATABASE_URL) { console.log('------------------------------') console.log('MISSING environment variable "DATABASE_URL" to dev or local database') console.log('------------------------------') process.exit(1) // Exit the process with a non-zero status code indicating an error } if (DATABASE_URL.includes('prod') || DATABASE_URL.includes('production')) { console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') console.log('------------------------------') console.log('------------------------------') console.log('WHAT THE HECK ARE YOU DOING!!! YOUR ON PROD!!!') console.log('------------------------------') console.log('------------------------------') console.log('Aborting reset: This is the production environment.') console.log('Change your environment variable "DATABASE_URL" to dev or local database') console.log('------------------------------') console.log('------------------------------') console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') process.exit(1) // Exit the process with a non-zero status code indicating an error } else { const usernameRegex = /\/\/([^:]+):/ // Captures the part after '//' and before ':' const hostRegex = /@([^.]+)\./ // Captures the part after '@' and before '.' // Extract the username and host const usernameMatch = DATABASE_URL.match(usernameRegex) const hostMatch = DATABASE_URL.match(hostRegex) // Check if matches were found const username = usernameMatch ? usernameMatch[1] : null const host = hostMatch ? hostMatch[1] : null console.log('------------------------------') console.log('------------------------------') console.log('RUNNING ON DATABASE') console.log('Username:', username) console.log('Host:', host) console.log('------------------------------') console.log('------------------------------') } } export const notifyIfProdEnvironment = async () => { const { DATABASE_URL } = process.env if (!DATABASE_URL) { console.log('------------------------------') console.log('MISSING environment variable "DATABASE_URL" to dev or local database') console.log('------------------------------') process.exit(1) // Exit the process with a non-zero status code indicating an error } const usernameRegex = /\/\/([^:]+):/ // Captures the part after '//' and before ':' const hostRegex = /@([^.]+)\./ // Captures the part after '@' and before '.' // Extract the username and host const usernameMatch = DATABASE_URL.match(usernameRegex) const hostMatch = DATABASE_URL.match(hostRegex) // Check if matches were found const username = usernameMatch ? usernameMatch[1] : null const host = hostMatch ? hostMatch[1] : null console.log('------------------------------') console.log('------------------------------') console.log('RUNNING ON DATABASE') console.log('Username:', username) console.log('Host:', host) console.log('------------------------------') console.log('------------------------------') if (DATABASE_URL.includes('prod') || DATABASE_URL.includes('production')) { console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') console.log('------------------------------') console.log('------------------------------') console.log('WHAT THE HECK ARE YOU DOING!!! YOUR ON PROD!!!') console.log('------------------------------') console.log('------------------------------') console.log('Aborting reset: This is the production environment.') console.log('Change your environment variable "DATABASE_URL" to dev or local database') console.log('------------------------------') console.log('------------------------------') console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') // Prompt the user to type "Yes" to continue const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }) const answer = await new Promise((resolve) => { rl.question('\nType "Y" to continue on PROD: ', (ans) => { rl.close() resolve(ans) }) }) if (answer.trim() !== 'y' && answer.trim() !== 'Y' && answer.trim() !== '') { console.log('Aborted: You did not type "Yes".') process.exit(1) } } } export const getRandomFirstName = () => { const names: string[] = ['Emily', 'Noah', 'Olivia', 'Liam', 'Sophia', 'Mason', 'Isabella', 'Jacob', 'Ava', 'William', 'Mia', 'Ethan', 'Charlotte', 'James', 'Amelia'] const randomFirst = names[Math.floor(Math.random() * names.length)] return randomFirst } export const getRandomSurname = () => { const names: string[] = ['Smith', 'Johnson', 'Williams', 'Brown', 'Jones', 'Garcia', 'Miller', 'Davis', 'Rodriguez', 'Martinez', 'Hernandez', 'Lopez', 'Gonzalez', 'Wilson', 'Anderson'] const randomFirst = names[Math.floor(Math.random() * names.length)] return randomFirst } export const getRandomPhoneNumber = () => { // Define the phone number formats const formats = { America: '(###) ###-####', Australia: '04## ### ###', Canada: '(###) ###-####', UK: '07### ######', } // Select a random country const countries = Object.keys(formats) const selectedCountry = countries[Math.floor(Math.random() * countries.length)] // Generate a random phone number based on the selected format const format = formats[selectedCountry] return format.replace(/#/g, () => Math.floor(Math.random() * 10)) } export const getRandomDecimalNumber = (): number => { const min = 2.4 const max = 3.5 // Generate a random number between min (inclusive) and max (inclusive) const randomNum = Math.random() * (max - min) + min // Round the number to one decimal place return parseFloat(randomNum.toFixed(1)) } interface CurrencyFormat { code: string; symbol: string; } export const currencyFormats: Record = { USD: { code: 'USD', symbol: '$' }, AUD: { code: 'AUD', symbol: '$' }, EUR: { code: 'EUR', symbol: '€' }, GBP: { code: 'GBP', symbol: '£' }, // Add more currency formats as needed } export const getCurrencySymbol = (currencyCode: string) => { const currencyFormat = currencyFormats[currencyCode] if (!currencyFormat) return '$' const { symbol } = currencyFormat return symbol } export const formatCurrency = (amount: number, currencyCode: string): string => { if (!amount) return '' const currencyFormat = currencyFormats[currencyCode] if (!currencyFormat) { return amount.toString() // Return as a string without formatting if currency code is not found } const formattedAmount = amount.toLocaleString(undefined, { // style: 'currency', currency: currencyCode, currencyDisplay: 'symbol', minimumFractionDigits: 0, maximumFractionDigits: 0, }) return `${getCurrencySymbol(currencyCode)}${formattedAmount}` }