90 lines
3.2 KiB
TypeScript
90 lines
3.2 KiB
TypeScript
import { BookingType, PrismaClient, SlotType } from '@prisma/client';
|
|
// import { PrismaClient } from '@prisma/client';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import csv from 'csv-parser';
|
|
import { CSVRow } from './model';
|
|
import { URLtoLatLng, convertAddressToDetail, convertWorkdayTiming } from '../../src/share/tool';
|
|
async function readCsv(csvFileName): Promise<CSVRow[]> {
|
|
function doParse(resolve) {
|
|
const results = [];
|
|
fs.createReadStream(csvFileName)
|
|
.pipe(csv())
|
|
.on('data', (data: CSVRow) => {
|
|
|
|
// convert lat lng
|
|
const latLng = URLtoLatLng(data.link);
|
|
// convert workday timing
|
|
const workdayTiming = convertWorkdayTiming(data.workday_timing);
|
|
// convert address to detail
|
|
const addressDetail = convertAddressToDetail(data.address);
|
|
results.push(<CSVRow>{ ...data, ...latLng, ...addressDetail, ...workdayTiming });
|
|
})
|
|
.on('end', () => {
|
|
resolve(results);
|
|
});
|
|
}
|
|
return new Promise((resolve) => doParse(resolve));
|
|
}
|
|
const prisma = new PrismaClient();
|
|
async function main() {
|
|
prisma.$transaction(async (tx: PrismaClient) => {
|
|
console.log('Seeding...');
|
|
try {
|
|
// get list file csv from forder dumps
|
|
const directoryPath = path.join(__dirname, 'csv');
|
|
// read directory
|
|
const files = fs.readdirSync(directoryPath);
|
|
// listing all files using forEach
|
|
for (const file of files) {
|
|
if (!file.endsWith('.csv')) continue;
|
|
const filePath = path.join(directoryPath, file);
|
|
const slotsData = await readCsv(filePath);
|
|
// convert to json
|
|
|
|
// insert to db
|
|
await tx.slot.createMany({
|
|
data: slotsData.map((e) => ({
|
|
lat: e.lat,
|
|
lng: e.lng,
|
|
address: e.address,
|
|
city: e.city,
|
|
district: e.district,
|
|
ward: e.ward,
|
|
name: e.name,
|
|
total: 99,
|
|
empty: 99,
|
|
alow_booking_type: BookingType.All,
|
|
slot_type: SlotType.Orther,
|
|
images: [],
|
|
phone: e.phone,
|
|
close: e.close,
|
|
open: e.open,
|
|
directions: {
|
|
competitors: e.competitors,
|
|
rating: e.rating,
|
|
reviews: e.reviews,
|
|
website: e.website,
|
|
},
|
|
})),
|
|
skipDuplicates: true,
|
|
});
|
|
console.log(`Inserted ${slotsData.length} slots from ${file}`);
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('Error reading CSV file:', error);
|
|
}
|
|
});
|
|
}
|
|
|
|
main()
|
|
.catch((e) => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
})
|
|
.finally(async () => {
|
|
// close the Prisma Client at the end
|
|
// await prisma.$disconnect();
|
|
});
|