Add 2-way sync for customer contact data, for CORE API <-> Rattail

This commit is contained in:
Lance Edgar 2020-03-17 18:52:08 -05:00
parent 15a99164f2
commit 9dbdb81f07
5 changed files with 80 additions and 10 deletions

View file

@ -94,11 +94,11 @@ class MemberImporter(ToCoreAPI):
# 'idCardUPC', # 'idCardUPC',
# 'startDate', # 'startDate',
# 'endDate', # 'endDate',
# 'addressFirstLine', 'addressFirstLine',
# 'addressSecondLine', 'addressSecondLine',
# 'city', 'city',
# 'state', 'state',
# 'zip', 'zip',
# 'contactAllowed', # 'contactAllowed',
# 'contactMethod', # 'contactMethod',
# 'modified', # 'modified',
@ -114,9 +114,9 @@ class MemberImporter(ToCoreAPI):
# 'discount', # 'discount',
'accountHolder', 'accountHolder',
# 'staff', # 'staff',
# 'phone', 'phone',
# 'altPhone', 'altPhone',
# 'email', 'email',
# 'memberPricingAllowed', # 'memberPricingAllowed',
# 'memberCouponsAllowed', # 'memberCouponsAllowed',
# 'lowIncomeBenefits', # 'lowIncomeBenefits',

View file

@ -26,6 +26,8 @@ Rattail -> CORE-POS data export
import logging import logging
from sqlalchemy import orm
from rattail import importing from rattail import importing
from rattail.db import model from rattail.db import model
from rattail.db.util import short_session from rattail.db.util import short_session
@ -71,28 +73,61 @@ class MemberImporter(FromRattail, corepos_importing.model.MemberImporter):
'cardNo', 'cardNo',
'customerAccountID', 'customerAccountID',
'customers', 'customers',
'addressFirstLine',
'addressSecondLine',
'city',
'state',
'zip',
] ]
supported_customer_fields = [ supported_customer_fields = [
'customerID', 'customerID',
'firstName', 'firstName',
'lastName', 'lastName',
'accountHolder', 'accountHolder',
'phone',
'altPhone',
'email',
] ]
def query(self):
query = super(MemberImporter, self).query()
query = query.options(orm.joinedload(model.Customer.addresses))\
.options(orm.joinedload(model.Customer._people)\
.joinedload(model.CustomerPerson.person)\
.joinedload(model.Person.phones))\
.options(orm.joinedload(model.Customer._people)\
.joinedload(model.CustomerPerson.person)\
.joinedload(model.Person.emails))
return query
def normalize_host_object(self, customer): def normalize_host_object(self, customer):
address = customer.addresses[0] if customer.addresses else None
people = [] people = []
for i, person in enumerate(customer.people, 1): for i, person in enumerate(customer.people, 1):
phones = person.phones
phone1 = phones[0] if phones else None
phone2 = phones[1] if len(phones) > 1 else None
email = person.emails[0] if person.emails else None
people.append({ people.append({
'customerID': str(person.corepos_customer_id), 'customerID': str(person.corepos_customer_id),
'firstName': person.first_name, 'firstName': person.first_name,
'lastName': person.last_name, 'lastName': person.last_name,
'accountHolder': i == 1, 'accountHolder': i == 1,
'phone': phone1.number if phone1 else '',
'altPhone': phone2.number if phone2 else '',
'email': email.address if email else '',
}) })
return { return {
'cardNo': customer.number, 'cardNo': customer.number,
'customerAccountID': customer.id, 'customerAccountID': customer.id,
'addressFirstLine': address.street if address else '',
'addressSecondLine': address.street2 if address else '',
'city': address.city if address else '',
'state': address.state if address else '',
'zip': address.zipcode if address else '',
'customers': people, 'customers': people,
} }

View file

@ -157,6 +157,9 @@ class FromRattailToCore(NewDataSyncImportConsumer):
'Customer', 'Customer',
'Person', 'Person',
'CustomerPerson', 'CustomerPerson',
'CustomerMailingAddress',
'PersonPhoneNumber',
'PersonEmailAddress',
] ]
for change in [c for c in changes if c.payload_type in types]: for change in [c for c in changes if c.payload_type in types]:
if change.payload_type == 'Customer' and change.deletion: if change.payload_type == 'Customer' and change.deletion:
@ -234,6 +237,21 @@ class FromRattailToCore(NewDataSyncImportConsumer):
if person: if person:
return person.customers return person.customers
if change.payload_type == 'CustomerMailingAddress':
address = session.query(model.CustomerMailingAddress).get(change.payload_key)
if address:
return [address.customer]
if change.payload_type == 'PersonPhoneNumber':
phone = session.query(model.PersonPhoneNumber).get(change.payload_key)
if phone:
return phone.person.customers
if change.payload_type == 'PersonEmailAddress':
email = session.query(model.PersonEmailAddress).get(change.payload_key)
if email:
return email.person.customers
return [] return []
def get_vendor(self, session, change): def get_vendor(self, session, change):

View file

@ -58,7 +58,7 @@ class FromCOREAPIToRattail(NewDataSyncImportConsumer):
# sync all Customer-related changes # sync all Customer-related changes
types = [ types = [
'Customer', 'Member',
] ]
for change in [c for c in changes if c.payload_type in types]: for change in [c for c in changes if c.payload_type in types]:
if change.deletion: if change.deletion:
@ -85,7 +85,7 @@ class FromCOREAPIToRattail(NewDataSyncImportConsumer):
self.invoke_importer(session, change) self.invoke_importer(session, change)
def get_host_object(self, session, change): def get_host_object(self, session, change):
if change.payload_type == 'Customer': if change.payload_type == 'Member':
return self.api.get_member(change.payload_key) return self.api.get_member(change.payload_key)
if change.payload_type == 'Department': if change.payload_type == 'Department':
return self.api.get_department(change.payload_key) return self.api.get_department(change.payload_key)

View file

@ -88,6 +88,11 @@ class CustomerImporter(FromCOREPOSAPI, importing.model.CustomerImporter):
'id', 'id',
'number', 'number',
'name', 'name',
'address_street',
'address_street2',
'address_city',
'address_state',
'address_zipcode',
] ]
def get_host_objects(self): def get_host_objects(self):
@ -111,6 +116,12 @@ class CustomerImporter(FromCOREPOSAPI, importing.model.CustomerImporter):
'number': member['cardNo'], 'number': member['cardNo'],
'name': normalize_full_name(customer['firstName'], 'name': normalize_full_name(customer['firstName'],
customer['lastName']), customer['lastName']),
'address_street': member['addressFirstLine'] or None,
'address_street2': member['addressSecondLine'] or None,
'address_city': member['city'] or None,
'address_state': member['state'] or None,
'address_zipcode': member['zip'] or None,
} }
@ -126,6 +137,9 @@ class PersonImporter(FromCOREPOSAPI, corepos_importing.model.PersonImporter):
'display_name', 'display_name',
'customer_uuid', 'customer_uuid',
'customer_person_ordinal', 'customer_person_ordinal',
'phone_number',
'phone_number_2',
'email_address',
] ]
def setup(self): def setup(self):
@ -194,6 +208,9 @@ class PersonImporter(FromCOREPOSAPI, corepos_importing.model.PersonImporter):
person['lastName']), person['lastName']),
'customer_uuid': customer.uuid, 'customer_uuid': customer.uuid,
'customer_person_ordinal': person['customer_person_ordinal'], 'customer_person_ordinal': person['customer_person_ordinal'],
'phone_number': person['phone'] or None,
'phone_number_2': person['altPhone'] or None,
'email_address': person['email'] or None,
} }