From a744df0a5d186b72330109cc429a35b3368e70c4 Mon Sep 17 00:00:00 2001 From: tangoyankee Date: Tue, 16 Apr 2024 09:30:30 -0400 Subject: [PATCH] legacy document links Add support for legacy links to documents closes #1508 --- server/src/document/document.controller.ts | 43 +++++++++++++++++++++ server/src/document/document.service.ts | 32 +++++++++++++++ server/src/sharepoint/sharepoint.service.ts | 42 ++++++++++++++++++++ 3 files changed, 117 insertions(+) diff --git a/server/src/document/document.controller.ts b/server/src/document/document.controller.ts index 0024065a..d62de0c2 100644 --- a/server/src/document/document.controller.ts +++ b/server/src/document/document.controller.ts @@ -93,6 +93,16 @@ export class DocumentController { } } + @Get("/package/sites/*") + async streamPackageDocByUrl(@Param() sharepointRelativePath, @Res() res) { + const id = await this.documentService.getPackageDocumentId( + sharepointRelativePath[0] + ); + + const stream = await this.documentService.getPackageDocument(id); + stream.pipe(res); + } + @Get("/package/*") async streamPackageDoc(@Param() sharepointRelativePath, @Res() res) { const stream = await this.documentService.getPackageDocument( @@ -102,6 +112,16 @@ export class DocumentController { stream.pipe(res); } + @Get("/artifact/sites/*") + async streamArtifactDocByUrl(@Param() sharepointRelativePath, @Res() res) { + const id = await this.documentService.getArtifactDocumentId( + sharepointRelativePath[0] + ); + + const stream = await this.documentService.getArtifactDocument(id); + stream.pipe(res); + } + @Get("/artifact/*") async streamArtifactDoc(@Param() sharepointRelativePath, @Res() res) { const stream = await this.documentService.getArtifactDocument( @@ -111,6 +131,19 @@ export class DocumentController { stream.pipe(res); } + @Get("/projectaction/sites/*") + async streamProjectactionDocByUrl( + @Param() sharepointRelativePath, + @Res() res + ) { + const id = await this.documentService.getProjectactionDocumentId( + sharepointRelativePath[0] + ); + + const stream = await this.documentService.getProjectactionDocument(id); + stream.pipe(res); + } + @Get("/projectaction/*") async streamProjectactionDoc(@Param() sharepointRelativePath, @Res() res) { const stream = await this.documentService.getProjectactionDocument( @@ -120,6 +153,16 @@ export class DocumentController { stream.pipe(res); } + @Get("/disposition/sites/*") + async streamDispositionDocByUrl(@Param() sharepointRelativePath, @Res() res) { + const id = await this.documentService.getDispositionDocumentId( + sharepointRelativePath[0] + ); + + const stream = await this.documentService.getDispositionDocument(id); + stream.pipe(res); + } + @Get("/disposition/*") async streamDispositionDoc(@Param() sharepointRelativePath, @Res() res) { const stream = await this.documentService.getDispositionDocument( diff --git a/server/src/document/document.service.ts b/server/src/document/document.service.ts index 6554be5e..56b78654 100644 --- a/server/src/document/document.service.ts +++ b/server/src/document/document.service.ts @@ -85,6 +85,13 @@ export class DocumentService { private readonly crmService: CrmService, private readonly sharepointService: SharepointService ) {} + public async getPackageDocumentId(relativeUrl: string) { + const driveId = this.sharepointService.driveIdMap.dcp_package; + return await this.sharepointService.getSharepointFileId( + driveId, + relativeUrl + ); + } // For info on the path param, // see above documentation for the getRecordIdFromDocumentPath function public async getPackageDocument(fileId) { @@ -135,6 +142,14 @@ export class DocumentService { } } + public async getArtifactDocumentId(relativeUrl: string) { + const driveId = this.sharepointService.driveIdMap.dcp_artifact; + return await this.sharepointService.getSharepointFileId( + driveId, + relativeUrl + ); + } + public async getArtifactDocument(fileId) { const driveId = this.sharepointService.driveIdMap.dcp_artifact; const { @@ -172,6 +187,14 @@ export class DocumentService { } } + public async getProjectactionDocumentId(relativeUrl: string) { + const driveId = this.sharepointService.driveIdMap.dcp_projectaction; + return await this.sharepointService.getSharepointFileId( + driveId, + relativeUrl + ); + } + public async getProjectactionDocument(fileId: string) { const driveId = this.sharepointService.driveIdMap.dcp_projectaction; const { @@ -209,6 +232,15 @@ export class DocumentService { } } + public async getDispositionDocumentId(relativeUrl: string) { + const driveId = this.sharepointService.driveIdMap + .dcp_communityboarddisposition; + return await this.sharepointService.getSharepointFileId( + driveId, + relativeUrl + ); + } + public async getDispositionDocument(fileId) { const driveId = this.sharepointService.driveIdMap .dcp_communityboarddisposition; diff --git a/server/src/sharepoint/sharepoint.service.ts b/server/src/sharepoint/sharepoint.service.ts index e2653dab..ab4a85d8 100644 --- a/server/src/sharepoint/sharepoint.service.ts +++ b/server/src/sharepoint/sharepoint.service.ts @@ -225,4 +225,46 @@ export class SharepointService { const response = await fetch(url, options); return await response.json(); } + + async getSharepointFileId(driveId: string, relativeUrl: string) { + const filePath = relativeUrl.split("/"); + if (filePath.length < 2) + throw new HttpException( + { + code: "DISPOSITION_ID_PATH", + title: "Disposition ID Path Error" + }, + HttpStatus.BAD_REQUEST + ); + const folderName = filePath[filePath.length - 2]; + const fileName = filePath[filePath.length - 1]; + + const { accessToken } = await this.msalProvider.getGraphClientToken(); + const url = `${ + this.msalProvider.sharePointSiteUrl + }/drives/${driveId}/root:/${folderName}:/children?$filter=(name eq '${fileName}')&$select=id`; + const options = { + headers: { + method: "GET", + Authorization: `Bearer ${accessToken}`, + Accept: "application/json" + } + }; + + const response = await fetch(url, options); + const data = (await response.json()) as + | { value: Array<{ id: string }> } + | { message: string }; + if ("value" in data && data.value.length > 0) { + return data.value[0].id; + } else { + throw new HttpException( + { + code: "DISPOSITION_ID_RESULT", + title: "Disposition ID Result Error" + }, + HttpStatus.NOT_FOUND + ); + } + } }