The minimalistic document archiving platform.
feat(documents): include organizationId in document repository crud operations
Corentin Thomassetb16b557733b6
diff --git a/apps/papra-server/src/modules/documents/documents.repository.test.ts b/apps/papra-server/src/modules/documents/documents.repository.test.tsindex aede97dd..de8309cf 100644--- a/apps/papra-server/src/modules/documents/documents.repository.test.ts+++ b/apps/papra-server/src/modules/documents/documents.repository.test.ts@@ -53,6 +53,7 @@ describe('documents repository', () => {await documentsRepository.softDeleteDocument({documentId: document.id,userId: 'user-1',+ organizationId: 'organization-1',});const { documents: documentsAfterDelete } = await documentsRepository.getOrganizationDocuments({diff --git a/apps/papra-server/src/modules/documents/documents.repository.ts b/apps/papra-server/src/modules/documents/documents.repository.tsindex 780c926f..8b34e3bb 100644--- a/apps/papra-server/src/modules/documents/documents.repository.ts+++ b/apps/papra-server/src/modules/documents/documents.repository.ts@@ -139,20 +139,28 @@ async function getOrganizationDeletedDocuments({ organizationId, pageIndex, page};}-async function getDocumentById({ documentId, db }: { documentId: string; db: Database }) {- const [[document], tags] = await Promise.all([- db- .select()- .from(documentsTable)- .where(eq(documentsTable.id, documentId)),- db- .select({- ...getTableColumns(tagsTable),- })- .from(documentsTagsTable)- .leftJoin(tagsTable, eq(tagsTable.id, documentsTagsTable.tagId))- .where(eq(documentsTagsTable.documentId, documentId)),- ]);+async function getDocumentById({ documentId, organizationId, db }: { documentId: string; organizationId: string; db: Database }) {+ const [document] = await db+ .select()+ .from(documentsTable)+ .where(+ and(+ eq(documentsTable.id, documentId),+ eq(documentsTable.organizationId, organizationId),+ ),+ );++ if (!document) {+ return { document: undefined };+ }++ const tags = await db+ .select({+ ...getTableColumns(tagsTable),+ })+ .from(documentsTagsTable)+ .leftJoin(tagsTable, eq(tagsTable.id, documentsTagsTable.tagId))+ .where(eq(documentsTagsTable.documentId, documentId));return {document: {@@ -162,7 +170,7 @@ async function getDocumentById({ documentId, db }: { documentId: string; db: Dat};}-async function softDeleteDocument({ documentId, userId, db, now = new Date() }: { documentId: string; userId: string; db: Database; now?: Date }) {+async function softDeleteDocument({ documentId, organizationId, userId, db, now = new Date() }: { documentId: string; organizationId: string;userId: string; db: Database; now?: Date }) {await db.update(documentsTable).set({@@ -170,10 +178,15 @@ async function softDeleteDocument({ documentId, userId, db, now = new Date() }:deletedBy: userId,deletedAt: now,})- .where(eq(documentsTable.id, documentId));+ .where(+ and(+ eq(documentsTable.id, documentId),+ eq(documentsTable.organizationId, organizationId),+ ),+ );}-async function restoreDocument({ documentId, db }: { documentId: string; db: Database }) {+async function restoreDocument({ documentId, organizationId, db }: { documentId: string;organizationId: string; db: Database }) {await db.update(documentsTable).set({@@ -181,7 +194,12 @@ async function restoreDocument({ documentId, db }: { documentId: string; db: DatdeletedBy: null,deletedAt: null,})- .where(eq(documentsTable.id, documentId));+ .where(+ and(+ eq(documentsTable.id, documentId),+ eq(documentsTable.organizationId, organizationId),+ ),+ );}async function hardDeleteDocument({ documentId, db }: { documentId: string; db: Database }) {diff --git a/apps/papra-server/src/modules/documents/documents.routes.ts b/apps/papra-server/src/modules/documents/documents.routes.tsindex 22910bec..a71dead7 100644--- a/apps/papra-server/src/modules/documents/documents.routes.ts+++ b/apps/papra-server/src/modules/documents/documents.routes.ts@@ -193,7 +193,7 @@ function setupGetDocumentRoute({ app }: { app: ServerInstance }) {await ensureUserIsInOrganization({ userId, organizationId, organizationsRepository });- const { document } = await getDocumentOrThrow({ documentId, documentsRepository });+ const { document } = await getDocumentOrThrow({ documentId, organizationId, documentsRepository });return context.json({document,@@ -219,9 +219,9 @@ function setupDeleteDocumentRoute({ app }: { app: ServerInstance }) {const organizationsRepository = createOrganizationsRepository({ db });await ensureUserIsInOrganization({ userId, organizationId, organizationsRepository });- await ensureDocumentExists({ documentId, documentsRepository });+ await ensureDocumentExists({ documentId, organizationId, documentsRepository });- await documentsRepository.softDeleteDocument({ documentId, userId });+ await documentsRepository.softDeleteDocument({ documentId, organizationId, userId });return context.json({success: true,@@ -248,13 +248,13 @@ function setupRestoreDocumentRoute({ app }: { app: ServerInstance }) {await ensureUserIsInOrganization({ userId, organizationId, organizationsRepository });- const { document } = await getDocumentOrThrow({ documentId, documentsRepository });+ const { document } = await getDocumentOrThrow({ documentId, organizationId, documentsRepository });if (!document.isDeleted) {throw createDocumentIsNotDeletedError();}- await documentsRepository.restoreDocument({ documentId });+ await documentsRepository.restoreDocument({ documentId, organizationId });return context.body(null, 204);},@@ -280,7 +280,7 @@ function setupGetDocumentFileRoute({ app }: { app: ServerInstance }) {await ensureUserIsInOrganization({ userId, organizationId, organizationsRepository });- const { document } = await getDocumentOrThrow({ documentId, documentsRepository });+ const { document } = await getDocumentOrThrow({ documentId, documentsRepository, organizationId });const documentsStorageService = await createDocumentStorageService({ config });diff --git a/apps/papra-server/src/modules/documents/documents.usecases.ts b/apps/papra-server/src/modules/documents/documents.usecases.tsindex 6726b024..8635396f 100644--- a/apps/papra-server/src/modules/documents/documents.usecases.ts+++ b/apps/papra-server/src/modules/documents/documents.usecases.ts@@ -75,12 +75,14 @@ export async function createDocument({export async function getDocumentOrThrow({documentId,+ organizationId,documentsRepository,}: {documentId: string;+ organizationId: string;documentsRepository: DocumentsRepository;}) {- const { document } = await documentsRepository.getDocumentById({ documentId });+ const { document } = await documentsRepository.getDocumentById({ documentId, organizationId });if (!document) {throw createDocumentNotFoundError();@@ -91,12 +93,14 @@ export async function getDocumentOrThrow({export async function ensureDocumentExists({documentId,+ organizationId,documentsRepository,}: {documentId: string;+ organizationId: string;documentsRepository: DocumentsRepository;}) {- await getDocumentOrThrow({ documentId, documentsRepository });+ await getDocumentOrThrow({ documentId, organizationId, documentsRepository });}export async function hardDeleteDocument({