Skip to content

Commit ad582ca

Browse files
committed
Checks and adds Withdrawn note based on Item's HOMELOCATION or CURRENTLOCATION
1 parent f721e4c commit ad582ca

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

libsys_airflow/plugins/folio/items.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
logger = logging.getLogger(__name__)
1717

1818
# TODO: once on python >= 3.11, can mark "codes" specifically as NotRequired; for now, total=False makes all keys optional
19-
BarcodeDict = TypedDict('BarcodeDict', {"suppress?": bool, "codes": list}, total=False)
19+
BarcodeDict = TypedDict('BarcodeDict', {"suppress?": bool, "codes": list, "withdrawn?": bool}, total=False)
2020

2121

2222
def _determine_discovery_suppress(row: dict, suppressed_locations: dict) -> bool:
@@ -47,6 +47,15 @@ def _determine_stat_codes(row: dict, stat_codes: dict) -> list:
4747
return codes
4848

4949

50+
def _determine_withdrawn(row: dict) -> bool:
51+
"""
52+
Determines if Item is withdrawn
53+
"""
54+
return row["HOMELOCATION"].startswith("WITHDRAWN") or row[
55+
"CURRENTLOCATION"
56+
].startswith("WITHDRAWN")
57+
58+
5059
def _generate_item_notes(
5160
item, tsv_note_df: pd.DataFrame, item_note_types: dict
5261
) -> None:
@@ -124,6 +133,9 @@ def _generate_items_lookups(
124133
if len(codes) > 0:
125134
items_lookup[row["BARCODE"]]["codes"] = codes
126135

136+
# Withdrawn
137+
items_lookup[row["BARCODE"]]["withdrawn?"] = _determine_withdrawn(row)
138+
127139
return items_lookup
128140

129141

@@ -195,6 +207,20 @@ def _retrieve_item_notes_ids(folio_client) -> dict:
195207
return note_types
196208

197209

210+
def _set_withdrawn_note(item: dict, item_lookups: dict, note_types: dict) -> None:
211+
"""
212+
Adds Withdrawn Note based on lookup
213+
"""
214+
if item_lookups.get(item.get('barcode'), {}).get("withdrawn?", False):
215+
note = {"note": "Withdrawn in Symphony", "type": note_types.get("Withdrawn")}
216+
if "notes" in item:
217+
item["notes"].append(note)
218+
else:
219+
item["notes"] = [
220+
note,
221+
]
222+
223+
198224
def _add_additional_info(**kwargs) -> None:
199225
"""Generates notes from tsv files"""
200226
airflow: str = kwargs["airflow"]
@@ -213,7 +239,7 @@ def _add_additional_info(**kwargs) -> None:
213239
tsv_notes_path = pathlib.Path(tsv_notes_path)
214240
tsv_notes_df = pd.read_csv(tsv_notes_path, sep="\t", dtype=object)
215241

216-
item_note_types = _retrieve_item_notes_ids(folio_client)
242+
item_note_types = _retrieve_item_notes_ids(folio_client)
217243

218244
items_lookup = _generate_items_lookups(airflow, items_tsv_path, folio_client)
219245

@@ -229,6 +255,7 @@ def _add_additional_info(**kwargs) -> None:
229255
_set_discovery_suppress(item, items_lookup)
230256
if "codes" in items_lookup.get(item.get("barcode"), {}):
231257
item["statisticalCodeIds"] = items_lookup[item.get("barcode")]["codes"]
258+
_set_withdrawn_note(item, items_lookup, item_note_types)
232259
items.append(item)
233260

234261
if not len(items) % 1000:

tests/test_items.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
run_items_transformer,
1616
_add_additional_info,
1717
_generate_item_notes,
18+
_set_withdrawn_note,
1819
_remove_on_order_items,
1920
)
2021

