-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Survey: sent survey response via email when survey feedback is submitted
- Loading branch information
1 parent
12c62cc
commit 6392fac
Showing
3 changed files
with
217 additions
and
1 deletion.
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,154 @@ | ||
import Mailjet from "node-mailjet" | ||
import previewEmail from "preview-email" | ||
import { getPrdOrStgDomain } from "src/core/components/links/getDomain" | ||
|
||
type SurveyFeedbackMailer = { | ||
userMail: string | ||
userFirstname: string | ||
userLastname: string | ||
feedbackLocation: { lng: number; lat: number } | ||
feedbackCategory: string | ||
feedbackText: string | ||
lineID: string | ||
} | ||
|
||
export function surveyFeedbackMailer({ | ||
userMail, | ||
userFirstname, | ||
userLastname, | ||
feedbackLocation, | ||
feedbackCategory, | ||
feedbackText, | ||
lineID, | ||
}: SurveyFeedbackMailer) { | ||
const origin = getPrdOrStgDomain() | ||
// mailjet format | ||
const msg = { | ||
From: { Email: "<[email protected]>", Name: "Trassenscout" }, | ||
To: [{ Email: userMail }], | ||
Subject: "Beteiligung zum Radnetz Brandenburg: Ihr eingereichter Hinweis", | ||
TextPart: ` | ||
Guten Tag, ${userFirstname} ${userLastname}, | ||
vielen Dank für Ihre Teilnahme! Hiermit bestätigen wir Ihnen den Eingang Ihres Beitrags mit folgenden Angaben: | ||
Eingangsdatum: ${new Date().toLocaleDateString("de-DE")} | ||
Kategorie: ${feedbackCategory} | ||
ID der gewählten Verbindung im Netzentwurf: ${lineID} | ||
Ihr Hinweis: | ||
${feedbackText} | ||
Ortsbezug des Beitrags (in OpenStreetMap): https://www.openstreetmap.org/?mlat=${ | ||
feedbackLocation.lat | ||
}&mlon=${feedbackLocation.lng}=16 | ||
Hinweis: Beachten Sie, dass Sie für jeden eingereichten Hinweis eine separate E-Mail erhalten. Dies ist eine automatisiert versandte E-Mail, auf die Sie nicht antworten können. | ||
Nach Abschluss der Beteiligung werden Ihre Hinweise fachlich geprüft und in die Abwägung zur Überarbeitung des Zielnetzentwurfs mit einbezogen. | ||
Mit freundlichen Grüßen | ||
i.A. das Team von FixMyCity | ||
FixMyCity GmbH | ||
Oberlandstraße 26-35 | ||
12099 Berlin | ||
mail: [email protected] | ||
www.fixmycity.de | ||
`, | ||
HTMLPart: ` | ||
<p> | ||
Guten Tag, ${userFirstname} ${userLastname}, | ||
</p> | ||
<p> | ||
vielen Dank für Ihre Teilnahme! Hiermit bestätigen wir Ihnen den Eingang Ihres Beitrags mit | ||
folgenden Angaben: | ||
</p> | ||
<p> | ||
Eingangsdatum: ${new Date().toLocaleDateString("de-DE")} <br/> | ||
Kategorie: ${feedbackCategory} <br /> | ||
ID der gewählten Verbindung im Netzentwurf: ${lineID} | ||
<br /> | ||
Ihr Hinweis: <br /> | ||
<i>${feedbackText}</i> | ||
<br /> | ||
<a target="_blank" | ||
href=https://www.openstreetmap.org/?mlat=${feedbackLocation.lat}&mlon=${ | ||
feedbackLocation.lng | ||
}=16 | ||
> | ||
Ortsbezug des Beitrags (in OpenStreetMap) | ||
</a> | ||
</p> | ||
<p> | ||
<i>Hinweis:</i> | ||
Beachten Sie, dass Sie für jeden eingereichten Hinweis eine separate E-Mail erhalten. Dies | ||
ist eine automatisiert versandte E-Mail, auf die Sie nicht antworten können. | ||
</p> | ||
<p> | ||
Nach Abschluss der Beteiligung werden Ihre Hinweise fachlich geprüft und in die Abwägung zur | ||
Überarbeitung des Zielnetzentwurfs mit einbezogen. | ||
</p> | ||
<p> | ||
Mit freundlichen Grüßen <br /> | ||
i.A. das Team von FixMyCity | ||
</p> | ||
<p> | ||
<small>FixMyCity GmbH</small> | ||
<br /> | ||
<small>Oberlandstraße 26-35</small> | ||
<br /> | ||
<small>12099 Berlin</small> | ||
<br /> | ||
<small> | ||
mail: <a href="mailto:[email protected]">[email protected]</a> | ||
</small> | ||
<br /> | ||
<small> | ||
<a href="https://www.fixmycity.de/">www.fixmycity.de</a> | ||
</small> | ||
<br /> | ||
<small> | ||
<a href="https://de.linkedin.com/company/fixmycity?trk=public_profile_topcard-current-company"> | ||
</a> | ||
</small> | ||
</p> | ||
<img | ||
src="https://images.squarespace-cdn.com/content/v1/605af904f08f5246bc415204/1616576639935-WCJVD5JU8LNMBBSFIYHV/Logo_Positiv%402x.png?format=1500w" | ||
alt="FixMyCity Logo" | ||
width="60" | ||
/> | ||
`, | ||
} | ||
|
||
return { | ||
async send() { | ||
if (process.env.NODE_ENV !== "development") { | ||
const mailjet = Mailjet.apiConnect( | ||
// @ts-ignore | ||
process.env.MAILJET_APIKEY_PUBLIC, | ||
process.env.MAILJET_APIKEY_PRIVATE, | ||
) | ||
const request = mailjet.post("send", { version: "v3.1" }).request({ Messages: [msg] }) | ||
request | ||
.then((result) => { | ||
console.log(result.body) | ||
}) | ||
.catch((err) => { | ||
console.error(err.statusCode) | ||
}) | ||
} else { | ||
// Preview email in the browser | ||
var { From, To, Subject, TextPart, HTMLPart } = msg | ||
await previewEmail({ | ||
from: `${From.Name} <${From.Email}>`, | ||
// @ts-ignore | ||
to: `${To[0]?.Name || ""} <${To[0].Email}>`, | ||
subject: Subject, | ||
text: TextPart, | ||
html: HTMLPart, | ||
}) | ||
} | ||
}, | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { resolver } from "@blitzjs/rpc" | ||
import db from "db" | ||
import { surveyFeedbackMailer } from "mailers/surveyFeedbackMailer" | ||
import { z } from "zod" | ||
import { feedbackDefinition } from "src/survey-public/radnetz-brandenburg/data/feedback" | ||
|
||
export const SurveyFeedbackMail = z.object({ | ||
surveySessionId: z.number(), | ||
data: z.record(z.any()), | ||
}) | ||
|
||
export default resolver.pipe( | ||
resolver.zod(SurveyFeedbackMail), | ||
async ({ surveySessionId, data }) => { | ||
// Get first survey part for personal data and email adress | ||
const surveyPart1 = await db.surveyResponse.findFirst({ | ||
where: { surveySessionId: surveySessionId, surveyPart: 1 }, | ||
}) | ||
|
||
const categories = Object.fromEntries( | ||
// @ts-expect-error | ||
feedbackDefinition.pages[0]?.questions | ||
// todo survey clean up or refactor after survey BB | ||
// the category ref is hard coded | ||
.find((q) => q.id == 22) | ||
// @ts-expect-error | ||
.props.responses.map((response) => [response.id, response.text.de]), | ||
) | ||
|
||
if (surveyPart1) { | ||
const parsedSurveyPart1 = JSON.parse(surveyPart1?.data) | ||
// Send the email | ||
// todo survey clean up or refactor after survey BB | ||
// the refs are hard coded to reduce the complexity of the code as we do not know if we keep this feature | ||
await surveyFeedbackMailer({ | ||
// @ts-expect-error | ||
userMail: parsedSurveyPart1["3"], | ||
// @ts-expect-error | ||
userFirstname: parsedSurveyPart1["1"], | ||
// @ts-expect-error | ||
userLastname: parsedSurveyPart1["2"], | ||
feedbackLocation: data["24"], | ||
feedbackCategory: categories[data["22"]], | ||
feedbackText: data["25"], | ||
lineID: data["20"], | ||
}).send() | ||
} | ||
return | ||
}, | ||
) |