Skip to content

Commit

Permalink
Added action updateBorrowerEmail
Browse files Browse the repository at this point in the history
  • Loading branch information
verheyenkoen committed Aug 21, 2023
1 parent 4a1dd8c commit a104bfe
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 30 deletions.
42 changes: 42 additions & 0 deletions actions/update-borrower-email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { toXML } from 'jstoxml'

import alephFetch from '../util/aleph-fetch'
import { borrowerInfo } from './borrower-info'

export async function updateBorrowerEmail(borId: string, newEmailAddress: string) {
if (!borId.match(/^PWD\d+$/)) {
const borInfo = await borrowerInfo(borId, false, false)
borId = borInfo.z303['z303-id']
}

const requestData = {
'p-file-20': {
'patron-record': {
z303: {
'match-id-type': '00',
'match-id': borId,
'record-action': 'X',
},
z304: {
'record-action': 'U',
'z304-email-address': newEmailAddress,
'z304-address-type': '01',
},
},
},
}

const params = {
update_flag: 'Y',
xml_full_req: toXML(requestData),
}

const result = await alephFetch('update-bor', params, undefined, true, 'POST')
if (result.error) {
const error = typeof result.error === 'string' ? result.error : result.error[0]

if (error && error !== `Succeeded to REWRITE table z304. cur-id ${borId}.`) {
throw new Error(error)
}
}
}
4 changes: 3 additions & 1 deletion dist/index.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,8 @@ declare function present(setNumber: string, setEntry: string): Promise<Aleph.Bas
declare function readItem(barcode: string): Promise<Aleph.ReadItemResponse>;
declare function readItemByDocument(doc_number: string, item_sequence: string): Promise<Aleph.BaseResponse>;

declare function updateBorrowerEmail(borId: string, newEmailAddress: string): Promise<void>;

declare function updateItem(docNumber: string, itemSequence: string, ...data: object[] | string[]): Promise<Aleph.BaseResponse>;

export { Aleph, borrowerInfo, find, findDocument, holdRequest, holdRequestCancel, itemData, present, readItem, readItemByDocument, updateItem };
export { Aleph, borrowerInfo, find, findDocument, holdRequest, holdRequestCancel, itemData, present, readItem, readItemByDocument, updateBorrowerEmail, updateItem };
4 changes: 3 additions & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,8 @@ declare function present(setNumber: string, setEntry: string): Promise<Aleph.Bas
declare function readItem(barcode: string): Promise<Aleph.ReadItemResponse>;
declare function readItemByDocument(doc_number: string, item_sequence: string): Promise<Aleph.BaseResponse>;

declare function updateBorrowerEmail(borId: string, newEmailAddress: string): Promise<void>;

declare function updateItem(docNumber: string, itemSequence: string, ...data: object[] | string[]): Promise<Aleph.BaseResponse>;

