Skip to content

Commit

Permalink
836: send verification emails
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahsporck committed Mar 16, 2023
1 parent e33bb3f commit 1f7c2f8
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const ApplyController = (): React.ReactElement | null => {
const [regionId, application] = validationResult.value

addBlueEakApplication({
variables: { regionId, application },
variables: { regionId, application, project: projectId },
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mutation addEakApplication($regionId: Int!, $application: ApplicationInput!) {
result: addEakApplication(regionId: $regionId, application: $application)
mutation addEakApplication($regionId: Int!, $application: ApplicationInput!, $project: String!) {
result: addEakApplication(regionId: $regionId, application: $application, project: $project)
}
6 changes: 6 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package app.ehrenamtskarte.backend.application.webservice

import app.ehrenamtskarte.backend.application.database.ApplicationEntity
import app.ehrenamtskarte.backend.application.database.ApplicationVerificationEntity
import app.ehrenamtskarte.backend.application.database.repos.ApplicationRepository
import app.ehrenamtskarte.backend.application.webservice.schema.create.Application
import app.ehrenamtskarte.backend.auth.database.AdministratorEntity
import app.ehrenamtskarte.backend.auth.service.Authorizer.mayDeleteApplicationsInRegion
import app.ehrenamtskarte.backend.common.webservice.GraphQLContext
import app.ehrenamtskarte.backend.common.webservice.UnauthorizedException
import app.ehrenamtskarte.backend.mail.Mailer
import com.expediagroup.graphql.generator.annotations.GraphQLDescription
import graphql.schema.DataFetchingEnvironment
import org.jetbrains.exposed.sql.transactions.transaction
import java.net.URLEncoder
import java.nio.charset.StandardCharsets

@Suppress("unused")
class EakApplicationMutationService {
Expand All @@ -18,29 +22,63 @@ class EakApplicationMutationService {
fun addEakApplication(
regionId: Int,
application: Application,
project: String,
dfe: DataFetchingEnvironment,
): Boolean {
val context = dfe.getContext<GraphQLContext>()
val backendConfig = context.backendConfiguration
val projectConfig = backendConfig.projects.first { it.id == project }

// Validate that all files are png, jpeg or pdf files and at most 5MB.
val allowedContentTypes = setOf("application/pdf", "image/png", "image/jpeg")
val maxFileSizeBytes = 5 * 1000 * 1000
if (!context.files.all { it.contentType in allowedContentTypes && it.size <= maxFileSizeBytes }) {
throw IllegalArgumentException("An uploaded file does not adhere to the file upload requirements.")
}

val (applicationEntity, verificationEntities) = ApplicationRepository.persistApplication(
application.toJsonField(),
application.extractApplicationVerifications(),
regionId,
context.applicationData,
context.files,
)
transaction {
val (applicationEntity, verificationEntities) = ApplicationRepository.persistApplication(
application.toJsonField(),
application.extractApplicationVerifications(),
regionId,
context.applicationData,
context.files,
)

// TODO: Send mails
for (applicationVerification in verificationEntities) {
Mailer.sendMail(
backendConfig,
projectConfig.smtp,
projectConfig.administrationName,
applicationVerification.contactEmailAddress,
"Antrag Verifizieren",
generateApplicationVerificationMailMessage(projectConfig.administrationName, projectConfig.administrationBaseUrl, applicationVerification)
)
}
}

return true
}

private fun generateApplicationVerificationMailMessage(
administrationName: String,
administrationBaseUrl: String,
applicationVerification: ApplicationVerificationEntity
): String {
return """
Guten Tag ${applicationVerification.contactName},
Sie wurden gebeten, die Angaben eines Antrags auf Ehrenamtskarte zu bestätigen. Die Antragsstellerin oder der
Antragssteller hat Sie als Kontaktperson der Organisation ${applicationVerification.organizationName} angegeben.
Sie können den Antrag unter folgenden Link einsehen und verifizieren oder ablehnen:
$administrationBaseUrl/antrag-verifizieren/${URLEncoder.encode(applicationVerification.accessKey, StandardCharsets.UTF_8)}
Dies ist eine automatisierte Nachricht. Antworten Sie nicht auf diese Email.
- $administrationName
""".trimIndent()
}

@GraphQLDescription("Deletes the application with specified id")
fun deleteApplication(
applicationId: Int,
Expand Down
2 changes: 1 addition & 1 deletion specs/backend-api.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type Mutation {
"Stores a batch of new digital entitlementcards"
addCards(cards: [CardGenerationModelInput!]!): Boolean!
"Stores a new application for an EAK"
addEakApplication(application: ApplicationInput!, regionId: Int!): Boolean!
addEakApplication(application: ApplicationInput!, project: String!, regionId: Int!): Boolean!
"Changes an administrator's password"
changePassword(currentPassword: String!, email: String!, newPassword: String!, project: String!): Boolean!
"Creates a new administrator"
Expand Down

0 comments on commit 1f7c2f8

Please sign in to comment.