@@ -35,6 +36,7 @@ def test_items_transformers():
3536
{"id": "e9f6de86-e564-4095-a61a-38c9e0e6b2fc", "name": "Tech Staff"},
3637
{"id": "62fd6fcc-5cde-4a74-849a-66e2d77a1f12", "name": "Public"},
3738
{"id": "1d14675c-c163-4502-98f9-961cd3d17ab2", "name": "Circ Staff"},
39+
{"id": "abeebfcc-53f9-4aea-ad80-561bebd99754", "name": "Withdrawn"},
3840
]
3941
}
4042

@@ -92,7 +94,8 @@ def mock_get(*args, **kwargs):
9294
"1233455\ta note\tCIRCSTAFF\n",
9395
"1233455\ta note for the public\tCIRCNOTE\n",
9496
"55678446243\tavailable for checkout\tPUBLIC\n",
95-
"55678446243\ttf:green, hbr 9/20/2013\tTECHSTAFF",
97+
"55678446243\ttf:green, hbr 9/20/2013\tTECHSTAFF\n",
98+
"8772580-1001\tAt Hoover\tHVSHELFLOC",
9699
]
97100

98101
items_recs = [
@@ -116,6 +119,10 @@ def mock_get(*args, **kwargs):
116119
"holdingsRecordId": "fc473c74-c811-4ae9-bcd9-387a1d10b967",
117120
"barcode": "0267132027",
118121
},
122+
{
123+
"holdingsRecordId": "a36e1aa4-e965-522a-8afb-dceac63f4206",
124+
"barcode": "8772580-1001",
125+
},
119126
]
120127

121128
items_tsv = [
@@ -125,6 +132,7 @@ def mock_get(*args, **kwargs):
125132
"145623\t1\t1\t4614642357\tSAL3\tSPECB-S\tINPROCESS\tSTKS-MONO\t\tFED-WEED\t0\tLC\tRH640 .I34 1996\t\t0\tMARC\t0",
126133
"262345\t1\t1\t7659908473\tSAL3\tINPROCESS\tINPROCESS\tSTKS-MONO\t\t\t1\tLC\tYU40 .J4 2096\t\t0\tMARC\t0", # ITEM SHADOW
127134
"5559991\t1\t1\t7659908473\tSAL3\tINPROCESS\tINPROCESS\tSTKS-MONO\tDIGI-SENT\tFED-WEED\t0\tLC\tEG640 .J4 1796\t\t1\tMARC\t0", # CALL SHADOW
135+
"8772580\t1\t1\t8772580-1001\tSUL\tSTACKS\tWITHDRAWN\tSTKS\t\t\t0\t1\tLC\tXX(8772580.1)\t0\tMARC\t0",
128136
]
129137

130138

@@ -237,10 +245,11 @@ def test_add_additional_info(
237245
assert "discoverySuppress" not in new_items_recs[1]
238246
assert new_items_recs[2]["discoverySuppress"] is True
239247
assert new_items_recs[3]["discoverySuppress"] is True
240-
assert new_items_recs[-2]["statisticalCodeIds"] == [
248+
assert new_items_recs[-3]["statisticalCodeIds"] == [
241249
"8be8d577-1cd7-4b84-ae71-d9472fc4d2b1",
242250
"9c98fbcc-1728-41f5-9382-038d9fa45c0f",
243251
]
252+
assert new_items_recs[-1]["notes"][1]["note"] == "Withdrawn in Symphony"
244253

245254

246255
def test_add_additional_info_missing_barcode(
@@ -308,3 +317,11 @@ def test_remove_on_order_items(tmp_path):
308317
_remove_on_order_items(source_path)
309318
filtered_df = pd.read_csv(source_path, sep="\t")
310319
assert len(filtered_df) == 1
320+
321+
322+
def test_set_withdrawn_note_no_prior_notes():
323+
item = {"barcode": "295013344"}
324+
item_lookups = {"295013344": {"withdrawn?": True}}
325+
_set_withdrawn_note(item, item_lookups, item_note_types)
326+
assert item["notes"][0]["note"] == "Withdrawn in Symphony"
327+
assert item["notes"][0]["type"] == item_note_types.get("Withdrawn")

0 commit comments

Comments
 (0)