131 lines
4.2 KiB
TypeScript
131 lines
4.2 KiB
TypeScript
import { MEMBERSHIP_TYPES, PATH_FILES_TO_IMPORT } from "./constants/constants";
|
|
import { unlink } from 'fs/promises';
|
|
|
|
|
|
export function createUsersRandomList(num: number) {
|
|
let randomUsers: { firstName: string; lastName: string; email: string; membershipType: string; startDate: string; endDate: string }[] = []
|
|
|
|
for (let i = 0; i < num; i++) {
|
|
let randomName = generateRandomName()
|
|
let fn = randomName.split(' ')[0]
|
|
let ln = randomName.split(' ')[1]
|
|
let em = fn.toLowerCase() + '.' + ln.toLowerCase() + '@example.org';
|
|
randomUsers.push({
|
|
firstName: fn,
|
|
lastName: ln,
|
|
email: em,
|
|
membershipType: '2',
|
|
startDate: '2022-01-01',
|
|
endDate: '2030-01-01'
|
|
});
|
|
}
|
|
let uniqueRandomUsers = randomUsers.filter((value, index, array) =>
|
|
index === array.findIndex(item =>
|
|
item.firstName === value.firstName &&
|
|
item.lastName === value.lastName &&
|
|
item.email === value.email
|
|
)
|
|
);
|
|
return uniqueRandomUsers
|
|
}
|
|
|
|
function generateRandomName() {
|
|
var firstNames = ["Jan", "Pablo", "Ana", "Mari", "Jean", "Li", "Pau", "Glori", "Olga", "Sasha", "Diego", "Carla"];
|
|
var lastNames = ["Garcia", "Sartre", "Cuevas", "Harrison", "Peña", "López", "Zapatero", "Dueñas", "Serrat", "Borgoñoz", "Suarez", "Craig"];
|
|
|
|
var randomFirstName = firstNames[Math.floor(Math.random() * firstNames.length)];
|
|
var randomLastName = lastNames[Math.floor(Math.random() * lastNames.length)];
|
|
|
|
return `${randomFirstName} ${randomLastName}`;
|
|
}
|
|
|
|
export function formatDate(dateStr: string) {
|
|
// Create a new Date object from the string
|
|
const date = new Date(dateStr);
|
|
|
|
// Define the names of the months
|
|
const monthNames = ["Jan.", "Feb.", "March", "April", "May", "June", "July", "Aug.", "Sept.", "Oct.", "Nov.", "Dec."];
|
|
|
|
// Extract the day, month, and year
|
|
const day = date.getDate();
|
|
const monthIndex = date.getMonth();
|
|
const year = date.getFullYear();
|
|
|
|
// Return the formatted date
|
|
return `${monthNames[monthIndex]} ${day}, ${year}`;
|
|
}
|
|
|
|
export async function copyFile(source: string, target: string) {
|
|
console.log("copyFile....");
|
|
const fs = require('fs').promises;
|
|
const path = require('path');
|
|
|
|
let sourceFilePath = path.join(PATH_FILES_TO_IMPORT, source);
|
|
let targetFilePath = path.join(PATH_FILES_TO_IMPORT, target);
|
|
|
|
console.log(`Current working directory: ${process.cwd()}`);
|
|
sourceFilePath = path.join(process.cwd(), sourceFilePath);
|
|
targetFilePath = path.join(process.cwd(), targetFilePath);
|
|
//
|
|
try {
|
|
await fs.access(sourceFilePath);
|
|
} catch (error) {
|
|
console.error(`Error accessing file: ${sourceFilePath}`);
|
|
throw error;
|
|
}
|
|
|
|
try {
|
|
await fs.copyFile(sourceFilePath, targetFilePath);
|
|
|
|
} catch (error) {
|
|
console.error(`Error renaming file: ${sourceFilePath} -> ${targetFilePath}`);
|
|
throw error;
|
|
}
|
|
|
|
let existsNewFile: boolean;
|
|
try {
|
|
await fs.access(targetFilePath);
|
|
existsNewFile = true;
|
|
} catch (error) {
|
|
existsNewFile = false;
|
|
}
|
|
|
|
if (!existsNewFile) {
|
|
throw new Error(`Failed to rename file: ${sourceFilePath} to ${targetFilePath}`);
|
|
}
|
|
|
|
}
|
|
|
|
export async function deleteFile(fileName: string): Promise<void> {
|
|
const path = require('path');
|
|
|
|
try {
|
|
let filePath = path.join(PATH_FILES_TO_IMPORT, fileName);
|
|
filePath = path.join(process.cwd(), filePath);
|
|
await unlink(filePath);
|
|
console.log(`File deleted: ${filePath}`);
|
|
} catch (error) {
|
|
console.error(`Error deleting file: ${fileName}. Error: ${error}`);
|
|
}
|
|
}
|
|
|
|
export function appendRandomNumberToFilename(filename: string) {
|
|
// Generate a random number between 100 and 999
|
|
const randomNumber = Math.floor(Math.random() * (999 - 100 + 1)) + 100;
|
|
|
|
// Extract the extension from the filename
|
|
const extensionIndex = filename.lastIndexOf('.');
|
|
const extension = extensionIndex !== -1 ? filename.slice(extensionIndex) : '';
|
|
|
|
// Remove the extension from the original filename
|
|
const basename = extensionIndex !== -1 ? filename.slice(0, extensionIndex) : filename;
|
|
|
|
// Append the random number and the extension to form the new filename
|
|
return `${basename}${randomNumber}${extension}`;
|
|
}
|
|
|
|
|
|
export function getMembershipTypeById(id: string) {
|
|
const item = MEMBERSHIP_TYPES.find(item => item.id === id);
|
|
return item ? item.type : null;
|
|
} |