Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/survey #10

Merged
merged 5 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ custom:
ssm:
QUALTRICS_API_TOKEN: ${ssm:qualtrics-api-token}
SURVEY_ID: ${ssm:survey-id}
HGSE_SURVEY_ID: ${ssm:hgse-survey-id}
DATABASE_URL: ${ssm:db-url}
CORE_BASE_URL: ${ssm:core-base-url}
DATABASE_PASSWORD: ${ssm:survey-db-password}
Expand Down
8 changes: 5 additions & 3 deletions src/http/get-generated-pdf/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import 'dotenv/config';
import { generatePdf } from './services';
import { ExtendedApiGateWayEvent } from './utils';

export const getGeneratedPdf = async ({
queryStringParameters: { generatedId },
}: ExtendedApiGateWayEvent) => {
export const getGeneratedPdf = async (event: ExtendedApiGateWayEvent) => {
console.info('getGeneratedPdf Event', JSON.stringify(event, null, 2));
const {
queryStringParameters: { generatedId },
} = event;
try {
const pdf = await generatePdf(generatedId);

Expand Down
8 changes: 5 additions & 3 deletions src/http/get-policy/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { longPolling } from './services/long-polling';
import { ExtendedApiGateWayEvent } from './utils/types';

// TODO - make this similar to update policy handler
export const getPolicyHandler = async ({
queryStringParameters: { generatedId },
}: ExtendedApiGateWayEvent) => {
export const getPolicyHandler = async (event: ExtendedApiGateWayEvent) => {
console.info('getPolicyHandler Event', JSON.stringify(event, null, 2));
const {
queryStringParameters: { generatedId },
} = event;
try {
const aiPolicy = await longPolling(generatedId);

Expand Down
11 changes: 6 additions & 5 deletions src/http/get-publish-policy/handler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import 'dotenv/config';
import { ExtendedApiGateWayEvent } from './utils/types';
import { db } from '../../../data/knex';
import { db } from '../../../data';

// TODO - make this similar to update policy handler
export const getPolicyHandler = async ({
queryStringParameters: { publishId },
}: ExtendedApiGateWayEvent) => {
export const getPolicyHandler = async (event: ExtendedApiGateWayEvent) => {
console.info('getPolicyHandler Event', JSON.stringify(event, null, 2));
const {
queryStringParameters: { publishId },
} = event;
try {
const aiPolicy = await db('publish_policies')
.where({ id: publishId })
Expand Down
22 changes: 9 additions & 13 deletions src/http/post-policy-webhook/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@ import { getFullResponseData } from './services/get-full-response-data';
import { hgseSurveyResponseMapper } from "./services/hgse-survey-response-mapper";
import { SurveyResponse } from "../../shared";

const { SURVEY_ID } = process.env;

export const postPolicyWebhookHandler = async ({
parsedBody: { responseId: surveyResponseId, organization },
}: ExtendedApiGateWayEvent) => {
export const postPolicyWebhookHandler = async (event: ExtendedApiGateWayEvent) => {
try {
console.info({
message: 'Qualtrics Webhook',
surveyId: SURVEY_ID,
responseId: surveyResponseId,
});
console.info('postPolicyWebhookHandler Event', JSON.stringify(event, null, 2));

const {
parsedBody: { responseId: surveyResponseId, organization: institution },
} = event;

const data = await getFullResponseData(surveyResponseId);
const data = await getFullResponseData(surveyResponseId, institution || '');

const mapper = (organization?: string) => (response: SurveyResponse) => {
if (organization === 'harvard') {
Expand All @@ -31,8 +27,8 @@ export const postPolicyWebhookHandler = async ({
}

const savedPolicyId = flow(
mapper(organization),
createCoursePolicy(organization),
mapper(institution),
createCoursePolicy(institution),
await saveCoursePolicy(data)
)(data.result);

Expand Down
11 changes: 7 additions & 4 deletions src/http/post-policy-webhook/services/get-full-response-data.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import 'dotenv/config';

import { QaultricsResponse, qualtricsBaseUrl } from '../../../shared';
import {surveyIdMapper} from "../utils/helpers";

const { SURVEY_ID, QUALTRICS_API_TOKEN } = process.env;
const { QUALTRICS_API_TOKEN } = process.env;

export const getFullResponseData = async (
id: string
id: string,
institution: string
): Promise<QaultricsResponse> => {
const surveyId = surveyIdMapper(institution);
const response = await fetch(
`${qualtricsBaseUrl}/surveys/${process.env.SURVEY_ID}/responses/${id}`,
`${qualtricsBaseUrl}/surveys/${surveyId}/responses/${id}`,
{
method: 'get',
headers: {
Expand All @@ -22,7 +25,7 @@ export const getFullResponseData = async (
const errorMessage = await response.text();
console.error({
message: `Qualtrics API call failed: ${errorMessage}`,
surveyId: SURVEY_ID,
surveyId,
QUALTRICS_API_TOKEN: QUALTRICS_API_TOKEN?.length,
responseId: id,
});
Expand Down
5 changes: 5 additions & 0 deletions src/http/post-policy-webhook/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ export const toTitleCase = (str: string) => {
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
};

export const surveyIdMapper = (institution: string) => {
const { SURVEY_ID, HGSE_SURVEY_ID } = process.env;
return institution === 'harvard' ? HGSE_SURVEY_ID : SURVEY_ID;
};
11 changes: 7 additions & 4 deletions src/http/post-publish-policy/handler.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import 'dotenv/config';
import { ExtendedApiGateWayEvent } from './utils/types';
import { db } from '../../../data/knex';
import { db } from '../../../data';

export const postPublishPolicyHandler = async ({
parsedBody: { publishId, policyId, policy },
}: ExtendedApiGateWayEvent) => {
export const postPublishPolicyHandler = async (event: ExtendedApiGateWayEvent) => {
console.info('postPublishPolicyHandler Event', JSON.stringify(event, null, 2));

const {
parsedBody: { publishId, policyId, policy },
} = event;
try {
const exists = await db('publish_policies')
.where({ id: publishId })
Expand Down
11 changes: 7 additions & 4 deletions src/http/update-policy/handler.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { ExtendedApiGateWayEvent } from './utils/types';
import { updatePolicy } from './services';

export const updatePolicyHandler = async ({
queryStringParameters: { id, generatedId },
parsedBody: { policy },
}: ExtendedApiGateWayEvent) => {
export const updatePolicyHandler = async (event: ExtendedApiGateWayEvent) => {
console.info('updatePolicyHandler Event', JSON.stringify(event, null, 2))

const {
queryStringParameters: { id, generatedId },
parsedBody: { policy },
} = event;
try {
const updated = await updatePolicy(id, policy, generatedId);

Expand Down
1 change: 1 addition & 0 deletions test/testEnvironments.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const QUALTRICS_API_TOKEN = 'apiToken';
export const SURVEY_ID = 'surveyId';
export const HGSE_SURVEY_ID = 'hgseSurveyId';
export const CORE_BASE_URL = 'https://coursepolicy.ai';
export const DATABASE_PASSWORD = 'password';
export const DATABASE_USERNAME = 'postgres';
Expand Down
Loading