From 9dbdb81f07de6b1fb007071cb17a52b5a363e7f9 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Tue, 17 Mar 2020 18:52:08 -0500 Subject: [PATCH] Add 2-way sync for customer contact data, for CORE API <-> Rattail --- rattail_corepos/corepos/importing/model.py | 16 ++++----- rattail_corepos/corepos/importing/rattail.py | 35 ++++++++++++++++++++ rattail_corepos/datasync/corepos.py | 18 ++++++++++ rattail_corepos/datasync/rattail.py | 4 +-- rattail_corepos/importing/corepos/api.py | 17 ++++++++++ 5 files changed, 80 insertions(+), 10 deletions(-) diff --git a/rattail_corepos/corepos/importing/model.py b/rattail_corepos/corepos/importing/model.py index 96bcea4..f3b74eb 100644 --- a/rattail_corepos/corepos/importing/model.py +++ b/rattail_corepos/corepos/importing/model.py @@ -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', diff --git a/rattail_corepos/corepos/importing/rattail.py b/rattail_corepos/corepos/importing/rattail.py index 96c9ffd..c4c19e2 100644 --- a/rattail_corepos/corepos/importing/rattail.py +++ b/rattail_corepos/corepos/importing/rattail.py @@ -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, } diff --git a/rattail_corepos/datasync/corepos.py b/rattail_corepos/datasync/corepos.py index 8419707..1acce3d 100644 --- a/rattail_corepos/datasync/corepos.py +++ b/rattail_corepos/datasync/corepos.py @@ -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): diff --git a/rattail_corepos/datasync/rattail.py b/rattail_corepos/datasync/rattail.py index 6d26529..c80d750 100644 --- a/rattail_corepos/datasync/rattail.py +++ b/rattail_corepos/datasync/rattail.py @@ -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) diff --git a/rattail_corepos/importing/corepos/api.py b/rattail_corepos/importing/corepos/api.py index d0a2fc1..f24826b 100644 --- a/rattail_corepos/importing/corepos/api.py +++ b/rattail_corepos/importing/corepos/api.py @@ -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, }