Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

refactor(intake-email): mutualized intake-emails creation in drivers (#137)

Corentin Thomasset416b9d50b61f
diff --git a/apps/papra-client/src/modules/intake-emails/intake-emails.models.ts b/apps/papra-client/src/modules/intake-emails/intake-emails.models.ts
deleted file mode 100644
index 17d8d5f7..00000000
--- a/apps/papra-client/src/modules/intake-emails/intake-emails.models.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export function formatIntakeEmail({ domain, username }: { domain: string; username: string }) {
- return `${username}@${domain}`;
-}
diff --git a/apps/papra-client/src/modules/intake-emails/intake-emails.types.ts b/apps/papra-client/src/modules/intake-emails/intake-emails.types.ts
index b21562ca..8fc9fc55 100644
--- a/apps/papra-client/src/modules/intake-emails/intake-emails.types.ts
+++ b/apps/papra-client/src/modules/intake-emails/intake-emails.types.ts
@@ -1,5 +1,6 @@
export type IntakeEmail = {
id: string;
+ emailAddress: string;
organizationId: string;
isEnabled: boolean;
allowedOrigins: string[];
diff --git a/apps/papra-client/src/modules/intake-emails/pages/intake-emails.page.tsx b/apps/papra-client/src/modules/intake-emails/pages/intake-emails.page.tsx
index 980692e9..d8b616db 100644
--- a/apps/papra-client/src/modules/intake-emails/pages/intake-emails.page.tsx
+++ b/apps/papra-client/src/modules/intake-emails/pages/intake-emails.page.tsx
@@ -17,11 +17,9 @@ import { createQuery } from '@tanstack/solid-query';
import { type Component, For, type JSX, Show, Suspense } from 'solid-js';
import { createSignal } from 'solid-js';
import * as v from 'valibot';
-import { formatIntakeEmail } from '../intake-emails.models';
import { createIntakeEmail, deleteIntakeEmail, fetchIntakeEmails, updateIntakeEmail } from '../intake-emails.services';
const AllowedOriginsDialog: Component<{ children: (props: DialogTriggerProps) => JSX.Element; intakeEmails: IntakeEmail }> = (props) => {
- const { config } = useConfig();
const [getAllowedOrigins, setAllowedOrigins] = createSignal([...props.intakeEmails.allowedOrigins]);
const update = async () => {
@@ -32,11 +30,6 @@ const AllowedOriginsDialog: Component<{ children: (props: DialogTriggerProps) =>
});
};
- const getIntakeAddress = () => formatIntakeEmail({
- domain: config.intakeEmails.emailGenerationDomain,
- username: props.intakeEmails.id,
- });
-
const deleteAllowedOrigin = async ({ origin }: { origin: string }) => {
setAllowedOrigins(origins => origins.filter(o => o !== origin));
await update();
@@ -76,7 +69,7 @@ const AllowedOriginsDialog: Component<{ children: (props: DialogTriggerProps) =>
<DialogDescription>
Only emails sent to
{' '}
- <span class="font-medium text-primary">{getIntakeAddress()}</span>
+ <span class="font-medium text-primary">{props.intakeEmails.emailAddress}</span>
{' '}
from these origins will be processed. If no origins are specified, all emails will be discarded.
</DialogDescription>
@@ -263,10 +256,7 @@ export const IntakeEmailsPage: Component = () => {
<div>
<div class="font-medium">
- {formatIntakeEmail({
- domain: config.intakeEmails.emailGenerationDomain,
- username: intakeEmail.id,
- })}
+ {intakeEmail.emailAddress}
<Show when={!intakeEmail.isEnabled}>
<span class="text-muted-foreground text-xs ml-2">(Disabled)</span>
diff --git a/apps/papra-server/migrations/0005_intake-emails-addresses.sql b/apps/papra-server/migrations/0005_intake-emails-addresses.sql
new file mode 100644
index 00000000..f6be2d04
--- /dev/null
+++ b/apps/papra-server/migrations/0005_intake-emails-addresses.sql
@@ -0,0 +1,3 @@
+ALTER TABLE `intake_emails` ADD `email_address` text NOT NULL;--> statement-breakpoint
+CREATE UNIQUE INDEX `intake_emails_email_address_unique` ON `intake_emails` (`email_address`);--> statement-breakpoint
+CREATE INDEX `documents_organization_id_is_deleted_index` ON `documents` (`organization_id`,`is_deleted`);
\ No newline at end of file
diff --git a/apps/papra-server/migrations/meta/0005_snapshot.json b/apps/papra-server/migrations/meta/0005_snapshot.json
new file mode 100644
index 00000000..21820fa8
--- /dev/null
+++ b/apps/papra-server/migrations/meta/0005_snapshot.json
@@ -0,0 +1,942 @@
+{
+ "version": "6",
+ "dialect": "sqlite",
+ "id": "64c5c91e-42f1-45e8-bda0-934852ef45c9",
+ "prevId": "2924235c-4fd1-461c-ba1c-6851da94e9a7",
+ "tables": {
+ "documents": {
+ "name": "documents",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "is_deleted": {
+ "name": "is_deleted",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "deleted_at": {
+ "name": "deleted_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "organization_id": {
+ "name": "organization_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "created_by": {
+ "name": "created_by",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "deleted_by": {
+ "name": "deleted_by",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "original_name": {
+ "name": "original_name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "original_size": {
+ "name": "original_size",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 0
+ },
+ "original_storage_key": {
+ "name": "original_storage_key",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "name": {
+ "name": "name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "mime_type": {
+ "name": "mime_type",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "content": {
+ "name": "content",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "''"
+ }
+ },
+ "indexes": {
+ "documents_organization_id_is_deleted_created_at_index": {
+ "name": "documents_organization_id_is_deleted_created_at_index",
+ "columns": [
+ "organization_id",
+ "is_deleted",
+ "created_at"
+ ],
+ "isUnique": false
+ },
+ "documents_organization_id_is_deleted_index": {
+ "name": "documents_organization_id_is_deleted_index",
+ "columns": [
+ "organization_id",
+ "is_deleted"
+ ],
+ "isUnique": false
+ }
+ },
+ "foreignKeys": {
+ "documents_organization_id_organizations_id_fk": {
+ "name": "documents_organization_id_organizations_id_fk",
+ "tableFrom": "documents",
+ "tableTo": "organizations",
+ "columnsFrom": [
+ "organization_id"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "cascade",
+ "onUpdate": "cascade"
+ },
+ "documents_created_by_users_id_fk": {
+ "name": "documents_created_by_users_id_fk",
+ "tableFrom": "documents",
+ "tableTo": "users",
+ "columnsFrom": [
+ "created_by"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "set null",
+ "onUpdate": "cascade"
+ },
+ "documents_deleted_by_users_id_fk": {
+ "name": "documents_deleted_by_users_id_fk",
+ "tableFrom": "documents",
+ "tableTo": "users",
+ "columnsFrom": [
+ "deleted_by"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "set null",
+ "onUpdate": "cascade"
+ }
+ },
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "organization_users": {
+ "name": "organization_users",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "user_id": {
+ "name": "user_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "organization_id": {
+ "name": "organization_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "role": {
+ "name": "role",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'member'"
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {
+ "organization_users_user_id_users_id_fk": {
+ "name": "organization_users_user_id_users_id_fk",
+ "tableFrom": "organization_users",
+ "tableTo": "users",
+ "columnsFrom": [
+ "user_id"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "cascade",
+ "onUpdate": "cascade"
+ },
+ "organization_users_organization_id_organizations_id_fk": {
+ "name": "organization_users_organization_id_organizations_id_fk",
+ "tableFrom": "organization_users",
+ "tableTo": "organizations",
+ "columnsFrom": [
+ "organization_id"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "cascade",
+ "onUpdate": "cascade"
+ }
+ },
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "organizations": {
+ "name": "organizations",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "name": {
+ "name": "name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "user_roles": {
+ "name": "user_roles",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "user_id": {
+ "name": "user_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "role": {
+ "name": "role",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ }
+ },
+ "indexes": {
+ "user_roles_role_index": {
+ "name": "user_roles_role_index",
+ "columns": [
+ "role"
+ ],
+ "isUnique": false
+ },
+ "user_roles_user_id_role_unique_index": {
+ "name": "user_roles_user_id_role_unique_index",
+ "columns": [
+ "user_id",
+ "role"
+ ],
+ "isUnique": true
+ }
+ },
+ "foreignKeys": {
+ "user_roles_user_id_users_id_fk": {
+ "name": "user_roles_user_id_users_id_fk",
+ "tableFrom": "user_roles",
+ "tableTo": "users",
+ "columnsFrom": [
+ "user_id"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "cascade",
+ "onUpdate": "cascade"
+ }
+ },
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "documents_tags": {
+ "name": "documents_tags",
+ "columns": {
+ "document_id": {
+ "name": "document_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "tag_id": {
+ "name": "tag_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {
+ "documents_tags_document_id_documents_id_fk": {
+ "name": "documents_tags_document_id_documents_id_fk",
+ "tableFrom": "documents_tags",
+ "tableTo": "documents",
+ "columnsFrom": [
+ "document_id"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "cascade",
+ "onUpdate": "cascade"
+ },
+ "documents_tags_tag_id_tags_id_fk": {
+ "name": "documents_tags_tag_id_tags_id_fk",
+ "tableFrom": "documents_tags",
+ "tableTo": "tags",
+ "columnsFrom": [
+ "tag_id"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "cascade",
+ "onUpdate": "cascade"
+ }
+ },
+ "compositePrimaryKeys": {
+ "documents_tags_pkey": {
+ "columns": [
+ "document_id",
+ "tag_id"
+ ],
+ "name": "documents_tags_pkey"
+ }
+ },
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "tags": {
+ "name": "tags",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "organization_id": {
+ "name": "organization_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "name": {
+ "name": "name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "color": {
+ "name": "color",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "description": {
+ "name": "description",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ }
+ },
+ "indexes": {
+ "tags_organization_id_name_unique": {
+ "name": "tags_organization_id_name_unique",
+ "columns": [
+ "organization_id",
+ "name"
+ ],
+ "isUnique": true
+ }
+ },
+ "foreignKeys": {
+ "tags_organization_id_organizations_id_fk": {
+ "name": "tags_organization_id_organizations_id_fk",
+ "tableFrom": "tags",
+ "tableTo": "organizations",
+ "columnsFrom": [
+ "organization_id"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "cascade",
+ "onUpdate": "cascade"
+ }
+ },
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "users": {
+ "name": "users",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "email": {
+ "name": "email",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "email_verified": {
+ "name": "email_verified",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "name": {
+ "name": "name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "image": {
+ "name": "image",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ }
+ },
+ "indexes": {
+ "users_email_unique": {
+ "name": "users_email_unique",
+ "columns": [
+ "email"
+ ],
+ "isUnique": true
+ },
+ "users_email_index": {
+ "name": "users_email_index",
+ "columns": [
+ "email"
+ ],
+ "isUnique": false
+ }
+ },
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "auth_accounts": {
+ "name": "auth_accounts",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "user_id": {
+ "name": "user_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "account_id": {
+ "name": "account_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "provider_id": {
+ "name": "provider_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "access_token": {
+ "name": "access_token",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "refresh_token": {
+ "name": "refresh_token",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "access_token_expires_at": {
+ "name": "access_token_expires_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "refresh_token_expires_at": {
+ "name": "refresh_token_expires_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "scope": {
+ "name": "scope",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "id_token": {
+ "name": "id_token",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "password": {
+ "name": "password",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {
+ "auth_accounts_user_id_users_id_fk": {
+ "name": "auth_accounts_user_id_users_id_fk",
+ "tableFrom": "auth_accounts",
+ "tableTo": "users",
+ "columnsFrom": [
+ "user_id"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "cascade",
+ "onUpdate": "cascade"
+ }
+ },
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "auth_sessions": {
+ "name": "auth_sessions",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "token": {
+ "name": "token",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "user_id": {
+ "name": "user_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "expires_at": {
+ "name": "expires_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "ip_address": {
+ "name": "ip_address",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "user_agent": {
+ "name": "user_agent",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ }
+ },
+ "indexes": {
+ "auth_sessions_token_index": {
+ "name": "auth_sessions_token_index",
+ "columns": [
+ "token"
+ ],
+ "isUnique": false
+ }
+ },
+ "foreignKeys": {
+ "auth_sessions_user_id_users_id_fk": {
+ "name": "auth_sessions_user_id_users_id_fk",
+ "tableFrom": "auth_sessions",
+ "tableTo": "users",
+ "columnsFrom": [
+ "user_id"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "cascade",
+ "onUpdate": "cascade"
+ }
+ },
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "auth_verifications": {
+ "name": "auth_verifications",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "identifier": {
+ "name": "identifier",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "value": {
+ "name": "value",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "expires_at": {
+ "name": "expires_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ }
+ },
+ "indexes": {
+ "auth_verifications_identifier_index": {
+ "name": "auth_verifications_identifier_index",
+ "columns": [
+ "identifier"
+ ],
+ "isUnique": false
+ }
+ },
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "intake_emails": {
+ "name": "intake_emails",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "email_address": {
+ "name": "email_address",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "organization_id": {
+ "name": "organization_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "allowed_origins": {
+ "name": "allowed_origins",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'[]'"
+ },
+ "is_enabled": {
+ "name": "is_enabled",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": true
+ }
+ },
+ "indexes": {
+ "intake_emails_email_address_unique": {
+ "name": "intake_emails_email_address_unique",
+ "columns": [
+ "email_address"
+ ],
+ "isUnique": true
+ }
+ },
+ "foreignKeys": {
+ "intake_emails_organization_id_organizations_id_fk": {
+ "name": "intake_emails_organization_id_organizations_id_fk",
+ "tableFrom": "intake_emails",
+ "tableTo": "organizations",
+ "columnsFrom": [
+ "organization_id"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "cascade",
+ "onUpdate": "cascade"
+ }
+ },
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ }
+ },
+ "views": {},
+ "enums": {},
+ "_meta": {
+ "schemas": {},
+ "tables": {},
+ "columns": {}
+ },
+ "internal": {
+ "indexes": {}
+ }
+}
diff --git a/apps/papra-server/migrations/meta/_journal.json b/apps/papra-server/migrations/meta/_journal.json
index 2a2b0298..15495f34 100644
--- a/apps/papra-server/migrations/meta/_journal.json
+++ b/apps/papra-server/migrations/meta/_journal.json
@@ -36,6 +36,13 @@
"when": 1738706983829,
"tag": "0004_auth-indexs",
"breakpoints": true
+ },
+ {
+ "idx": 5,
+ "version": "6",
+ "when": 1740570765157,
+ "tag": "0005_intake-emails-addresses",
+ "breakpoints": true
}
]
}
diff --git a/apps/papra-server/package.json b/apps/papra-server/package.json
index 030b47a6..fa0e6462 100644
--- a/apps/papra-server/package.json
+++ b/apps/papra-server/package.json
@@ -17,7 +17,7 @@
"test": "vitest run",
"test:watch": "vitest watch",
"typecheck": "tsc --noEmit",
- "migrate:up": "tsx --env-file-if-exists=.env src/scripts/migrate-up.script.ts",
+ "migrate:up": "tsx --env-file-if-exists=.env src/scripts/migrate-up.script.ts | crowlog-pretty",
"migrate:push": "drizzle-kit push",
"db:studio": "drizzle-kit studio",
"clean:dist": "rm -rf dist",
diff --git a/apps/papra-server/src/modules/intake-emails/drivers/intake-emails.drivers.models.ts b/apps/papra-server/src/modules/intake-emails/drivers/intake-emails.drivers.models.ts
new file mode 100644
index 00000000..85a95e98
--- /dev/null
+++ b/apps/papra-server/src/modules/intake-emails/drivers/intake-emails.drivers.models.ts
@@ -0,0 +1,12 @@
+import type { Config } from '../../config/config.types';
+
+export type IntakeEmailsServices = {
+ name: string;
+ generateEmailAddress: () => Promise<{ emailAddress: string }>;
+};
+
+export type IntakeEmailDriverFactory = (args: { config: Config }) => IntakeEmailsServices;
+
+export function defineIntakeEmailDriver(factory: IntakeEmailDriverFactory) {
+ return factory;
+}
diff --git a/apps/papra-server/src/modules/intake-emails/drivers/intake-emails.drivers.ts b/apps/papra-server/src/modules/intake-emails/drivers/intake-emails.drivers.ts
new file mode 100644
index 00000000..b40010fb
--- /dev/null
+++ b/apps/papra-server/src/modules/intake-emails/drivers/intake-emails.drivers.ts
@@ -0,0 +1,7 @@
+import { RANDOM_USERNAME_INTAKE_EMAIL_DRIVER_NAME, randomUsernameIntakeEmailDriverFactory } from './random-username/random-username.intake-email-driver';
+
+export const intakeEmailDrivers = {
+ [RANDOM_USERNAME_INTAKE_EMAIL_DRIVER_NAME]: randomUsernameIntakeEmailDriverFactory,
+} as const;
+
+export type IntakeEmailDriverName = keyof typeof intakeEmailDrivers;
diff --git a/apps/papra-server/src/modules/intake-emails/drivers/random-username/random-username.intake-email-driver.config.ts b/apps/papra-server/src/modules/intake-emails/drivers/random-username/random-username.intake-email-driver.config.ts
new file mode 100644
index 00000000..2b63bf66
--- /dev/null
+++ b/apps/papra-server/src/modules/intake-emails/drivers/random-username/random-username.intake-email-driver.config.ts
@@ -0,0 +1,10 @@
+import type { ConfigDefinition } from 'figue';
+import { z } from 'zod';
+
+export const randomUsernameIntakeEmailDriverConfig = {
+ domain: {
+ doc: 'The domain to use when generating email addresses for intake emails',
+ schema: z.string(),
+ default: 'papra.email',
+ },
+} as const satisfies ConfigDefinition;
diff --git a/apps/papra-server/src/modules/intake-emails/drivers/random-username/random-username.intake-email-driver.ts b/apps/papra-server/src/modules/intake-emails/drivers/random-username/random-username.intake-email-driver.ts
new file mode 100644
index 00000000..c87d1e1b
--- /dev/null
+++ b/apps/papra-server/src/modules/intake-emails/drivers/random-username/random-username.intake-email-driver.ts
@@ -0,0 +1,19 @@
+import { generateId as generateHumanReadableId } from '@corentinth/friendly-ids';
+import { defineIntakeEmailDriver } from '../intake-emails.drivers.models';
+
+export const RANDOM_USERNAME_INTAKE_EMAIL_DRIVER_NAME = 'random-username';
+
+export const randomUsernameIntakeEmailDriverFactory = defineIntakeEmailDriver(({ config }) => {
+ const { domain } = config.intakeEmails.drivers.randomUsername;
+
+ return {
+ name: RANDOM_USERNAME_INTAKE_EMAIL_DRIVER_NAME,
+ generateEmailAddress: async () => {
+ const randomUsername = generateHumanReadableId();
+
+ return {
+ emailAddress: `${randomUsername}@${domain}`,
+ };
+ },
+ };
+});
diff --git a/apps/papra-server/src/modules/intake-emails/intake-emails.config.ts b/apps/papra-server/src/modules/intake-emails/intake-emails.config.ts
index 32f262c7..e603531f 100644
--- a/apps/papra-server/src/modules/intake-emails/intake-emails.config.ts
+++ b/apps/papra-server/src/modules/intake-emails/intake-emails.config.ts
@@ -1,5 +1,8 @@
import type { ConfigDefinition } from 'figue';
import { z } from 'zod';
+import { intakeEmailDrivers } from './drivers/intake-emails.drivers';
+import { RANDOM_USERNAME_INTAKE_EMAIL_DRIVER_NAME } from './drivers/random-username/random-username.intake-email-driver';
+import { randomUsernameIntakeEmailDriverConfig } from './drivers/random-username/random-username.intake-email-driver.config';
export const intakeEmailsConfig = {
isEnabled: {
@@ -19,10 +22,19 @@ export const intakeEmailsConfig = {
default: 'papra.email',
env: 'INTAKE_EMAILS_EMAIL_GENERATION_DOMAIN',
},
+ driver: {
+ doc: 'The driver to use when generating email addresses for intake emails',
+ schema: z.enum(Object.keys(intakeEmailDrivers) as [string, ...string[]]),
+ default: RANDOM_USERNAME_INTAKE_EMAIL_DRIVER_NAME,
+ env: 'INTAKE_EMAILS_DRIVER',
+ },
webhookSecret: {
doc: 'The secret to use when verifying webhooks',
schema: z.string(),
default: 'change-me',
env: 'INTAKE_EMAILS_WEBHOOK_SECRET',
},
+ drivers: {
+ randomUsername: randomUsernameIntakeEmailDriverConfig,
+ },
} as const satisfies ConfigDefinition;
diff --git a/apps/papra-server/src/modules/intake-emails/intake-emails.models.test.ts b/apps/papra-server/src/modules/intake-emails/intake-emails.models.test.ts
index 5e091cfb..f7997608 100644
--- a/apps/papra-server/src/modules/intake-emails/intake-emails.models.test.ts
+++ b/apps/papra-server/src/modules/intake-emails/intake-emails.models.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, test } from 'vitest';
-import { getEmailUsername, getIsFromAllowedOrigin, getIsIntakeEmailWebhookSecretValid } from './intake-emails.models';
+import { buildEmailAddress, getEmailUsername, getIsFromAllowedOrigin, getIsIntakeEmailWebhookSecretValid, parseEmailAddress } from './intake-emails.models';
describe('intake-emails models', () => {
describe('getIsIntakeEmailWebhookSecretValid', () => {
@@ -77,4 +77,19 @@ describe('intake-emails models', () => {
).to.eql(false);
});
});
+
+ describe('buildEmailAddress', () => {
+ test('it builds an email address from a username, a domain and an optional plus part', () => {
+ expect(buildEmailAddress({ username: 'foo', domain: 'example.fr' })).to.eql('[email protected]');
+ expect(buildEmailAddress({ username: 'foo', domain: 'example.fr', plusPart: 'bar' })).to.eql('[email protected]');
+ });
+ });
+
+ describe('parseEmailAddress', () => {
+ test('it parses an email address into a username, a domain and an optional plus part', () => {
+ expect(parseEmailAddress({ email: '[email protected]' })).to.eql({ username: 'foo', domain: 'example.fr', plusPart: undefined });
+ expect(parseEmailAddress({ email: '[email protected]' })).to.eql({ username: 'foo', domain: 'example.fr', plusPart: 'bar' });
+ expect(parseEmailAddress({ email: '[email protected]' })).to.eql({ username: 'foo', domain: 'example.fr', plusPart: 'bar+baz' });
+ });
+ });
});
diff --git a/apps/papra-server/src/modules/intake-emails/intake-emails.models.ts b/apps/papra-server/src/modules/intake-emails/intake-emails.models.ts
index 6bbe780e..b527f30f 100644
--- a/apps/papra-server/src/modules/intake-emails/intake-emails.models.ts
+++ b/apps/papra-server/src/modules/intake-emails/intake-emails.models.ts
@@ -1,3 +1,23 @@
+export function buildEmailAddress({
+ username,
+ domain,
+ plusPart,
+}: {
+ username: string;
+ domain: string;
+ plusPart?: string;
+}) {
+ return `${username}${plusPart ? `+${plusPart}` : ''}@${domain}`;
+}
+
+export function parseEmailAddress({ email }: { email: string }) {
+ const [fullUsername, domain] = email.split('@');
+ const [username, ...plusParts] = fullUsername.split('+');
+ const plusPart = plusParts.length > 0 ? plusParts.join('+') : undefined;
+
+ return { username, domain, plusPart };
+}
+
export function getIsIntakeEmailWebhookSecretValid({
secret,
authorizationHeader,
diff --git a/apps/papra-server/src/modules/intake-emails/intake-emails.repository.test.ts b/apps/papra-server/src/modules/intake-emails/intake-emails.repository.test.ts
index 0f3895b4..53b6a4ab 100644
--- a/apps/papra-server/src/modules/intake-emails/intake-emails.repository.test.ts
+++ b/apps/papra-server/src/modules/intake-emails/intake-emails.repository.test.ts
@@ -14,6 +14,7 @@ describe('intake-emails repository', () => {
const { intakeEmail } = await intakeEmailsRepository.createIntakeEmail({
organizationId: 'organization-1',
+ emailAddress: '[email protected]',
});
expect(
@@ -22,6 +23,7 @@ describe('intake-emails repository', () => {
organizationId: 'organization-1',
isEnabled: true,
allowedOrigins: [],
+ emailAddress: '[email protected]',
});
const { intakeEmail: updatedIntakeEmail } = await intakeEmailsRepository.updateIntakeEmail({
@@ -37,6 +39,7 @@ describe('intake-emails repository', () => {
organizationId: 'organization-1',
isEnabled: false,
allowedOrigins: ['[email protected]'],
+ emailAddress: '[email protected]',
});
const { intakeEmail: retrievedIntakeEmail } = await intakeEmailsRepository.getIntakeEmail({
@@ -49,6 +52,7 @@ describe('intake-emails repository', () => {
organizationId: 'organization-1',
isEnabled: false,
allowedOrigins: ['[email protected]'],
+ emailAddress: '[email protected]',
});
const { intakeEmails: orgIntakeEmails } = await intakeEmailsRepository.getOrganizationIntakeEmails({
diff --git a/apps/papra-server/src/modules/intake-emails/intake-emails.repository.ts b/apps/papra-server/src/modules/intake-emails/intake-emails.repository.ts
index 9646bee7..52f0cf54 100644
--- a/apps/papra-server/src/modules/intake-emails/intake-emails.repository.ts
+++ b/apps/papra-server/src/modules/intake-emails/intake-emails.repository.ts
@@ -13,14 +13,15 @@ export function createIntakeEmailsRepository({ db }: { db: Database }) {
updateIntakeEmail,
getIntakeEmail,
getOrganizationIntakeEmails,
+ getIntakeEmailByEmailAddress,
deleteIntakeEmail,
},
{ db },
);
}
-async function createIntakeEmail({ organizationId, db }: { organizationId: string; db: Database }) {
- const [intakeEmail] = await db.insert(intakeEmailsTable).values({ organizationId }).returning();
+async function createIntakeEmail({ organizationId, emailAddress, db }: { organizationId: string; emailAddress: string; db: Database }) {
+ const [intakeEmail] = await db.insert(intakeEmailsTable).values({ organizationId, emailAddress }).returning();
return { intakeEmail };
}
@@ -56,6 +57,15 @@ async function getIntakeEmail({ intakeEmailId, db }: { intakeEmailId: string; db
return { intakeEmail };
}
+async function getIntakeEmailByEmailAddress({ emailAddress, db }: { emailAddress: string; db: Database }) {
+ const [intakeEmail] = await db
+ .select()
+ .from(intakeEmailsTable)
+ .where(eq(intakeEmailsTable.emailAddress, emailAddress));
+
+ return { intakeEmail };
+}
+
async function getOrganizationIntakeEmails({ organizationId, db }: { organizationId: string; db: Database }) {
const intakeEmails = await db
.select()
diff --git a/apps/papra-server/src/modules/intake-emails/intake-emails.routes.ts b/apps/papra-server/src/modules/intake-emails/intake-emails.routes.ts
index 30887dd1..b896c857 100644
--- a/apps/papra-server/src/modules/intake-emails/intake-emails.routes.ts
+++ b/apps/papra-server/src/modules/intake-emails/intake-emails.routes.ts
@@ -16,7 +16,8 @@ import { validateFormData, validateJsonBody, validateParams } from '../shared/va
import { getIsIntakeEmailWebhookSecretValid } from './intake-emails.models';
import { createIntakeEmailsRepository } from './intake-emails.repository';
import { intakeEmailsIngestionMetaSchema, parseJson } from './intake-emails.schemas';
-import { processIntakeEmailIngestion } from './intake-emails.usecases';
+import { createIntakeEmailsServices } from './intake-emails.services';
+import { createIntakeEmail, processIntakeEmailIngestion } from './intake-emails.usecases';
const logger = createLogger({ namespace: 'intake-emails.routes' });
@@ -64,13 +65,15 @@ export function setupCreateIntakeEmailRoute({ app }: { app: ServerInstance }) {
const { userId } = getUser({ context });
const { organizationId } = context.req.valid('param');
const { db } = getDb({ context });
+ const { config } = getConfig({ context });
const organizationsRepository = createOrganizationsRepository({ db });
const intakeEmailsRepository = createIntakeEmailsRepository({ db });
+ const intakeEmailsServices = createIntakeEmailsServices({ config });
await ensureUserIsInOrganization({ userId, organizationId, organizationsRepository });
- const { intakeEmail } = await intakeEmailsRepository.createIntakeEmail({ organizationId });
+ const { intakeEmail } = await createIntakeEmail({ organizationId, intakeEmailsRepository, intakeEmailsServices });
return context.json({ intakeEmail });
},
diff --git a/apps/papra-server/src/modules/intake-emails/intake-emails.services.ts b/apps/papra-server/src/modules/intake-emails/intake-emails.services.ts
new file mode 100644
index 00000000..c4aac704
--- /dev/null
+++ b/apps/papra-server/src/modules/intake-emails/intake-emails.services.ts
@@ -0,0 +1,21 @@
+import type { Config } from '../config/config.types';
+import type { IntakeEmailDriverName } from './drivers/intake-emails.drivers';
+import { createError } from '../shared/errors/errors';
+import { intakeEmailDrivers } from './drivers/intake-emails.drivers';
+
+export function createIntakeEmailsServices({ config }: { config: Config }) {
+ const intakeEmailDriver = intakeEmailDrivers[config.intakeEmails.driver as IntakeEmailDriverName];
+
+ if (!intakeEmailDriver) {
+ throw createError({
+ message: `Invalid intake email driver ${config.intakeEmails.driver}`,
+ code: 'intake-emails.invalid_driver',
+ statusCode: 500,
+ isInternal: true,
+ });
+ }
+
+ const intakeEmailsServices = intakeEmailDriver({ config });
+
+ return intakeEmailsServices;
+}
diff --git a/apps/papra-server/src/modules/intake-emails/intake-emails.tables.ts b/apps/papra-server/src/modules/intake-emails/intake-emails.tables.ts
index 0e3a0268..c4269a8b 100644
--- a/apps/papra-server/src/modules/intake-emails/intake-emails.tables.ts
+++ b/apps/papra-server/src/modules/intake-emails/intake-emails.tables.ts
@@ -1,12 +1,12 @@
-import { generateId as generateHumanReadableId } from '@corentinth/friendly-ids';
import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';
import { organizationsTable } from '../organizations/organizations.table';
import { createPrimaryKeyField, createTimestampColumns } from '../shared/db/columns.helpers';
export const intakeEmailsTable = sqliteTable('intake_emails', {
- ...createPrimaryKeyField({ idGenerator: generateHumanReadableId }),
+ ...createPrimaryKeyField({ prefix: 'ie' }),
...createTimestampColumns(),
+ emailAddress: text('email_address').notNull().unique(),
organizationId: text('organization_id').notNull().references(() => organizationsTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),
allowedOrigins: text('allowed_origins', { mode: 'json' }).notNull().$type<string[]>().default([]),
isEnabled: integer('is_enabled', { mode: 'boolean' }).notNull().default(true),
diff --git a/apps/papra-server/src/modules/intake-emails/intake-emails.usecases.test.ts b/apps/papra-server/src/modules/intake-emails/intake-emails.usecases.test.ts
index 1f55e1a1..80b65a1d 100644
--- a/apps/papra-server/src/modules/intake-emails/intake-emails.usecases.test.ts
+++ b/apps/papra-server/src/modules/intake-emails/intake-emails.usecases.test.ts
@@ -16,7 +16,7 @@ describe('intake-emails usecases', () => {
test(`when an intake email is is configured, enabled and match the recipient, and the sender is allowed, a document is created for each attachment`, async () => {
const { db } = await createInMemoryDatabase({
organizations: [{ id: 'org-1', name: 'Organization 1' }],
- intakeEmails: [{ id: 'ie-1', organizationId: 'org-1', allowedOrigins: ['[email protected]'] }],
+ intakeEmails: [{ id: 'ie-1', organizationId: 'org-1', allowedOrigins: ['[email protected]'], emailAddress: '[email protected]' }],
});
const intakeEmailsRepository = createIntakeEmailsRepository({ db });
@@ -25,7 +25,7 @@ describe('intake-emails usecases', () => {
await ingestEmailForRecipient({
fromAddress: '[email protected]',
- recipientAddress: '[email protected]',
+ recipientAddress: '[email protected]',
attachments: [
new File(['content1'], 'file1.txt', { type: 'text/plain' }),
new File(['content2'], 'file2.txt', { type: 'text/plain' }),
@@ -51,7 +51,7 @@ describe('intake-emails usecases', () => {
const { db } = await createInMemoryDatabase({
organizations: [{ id: 'org-1', name: 'Organization 1' }],
- intakeEmails: [{ id: 'ie-1', organizationId: 'org-1', isEnabled: false }],
+ intakeEmails: [{ id: 'ie-1', organizationId: 'org-1', isEnabled: false, emailAddress: '[email protected]' }],
});
const intakeEmailsRepository = createIntakeEmailsRepository({ db });
@@ -60,7 +60,7 @@ describe('intake-emails usecases', () => {
await ingestEmailForRecipient({
fromAddress: '[email protected]',
- recipientAddress: '[email protected]',
+ recipientAddress: '[email protected]',
attachments: [new File(['content'], 'file.txt', { type: 'text/plain' })],
intakeEmailsRepository,
documentsRepository,
@@ -108,7 +108,7 @@ describe('intake-emails usecases', () => {
const { db } = await createInMemoryDatabase({
organizations: [{ id: 'org-1', name: 'Organization 1' }],
- intakeEmails: [{ id: 'ie-1', organizationId: 'org-1', allowedOrigins: ['[email protected]'] }],
+ intakeEmails: [{ id: 'ie-1', organizationId: 'org-1', allowedOrigins: ['[email protected]'], emailAddress: '[email protected]' }],
});
const intakeEmailsRepository = createIntakeEmailsRepository({ db });
@@ -117,7 +117,7 @@ describe('intake-emails usecases', () => {
await ingestEmailForRecipient({
fromAddress: '[email protected]',
- recipientAddress: '[email protected]',
+ recipientAddress: '[email protected]',
attachments: [new File(['content'], 'file.txt', { type: 'text/plain' })],
intakeEmailsRepository,
documentsRepository,
@@ -148,8 +148,8 @@ describe('intake-emails usecases', () => {
{ id: 'org-2', name: 'Organization 2' },
],
intakeEmails: [
- { id: 'ie-1', organizationId: 'org-1', allowedOrigins: ['[email protected]'] },
- { id: 'ie-2', organizationId: 'org-2', allowedOrigins: ['[email protected]'] },
+ { id: 'ie-1', organizationId: 'org-1', allowedOrigins: ['[email protected]'], emailAddress: '[email protected]' },
+ { id: 'ie-2', organizationId: 'org-2', allowedOrigins: ['[email protected]'], emailAddress: '[email protected]' },
],
});
@@ -159,7 +159,7 @@ describe('intake-emails usecases', () => {
await processIntakeEmailIngestion({
fromAddress: '[email protected]',
- recipientsAddresses: ['[email protected]', '[email protected]'],
+ recipientsAddresses: ['[email protected]', '[email protected]'],
attachments: [
new File(['content1'], 'file1.txt', { type: 'text/plain' }),
],
diff --git a/apps/papra-server/src/modules/intake-emails/intake-emails.usecases.ts b/apps/papra-server/src/modules/intake-emails/intake-emails.usecases.ts
index d790de65..d5edd2be 100644
--- a/apps/papra-server/src/modules/intake-emails/intake-emails.usecases.ts
+++ b/apps/papra-server/src/modules/intake-emails/intake-emails.usecases.ts
@@ -1,11 +1,28 @@
import type { DocumentsRepository } from '../documents/documents.repository';
import type { DocumentStorageService } from '../documents/storage/documents.storage.services';
import type { Logger } from '../shared/logger/logger';
+import type { IntakeEmailsServices } from './drivers/intake-emails.drivers.models';
import type { IntakeEmailsRepository } from './intake-emails.repository';
import { safely } from '@corentinth/chisels';
import { createDocument } from '../documents/documents.usecases';
import { addLogContext, createLogger } from '../shared/logger/logger';
-import { getEmailUsername, getIsFromAllowedOrigin } from './intake-emails.models';
+import { getIsFromAllowedOrigin } from './intake-emails.models';
+
+export async function createIntakeEmail({
+ organizationId,
+ intakeEmailsRepository,
+ intakeEmailsServices,
+}: {
+ organizationId: string;
+ intakeEmailsRepository: IntakeEmailsRepository;
+ intakeEmailsServices: IntakeEmailsServices;
+}) {
+ const { emailAddress } = await intakeEmailsServices.generateEmailAddress();
+
+ const { intakeEmail } = await intakeEmailsRepository.createIntakeEmail({ organizationId, emailAddress });
+
+ return { intakeEmail };
+}
export function processIntakeEmailIngestion({
fromAddress,
@@ -53,15 +70,7 @@ export async function ingestEmailForRecipient({
documentsStorageService: DocumentStorageService;
logger?: Logger;
}) {
- const { username } = getEmailUsername({ email: recipientAddress });
-
- if (!username) {
- logger.warn('Invalid recipient address, no username found');
-
- return;
- }
-
- const { intakeEmail } = await intakeEmailsRepository.getIntakeEmail({ intakeEmailId: username });
+ const { intakeEmail } = await intakeEmailsRepository.getIntakeEmailByEmailAddress({ emailAddress: recipientAddress });
if (!intakeEmail) {
logger.info('Intake email not found');