-
Notifications
You must be signed in to change notification settings - Fork 151
/
generateXml.ts
107 lines (94 loc) · 4.28 KB
/
generateXml.ts
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
import { get } from 'https';
import { IncomingMessage } from 'http';
import { JobBoardPost } from '@bullhorn/bullhorn-types';
import * as jsonxml from 'jsontoxml';
import { ISettings } from './src/app/typings/settings';
export function generateSitemap(appConfig: ISettings, res: any, req: any): any {
let sitemapUrls: { name: 'url', children: [{ name: 'loc', text: string }, { name: 'lastmod', text: string }] }[] = [];
let jobsUrl: string = `https://public-rest${appConfig.service.swimlane}.bullhornstaffing.com/rest-services/${appConfig.service.corpToken}/search/JobOrder?query=(isOpen:1%20AND%20isDeleted:0)${getQuery(appConfig)}&fields=id,title,address(city,state,zip),employmentType,dateLastPublished,publicDescription&count=500&sort=-dateLastPublished&start=0`;
let body: string = '';
get(jobsUrl, (response: IncomingMessage) => {
response.on('data', function (chunk: any): any {
body += chunk;
});
response.on('end', function (): any {
let jobs: JobBoardPost[] = JSON.parse(body).data;
jobs.forEach((job: JobBoardPost) => {
let postDate: Date = new Date(job.dateLastPublished);
sitemapUrls.push({
name: 'url',
children: [
{ name: 'loc', text: `${req.protocol}://${req.hostname}${req.originalUrl.replace('/sitemap', '/jobs')}/${job.id}` },
{ name: 'lastmod', text: `${postDate.getFullYear()}-${('0' + (postDate.getMonth() + 1)).slice(-2)}-${('0' + postDate.getDate()).slice(-2)}` },
],
});
});
res.send(`<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> ${jsonxml(sitemapUrls)}</urlset>`);
});
});
}
export function generateRss(appConfig: ISettings, res: any, req: any): any {
let jobListings: any = {
children: [], title: `${appConfig.companyName} Job Opportunities`, link: `${req.protocol}://${req.hostname}${req.originalUrl}`, pubDate: new Date().toUTCString(), ttl: 5};
let jobsUrl: string = `https://public-rest${appConfig.service.swimlane}.bullhornstaffing.com/rest-services/${appConfig.service.corpToken}/search/JobOrder?query=(isOpen:1%20AND%20isDeleted:0)${getQuery(appConfig)}&fields=id,title,address(city,state,zip),employmentType,dateLastPublished,publicDescription&count=500&sort=-dateLastPublished&start=0`;
let body: string = '';
get(jobsUrl, (response: IncomingMessage) => {
response.on('data', function (chunk: any): any {
body += chunk;
});
response.on('end', function (): any {
let jobs: JobBoardPost[] = JSON.parse(body).data;
jobs.forEach((job: JobBoardPost) => {
let postDate: Date = new Date(job.dateLastPublished);
jobListings.children.push({
name: 'item',
children: [
{ name: 'title', text: escapeHtml(job.title) },
{ name: 'description', text: escapeHtml(job.publicDescription) },
{ name: 'city', text: job.address.city },
{ name: 'state', text: job.address.state },
{ name: 'zip', text: job.address.zip },
{ name: 'pubDate', text: postDate.toUTCString() },
{ name: 'link', text: `${req.protocol}://${req.hostname}${req.originalUrl.replace('/feed', '/jobs')}/${job.id}`},
],
});
});
res.send(`<rss version="2.0">
<channel>
${jsonxml(jobListings)}</channel>
</rss>`);
});
});
}
function getQuery(appConfig: ISettings): string {
const isSearch: boolean = true;
let field: string = appConfig.additionalJobCriteria.field;
let values: string[] = appConfig.additionalJobCriteria.values;
let query: string = '';
let delimiter: '"' | '\'' = isSearch ? '"' : '\'';
let equals: ':' | '=' = isSearch ? ':' : '=';
if (field && values.length > 0 && field !== '[ FILTER FIELD HERE ]' && values[0] !== '[ FILTER VALUE HERE ]') {
for (let i: number = 0; i < values.length; i++) {
if (i > 0) {
query += ` OR `;
} else {
query += ' AND (';
}
query += `${field}${equals}${delimiter}${values[i]}${delimiter}`;
}
query += ')';
}
return query;
}
function escapeHtml(text: any): string {
if (!text) {
return '';
}
return text
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}