Skip to content

Commit

Permalink
Merge pull request #416 from SciCatProject/SWAP-3178-throw-error-if-r…
Browse files Browse the repository at this point in the history
…egister-not-successful

fix: throw error instead of returning null with success if register DOI failed and fix the register endpoint bugs
  • Loading branch information
nitrosx authored Apr 4, 2023
2 parents a016ab4 + 8affd53 commit 52ec511
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 39 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=<SITE>
SMTP_HOST=<SMTP_HOST>
SMTP_MESSAGE_FROM=<SMTP_MESSAGE_FROM>
Expand Down
2 changes: 2 additions & 0 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 0 additions & 4 deletions src/config/doiconfig.local.example.json

This file was deleted.

71 changes: 37 additions & 34 deletions src/published-data/published-data.controller.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
Controller,
Get,
Expand All @@ -11,6 +12,7 @@ import {
UseInterceptors,
HttpException,
HttpStatus,
NotFoundException,
} from "@nestjs/common";
import { PublishedDataService } from "./published-data.service";
import { CreatePublishedDataDto } from "./dto/create-published-data.dto";
Expand Down Expand Up @@ -181,9 +183,6 @@ export class PublishedDataController {
@CheckPolicies((ability: AppAbility) =>
ability.can(Action.Update, PublishedData),
)
// @UseInterceptors(
// new SetCreatedUpdatedAtInterceptor<PublishedData>("updatedAt"),
// )
@Patch("/:id")
async update(
@Param("id") id: string,
Expand All @@ -210,9 +209,6 @@ export class PublishedDataController {
@CheckPolicies((ability: AppAbility) =>
ability.can(Action.Update, PublishedData),
)
// @UseInterceptors(
// new SetCreatedUpdatedAtInterceptor<PublishedData>("updatedAt"),
// )
@Post("/:id/register")
async register(@Param("id") id: string): Promise<IRegister | null> {
const publishedData = await this.publishedDataService.findOne({ doi: id });
Expand Down Expand Up @@ -248,16 +244,20 @@ export class PublishedDataController {
password: "removed",
};

if (existsSync(this.doiConfigPath)) {
doiProviderCredentials = JSON.parse(
readFileSync(this.doiConfigPath).toString(),
);
const username = this.configService.get<string>("doiUsername");
const password = this.configService.get<string>("doiPassword");

if (username && password) {
doiProviderCredentials = {
username,
password,
};
}

const registerDataciteMetadataOptions = {
method: "PUT",
data: xml,
url: registerMetadataUri,
url: `${registerMetadataUri}/${fullDoi}`,
headers: {
"content-type": "application/xml;charset=UTF-8",
},
Expand All @@ -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<string>(
"publicURLprefix",
)}${encodeDoi}`,
].join("\n"),
url: registerDoiUri,
data: `#Content-Type:text/plain;charset=UTF-8\ndoi= ${fullDoi}\nurl=${this.configService.get<string>(
"publicURLprefix",
)}${encodeDoi}`,
url: `${registerDoiUri}/${fullDoi}`,
headers: {
"content-type": "text/plain;charset=UTF-8",
},
Expand All @@ -300,14 +296,17 @@ export class PublishedDataController {
let res;
try {
res = await firstValueFrom(
this.httpService.request<IRegister>({
this.httpService.request({
...registerDataciteMetadataOptions,
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 {
Expand All @@ -317,9 +316,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 {
Expand All @@ -331,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<string>("oaiProviderRoute")) {
try {
await this.publishedDataService.update(
Expand All @@ -354,14 +356,17 @@ export class PublishedDataController {
let res;
try {
res = await firstValueFrom(
this.httpService.request<IRegister>({
this.httpService.request({
...syncOAIPublication,
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 {
Expand All @@ -373,20 +378,18 @@ export class PublishedDataController {
console.error(error);
}

return res ? res.data : null;
return res ? { doi: res.data } : null;
}
}
return null;

throw new NotFoundException();
}

// POST /publisheddata/:id/resync
@UseGuards(PoliciesGuard)
@CheckPolicies((ability: AppAbility) =>
ability.can(Action.Update, PublishedData),
)
// @UseInterceptors(
// new SetCreatedUpdatedAtInterceptor<PublishedData>("updatedAt"),
// )
@Post("/:id/resync")
async resync(
@Param("id") id: string,
Expand Down Expand Up @@ -472,7 +475,7 @@ function formRegistrationXML(publishedData: PublishedData): string {
});

return `<?xml version="1.0" encoding="UTF-8"?>
<resource xmlns="http://datacite.org/schema/kernel-4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://datacite.org/schema/kernel-4 http://schema.datacite.org/meta/kernel-4/metadata.xsd">
<resource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://datacite.org/schema/kernel-4" xsi:schemaLocation="http://datacite.org/schema/kernel-4 https://schema.datacite.org/meta/kernel-4.4/metadata.xsd">
<identifier identifierType="doi">${doi}</identifier>
<creators>
${creatorElements.join("\n")}
Expand Down
2 changes: 1 addition & 1 deletion test/DatasetFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down

0 comments on commit 52ec511

Please sign in to comment.