Files
costar-data-mapping/index.js

219 lines
6.9 KiB
JavaScript

const csvToJson = require('csvtojson');
const { Parser } = require('json2csv');
const fs = require('fs');
const ownerFiles = [];
const tenantFiles = [];
function formatPhoneNumber(phoneNumber){
if (phoneNumber.length > 0){
return `(${phoneNumber.slice(0, 3)}) ${phoneNumber.slice(3, 6)}-${phoneNumber.slice(6)}`;
}
return phoneNumber;
}
function addQuotes(value){
if (value.includes(',')){
return `"${value}"`;
}
return value;
}
function addCommaToNumber(value){
for(var i = value.length; i > 0; i = i - 3){
value = `${value.slice(0, i)},${value.slice(i)}`;
}
return value.slice(0, value.length - 1);
}
function removeExtraZipCode(value){
var pieces = value.split('-');
return pieces[0];
}
function readFilesFromDir(){
var files = fs.readdirSync('./intake');
for (var i = 0; i < files.length; i++){
if (files[i].toLowerCase().includes('owner')){
ownerFiles.push(files[i]);
} else
if (files[i].toLowerCase().includes('tenant')){
tenantFiles.push(files[i]);
} else {
console.log(`${files[i]} doesn't match owner or tenant.`);
}
}
}
function createDirectorys() {
try {
fs.mkdirSync('./data');
} catch {
console.log('./data already exsists.');
}
try {
fs.mkdirSync('./intake');
} catch {
console.log('./intake already exists.');
}
try {
fs.mkdirSync('./log');
} catch {
console.log('./log already exists.');
}
}
function processOwnerFile(fileName){
csvToJson({
noheader: false,
headers: [
'PropertyAddress',
'PropertyName',
'RBA',
'City',
'State',
'Zip',
'CountyName',
'LandAreaAC',
'ParcelNumber1',
'PropertyType',
'TrueOwnerAddress',
'TrueOwnerCityStateZip',
'TrueOwnerContact',
'TrueOwnerName',
'TrueOwnerPhone',
'SecondaryType',
'OwnerName',
'OwnerPhone'
]
}).fromFile(`./intake/${fileName}`).then((jsonObj) => {
let data = jsonObj.map((x) => {
let y = {};
y['Description Address L Name'] = `${x.PropertyAddress} ${x.City} ${x.State} ${x.Zip.slice(0, 5)}`;
y['Property Address L Custom Building Address'] = `${x.PropertyAddress} ${x.City} ${x.State} ${x.Zip.slice(0, 5)}`;
y['Property Name L Custom Property Name'] = addQuotes(x.PropertyName);
y['RBA L Custom SF Building'] = addQuotes(addCommaToNumber(x.RBA));
y['Property Address P'] = x.PropertyAddress;
y['City P'] = x.City;
y['State P'] = x.State;
y['No Zip'] = x.Zip;
y['Zip P'] = x.Zip.slice(0, 5);
y['County Name C Territory'] = `${x.CountyName} County`;
y['Land Area (AC) L Custom'] = x.LandAreaAC;
y['Parcel Number L Custom'] = x.ParcelNumber1;
y['Property Type L Tag'] = 'Industrial';
y['True Owner Address C Address: Main Address 1 Full Adress'] = addQuotes(x.TrueOwnerAddress);
y['True Owner City State Zip C Address: Main City'] = x.TrueOwnerCityStateZip.length > 0 ? addQuotes(`${removeExtraZipCode(x.TrueOwnerCityStateZip)}`): x.TrueOwnerCityStateZip;
y['True Owner Contact P Full Name'] = addQuotes(x.TrueOwnerContact);
y['True Owner Name L Custom True Owner'] = addQuotes(x.TrueOwnerName);
y['True Owner Phone P Work'] = formatPhoneNumber(x.TrueOwnerPhone);
y['Secondary Type L Custom'] = addQuotes(x.SecondaryType);
y['Owner Name L Custom Owners Name'] = '';
y['Owner Phone L owner Phone'] = '';
y['Owner Name Name C Name'] = x.TrueOwnerName.length > 0 ? addQuotes(x.TrueOwnerName): addQuotes(x.OwnerName);
y['Assignee L Assignee'] = 'EJR';
if (fileName.toLowerCase().includes('office')){
y['C Tag Office'] = 'Office';
} else {
y['C Tag Industrial'] = 'Industrial';
}
y['Market Area L Custom'] = 'Central I-96 Corridor';
y['Owner C Tag'] = 'Owner';
y['Status L'] = 'Pending';
y['C Description'] = 'Owner';
return y;
});
fs.writeFileSync(`./log/${fileName.slice(0, -4)}.result.json`, JSON.stringify(data, null, 2));
const parser = new Parser({
quote: '',
});
const csv = parser.parse(data);
// console.log(csv);
fs.writeFileSync(`./data/${fileName.slice(0, -4)}.result.csv`, csv);
});
}
function processTenantFile(fileName){
csvToJson({
noheader: false,
headers: [
'FullName',
'LastName',
'FirstName',
'Title',
'CompanyName',
'Phone',
'Address',
'Suite',
'City',
'State',
'Zip',
'TenantWebAddress',
'Zip4',
'TenantSICDescription',
'TenantSFOccupied',
'TenantLocation',
'TenantEmp',
'TenPhone',
'MoveDate',
'MailingAddressAddress',
'MailingAddressCity',
'MailingAddressState',
'MailingAddressZip'
]
}).fromFile(`./intake/${fileName}`).then(jsonObj => {
let data = jsonObj.map(x => {
let y = {};
y['Full Name P Name Full Name'] = x.FullName;
y['Last Name'] = x.LastName;
y['First Name'] = x.FirstName;
y['Title P Job Title'] = x.Title;
y['Company Name C Name'] = addQuotes(x.CompanyName);
y['Phone P Phone Work'] = x.Phone;
y['Address C Address: Main Address 1'] = x.Address;
y['Suite C Address: Other Adress 2'] = x.Suite;
y['City C City'] = x.City;
y['State C State'] = x.State;
y['Zip C Zip'] = x.Zip;
y['Tenant: Web Address C URL'] = x.TenantWebAddress;
y['Tenant: SIC Description C SIC Description'] = addQuotes(x.TenantSICDescription);
y['Tenant: SF Occupied C Custom SF Occupied'] = x.TenantSFOccupied;
y['Tenant: Location C Custom Location Type'] = x.TenantLocation;
y['Tenant: # Emp C Custom Employee Count'] = x.TenantEmp;
y['Ten_Phone C Phone work'] = x.TenPhone;
y['Move Date C Custom Move Date'] = x.MoveDate;
y['Mailing Address: Address P Address: Main Address 1'] = x.MailingAddressAddress;
y['Mailing Address: City P Address: Main City'] = x.MailingAddressCity;
y['Mailing Address: State P Address: Main State'] = x.MailingAddressState;
y['Mailing Address: Zip P Address: Main State'] = x.MailingAddressZip;
if (fileName.toLowerCase().includes('office')){
y['C Tag'] = `"Office, Tenant"`;
} else {
y['C Tag'] = `"Industrial, Tenant"`;
}
y['C Description'] = 'Tenant';
return y;
});
fs.writeFileSync(`./log/${fileName.slice(0, -4)}.result.json`, JSON.stringify(data, null, 2));
const parser = new Parser({
quote: '',
});
const csv = parser.parse(data);
// console.log(csv);
fs.writeFileSync(`./data/${fileName.slice(0, -4)}.result.csv`, csv);
})
}
function processFiles(){
createDirectorys();
readFilesFromDir();
ownerFiles.forEach(x => processOwnerFile(x));
tenantFiles.forEach(x => processTenantFile(x));
}
processFiles();