-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHCPCS_Loader.js
122 lines (110 loc) · 3.78 KB
/
HCPCS_Loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const https = require("https");
const fs = require("fs");
const got = require("got");
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const extract = require("extract-zip");
const xlsx = require("node-xlsx").default;
const MongoClient = require("mongodb").MongoClient;
const dotenv = require('dotenv');
dotenv.config();
const CONNECTION_URL = process.env.CONNECTION_URL;
console.log({ CONNECTION_URL });
const client = new MongoClient(CONNECTION_URL, { useNewUrlParser: true });
let newHCPCSZipFile;
const baseurl = "https://www.cms.gov";
const url =
"https://www.cms.gov/Medicare/Coding/HCPCSReleaseCodeSets/Alpha-Numeric-HCPCS"; // link to file you want to download
const getHCPCSZipFile = async () => {
got(url)
.then((response) => {
const dom = new JSDOM(response.body);
let currentYear = 2020;
dom.window.document.querySelectorAll("a").forEach((link) => {
if (link.href.includes(`${currentYear}-Alpha-Numeric-HCPCS-File`)) {
console.log(link.href);
got(baseurl + link.href).then((response) => {
const dom = new JSDOM(response.body);
dom.window.document.querySelectorAll("a").forEach((link) => {
if (
link.href.includes(`${currentYear}-Alpha-Numeric-HCPCS-File`)
) {
newHCPCSZipFile =
__dirname +
`/data/zip/${currentYear}-Alpha-Numeric-HCPCS-File.zip`;
const file = fs.createWriteStream(newHCPCSZipFile);
const request = https.get(
baseurl + link.href,
function (response) {
console.log("Downloading Zip file from HCPCS website");
response.pipe(file).on("finish", function () {
unZipHCSCFile(newHCPCSZipFile);
});
}
);
}
});
});
}
});
})
.catch((err) => {
console.log(err);
});
};
const unZipHCSCFile = async (file) => {
try {
await extract(file, { dir: __dirname + "/data/unzip/" });
console.log("Extraction complete");
console.log("Sending Codes to DB");
XLSToMongoDB();
} catch (err) {
// handle any errors
console.log(err);
}
};
const XLSToMongoDB = async () => {
const unzippedDir = __dirname + "/data/unzip/";
fs.readdir(unzippedDir, function async(err, files) {
if (err) {
console.log(err);
return;
}
HCPCSFiles = files.filter((filename) =>
filename.includes("ANWEB_w_disclaimer.xls")
);
console.log(HCPCSFiles);
HCPCSFiles.forEach(async (file) => {
const workSheetsFromFile = xlsx.parse(`${unzippedDir}/${file}`);
//Column Names
console.log(workSheetsFromFile[0].data[10]);
try {
await client.connect();
const collection = client.db("CodeTable").collection("HCPCS");
datasheet = workSheetsFromFile[0].data;
// Insert the rows into MONGODB
for (let i = 10; i < workSheetsFromFile[0].data.length; i++) {
// Only load the HCPCS codes (no modifiers)
if (datasheet[i][0].replace(/\s+/g, "").length == 5) {
doc = {
code: datasheet[i][0].replace(/\s+/g, ""),
longDescription: datasheet[i][3],
shortDescription: datasheet[i][4],
effDate: datasheet[i][44],
termDate: datasheet[i][46],
};
console.log(
`Updating ${i - 10} of ${workSheetsFromFile[0].data.length - 10}`
);
await collection.update(doc, doc, { upsert: true });
}
}
console.log("Finsihed Updated HCPCS Codes");
client.close();
} finally {
await client.close;
}
});
});
};
getHCPCSZipFile();