-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b11b26b
commit 9b32055
Showing
7 changed files
with
225 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { generateFeedbackCSV } from "./service/feedback/downloadFeedbackCSV"; | ||
import { DownloadFeedbackCSVController } from "./service/feedback/types"; | ||
|
||
export const downloadFeedbackCSV: DownloadFeedbackCSVController = async ( | ||
req, | ||
res, | ||
next, | ||
) => { | ||
try { | ||
const feedbackFishCookie = res.locals.parsedReq.query.cookie; | ||
const csvStream = await generateFeedbackCSV(feedbackFishCookie); | ||
res.header("Content-type", "text/csv"); | ||
return csvStream.pipe(res); | ||
} catch (error) { | ||
return next({ | ||
message: | ||
"Failed to generate FeedbackFish CSV: " + (error as Error).message, | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
openapi: 3.1.0 | ||
info: | ||
title: Plan✕ API | ||
version: 0.1.0 | ||
tags: | ||
- name: admin | ||
description: Admin only utility endpoints | ||
paths: | ||
/admin/feedback: | ||
get: | ||
tags: ["admin"] | ||
security: | ||
- bearerAuth: [] | ||
summary: Download FeedbackFish CSV | ||
parameters: | ||
- in: query | ||
name: cookie | ||
type: string | ||
description: Cookie from FeedbackFish to authenticate request | ||
required: true | ||
responses: | ||
"200": | ||
content: | ||
text/csv: | ||
schema: | ||
type: string | ||
"500": | ||
$ref: "#/components/responses/ErrorMessage" |
161 changes: 0 additions & 161 deletions
161
api.planx.uk/modules/admin/feedback/downloadFeedbackCSV.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
api.planx.uk/modules/admin/service/feedback/downloadFeedbackCSV.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { gql } from "graphql-request"; | ||
import Axios from "axios"; | ||
import { stringify } from "csv-stringify"; | ||
import { Feedback, METADATA_KEYS, ParsedFeedback } from "./types"; | ||
|
||
export const generateFeedbackCSV = async (feedbackFishCookie: string) => { | ||
const feedback = await fetchFeedback(feedbackFishCookie); | ||
const parsedFeedback = parseFeedback(feedback); | ||
const csvStream = stringify(parsedFeedback, { | ||
header: true, | ||
columns: [ | ||
"id", | ||
"text", | ||
"category", | ||
"createdAt", | ||
"location", | ||
"screenshotUrl", | ||
"device", | ||
...METADATA_KEYS, | ||
], | ||
}); | ||
return csvStream; | ||
}; | ||
|
||
const fetchFeedback = async (cookie: string): Promise<Feedback[]> => { | ||
const feedbackFishGraphQLEndpoint = "https://graphcdn.api.feedback.fish/"; | ||
const body = { | ||
query: gql` | ||
query getFeedback($projectId: String!) { | ||
feedback(projectId: $projectId) { | ||
id | ||
text | ||
category | ||
createdAt | ||
location | ||
screenshotUrl | ||
metadata { | ||
key | ||
value | ||
} | ||
device { | ||
client { | ||
name | ||
version | ||
} | ||
os { | ||
name | ||
version | ||
} | ||
} | ||
} | ||
} | ||
`, | ||
operationName: "getFeedback", | ||
variables: { | ||
projectId: "65f02de00b90d1", | ||
}, | ||
}; | ||
|
||
try { | ||
const result = await Axios.post(feedbackFishGraphQLEndpoint, body, { | ||
headers: { cookie }, | ||
}); | ||
const feedback: Feedback[] = result.data.data.feedback; | ||
return feedback; | ||
} catch (error) { | ||
throw Error( | ||
"Failed to connect to FeedbackFish: " + (error as Error).message, | ||
); | ||
} | ||
}; | ||
|
||
const generateMetadata = (feedback: ParsedFeedback): ParsedFeedback => { | ||
// Transform metadata into kv pairs | ||
feedback.metadata?.forEach(({ key, value }) => (feedback[key] = value)); | ||
// Drop redundant raw metadata | ||
delete feedback.metadata; | ||
return feedback; | ||
}; | ||
|
||
export const parseFeedback = (feedback: Feedback[]): ParsedFeedback[] => { | ||
const parsedFeedback: ParsedFeedback[] = [...feedback]; | ||
parsedFeedback.map(generateMetadata); | ||
return parsedFeedback; | ||
}; |
Oops, something went wrong.