-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
163 lines (146 loc) · 4.49 KB
/
index.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"use strict";
import { ReportGeneratorR51TR } from "./r51/tr";
require("dotenv").config();
import { ReportGeneratorR50TR } from "./r50/tr";
import { ReportGeneratorR50DR } from "./r50/dr";
import { parseMonthToDate, startOfMonthStr } from "./lib/dates";
import { ReportGeneratorR51DR } from "./r51/dr";
import { addMonths } from "date-fns";
import endOfMonth from "date-fns/endOfMonth";
import { parseConfig } from "./lib/config";
const Hapi = require("@hapi/hapi");
const serverName = process.env.SERVER_NAME || "localhost";
const port = process.env.PORT || 3000;
function queryToContextObj(query) {
let customerId = query.customer_id || "foo";
let requestorId = query.requestor_id || "";
let platform = query.platform || customerId || "Sashimi test platform";
let apiKey = query.api_key || "";
return {
customerId,
requestorId,
platform,
attributesToShow: (query.attributes_to_show || "").split("|"),
config: parseConfig(apiKey),
};
}
function handleRequest(h, query, generatorClass) {
let monthStart =
query.begin_date || startOfMonthStr(addMonths(new Date(), -1));
let monthEnd = query.end_date || monthStart;
let context = queryToContextObj(query);
console.info("Context", context);
let generator = new generatorClass(context);
// some basic checks
let startDate = parseMonthToDate(monthStart);
let endDate = endOfMonth(parseMonthToDate(monthEnd));
if (startDate > endDate) {
return h
.response({
message: "Begin date cannot be after end date",
})
.code(400);
}
let out = generator.createReportData(startDate, endDate);
const respCode = context.config.response_code
? parseInt(context.config.response_code)
: 200;
return h.response(out).code(respCode);
}
const init = async () => {
const server = Hapi.server({
port: port,
host: serverName,
});
// COP 5.0
server.route({
method: "GET",
path: "/reports/tr",
handler: (request, h) => {
console.info("New TR 5.0 request", request.query);
return handleRequest(h, request.query, ReportGeneratorR50TR);
},
});
server.route({
method: "GET",
path: "/reports/dr",
handler: (request, h) => {
console.info("New DR 5.0 request", request.query);
return handleRequest(h, request.query, ReportGeneratorR50DR);
},
});
server.route({
method: "GET",
path: "/reports",
handler: (request, h) => {
return [
{
Report_Name: "Title Master Report",
Report_ID: "TR",
Report_Description:
"A customizable report detailing activity at the title level that allows the user to apply filters and select other configuration options for the report.",
Path: "/reports/tr",
Release: "5",
},
{
Report_Name: "Database Master Report",
Report_ID: "DR",
Report_Description:
"A customizable report detailing activity at the database level that allows the user to apply filters and select other configuration options for the report.",
Path: "/reports/dr",
Release: "5",
},
];
},
});
// COP 5.1
server.route({
method: "GET",
path: "/r51/reports/tr",
handler: (request, h) => {
console.info("New TR 5.1 request", request.query);
return handleRequest(h, request.query, ReportGeneratorR51TR);
},
});
server.route({
method: "GET",
path: "/r51/reports/dr",
handler: (request, h) => {
console.info("New DR 5.1 request", request.query);
return handleRequest(h, request.query, ReportGeneratorR51DR);
},
});
// Common paths
server.route({
method: "GET",
path: "/",
handler: (request, h) => {
return {
message:
'This is a fake SUSHI server "sashimi". Use /reports/tr or /reports/dr to ' +
"get sample data for CoP 5.0 reports, or /r51/reports/tr and /r51/reports/dr for " +
"CoP 5.1 reports. Read more at https://github.com/Big-Dig-Data/sashimi",
};
},
});
server.route({
method: "GET",
path: "/{any*}",
handler: (request, h) => {
return h
.response(
"Sorry, this page does not exist. Try /reports/tr or /reports/dr. More info at " +
"https://github.com/Big-Dig-Data/sashimi"
)
.code(404)
.type("text/plain");
},
});
await server.start();
console.log("Server running on %s", server.info.uri);
};
process.on("unhandledRejection", (err) => {
console.log(err);
process.exit(1);
});
init();