diff --git a/Gemfile b/Gemfile index f744f91..b64068a 100644 --- a/Gemfile +++ b/Gemfile @@ -24,7 +24,7 @@ gem "yabeda-prometheus" gem "alma_rest_client", git: "https://github.com/mlibrary/alma_rest_client", - tag: "1.3.1" + tag: "v2.0.0" group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console diff --git a/Gemfile.lock b/Gemfile.lock index ab8f692..3724122 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,11 +1,13 @@ GIT remote: https://github.com/mlibrary/alma_rest_client - revision: 2014809367ece3f21935bd24b45a12285f61e253 - tag: 1.3.1 + revision: 9606225d82480b6d1568902813ae9018dd8c1acc + tag: v2.0.0 specs: - alma_rest_client (1.3.1) + alma_rest_client (2.0.0) activesupport (~> 7.0, >= 4.2) - httparty + faraday + faraday-retry + httpx rexml GEM @@ -101,7 +103,6 @@ GEM bigdecimal rexml crass (1.0.6) - csv (3.3.2) database_cleaner (2.1.0) database_cleaner-active_record (>= 2, < 3) database_cleaner-active_record (2.2.0) @@ -121,14 +122,21 @@ GEM railties (>= 5.0.0) faker (3.5.1) i18n (>= 1.8.11, < 2) + faraday (2.12.2) + faraday-net_http (>= 2.0, < 3.5) + json + logger + faraday-net_http (3.4.0) + net-http (>= 0.5.0) + faraday-retry (2.2.1) + faraday (~> 2.0) ffi (1.17.0-x86_64-linux-gnu) globalid (1.2.1) activesupport (>= 6.1) hashdiff (1.1.2) - httparty (0.22.0) - csv - mini_mime (>= 1.0.0) - multi_xml (>= 0.5.2) + http-2 (1.0.2) + httpx (1.4.0) + http-2 (>= 1.0.0) i18n (1.14.6) concurrent-ruby (~> 1.0) io-console (0.8.0) @@ -157,9 +165,9 @@ GEM mini_mime (1.1.5) minitest (5.25.4) msgpack (1.7.5) - multi_xml (0.7.1) - bigdecimal (~> 3.1) mysql2 (0.5.6) + net-http (0.6.0) + uri net-imap (0.5.4) date net-protocol @@ -301,6 +309,7 @@ GEM unicode-display_width (3.1.2) unicode-emoji (~> 4.0, >= 4.0.4) unicode-emoji (4.0.4) + uri (1.0.2) useragent (0.16.11) webmock (3.24.0) addressable (>= 2.8.0) diff --git a/app/utils/checkout_history_loader.rb b/app/utils/checkout_history_loader.rb new file mode 100644 index 0000000..7e27520 --- /dev/null +++ b/app/utils/checkout_history_loader.rb @@ -0,0 +1,5 @@ +module CheckoutHistoryLoader + class LoanLoadError < StandardError; end + + class FetchReportError < StandardError; end +end diff --git a/app/utils/checkout_history_loader/loan_loader.rb b/app/utils/checkout_history_loader/loan_loader.rb new file mode 100644 index 0000000..7d24c60 --- /dev/null +++ b/app/utils/checkout_history_loader/loan_loader.rb @@ -0,0 +1,48 @@ +module CheckoutHistoryLoader + class LoanLoader + def initialize(report_item:, user:, loan:) + @report_item = report_item + @user = user + @loan = loan + end + + def has_identical_checkout_date? + @loan.checkout_date&.to_date&.to_fs(:db) == @report_item.checkout_date + end + + def load + return if @loan.return_date.present? + return if @report_item.return_date.blank? + + @loan.tap do |l| + l.user = @user + l.id = @report_item.id + l.title = @report_item.title + l.author = @report_item.author + l.mms_id = @report_item.mms_id + l.return_date = @report_item.return_date + l.checkout_date = @report_item.checkout_date + l.barcode = @report_item.barcode + l.call_number = @report_item.call_number + l.description = @report_item.description + end + + if @loan.save + Rails.logger.info("item_loan '#{@loan.id}' saved") + else + Rails.logger.error("item_loan '#{@loan.id}' not saved: #{@loan.errors.full_messages}") + raise LoanLoadError, @loan.errors.full_messages + end + end + + def self.load(report_item) + user = User.find_or_create_by_uniqname(report_item.uniqname) + unless user.retain_history + Rails.logger.warn("item_loan '#{report_item.id}' not saved: patron opt out") + return + end + loan = Loan.find_or_create_by(id: report_item.id) + new(report_item: report_item, user: user, loan: loan).load + end + end +end diff --git a/app/utils/checkout_history_loader/report.rb b/app/utils/checkout_history_loader/report.rb new file mode 100644 index 0000000..6c8774a --- /dev/null +++ b/app/utils/checkout_history_loader/report.rb @@ -0,0 +1,28 @@ +module CheckoutHistoryLoader + class Report + include Enumerable + + def initialize(items) + @items = items + end + + def each(&block) + @items.each(&block) + end + + def self.for_rows(rows) + new(rows.map { |x| ReportItem.new(x) }) + end + + def self.fetch + rows = [] + response = AlmaRestClient.client.get_report(path: ENV.fetch("CIRC_REPORT_PATH")) do |row| + rows.push ReportItem.new(row) + end + if response.status != 200 + raise FetchReportError, response.body + end + new(rows) + end + end +end diff --git a/app/utils/checkout_history_loader/report_item.rb b/app/utils/checkout_history_loader/report_item.rb new file mode 100644 index 0000000..32480a0 --- /dev/null +++ b/app/utils/checkout_history_loader/report_item.rb @@ -0,0 +1,47 @@ +module CheckoutHistoryLoader + class ReportItem + def initialize(row) + @row = row + end + + def uniqname + @row["User Primary Identifier"] + end + + def id + @row["Item Loan Id"] + end + + def title + @row["Title"]&.slice(0, 255) + end + + def author + @row["Author"]&.slice(0, 255) + end + + def mms_id + @row["MMS Id"] + end + + def checkout_date + @row["Loan Date"] + end + + def return_date + @row["Return Date"] + end + + def barcode + @row["Barcode"] + end + + def call_number + @row["Call Number"] + end + + def description + @row["Description"] + end + end +end diff --git a/app/utils/fix_uniqname_problem.rb b/app/utils/fix_uniqname_problem.rb new file mode 100644 index 0000000..52084d7 --- /dev/null +++ b/app/utils/fix_uniqname_problem.rb @@ -0,0 +1,23 @@ +module FixUniqnameProblem + def self.opted_in?(uniqname) + User.find(uniqname)&.retain_history + rescue + false + end + + def self.list_of_opted_in + path = "#{Rails.root}/app/utils/uniqnames.txt" + list = [] + File.open(path, "r") do |f| + f.each_line do |line| + if opted_in?(line.downcase.strip) + list.push(line) + end + end + end + output = "#{Rails.root}/app/utils/opted_in.txt" + File.open(output, "w") do |f| + f.puts list + end + end +end diff --git a/app/utils/opted_in.txt b/app/utils/opted_in.txt new file mode 100644 index 0000000..33ef2db --- /dev/null +++ b/app/utils/opted_in.txt @@ -0,0 +1,458 @@ +HORNING@OAKLAND.EDU +IDHATCH +KROSENB +LEWISM@MSU.EDU +RLAMEY +SASHTON +aakimen +abenlouc +acaburke +acborl +adeese +adelpin +adibha +aelliot +aepare +afriedm +aganes +ahazelti +ajrutled +akanchi +akeddy +albertca +aleontis +alexaobu +alknysh +almantas +alyssiaw +amanuel +ambnicol +amendl +amengo +amlemon +amoskow +andreame +anjashep +annikalb +arconway +argere +aricken +arnoldpd +ashchow +asher +asottil +atanga +auerback +autumnnw +avalade +avpai +aykalan +balexp +barbara +barillas +basilisc +bawardi +bbrose +bdede +bdwulf +belot +benkorn +bernhaut +bjcoyle +bjessie +bkmil +bpuroll +briklein +brismi +brmaya +brownash +btalman +camerowh +carterob +cdepee +cedsteve +celiaes +cfeak +chiarleo +chilling +chriswh +cjbond +ckayko +clangw +cmodey +cpachika +crhogan +danocone +daweaver +dbathala +dejenlin +dhigbeew +dhoonlee +dhruvkul +diu +divyanrn +djsinger +dkforger +dkwabi +dkyeom +dlehman +dlstone +dmleyton +dnemser +dpomera +dporter +drolston +dsp +dsuolang +dthacher +dufallo +dworkin +dylanw +eandersn +eboneew +edenpeja +edwinwwh +eemarti +ekflanag +eklovya +ekosu +ekropf +eliotsch +ellastep +elwillou +ensorh +erobi +esears +esvoboda +ewhitmer +falmutaw +fanjj +felixzg +fishman +fmir +fostermc +frankrd +franku +frenchk +funkyyue +gabbimck +gastonp +gblichar +gbocca +gdcrane +georgeh +geostein +gfharnik +gfreitas +gilligem +ginevram +gingerje +giorgiob +gjbaise +gkubi +gliddell +gmangan +grahdeth +grenberg +grocho +gshultz +gthallid +gunckel +gypin +hafizkm +halquist +hancockd +hantingc +hardyb +hastineo +hcako +hcclark +hell +hellpop +hgiza +hglay +hillerma +hlandon +hoytb +hxn +hysandy +ianmoyer +imuchnik +iparrag +iracine +israeli +jabaril +jabutzin +janetjw +jangeli +jansenjc +jarjenk +jaykayse +jcantu +jdlitman +jebt +jeffmart +jentramb +jesmidd +jggeorge +jgotlib +jhsansom +jimburn +jlausch +jlfreem +jllovett +jlwall +jmancina +jmcloone +jnasond +joanm +joeyu +johacket +johnanon +jonesand +joque +joshcole +joshmoss +joymar +jpboyd +jpieter +jpsot +jready +jreding +jscarson +jstuartb +juandel +junghj +jvansant +jzechel +kacalh +karinc +karsendi +katdav +kbowker +kcb +kchalipa +kdimmery +keenanc +kelleyl +kevinyan +kimmyung +kjenckes +kjgadd +kkarmen +kmkr +kooipond +kreedl +krlacy +kstegema +kstratt +ksunhong +kterryah +kurttb +kylemcc +kyokotac +lagarias +lanaghan +lapappas +lattaj +lauradh +lawrenm +lbciddio +leinaik +lggrey +lianars +lindhoek +linusw +lizx +lmmcc +lmsoo +lucicook +lunadiaz +lwexler +lzh +mabbaig +mahoneyg +majmajma +mandayu +mankouch +marand +marcins +margomen +marikoo +markwest +marygil +masonsea +mayanknm +mayorski +mbengts +mcerjak +mcgehee +mdelphia +mendlow +mfmcc +mgradoz +mgryan +mhartt +mhuntley +micwoods +milhaney +mironliu +mkatart +mkietzma +mladair +mlmunro +mmanu +mmcanear +moconway +mogan +moltke +mordoch +moreiras +mpsolo +mschlan +msgumiel +mslevine +mueggler +mullenpm +murphyan +mylim +mzimlich +narenlob +nelevin +nesilver +ngclark +nikithah +nklait +nornes +nshagri +ohbang +omairh +onatsu +osteiss +pagreen +panew +pannier +partner +pasqua +paulcjoh +paulq +pdeotare +pedrol +peggymcc +penarc +perezme +peterjon +pfa +prosserj +pselcer +psphang +ptorresp +ptydepe +puffh +pullerb +punnu +purzel +pyocum +racadler +raevinj +ravshari +rawlins +rbneal +rcaston +reckhoff +reneeran +reubenr +rhaefner +ribachm +rjanko +robmosc +robyb +rove +rparrine +rpl +rsjansen +rthumob +ruiqit +samfarm +sarafang +sarahrom +sbmills +sbuss +scottell +sdalcin +sdpage +seberry +sedarous +seow +sfrida +sharbu +sheiraco +shelbaum +shobita +shoemak +shreyad +sigrid +sikandar +simkraig +sinnanyj +sjcernak +sjco +skhagi +skolison +slavens +slonn +smkhist +smward +soleeb +soyolee +spinsker +spunar +srakei +srk +stanz +stewelde +sungr +surajka +swapnilr +sylviacc +tamaslyk +taymazp +tblackw +tebulun +thcarey +thejasv +thurmank +tiendn +tmunson +tomitono +tonyrev +trosenbl +tyralb +ukaya +uns +vcaston +verhulst +vickik +vishguru +vivikam +vryoung +wakeje +wbaxter +wellmanr +wenchang +wwumlib@gmail.com +xiomara +yardi +yashas +yemee +yingfeng +ymoll +yuayuan +yunahs +yunshuz +zaina +zeiger +zengqy +zkmiclin +zlquint +zqian diff --git a/app/utils/uniqnames.txt b/app/utils/uniqnames.txt new file mode 100644 index 0000000..cb53b39 --- /dev/null +++ b/app/utils/uniqnames.txt @@ -0,0 +1,1031 @@ +BYZANTION@WOWWAY.COM +HORNING@OAKLAND.EDU +IDHATCH +KROSENB +LEWISM@MSU.EDU +MGERSTE87@YAHOO.COM +RLAMEY +SASHTON +aaishah +aakimen +aanieto +aaronhe +aaronrc +aaronseq +abbyochs +abenlouc +abhiti +acaburke +acborl +adeese +adelpin +adibha +adimoji +admagee +adontu +aelliot +aepare +aesmail +aeun +afriedm +aganes +aggrohan +agilber +ahazelti +aibhlinn +ajrutled +akanchi +akeddy +akilkasu +akinzie +alaaabdu +alanoa +albertca +aldada +aleontis +aleroaq +alexaobu +alexaph +alexmw +aliagmz +alimosh +alipo +alknysh +allushen +almantas +almounaa +alynalim +alysea +alyssiaw +amandayx +amanuel +ambnicol +amendl +amengo +amfoerg +aminataz +amjiao +amlemon +amoskow +amyhz +amzpapag +anatoll +anche +andhope +andlars +andreame +andrefi +aneeshap +anemkhol +angelmil +angievit +anjashep +annakoch +annejli +annikalb +anuriti +apaegert +apbapat +aquilas +araymon +arcampb +architgo +arconway +argere +ariannse +aricken +arnoldpd +asaputro +ashchow +asher +ashleeli +ashwak +asledz +asoghoya +asottil +astark +astenge +atanga +atzmon +auerback +autumnnw +autumr +avalade +avpai +awfuller +awhitton +aykalan +azang +azariatu +azyounus +babdalla +balexp +barbara +barillas +barne +basilisc +bawardi +bbbent +bbrose +bdede +bdoc +bdoh +bdwulf +bekillen +belot +benkorn +bennett.justin7@gmail.com +benroot +benskahn +bernhaut +bgrem +birchd +bjcoyle +bjessie +bjustine +bkmil +bkwalter +bmarce +bmdan +bommers +bpuroll +briklein +brismi +brmaya +brookeib +brownash +bservos +bsjones +btalman +buchmana +bundye +caecilia +cahr +camcarp +camerowh +carterob +ccerio +ccoveyou +cdepee +cdpsop +ceberle +cedsteve +celiaes +cfeak +cggoode +chazmack +chenange +chengxw +cheyaden +chiarleo +chilling +chintsuh +chrisack +chrisel +chriswh +chrzhang +cjanney +cjbond +cjrobin +ckayko +clangw +clugger +cmeverts +cmodey +cnemez +cobrenda +cobychri +colemar +colesen +congma +corinnak +cpachika +crhogan +cstano +csyoung +cyanlee +cypatel +daijy +dalc +daniegao +danocone +dapengw +davebyun +daweaver +dbathala +ddepa +dejenlin +demick +dengisa +desiraek +devmk +dfujah +dgernert +dhigbeew +dhoonlee +dhruvkul +diu +diven +divyanrn +djsinger +dkforger +dkwabi +dkyeom +dlehman +dlmorg +dlstone +dmleyton +dmuzyka +dnemser +domatg +doyunk +dpomera +dporter +drolston +dshango +dsp +dstoops +dsuolang +dthacher +duahz +dufallo +dvail +dworkin +dylanpat +dylanw +dzeglen +eandersn +ebiagi +ebmalone +eboneew +edanaher +edenpeja +edingerm +edmister +edwardq +edwinwwh +eemarti +eeyore +egbeer +egeorgie +egomish +ekarlsen +ekflanag +eklaver +eklovya +eknaup +ekosu +ekropf +ekub +elimillr +elinac +eliotsch +ellalars +ellastep +ellieve +elwillou +emfoley +emhsi +emnetk +empalac +emparra +emtierst +enaumann +ensorh +erekque +ericderr +erichead +ermals +erobi +eruiznar +escriven +esears +esvoboda +evabard +evansbro +evar +evkarr +ewhitmer +ewhitt +ewyatt +ezrine +faithwg +falmutaw +fanjj +farhana +fbeldin +fdraper +felixzg +fime +fishman +flynngr +fmir +fokaalo +fostermc +foxjenni +frankrd +franku +fredyin +freeljay +frenchk +funkyyue +gabbimck +gabim +gailshi +gaogavin +garnett +gastonp +gblichar +gbocca +gcharn +gdcrane +gengrunl +georgeh +geostein +gfharnik +gfreitas +gilligem +ginevram +gingerje +giorgiob +gjbaise +gkubi +glenngr +gliddell +gmangan +gmeimann +grahdeth +gratikri +grenberg +grifcool +grocho +gshultz +gthallid +gunckel +gvedock +gypin +hafizkm +hallg +halquist +hancockd +hangexie +hantingc +harburnm +hardyb +harishj +harshitk +hastenh +hastineo +haysjake +hcako +hcclark +hell +hellpop +henrysam +herrong +hgiza +hglay +hhanpark +hhzh +hillerma +himanirb +hjasmin +hjoy +hlandon +hmike +houstona +hoytb +hrmccomb +hsiangrl +huankuo +hxn +hxzou +hysandy +ianmoyer +iantonio +ibeebe +iboncher +ichevres +idugger +illhyukh +iluns +imuchnik +iparrag +iraaa +iracine +isgarcia +isisrez +ispivack +israeli +jabaril +jabutzin +jackfair +jackiela +jackly +jaimielo +jakeang +jamejo +jameshac +jamesjar +jamifi +janetjw +jangeli +jansenjc +jarjenk +jaxong +jaykayse +jaylenmw +jbadger +jbrulla +jburnh +jcantu +jclebo +jcrane +jdior +jdlitman +jdorr +jdstreet +jeanner +jebt +jeffmart +jendara +jeneal +jentramb +jesmidd +jessiba +jessicq +jggeorge +jgotlib +jheyingm +jhnsmith +jhsansom +jialhe +jiaqian +jimburn +jknoblau +jlausch +jlesiewi +jlfreem +jllovett +jlwall +jmancina +jmcloone +jmercie +jnasond +jnkulick +joaburkh +joanm +jodimae +joejoh +joeyu +johacket +johnanon +johnchun +jonahts +jonesand +joque +joshchy +joshcole +joshmoss +joymar +jpboyd +jpieter +jpsot +jqk +jrcole +jready +jreding +jscarson +jstuartb +jtraig +juandel +jubzhou +judzhang +juliettb +junebumg +junghj +junon +jvansant +jzaeske +jzechel +kacalh +kaclaw +kadijahr +kalelago +kamaiyaj +karinc +karsendi +kartiksh +katdav +kathahn +katrynac +kawalkow +kaykaur +kaylatr +kblanks +kblevs +kbowker +kbyrnes +kcb +kchalipa +kdancey +kdimmery +keenanc +kelleyl +kemifal +kenjii +kevinyan +kgates +kholkebo +kimmyung +kirschr +kjenckes +kjgadd +kkarmen +kmhaines +kmkr +knollky +kohaku +kooipond +kreedl +krishpa +krlacy +krokakis +kscarbor +ksheri +kstegema +kstratt +ksunhong +kterryah +ktolu +kumaxwel +kurttb +kylemcc +kyokotac +lacresia +lagarias +lambeth +lanaghan +lapappas +lataviou +lattaj +lauradh +laurietg +lawrenm +lbciddio +leahdr +leeajiah +leinaik +leosolis +lexijenk +lggrey +lgrusz +liamz +lianars +liangxux +lilyeth +lilyspen +lindayan +lindhoek +linqih +linusw +lithium +lizx +lkassem +lkirssin +llapeen +llavey +lmmcc +lmsoo +loebjl +lshowman +ltz +lucasilv +lucianv +lucicook +lunadiaz +lupeters +lvanessa +lvdunn +lwarnock +lwexler +lwhetsto +lzh +mabbaig +mackmock +maggiecw +mahoneyg +majeski +majmajma +malucero +mananj +mandayu +manerkar +mankouch +marand +marcins +marcusla +margomen +marikoo +markayla +markwest +marygil +masonsea +mayanknm +mayorski +mbengts +mcerjak +mcgehee +mclinda +mdelphia +meggro +meilianw +melannsc +mendlow +mercurio +mfmcc +mfornes +mgibs +mgradoz +mgryan +mhartt +mhobbins +mhuntley +michzand +micjk +micwoods +mihikas +mihwang +milhaney +mingyanw +minion +miraab +mirabc +miriamca +mironliu +mkatart +mkgg +mkietzma +mkortenh +mkost +mladair +mlmunro +mlucber +mmanu +mmcanear +moconway +mogan +moltke +monasech +monmonli +mordoch +moreiras +morerica +morgab +mpsolo +mrezvan +mschantz +mschlan +msgumiel +mslevine +mstancro +mtadrous +mueggler +mullenpm +murphyan +mwizinsk +myanga +mylim +myott +mzimlich +nakeller +namadeus +naomirai +narenlob +nargizh +natfeld +nave +ncmouw +ndiom +nehapant +neilzhao +nelevin +nesilver +ngclark +nglitsas +niketn +nikithah +niky +ningbo +niomo +nivedi +njdixon +nklait +nkurt +nmchoi +nornes +nosirova +nshagri +ntbailey +ntrevino +nviviano +nylacic +oacruzro +odbn +ohbang +oliversm +olivih +omairh +onatsu +osteiss +otamendi +paaditya +padairh +pagreen +paigez +panagip +panew +pannier +parkerrn +partner +pasqua +paulcjoh +paulq +pdeotare +pdheeraj +pedriv +pedrol +peggymcc +penarc +peneyra +perezme +petebrad +peterjon +petnoble +petula +pfa +piersma +plina +plwood +pmedrie +pnhalim +poojavin +porterse +powersdj +pradhand +prosserj +pselcer +psphang +ptorresp +ptydepe +puffh +pullerb +punnu +purzel +pvenu +pyocum +qcchen +qengel +qifengho +qinglanm +racadler +raevinj +ravshari +rawlins +rbneal +rcaston +rdillard +reckhoff +redooley +reesearm +rejoanae +rejya +reneeran +reubenr +rgruszec +rhaefner +ribachm +rigby +rishavch +riyamun +rjanko +rjrroyer +rmarisa +rmkamau +rnrich +robinkwi +robkli +robmosc +robyb +rohansom +roisin +romafa +rostampe +rothstee +rove +rparrine +rpl +rsamanth +rschaller@g.ucla.edu +rserie +rshavers +rsjansen +rteare +rthumob +ruiqit +saanvij +sahithia +sajarbou +salamada +samarti +samfarm +samwh +sandmik +sanilm +sarafang +sarahrom +sarahtah +sawoongm +sbgelb +sbmills +sbovitz +sbuss +scast +scfanch +schneem +scottell +sdalcin +sdpage +seayreb +seberry +sebsco +sedarous +seow +seoykang +sethcou +seungok +sfrida +sharbu +shariff +sharifih +shchetka +shchpeng +sheiraco +shelbaum +sherchan +shinian +shobita +shoemak +shreyad +shreyanb +shreyasa +shruz +shshriya +shushuya +sidme +sigrid +sikandar +simkraig +sinnanyj +sjcernak +sjco +sjrush +skhagi +skneelam +skolison +skylerm +slavens +slonn +smithjay +smithys +smkhist +smward +sniesen +solari +soleeb +sophiago +sophiakb +sophienh +sophiezy +soyeonn +soyolee +spelosi +spinsker +spotturi +spunar +srakei +srk +srschaus +ssamaha +sstress +stanz +stewelde +stroshyn +studyroom +sudipdeb +sumitag +sungr +sunken +surajka +sushrita +swanemma +swapnilb +swapnilr +syalsm +sylviacc +tahar +taliama +tamaranf +tamaslyk +tassos +tastone +taymazp +tblackw +tebulun +tesscl +thaus +thcarey +thejasv +thibbs +thurmank +tianycli +tiendn +tjwacnik +tkaslan +tlawhead +tmatel +tmunson +tokowitz +tomitono +tonyrev +trosenbl +tshorkey +tynguyen +tyralb +tyrebre +ukaya +uns +vaditi +vangb +vcardon +vcaston +venkiyer +verhulst +vickik +vishguru +vivikam +vryoung +wakeje +warache +wayin +wbaxter +weizhe +wellmanr +wenchang +wenyuhe +willemst +wisemc +wkeely +wolfha +wood@oakland.edu +wwumlib@gmail.com +xiaping +xiomara +xychang +yaelross +yalex +yardi +yashas +yatirajr +yazuardi +ydantian +yemee +yihuang +yijiab +yingfeng +yipengzh +ymoll +yongwen +younghan +yuayuan +yuchanl +yuchusun +yueshan +yukunz +yunahs +yunshuz +zaina +zeiger +zeinabmu +zengqy +zhaa +zhesu +zjoelle +zjsiegel +zkmiclin +zlquint +zoedong +zqian +zrabine +zrkenedy +zshafie diff --git a/lib/tasks/alma_circ_history.rake b/lib/tasks/alma_circ_history.rake index 0863959..b981976 100644 --- a/lib/tasks/alma_circ_history.rake +++ b/lib/tasks/alma_circ_history.rake @@ -6,41 +6,27 @@ namespace :alma_circ_history do Rails.logger.tagged("Circ Load") do Rails.logger.info("Started") Yabeda.checkout_history_load_last_success.set({}, Time.now.to_i) - client = AlmaRestClient.client - response = client.get_report(path: ENV.fetch("CIRC_REPORT_PATH")) - if response.code != 200 - Rails.logger.error("Alma Report Failed to Load") - next + # client = AlmaRestClient.client + # response = client.get_report(path: ENV.fetch("CIRC_REPORT_PATH")) + # if response.code != 200 + # Rails.logger.error("Alma Report Failed to Load") + # next + # end + # report_items = CheckoutHistoryLoader::Report.for_rows(response.parsed_response) + begin + report_items = CheckoutHistoryLoader::Report.fetch + rescue CheckoutHistoryLoader::FetchReportError => e + Rails.logger.error("Alma Report Failed to Load: #{e}") + exit end - summary[:active_loans] = response.parsed_response.count - response.parsed_response.each do |row| - u = User.find_or_create_by_uniqname(row["User Primary Identifier"]) - unless u.retain_history - Rails.logger.warn("item_loan '#{row["Item Loan Id"]}' not saved: patron opt out") + report_items.each do |report_item| + summary[:active_loans] += 1 + begin + CheckoutHistoryLoader::LoanLoader.load(report_item) + rescue CheckoutHistoryLoader::LoanLoadError next end - loan = Loan.find_or_create_by(id: row["Item Loan Id"]) - next if loan.return_date.present? - next if loan.checkout_date&.to_date&.to_fs(:db) == row["Loan Date"] && row["Return Date"].blank? - # mrio: using `tap` so I can use block syntax - loan.tap do |l| - l.user = u - l.id = row["Item Loan Id"] - l.title = row["Title"]&.slice(0, 255) - l.author = row["Author"]&.slice(0, 255) - l.mms_id = row["MMS Id"] - l.return_date = row["Return Date"] - l.checkout_date = row["Loan Date"] - l.barcode = row["Barcode"] - l.call_number = row["Call Number"] - l.description = row["Description"] - end - if loan.save - Rails.logger.info("item_loan '#{loan.id}' saved") - summary[:loans_loaded] = summary[:loans_loaded] + 1 - else - Rails.logger.warn("item_loan '#{loan.id}' not saved: #{loan.errors.full_messages}") - end + summary[:loans_loaded] += 1 end Rails.logger.info("Finished") Rails.logger.info("Summary: #{summary}") @@ -57,11 +43,11 @@ namespace :alma_circ_history do Rails.logger.info("Started") client = AlmaRestClient.client response = client.get_report(path: ENV.fetch("PATRON_REPORT_PATH")) - if response.code != 200 + if response.status != 200 Rails.logger.error("Alma Report Failed to Load") next end - non_expired_users = response.parsed_response.map { |row| row["User Primary Identifier"].downcase } + non_expired_users = response.body.map { |row| row["User Primary Identifier"].downcase } User.all.each do |user| uniqname = user.uniqname if non_expired_users.include?(uniqname) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 19f5bbb..74f422b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,7 +13,10 @@ # it. # # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +require "webmock" +require "alma_rest_client" RSpec.configure do |config| + include AlmaRestClient::Test::Helpers # rspec-expectations config goes here. You can use an alternate # assertion/expectation library such as wrong or the stdlib/minitest # assertions if you prefer. @@ -92,14 +95,3 @@ # # as the one that triggered the failure. # Kernel.srand config.seed end -def stub_alma_get_request(url:, body: "{}", status: 200, query: {}) - stub_request(:get, "#{ENV["ALMA_API_HOST"]}/almaws/v1/#{url}").with( - headers: { - :accept => "application/json", - :Authorization => "apikey #{ENV["ALMA_API_KEY"]}", - "Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3", - "User-Agent" => "Ruby" - }, - query: query - ).to_return(body: body, status: status, headers: {content_type: "application/json"}) -end diff --git a/spec/tasks/alma_circ_history_task_spec.rb b/spec/tasks/alma_circ_history_task_spec.rb index c605e2e..1462845 100644 --- a/spec/tasks/alma_circ_history_task_spec.rb +++ b/spec/tasks/alma_circ_history_task_spec.rb @@ -5,7 +5,7 @@ @pushgateway_stub = stub_request(:post, "#{ENV["PROMETHEUS_PUSH_GATEWAY"]}/metrics/job/checkout_history") @stub = stub_alma_get_request(url: "analytics/reports", query: {path: ENV.fetch("CIRC_REPORT_PATH"), col_names: true, limit: 1000}, - body: File.read("./spec/fixtures/circ_history.json")) + output: File.read("./spec/fixtures/circ_history.json")) end after(:each) do Rake::Task["alma_circ_history:load"].reenable @@ -22,8 +22,8 @@ it "logs an error if it can't load the report" do @stub.to_return(body: File.read("./spec/fixtures/alma_error.json"), status: 500, headers: {content_type: "application/json"}) @stub.response # clear out original response - expect(Rails.logger).to receive(:error).with("Alma Report Failed to Load") - load_circ_history + expect(Rails.logger).to receive(:error).with(/Alma Report Failed to Load/) + expect { load_circ_history }.to raise_error SystemExit end it "loads new circ history into the db and downcases uniqnames" do user_ajones @@ -144,7 +144,7 @@ before(:each) do @stub = stub_alma_get_request(url: "analytics/reports", query: {path: ENV.fetch("PATRON_REPORT_PATH"), col_names: true, limit: 1000}, - body: File.read("./spec/fixtures/non_expired_patrons.json")) + output: File.read("./spec/fixtures/non_expired_patrons.json")) end after(:each) do Rake::Task["alma_circ_history:purge"].reenable