From 4a81debdd7689ee30527919b82646dcc6bde2a0a Mon Sep 17 00:00:00 2001 From: Martin Trajanovski Date: Mon, 3 Apr 2023 09:11:00 +0200 Subject: [PATCH 1/3] fix: throw error instead of returning null with success if register DOI failed --- .../published-data.controller.ts | 34 ++++++++++--------- test/DatasetFilter.js | 2 +- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/published-data/published-data.controller.ts b/src/published-data/published-data.controller.ts index 4f9ed8f5b..022e01c12 100644 --- a/src/published-data/published-data.controller.ts +++ b/src/published-data/published-data.controller.ts @@ -11,6 +11,7 @@ import { UseInterceptors, HttpException, HttpStatus, + NotFoundException, } from "@nestjs/common"; import { PublishedDataService } from "./published-data.service"; import { CreatePublishedDataDto } from "./dto/create-published-data.dto"; @@ -181,9 +182,6 @@ export class PublishedDataController { @CheckPolicies((ability: AppAbility) => ability.can(Action.Update, PublishedData), ) - // @UseInterceptors( - // new SetCreatedUpdatedAtInterceptor("updatedAt"), - // ) @Patch("/:id") async update( @Param("id") id: string, @@ -210,9 +208,6 @@ export class PublishedDataController { @CheckPolicies((ability: AppAbility) => ability.can(Action.Update, PublishedData), ) - // @UseInterceptors( - // new SetCreatedUpdatedAtInterceptor("updatedAt"), - // ) @Post("/:id/register") async register(@Param("id") id: string): Promise { const publishedData = await this.publishedDataService.findOne({ doi: id }); @@ -305,9 +300,12 @@ export class PublishedDataController { method: "PUT", }), ); - } catch (err) { + } catch (err: any) { handleAxiosRequestError(err, "PublishedDataController.register"); - return null; + throw new HttpException( + `Error occurred: ${err}`, + err.response.status || HttpStatus.FAILED_DEPENDENCY, + ); } try { @@ -317,9 +315,12 @@ export class PublishedDataController { method: "PUT", }), ); - } catch (err) { + } catch (err: any) { handleAxiosRequestError(err, "PublishedDataController.register"); - return null; + throw new HttpException( + `Error occurred: ${err}`, + err.response.status || HttpStatus.FAILED_DEPENDENCY, + ); } try { @@ -359,9 +360,12 @@ export class PublishedDataController { method: "POST", }), ); - } catch (err) { + } catch (err: any) { handleAxiosRequestError(err, "PublishedDataController.register"); - return null; + throw new HttpException( + `Error occurred: ${err}`, + err.response.status || HttpStatus.FAILED_DEPENDENCY, + ); } try { @@ -376,7 +380,8 @@ export class PublishedDataController { return res ? res.data : null; } } - return null; + + throw new NotFoundException(); } // POST /publisheddata/:id/resync @@ -384,9 +389,6 @@ export class PublishedDataController { @CheckPolicies((ability: AppAbility) => ability.can(Action.Update, PublishedData), ) - // @UseInterceptors( - // new SetCreatedUpdatedAtInterceptor("updatedAt"), - // ) @Post("/:id/resync") async resync( @Param("id") id: string, diff --git a/test/DatasetFilter.js b/test/DatasetFilter.js index 46f20872b..f8ab7da5d 100644 --- a/test/DatasetFilter.js +++ b/test/DatasetFilter.js @@ -747,7 +747,7 @@ describe("DatasetFilter: Test retrieving datasets using filtering capabilities", }); }); - it("Adding EQUAL_TO_NUMERIC condition on the fullquery endpoint should work", async () => { + it("Adding EQUAL_TO_STRING condition on the fullquery endpoint should work", async () => { const fields = { mode: {}, scientific: [ From ac0d4812b382e2cc77770eb28ff7ff79db6c0898 Mon Sep 17 00:00:00 2001 From: Martin Trajanovski Date: Mon, 3 Apr 2023 12:44:16 +0200 Subject: [PATCH 2/3] read DOI username and password from env --- .env.example | 2 ++ src/published-data/published-data.controller.ts | 13 +++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index d89f0b55a..594465e22 100644 --- a/.env.example +++ b/.env.example @@ -28,6 +28,8 @@ RABBITMQ_USERNAME="rabbitmq" RABBITMQ_PASSWORD="rabbitmq" REGISTER_DOI_URI="https://mds.test.datacite.org/doi" REGISTER_METADATA_URI="https://mds.test.datacite.org/metadata" +DOI_USERNAME="username" +DOI_PASSWORD="password" SITE= SMTP_HOST= SMTP_MESSAGE_FROM= diff --git a/src/published-data/published-data.controller.ts b/src/published-data/published-data.controller.ts index 022e01c12..2187513b4 100644 --- a/src/published-data/published-data.controller.ts +++ b/src/published-data/published-data.controller.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ import { Controller, Get, @@ -243,10 +244,14 @@ export class PublishedDataController { password: "removed", }; - if (existsSync(this.doiConfigPath)) { - doiProviderCredentials = JSON.parse( - readFileSync(this.doiConfigPath).toString(), - ); + const username = this.configService.get("doiUsername"); + const password = this.configService.get("doiPassword"); + + if (username && password) { + doiProviderCredentials = { + username, + password, + }; } const registerDataciteMetadataOptions = { From 464693ae6d1065340eddebc1da9868f8c2c77187 Mon Sep 17 00:00:00 2001 From: Martin Trajanovski Date: Tue, 4 Apr 2023 10:43:02 +0200 Subject: [PATCH 3/3] fix: make the register endpoint work with test doi datacite --- src/config/configuration.ts | 2 ++ src/config/doiconfig.local.example.json | 4 ---- .../published-data.controller.ts | 24 ++++++++----------- 3 files changed, 12 insertions(+), 18 deletions(-) delete mode 100644 src/config/doiconfig.local.example.json diff --git a/src/config/configuration.ts b/src/config/configuration.ts index 598936f27..aedc018a2 100644 --- a/src/config/configuration.ts +++ b/src/config/configuration.ts @@ -123,6 +123,8 @@ const configuration = () => ({ }, registerDoiUri: process.env.REGISTER_DOI_URI, registerMetadataUri: process.env.REGISTER_METADATA_URI, + doiUsername: process.env.DOI_USERNAME, + doiPassword: process.env.DOI_PASSWORD, site: process.env.SITE, smtp: { host: process.env.SMTP_HOST, diff --git a/src/config/doiconfig.local.example.json b/src/config/doiconfig.local.example.json deleted file mode 100644 index c27023f96..000000000 --- a/src/config/doiconfig.local.example.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "username": "", - "password": "" -} \ No newline at end of file diff --git a/src/published-data/published-data.controller.ts b/src/published-data/published-data.controller.ts index 2187513b4..259c17c47 100644 --- a/src/published-data/published-data.controller.ts +++ b/src/published-data/published-data.controller.ts @@ -257,7 +257,7 @@ export class PublishedDataController { const registerDataciteMetadataOptions = { method: "PUT", data: xml, - url: registerMetadataUri, + url: `${registerMetadataUri}/${fullDoi}`, headers: { "content-type": "application/xml;charset=UTF-8", }, @@ -267,14 +267,10 @@ export class PublishedDataController { const encodeDoi = encodeURIComponent(encodeURIComponent(fullDoi)); //Needed to make sure that the "/" between DOI prefix and ID stays encoded in datacite const registerDataciteDoiOptions = { method: "PUT", - data: [ - "#Content-Type:text/plain;charset=UTF-8", - `doi= ${fullDoi}`, - `url= ${this.configService.get( - "publicURLprefix", - )}${encodeDoi}`, - ].join("\n"), - url: registerDoiUri, + data: `#Content-Type:text/plain;charset=UTF-8\ndoi= ${fullDoi}\nurl=${this.configService.get( + "publicURLprefix", + )}${encodeDoi}`, + url: `${registerDoiUri}/${fullDoi}`, headers: { "content-type": "text/plain;charset=UTF-8", }, @@ -300,7 +296,7 @@ export class PublishedDataController { let res; try { res = await firstValueFrom( - this.httpService.request({ + this.httpService.request({ ...registerDataciteMetadataOptions, method: "PUT", }), @@ -337,7 +333,7 @@ export class PublishedDataController { console.error(error); } - return res ? res.data : null; + return res ? { doi: res.data } : null; } else if (!this.configService.get("oaiProviderRoute")) { try { await this.publishedDataService.update( @@ -360,7 +356,7 @@ export class PublishedDataController { let res; try { res = await firstValueFrom( - this.httpService.request({ + this.httpService.request({ ...syncOAIPublication, method: "POST", }), @@ -382,7 +378,7 @@ export class PublishedDataController { console.error(error); } - return res ? res.data : null; + return res ? { doi: res.data } : null; } } @@ -479,7 +475,7 @@ function formRegistrationXML(publishedData: PublishedData): string { }); return ` - + ${doi} ${creatorElements.join("\n")}