export { Aleph, borrowerInfo, find, findDocument, holdRequest, holdRequestCancel, itemData, present, readItem, readItemByDocument, updateItem };
export { Aleph, borrowerInfo, find, findDocument, holdRequest, holdRequestCancel, itemData, present, readItem, readItemByDocument, updateBorrowerEmail, updateItem };
67 changes: 55 additions & 12 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"use strict";
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
Expand All @@ -19,7 +17,6 @@ var __spreadValues = (a, b) => {
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
Expand Down Expand Up @@ -66,6 +63,7 @@ __export(aleph_fetch_exports, {
present: () => present,
readItem: () => readItem,
readItemByDocument: () => readItemByDocument,
updateBorrowerEmail: () => updateBorrowerEmail,
updateItem: () => updateItem
});
module.exports = __toCommonJS(aleph_fetch_exports);
Expand All @@ -80,16 +78,22 @@ envVariables.parse(process.env);

// util/aleph-fetch.ts
var import_xml2js = require("xml2js");
function alephFetch(op, params, explicitArray = false, ignoreErrors = false) {
function alephFetch(op, params, explicitArray = false, ignoreErrors = false, method = "GET") {
return __async(this, null, function* () {
const url = new URL("X", process.env.ALEPH_HOST);
params = __spreadProps(__spreadValues({
params = __spreadValues({
op,
library: "rug50"
}, params), {
op
});
Object.entries(params).forEach(([key, value]) => url.searchParams.append(key, value));
const response = yield fetch(url.toString());
}, params);
let requestBody = null;
const headers = {};
if (method === "POST") {
requestBody = new URLSearchParams(params);
headers["Content-Type"] = "application/x-www-form-urlencoded";
} else {
Object.entries(params).forEach(([key, value]) => url.searchParams.append(key, value));
}
const response = yield fetch(url.toString(), { method, body: requestBody, headers });
const body = yield response.text();
if (body.includes("Error 403")) {
throw new Error(`Cannot reach ALEPH_HOST: ${process.env.ALEPH_HOST}`);
Expand Down Expand Up @@ -211,8 +215,46 @@ function readItemByDocument(doc_number, item_sequence) {
});
}

// actions/update-item.ts
// actions/update-borrower-email.ts
var import_jstoxml = require("jstoxml");
function updateBorrowerEmail(borId, newEmailAddress) {
return __async(this, null, function* () {
if (!borId.match(/^PWD\d+$/)) {
const borInfo = yield borrowerInfo(borId, false, false);
borId = borInfo.z303["z303-id"];
}
const requestData = {
"p-file-20": {
"patron-record": {
z303: {
"match-id-type": "00",
"match-id": borId,
"record-action": "X"
},
z304: {
"record-action": "U",
"z304-email-address": newEmailAddress,
"z304-address-type": "01"
}
}
}
};
const params = {
update_flag: "Y",
xml_full_req: (0, import_jstoxml.toXML)(requestData)
};
const result = yield alephFetch("update-bor", params, void 0, true, "POST");
if (result.error) {
const error = typeof result.error === "string" ? result.error : result.error[0];
if (error && error !== `Succeeded to REWRITE table z304. cur-id ${borId}.`) {
throw new Error(error);
}
}
});
}

// actions/update-item.ts
var import_jstoxml2 = require("jstoxml");
var import_lodash = require("lodash");
function updateItem(docNumber, itemSequence, ...data) {
return __async(this, null, function* () {
Expand All @@ -235,7 +277,7 @@ function updateItem(docNumber, itemSequence, ...data) {
for (let [key, value] of dataset) {
updateItemRequest["update-item"]["z30"][key] = value;
}
const response = yield alephFetch("update-item", { xml_full_req: (0, import_jstoxml.toXML)(updateItemRequest) }, false, true);
const response = yield alephFetch("update-item", { xml_full_req: (0, import_jstoxml2.toXML)(updateItemRequest) }, false, true);
if (response.error) {
const error = typeof response.error !== "string" ? response.error[0] : response.error;
if (error !== "Item has been updated successfully") {
Expand All @@ -256,5 +298,6 @@ function updateItem(docNumber, itemSequence, ...data) {
present,
readItem,
readItemByDocument,
updateBorrowerEmail,
updateItem
});
66 changes: 54 additions & 12 deletions dist/index.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
Expand All @@ -16,7 +14,6 @@ var __spreadValues = (a, b) => {
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
Expand Down Expand Up @@ -48,16 +45,22 @@ envVariables.parse(process.env);

// util/aleph-fetch.ts
import { parseStringPromise } from "xml2js";
function alephFetch(op, params, explicitArray = false, ignoreErrors = false) {
function alephFetch(op, params, explicitArray = false, ignoreErrors = false, method = "GET") {
return __async(this, null, function* () {
const url = new URL("X", process.env.ALEPH_HOST);
params = __spreadProps(__spreadValues({
params = __spreadValues({
op,
library: "rug50"
}, params), {
op
});
Object.entries(params).forEach(([key, value]) => url.searchParams.append(key, value));
const response = yield fetch(url.toString());
}, params);
let requestBody = null;
const headers = {};
if (method === "POST") {
requestBody = new URLSearchParams(params);
headers["Content-Type"] = "application/x-www-form-urlencoded";
} else {
Object.entries(params).forEach(([key, value]) => url.searchParams.append(key, value));
}
const response = yield fetch(url.toString(), { method, body: requestBody, headers });
const body = yield response.text();
if (body.includes("Error 403")) {
throw new Error(`Cannot reach ALEPH_HOST: ${process.env.ALEPH_HOST}`);
Expand Down Expand Up @@ -179,8 +182,46 @@ function readItemByDocument(doc_number, item_sequence) {
});
}

// actions/update-item.ts
// actions/update-borrower-email.ts
import { toXML } from "jstoxml";
function updateBorrowerEmail(borId, newEmailAddress) {
return __async(this, null, function* () {
if (!borId.match(/^PWD\d+$/)) {
const borInfo = yield borrowerInfo(borId, false, false);
borId = borInfo.z303["z303-id"];
}
const requestData = {
"p-file-20": {
"patron-record": {
z303: {
"match-id-type": "00",
"match-id": borId,
"record-action": "X"
},
z304: {
"record-action": "U",
"z304-email-address": newEmailAddress,
"z304-address-type": "01"
}
}
}
};
const params = {
update_flag: "Y",
xml_full_req: toXML(requestData)
};
const result = yield alephFetch("update-bor", params, void 0, true, "POST");
if (result.error) {
const error = typeof result.error === "string" ? result.error : result.error[0];
if (error && error !== `Succeeded to REWRITE table z304. cur-id ${borId}.`) {
throw new Error(error);
}
}
});
}

// actions/update-item.ts
import { toXML as toXML2 } from "jstoxml";
import { isPlainObject, chunk } from "lodash";
function updateItem(docNumber, itemSequence, ...data) {
return __async(this, null, function* () {
Expand All @@ -203,7 +244,7 @@ function updateItem(docNumber, itemSequence, ...data) {
for (let [key, value] of dataset) {
updateItemRequest["update-item"]["z30"][key] = value;
}
const response = yield alephFetch("update-item", { xml_full_req: toXML(updateItemRequest) }, false, true);
const response = yield alephFetch("update-item", { xml_full_req: toXML2(updateItemRequest) }, false, true);
if (response.error) {
const error = typeof response.error !== "string" ? response.error[0] : response.error;
if (error !== "Item has been updated successfully") {
Expand All @@ -223,5 +264,6 @@ export {
present,
readItem,
readItemByDocument,
updateBorrowerEmail,
updateItem
};
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from './actions/hold-request'
export * from './actions/item-data'
export * from './actions/present'
export * from './actions/read-item'
export * from './actions/update-borrower-email'
export * from './actions/update-item'

export * from './typings/aleph'
16 changes: 12 additions & 4 deletions util/aleph-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@ export default async function alephFetch<TResponse extends Aleph.BaseResponse>(
op: string,
params: Record<string, string>,
explicitArray = false,
ignoreErrors = false
ignoreErrors = false,
method = 'GET'
): Promise<TResponse | never> {
const url = new URL('X', process.env.ALEPH_HOST)

params = {
op,
library: 'rug50',
...params,
op,
}

Object.entries(params).forEach(([key, value]) => url.searchParams.append(key, value))
let requestBody: URLSearchParams | null = null
const headers: HeadersInit = {}
if (method === 'POST') {
requestBody = new URLSearchParams(params)
headers['Content-Type'] = 'application/x-www-form-urlencoded'
} else {
Object.entries(params).forEach(([key, value]) => url.searchParams.append(key, value))
}

const response = await fetch(url.toString())
const response = await fetch(url.toString(), { method, body: requestBody, headers })
const body = await response.text()

if (body.includes('Error 403')) {
Expand Down

0 comments on commit a104bfe

Please sign in to comment.