Add 2-way sync for customer contact data, for CORE API <-> Rattail
This commit is contained in:
parent
15a99164f2
commit
9dbdb81f07
|
@ -94,11 +94,11 @@ class MemberImporter(ToCoreAPI):
|
|||
# 'idCardUPC',
|
||||
# 'startDate',
|
||||
# 'endDate',
|
||||
# 'addressFirstLine',
|
||||
# 'addressSecondLine',
|
||||
# 'city',
|
||||
# 'state',
|
||||
# 'zip',
|
||||
'addressFirstLine',
|
||||
'addressSecondLine',
|
||||
'city',
|
||||
'state',
|
||||
'zip',
|
||||
# 'contactAllowed',
|
||||
# 'contactMethod',
|
||||
# 'modified',
|
||||
|
@ -114,9 +114,9 @@ class MemberImporter(ToCoreAPI):
|
|||
# 'discount',
|
||||
'accountHolder',
|
||||
# 'staff',
|
||||
# 'phone',
|
||||
# 'altPhone',
|
||||
# 'email',
|
||||
'phone',
|
||||
'altPhone',
|
||||
'email',
|
||||
# 'memberPricingAllowed',
|
||||
# 'memberCouponsAllowed',
|
||||
# 'lowIncomeBenefits',
|
||||
|
|
|
@ -26,6 +26,8 @@ Rattail -> CORE-POS data export
|
|||
|
||||
import logging
|
||||
|
||||
from sqlalchemy import orm
|
||||
|
||||
from rattail import importing
|
||||
from rattail.db import model
|
||||
from rattail.db.util import short_session
|
||||
|
@ -71,28 +73,61 @@ class MemberImporter(FromRattail, corepos_importing.model.MemberImporter):
|
|||
'cardNo',
|
||||
'customerAccountID',
|
||||
'customers',
|
||||
'addressFirstLine',
|
||||
'addressSecondLine',
|
||||
'city',
|
||||
'state',
|
||||
'zip',
|
||||
]
|
||||
supported_customer_fields = [
|
||||
'customerID',
|
||||
'firstName',
|
||||
'lastName',
|
||||
'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):
|
||||
|
||||
address = customer.addresses[0] if customer.addresses else None
|
||||
|
||||
people = []
|
||||
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({
|
||||
'customerID': str(person.corepos_customer_id),
|
||||
'firstName': person.first_name,
|
||||
'lastName': person.last_name,
|
||||
'accountHolder': i == 1,
|
||||
'phone': phone1.number if phone1 else '',
|
||||
'altPhone': phone2.number if phone2 else '',
|
||||
'email': email.address if email else '',
|
||||
})
|
||||
|
||||
return {
|
||||
'cardNo': customer.number,
|
||||
'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,
|
||||
}
|
||||
|
||||
|
|
|
@ -157,6 +157,9 @@ class FromRattailToCore(NewDataSyncImportConsumer):
|
|||
'Customer',
|
||||
'Person',
|
||||
'CustomerPerson',
|
||||
'CustomerMailingAddress',
|
||||
'PersonPhoneNumber',
|
||||
'PersonEmailAddress',
|
||||
]
|
||||
for change in [c for c in changes if c.payload_type in types]:
|
||||
if change.payload_type == 'Customer' and change.deletion:
|
||||
|
@ -234,6 +237,21 @@ class FromRattailToCore(NewDataSyncImportConsumer):
|
|||
if person:
|
||||
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 []
|
||||
|
||||
def get_vendor(self, session, change):
|
||||
|
|
|
@ -58,7 +58,7 @@ class FromCOREAPIToRattail(NewDataSyncImportConsumer):
|
|||
|
||||
# sync all Customer-related changes
|
||||
types = [
|
||||
'Customer',
|
||||
'Member',
|
||||
]
|
||||
for change in [c for c in changes if c.payload_type in types]:
|
||||
if change.deletion:
|
||||
|
@ -85,7 +85,7 @@ class FromCOREAPIToRattail(NewDataSyncImportConsumer):
|
|||
self.invoke_importer(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)
|
||||
if change.payload_type == 'Department':
|
||||
return self.api.get_department(change.payload_key)
|
||||
|
|
|
@ -88,6 +88,11 @@ class CustomerImporter(FromCOREPOSAPI, importing.model.CustomerImporter):
|
|||
'id',
|
||||
'number',
|
||||
'name',
|
||||
'address_street',
|
||||
'address_street2',
|
||||
'address_city',
|
||||
'address_state',
|
||||
'address_zipcode',
|
||||
]
|
||||
|
||||
def get_host_objects(self):
|
||||
|
@ -111,6 +116,12 @@ class CustomerImporter(FromCOREPOSAPI, importing.model.CustomerImporter):
|
|||
'number': member['cardNo'],
|
||||
'name': normalize_full_name(customer['firstName'],
|
||||
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',
|
||||
'customer_uuid',
|
||||
'customer_person_ordinal',
|
||||
'phone_number',
|
||||
'phone_number_2',
|
||||
'email_address',
|
||||
]
|
||||
|
||||
def setup(self):
|
||||
|
@ -194,6 +208,9 @@ class PersonImporter(FromCOREPOSAPI, corepos_importing.model.PersonImporter):
|
|||
person['lastName']),
|
||||
'customer_uuid': customer.uuid,
|
||||
'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,
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue