Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bibdk2021 1568 checkorder v1 #134

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 23 additions & 84 deletions src/datasources/checkorder.datasource.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,24 @@
/**
* @file Get suggestions
*
* This is temporary until real suggester is implemented
*/

import request from "superagent";
import config from "../config";

let parseString = require("xml2js").parseString;
let stripNS = require("xml2js").processors.stripPrefix;

const {
authenticationUser,
authenticationGroup,
authenticationPassword,
serviceRequester,
url,
ttl,
prefix,
} = config.datasources.openorder;

function createRequest(pid, pickupAgency) {
return `<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:open="http://oss.dbc.dk/ns/openorder">
<soapenv:Header/>
<soapenv:Body>
<open:checkOrderPolicyRequest xmlns:open="http://oss.dbc.dk/ns/openorder">
<open:authentication>
<open:groupIdAut>${authenticationGroup}</open:groupIdAut>
<open:passwordAut>${authenticationPassword}</open:passwordAut>
<open:userIdAut>${authenticationUser}</open:userIdAut>
</open:authentication>
<open:pickUpAgencyId>${pickupAgency}</open:pickUpAgencyId>
<!--Zero or more repetitions:-->
<open:pid>${pid}</open:pid>
<open:serviceRequester>${serviceRequester}</open:serviceRequester>
</open:checkOrderPolicyRequest>
</soapenv:Body>
</soapenv:Envelope>`;
}

export async function load({ pid, pickupBranch }) {
const xml = createRequest(pid, pickupBranch);
const policy = await request
.post(url)
.set("Content-Type", "text/xml")
.send(xml)
.then((body) => {
parseString(
body.text,
{ trim: true, tagNameProcessors: [stripNS] },
function (err, result) {
body = result.Envelope.Body[0].checkOrderPolicyResponse[0];
}
);
const data = {};

if (body.checkOrderPolicyError) {
// @TODO log
return {
statusCode: 500,
error: body.checkOrderPolicyError[0],
};
}

if (body.orderPossible) {
data.orderPossible = body.orderPossible[0] !== "false";
}
if (body.orderPossibleReason) {
data.orderPossibleReason = body.orderPossibleReason[0];
}
if (body.lookUpUrl) {
data.lookUpUrl = body.lookUpUrl[0];
}
return { statusCode: 200, data: data };
})
.catch((err) => {
// @TODO log
console.log(err);
});

return policy.data;
const url =
"http://copa-rs.iscrum-ors-staging.svc.cloud.dbc.dk/copa-rs/api/v1/checkorderpolicy/";
const serviceRequester = "190101";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

skal url'en og serviceReq være hardcoded eller heller være env-variabler?


export async function load({ pid, pickupBranch, accessToken }, context) {
const post = {
pickUpAgencyId: pickupBranch,
pid: [pid],
serviceRequester: serviceRequester,
};

const policy = await context.fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify(post),
});

return policy?.body || null;
}

/**
Expand All @@ -94,7 +33,7 @@ export async function load({ pid, pickupBranch }) {

export const options = {
redis: {
prefix,
ttl,
prefix: "checkorder-1",
ttl: 5,
},
};
2 changes: 0 additions & 2 deletions src/datasources/localizations.datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ export async function load({ pids }, context) {
}),
});

console.log(JSON.stringify(response, null, 4));

return parseResponse(response?.body?.localizations);
} catch (e) {
log.error("Request to holdingsservice failed." + " Message: " + e.message);
Expand Down