Skip to content

Commit

Permalink
Merge pull request #6 from hathitrust/DEV-726
Browse files Browse the repository at this point in the history
Use nock for mocking jira API
  • Loading branch information
aelkiss authored Jun 8, 2023
2 parents 6d45b9c + 188f29f commit 4f86833
Show file tree
Hide file tree
Showing 7 changed files with 407 additions and 40 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
JIRA_ENDPOINT='https://whatever.atlassian.net'
JIRA_USERNAME = ''
JIRA_KEY = ''
HT_ACCOUNT_ID = ''

# General Support queue
GS_SERVICE_DESK_ID = ''
GS_REQUEST_TYPE_ID = ''
GS_REQUEST_TYPE_ID = ''
4 changes: 4 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const express = require("express");
const cors = require("cors");
const morgan = require("morgan");
const rateLimit = require("express-rate-limit");

const app = express();
Expand All @@ -12,6 +13,9 @@ app.use("/api", express.json());
// Enable cors
app.use("/api", cors());

// enable logging
app.use(morgan('combined'))

// Rate limiting
// returns 429 'Too many requests' error if limit is hit
const limiter = rateLimit({
Expand Down
16 changes: 11 additions & 5 deletions customers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const needle = require("needle");

const JIRA_USERNAME = process.env.JIRA_USERNAME;
const JIRA_KEY = process.env.JIRA_KEY;
const GS_SERVICE_DESK_ID = process.env.GS_SERVICE_DESK_ID;
const HT_ACCOUNT_ID = process.env.HT_ACCOUNT_ID;
const JIRA_ENDPOINT = process.env.JIRA_ENDPOINT;
const JIRA_KEY = process.env.JIRA_KEY;
const JIRA_USERNAME = process.env.JIRA_USERNAME;

const headerOptions = {
username: JIRA_USERNAME,
Expand All @@ -22,7 +24,7 @@ createNewCustomer = async (email, name) => {
try {
let createCustomer = await needle(
"post",
"https://hathitrust.atlassian.net/rest/servicedeskapi/customer",
`${JIRA_ENDPOINT}/rest/servicedeskapi/customer`,
createCustomerData,
headerOptions
);
Expand Down Expand Up @@ -55,7 +57,7 @@ addCustomerToServiceDesk = async (account) => {
try {
let addCustomer = await needle(
"post",
"https://hathitrust.atlassian.net/rest/servicedeskapi/servicedesk/8/customer",
`${JIRA_ENDPOINT}/rest/servicedeskapi/servicedesk/${GS_SERVICE_DESK_ID}/customer`,
customerAccountID,
headerOptions
);
Expand All @@ -78,14 +80,18 @@ addCustomerToServiceDesk = async (account) => {

//returns account ID of customer
exports.getCustomerRecord = async (email, name) => {

// bail out early if no email was provided
if(!email) { return HT_ACCOUNT_ID }

//encode symbols in email address before passing to Jira
const encodedEmail = encodeURIComponent(email);

try {
//send GET request to general system /user endpoint
let getCustomerData = await needle(
"get",
`https://hathitrust.atlassian.net/rest/api/latest/user/search?query=${encodedEmail}`,
`${JIRA_ENDPOINT}/rest/api/latest/user/search?query=${encodedEmail}`,
{
headers: { "X-ExperimentalApi": "opt-in" },
username: JIRA_USERNAME,
Expand Down
Loading

0 comments on commit 4f86833

Please sign in to comment.