Add core-office patch-customer-gaps command

hopefully this is not often needed, but definitely might be sometimes..
This commit is contained in:
Lance Edgar 2023-06-12 16:47:00 -05:00
parent cc517702e1
commit a6541a2c32
2 changed files with 75 additions and 0 deletions

View file

@ -24,6 +24,7 @@
CORE Office commands
"""
import logging
import sys
import requests
@ -33,9 +34,14 @@ from rattail import commands
from rattail_corepos import __version__
from rattail.util import load_object
from rattail_corepos.corepos.office.util import get_fannie_config_value
from rattail_corepos.corepos.api import make_corepos_api
from rattail_corepos.corepos.util import get_core_members
from rattail_corepos.config import core_office_url
log = logging.getLogger(__name__)
def main(*args):
"""
Entry point for 'core-office' commands
@ -144,6 +150,74 @@ class ImportSelf(commands.ImportSubcommand):
handler_key = 'to_self.from_corepos_db_office_op.import'
class PatchCustomerGaps(commands.Subcommand):
"""
POST to the CORE API as needed, to patch gaps for customerID
"""
name = 'patch-customer-gaps'
description = __doc__.strip()
def add_parser_args(self, parser):
parser.add_argument('--dry-run', action='store_true',
help="Do not POST anything, but log members needing it.")
def run(self, args):
from corepos.db.office_op import model as corepos
corepos_api = make_corepos_api(self.config)
members = get_core_members(self.config, corepos_api, progress=self.progress)
tally = self.app.make_object(updated=0)
self.maxlen_phone = self.app.maxlen(corepos.Customer.phone)
# nb. just in case the smallest one changes in future..
other = self.app.maxlen(corepos.MemberInfo.phone)
if other < self.maxlen_phone:
self.maxlen_phone = other
def inspect(member, i):
for customer in member['customers']:
customer_id = int(customer['customerID'])
if not customer_id:
data = dict(member)
self.trim_phones(data)
cardno = data.pop('cardNo')
log.debug("%s call set_member() for card no %s: %s",
'should' if args.dry_run else 'will',
cardno, data)
if not args.dry_run:
corepos_api.set_member(cardno, **data)
tally.updated += 1
return
action = "Finding"
if not args.dry_run:
action += " and fixing"
self.progress_loop(inspect, members,
message=f"{action} customerID gaps")
self.stdout.write("\n")
if args.dry_run:
self.stdout.write("would have ")
self.stdout.write(f"updated {tally.updated} members\n")
def trim_phones(self, data):
# the `meminfo` table allows 30 chars for phone, but
# `Customers` table only allows 20 chars. so we must trim to
# 20 chars or else the CORE API will silently fail to update
# tables correctly when we POST to it
for customer in data['customers']:
for field in ['phone', 'altPhone']:
value = customer[field]
if len(value) > self.maxlen_phone:
log.warning("phone value for cardno %s is too long (%s chars) "
"and will be trimmed to %s chars: %s",
data['cardNo'],
len(value),
self.maxlen_phone,
value)
customer[field] = value[:self.maxlen_phone]
class PingInstall(commands.Subcommand):
"""
Ping the /install URL in CORE Office (for DB setup)

View file

@ -46,6 +46,7 @@ core_office.commands =
import-csv = rattail_corepos.corepos.office.commands:ImportCSV
import-self = rattail_corepos.corepos.office.commands:ImportSelf
get-config-value = rattail_corepos.corepos.office.commands:GetConfigValue
patch-customer-gaps = rattail_corepos.corepos.office.commands:PatchCustomerGaps
ping-install = rattail_corepos.corepos.office.commands:PingInstall
crepes.commands =