-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhandler.js
134 lines (123 loc) · 4.3 KB
/
handler.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
import { createReadStream, createWriteStream } from 'node:fs';
import { Logger } from './src/logger.js';
import { PutObjectCommand } from '@aws-sdk/client-s3';
import { S3Client } from '@aws-sdk/client-s3';
import { getSurveyDetails } from './src/get-survey-details.js';
import { getSurveyResponseSchema } from './src/get-survey-response-schema.js';
import { getSurveyResponses } from './src/get-survey-responses.js';
//create the client outside of the handler:
//https://github.com/aws/aws-sdk-js-v3?tab=readme-ov-file#best-practices
const s3Client = new S3Client({});
export async function storeSurveys(event) {
const ids = process.env.SURVEY_IDS.split(',').filter(id => {
//filter out empty "\n" string values
return Boolean(id) && typeof id === 'string' && id.length > 5;
});
console.log('Processing Survey Ids: ', ids);
const promises = ids.map(id => storeSurvey(id.trim()));
const loggers = await Promise.all(promises);
const output = loggers.map(logger => logger?.getEvents());
console.log(output);
return { message: output, event };
}
const storeSurvey = async (surveyId) => {
const logger = new Logger();
try {
await Promise.all([
storeSurveyResponses(surveyId, logger),
storeSurveyResponseSchema(surveyId, logger),
storeSurveyDetails(surveyId, logger),
storeCSVSurvey(surveyId, logger),
]);
logger.addEvent(`Stored ${surveyId}`);
return logger;
} catch (e) {
console.log(`Error fetching: ${surveyId}`);
console.log(e);
console.log(logger.getEvents());
}
};
const storeCSVSurvey = async (surveyId, logger) => {
logger.addEvent(`getting csv response data for survey: ${surveyId}`);
const fileName = `/tmp/${surveyId}.csv`;
const destinationStream = createWriteStream(fileName);
await getSurveyResponses(
process.env.QUALTRICS_API_TOKEN,
process.env.QUALTRICS_DATA_CENTER,
surveyId,
destinationStream,
'csv',
logger,
);
logger.addEvent(`csv response data extracted, writing to S3 bucket ${process.env.BUCKET} ${surveyId}.csv`);
const params = {
Bucket: process.env.BUCKET,
Key: `${surveyId}.csv`,
Body: createReadStream(fileName)
};
const command = new PutObjectCommand(params);
await s3Client.send(command);
logger.addEvent('done!');
};
const storeSurveyResponses = async (surveyId, logger) => {
logger.addEvent(`getting response data for survey: ${surveyId}`);
const fileName = `/tmp/${surveyId}-responses.json`;
const destinationStream = createWriteStream(fileName);
await getSurveyResponses(
process.env.QUALTRICS_API_TOKEN,
process.env.QUALTRICS_DATA_CENTER,
surveyId,
destinationStream,
'json',
logger,
);
logger.addEvent(`response data extracted, writing to S3 bucket ${process.env.BUCKET} ${surveyId}-responses.json`);
const params = {
Bucket: process.env.BUCKET,
Key: `${surveyId}-responses.json`,
Body: createReadStream(fileName)
};
const command = new PutObjectCommand(params);
await s3Client.send(command);
logger.addEvent('done!');
};
const storeSurveyResponseSchema = async (surveyId, logger) => {
logger.addEvent(`getting schema for survey: ${surveyId}`);
const fileName = `/tmp/${surveyId}-schema.json`;
await getSurveyResponseSchema(
process.env.QUALTRICS_API_TOKEN,
process.env.QUALTRICS_DATA_CENTER,
surveyId,
fileName,
logger,
);
logger.addEvent(`schema extracted, writing to S3 bucket ${process.env.BUCKET} ${surveyId}-schema.json`);
const params = {
Bucket: process.env.BUCKET,
Key: `${surveyId}-schema.json`,
Body: createReadStream(fileName)
};
const command = new PutObjectCommand(params);
await s3Client.send(command);
logger.addEvent('done!');
};
const storeSurveyDetails = async (surveyId, logger) => {
logger.addEvent(`getting details for survey: ${surveyId}`);
const fileName = `/tmp/${surveyId}-survey.json`;
await getSurveyDetails(
process.env.QUALTRICS_API_TOKEN,
process.env.QUALTRICS_DATA_CENTER,
surveyId,
fileName,
logger,
);
logger.addEvent(`details extracted, writing to S3 bucket ${process.env.BUCKET} ${surveyId}-survey.json`);
const params = {
Bucket: process.env.BUCKET,
Key: `${surveyId}-survey.json`,
Body: createReadStream(fileName)
};
const command = new PutObjectCommand(params);
await s3Client.send(command);
logger.addEvent('done!');
};