-
Notifications
You must be signed in to change notification settings - Fork 3
E-Mail to Peppol sending #10
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
Open
daanlenaerts
wants to merge
6
commits into
main
Choose a base branch
from
feat-email-to-peppol-sending
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c62f1fe
Added endpoint ID handling in XML parsing utilities and schemas
kobeindemans b1c942e
Added email-to-Peppol sending feature
kobeindemans cdaecb2
Enhanced email notifications for email-to-peppol functionality
kobeindemans bb9718a
Make sendEmailSlug input read-only in CompanySendEmailManager
kobeindemans a02f610
Refactored email handling
kobeindemans 555a41f
Improved email styling
kobeindemans File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,73 @@ | ||
| import { Server } from "@recommand/lib/api"; | ||
| import { describeRoute } from "hono-openapi"; | ||
| import { zodValidator } from "@recommand/lib/zod-validator"; | ||
| import { actionSuccess, actionFailure } from "@recommand/lib/utils"; | ||
| import { requireCompanyAccess, type CompanyAccessContext } from "@peppol/utils/auth-middleware"; | ||
| import { updateCompany } from "@peppol/data/companies"; | ||
| import type { Context } from "@recommand/lib/api"; | ||
| import type { AuthenticatedUserContext, AuthenticatedTeamContext } from "@core/lib/auth-middleware"; | ||
| import { z } from "zod"; | ||
|
|
||
| const server = new Server(); | ||
|
|
||
| const updateOutboundEmailSchema = z.object({ | ||
| slug: z.string().min(1).max(50).regex(/^[a-z0-9-]+$/, "Slug must contain only lowercase letters, numbers, and hyphens"), | ||
| enabled: z.boolean(), | ||
| }); | ||
|
|
||
| type UpdateOutboundEmailContext = Context< | ||
| AuthenticatedUserContext & AuthenticatedTeamContext & CompanyAccessContext, | ||
| string, | ||
| { | ||
| in: { json: z.input<typeof updateOutboundEmailSchema> }; | ||
| out: { json: z.infer<typeof updateOutboundEmailSchema> }; | ||
| } | ||
| >; | ||
|
|
||
| const _updateOutboundEmail = server.put( | ||
| "/:teamId/companies/:companyId/email/outbound", | ||
| requireCompanyAccess(), | ||
| describeRoute({ | ||
| operationId: "updateCompanyOutboundEmail", | ||
| summary: "Update company outbound email settings", | ||
| description: "Update the email-to-Peppol sending settings for a company", | ||
| tags: ["Companies"], | ||
| }), | ||
| zodValidator("json", updateOutboundEmailSchema), | ||
| async (c: UpdateOutboundEmailContext) => { | ||
| try { | ||
| const { slug, enabled } = c.req.valid("json"); | ||
| const company = c.var.company; | ||
|
|
||
| const updatedCompany = await updateCompany({ | ||
| id: company.id, | ||
| teamId: company.teamId, | ||
| outboundEmailSlug: slug, | ||
| outboundEmailEnabled: enabled, | ||
| }); | ||
|
|
||
| const outboundEmailAddress = slug | ||
| ? `${slug}@out.recommand.eu` | ||
| : null; | ||
|
|
||
| return c.json( | ||
| actionSuccess({ | ||
| company: updatedCompany, | ||
| outboundEmailAddress, | ||
| }) | ||
| ); | ||
| } catch (error) { | ||
| console.error("Error updating outbound email settings:", error); | ||
| return c.json( | ||
| actionFailure( | ||
| error instanceof Error ? error.message : "Failed to update outbound email settings" | ||
| ), | ||
| 400 | ||
| ); | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| export type CompanyEmailOutbound = typeof _updateOutboundEmail; | ||
|
|
||
| export default server; | ||
This file contains hidden or 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 hidden or 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 hidden or 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,7 @@ | ||
| import { Server } from "@recommand/lib/api"; | ||
| import outboundServer from "./outbound"; | ||
|
|
||
| const server = new Server(); | ||
| server.route("/", outboundServer); | ||
|
|
||
| export default server; |
This file contains hidden or 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,101 @@ | ||
| import { Server } from "@recommand/lib/api"; | ||
| import { describeRoute } from "hono-openapi"; | ||
| import { actionSuccess, actionFailure } from "@recommand/lib/utils"; | ||
| import { sendEmail } from "@core/lib/email"; | ||
| import { sendSystemAlert } from "@peppol/utils/system-notifications/telegram"; | ||
| import { sendDocumentFromEmail } from "@peppol/data/email/send-document-from-email"; | ||
| import { requirePostmarkWebhookAuth } from "@peppol/utils/auth-middleware"; | ||
| import { EmailToPeppolError } from "@peppol/emails/email-to-peppol-error"; | ||
|
|
||
| const server = new Server(); | ||
|
|
||
| interface PostmarkInboundEmail { | ||
| FromFull: { | ||
| Email: string; | ||
| Name: string; | ||
| }; | ||
| To: string; | ||
| Subject: string; | ||
| TextBody: string; | ||
| HtmlBody: string; | ||
| Attachments: Array<{ | ||
| Name: string; | ||
| Content: string; | ||
| ContentType: string; | ||
| ContentLength: number; | ||
| }>; | ||
| MessageID: string; | ||
| Date: string; | ||
| } | ||
|
|
||
| server.post("/webhooks/email/outbound", requirePostmarkWebhookAuth(), describeRoute({ hide: true }), async (c) => { | ||
| try { | ||
| const inbound: PostmarkInboundEmail = await c.req.json(); | ||
| const toEmail = inbound.To.toLowerCase(); | ||
| const fromEmail = inbound.FromFull.Email; | ||
|
|
||
| const xmlAttachment = inbound.Attachments.find( | ||
| (a) => | ||
| a.ContentType === "application/xml" || | ||
| a.ContentType === "text/xml" || | ||
| a.Name.toLowerCase().endsWith(".xml") | ||
| ); | ||
|
|
||
| if (!xmlAttachment) { | ||
| await sendEmail({ | ||
| to: fromEmail, | ||
| subject: "Error: No XML attachment found", | ||
| email: EmailToPeppolError({ | ||
| error: "No XML attachment found", | ||
| hasXmlAttachment: false, | ||
| }), | ||
| }); | ||
| return c.json(actionSuccess({ error: "No XML attachment" })); | ||
| } | ||
|
|
||
| const xmlContent = Buffer.from(xmlAttachment.Content, "base64").toString( | ||
| "utf-8" | ||
| ); | ||
|
|
||
| const result = await sendDocumentFromEmail({ | ||
| toEmail, | ||
| fromEmail, | ||
| xmlContent, | ||
| }); | ||
|
|
||
| if (result.success) { | ||
| return c.json( | ||
| actionSuccess({ | ||
| documentId: result.documentId, | ||
| company: result.company, | ||
| type: result.type, | ||
| recipient: result.recipient, | ||
| }) | ||
| ); | ||
| } else { | ||
| return c.json( | ||
| actionSuccess({ | ||
| error: result.error, | ||
| company: result.company, | ||
| details: result.details, | ||
| }) | ||
| ); | ||
| } | ||
| } catch (error) { | ||
| console.error("Email webhook error:", error); | ||
| sendSystemAlert( | ||
| "Email to Peppol Processing Failed", | ||
| `Failed to process email. Error: \`\`\`\n${error}\n\`\`\``, | ||
| "error" | ||
| ); | ||
|
|
||
| return c.json( | ||
| actionFailure( | ||
| error instanceof Error ? error.message : "Failed to process email" | ||
| ), | ||
| 500 | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| export default server; |
This file contains hidden or 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,7 @@ | ||
| import { Server } from "@recommand/lib/api"; | ||
| import emailServer from "./email"; | ||
|
|
||
| const server = new Server(); | ||
| server.route("/", emailServer); | ||
|
|
||
| export default server; |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.