[offers][feat] add data seeding code to fetch from excel

pull/501/head
Stuart Long Chay Boon 3 years ago
parent 9ca5935ab1
commit 000867653b

@ -1,15 +1,27 @@
// const xlsxFile = require('read-excel-file/node'); import reader from "xlsx";
import { PrismaClient } from '@prisma/client';
// xlsxFile('/Users/stuartlong/Desktop/tech-interview-handbook/apps/portal/prisma/salaries.xlsx').then((rows) => { import crypto from 'crypto';
// console.log(rows) import { baseCurrencyString } from '../src/utils/offers/currency';
// }) import { convert } from '../src/utils/offers/currency/currencyExchange';
const reader = require("xlsx")
const prisma = new PrismaClient();
// Reading our test file // Reading our test file
const file = reader.readFile('/Users/stuartlong/Desktop/tech-interview-handbook/apps/portal/prisma/salaries.xlsx') const file = reader.readFile('/Users/stuartlong/Desktop/tech-interview-handbook/apps/portal/prisma/salaries.xlsx')
let data = [] let data: Array<excelData> = []
type excelData = {
Timestamp: Date;
Type: string;
Company: string;
Role: string,
Income?: number | string;
Stocks?: number | string;
SignOn?: number | string;
TC?: number | string;
Bonus?: number | string;
Comments?: string
}
const sheets = file.SheetNames const sheets = file.SheetNames
@ -17,11 +29,206 @@ for(let i = 0; i < sheets.length; i++)
{ {
const temp = reader.utils.sheet_to_json( const temp = reader.utils.sheet_to_json(
file.Sheets[file.SheetNames[i]]) file.Sheets[file.SheetNames[i]])
temp.forEach((res) => { temp.forEach((res: excelData) => {
data.push(res) data.push(res)
}) })
} }
function xlSerialToJsDate(xlSerial){
return new Date(Date.UTC(0, 0, xlSerial - 1));
}
function generateSpecialization() {
const specializations = ["Frontend", "Backend", "Fullstack"];
return specializations[Math.floor((Math.random() * 300)) % 3];
}
async function seedSalaries() {
console.log('Seeding from salaries sheet...');
const companyIdMappings = {};
(await prisma.company.findMany()).forEach((company) => {
companyIdMappings[company.name] = company.id
});
console.log(companyIdMappings);
const createdProfileIds : Array<string> = [];
//seed here
(await Promise.all([
data.map(async (data: excelData) => {
// only add swe roles
if (data.Role.toUpperCase() === 'SOFTWARE ENGINEER') {
if (data.Income && typeof (data.Income) === "number") {
// check if we have company id
// console.log(data.Income)
// console.log()
if (companyIdMappings[data.Company]) {
const token = crypto.createHash('sha256').update(xlSerialToJsDate(data.Timestamp).toString()).digest('hex')
if (data.Type.toUpperCase() === 'INTERNSHIP') {
// create profile
const dataAdded = await prisma.offersProfile.create({
data: {
profileName: crypto.randomUUID().substring(0, 10),
createdAt: xlSerialToJsDate(data.Timestamp),
editToken: token,
background: {
create: {
totalYoe: 0
}
},
offers: {
create: {
comments: data.Comments ?? "",
company: {
connect: {
id: companyIdMappings[data.Company]
}
},
jobType: "INTERN",
location: "Singapore, Singapore", // TODO: DEFAULT AS SG
monthYearReceived: xlSerialToJsDate(data.Timestamp),
negotiationStrategy: "",
offersIntern: {
create: {
internshipCycle: "Summer",
monthlySalary: {
create: {
baseCurrency: baseCurrencyString,
baseValue: await convert(
data.Income,
'SGD', // assume sgd
baseCurrencyString,
),
currency: 'SGD', // assume sgd
value: data.Income
}
},
specialization: generateSpecialization(), // TODO: check about this
startYear: xlSerialToJsDate(data.Timestamp).getFullYear(),
title: data.Role // TODO: check about this
}
}
}
}
}
})
console.log(dataAdded)
createdProfileIds.push(dataAdded.id)
} else {
// assume rest full time
const dataAdded = await prisma.offersProfile.create({
data: {
profileName: crypto.randomUUID().substring(0, 10),
createdAt: xlSerialToJsDate(data.Timestamp),
editToken: token,
background: {
create: {
totalYoe: 0
}
},
offers: {
create: {
comments: data.Comments ?? "",
company: {
connect: {
id: companyIdMappings[data.Company]
}
},
jobType: "FULLTIME",
location: "Singapore, Singapore", // TODO: DEFAULT AS SG
monthYearReceived: xlSerialToJsDate(data.Timestamp),
negotiationStrategy: "",
offersFullTime: {
create: {
baseSalary: {
create: {
baseCurrency: baseCurrencyString,
baseValue: await convert(
data.Income,
'SGD', // assume sgd
baseCurrencyString,
),
currency: 'SGD', // assume sgd
value: data.Income
}
},
bonus: {
create: {
baseCurrency: baseCurrencyString,
baseValue: await convert(
data.Bonus ? (typeof data.Bonus === 'number' ? data.Bonus : 0) : 0,
'SGD',
baseCurrencyString,
),
currency: 'SGD',
value: data.Bonus ? (typeof data.Bonus === 'number' ? data.Bonus : 0) : 0,
}
},
level: data.Type,
specialization: generateSpecialization(), // TODO: check about this
stocks: {
create: {
baseCurrency: baseCurrencyString,
baseValue: await convert(
data.Stocks ? (typeof data.Stocks === "number" ? data.Stocks : 0) : 0,
'SGD',
baseCurrencyString,
),
currency: 'SGD',
value: data.Stocks ? (typeof data.Stocks === "number" ? data.Stocks : 0) : 0,
}
},
title: data.Role, // TODO: check about this
totalCompensation: {
create: {
baseCurrency: baseCurrencyString,
baseValue: await convert(
data.TC ? (typeof data.TC === "number" ? data.TC : 0) : 0,
'SGD',
baseCurrencyString,
),
currency: 'SGD',
value: data.TC ? (typeof data.TC === "number" ? data.TC : 0) : 0,
}
},
}
}
}
}
}
})
console.log(dataAdded)
createdProfileIds.push(dataAdded.id)
}
} else {
console.log("Invalid Company: " + data.Company)
}
} else {
console.log("Invalid Income not a number: " + data.Income)
}
}
})
]).then((_data) => {
console.log('Seeding from salaries sheet complete')
}));
}
seedSalaries()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});
// Printing data // Printing data
console.log(data.splice(0,100)) // console.log(data.splice(0,100))
console.table(data.splice(0,100)) // // console.table(data.splice(0,100))
console.log(xlSerialToJsDate(data[0].Timestamp))
export {}

@ -62,4 +62,4 @@ main()
console.error(e); console.error(e);
await prisma.$disconnect(); await prisma.$disconnect();
process.exit(1); process.exit(1);
}); });
Loading…
Cancel
Save