fix: add Employee support for CORE API -> Rattail import/datasync

This commit is contained in:
Lance Edgar 2024-07-04 18:29:05 -05:00
parent 4752409a45
commit 1b04b4097c
3 changed files with 60 additions and 0 deletions

View file

@ -38,6 +38,7 @@ class CoreTriggerHandler(GenericHandler):
supported_triggers = [
'custdata',
'meminfo',
'employees',
'departments',
'subdepts',
'vendors',
@ -224,6 +225,26 @@ class CoreTriggerHandler(GenericHandler):
FOR EACH ROW INSERT INTO {changes_table} (object_type, object_key, deleted) VALUES ('Member', CONVERT(OLD.card_no, CHAR));
"""))
def create_triggers_employees(self, op_session, changes_table):
op_session.execute(sa.text(f"""
CREATE TRIGGER record_employees_create
AFTER INSERT ON employees
FOR EACH ROW INSERT INTO {changes_table} (object_type, object_key, deleted) VALUES ('Employee', CONVERT(NEW.emp_no, CHAR), 0);
"""))
op_session.execute(sa.text(f"""
CREATE TRIGGER record_employees_update
AFTER UPDATE ON employees
FOR EACH ROW INSERT INTO {changes_table} (object_type, object_key, deleted) VALUES ('Employee', CONVERT(NEW.emp_no, CHAR), 0);
"""))
op_session.execute(sa.text(f"""
CREATE TRIGGER record_employees_delete
AFTER DELETE ON employees
FOR EACH ROW INSERT INTO {changes_table} (object_type, object_key, deleted) VALUES ('Employee', CONVERT(OLD.emp_no, CHAR));
"""))
def create_triggers_departments(self, op_session, changes_table):
op_session.execute(sa.text(f"""

View file

@ -78,6 +78,14 @@ class FromCOREAPIToRattail(DataSyncImportConsumer):
self.process_change(session, self.importers['Member'],
host_object=member)
# sync all Employee-related changes
types = [
'Employee',
]
for change in [c for c in changes if c.payload_type in types]:
# normal logic works fine here
self.invoke_importer(session, change)
# sync all "product meta" changes
types = [
'Department',
@ -118,6 +126,8 @@ class FromCOREAPIToRattail(DataSyncImportConsumer):
def get_host_object(self, session, change):
if change.payload_type == 'Member':
return self.api.get_member(change.payload_key)
if change.payload_type == 'Employee':
return self.api.get_employee(change.payload_key)
if change.payload_type == 'Department':
return self.api.get_department(change.payload_key)
if change.payload_type == 'Subdepartment':

View file

@ -56,6 +56,7 @@ class FromCOREPOSToRattail(importing.ToRattailHandler):
importers['CustomerShopper'] = CustomerShopperImporter
importers['MembershipType'] = MembershipTypeImporter
importers['Member'] = MemberImporter
importers['Employee'] = EmployeeImporter
importers['Store'] = StoreImporter
importers['Department'] = DepartmentImporter
importers['Subdepartment'] = SubdepartmentImporter
@ -341,6 +342,34 @@ class CustomerShopperImporter(FromCOREPOSAPI, corepos_importing.model.CustomerSh
return data
class EmployeeImporter(FromCOREPOSAPI, corepos_importing.model.EmployeeImporter):
"""
Importer for employee data from CORE POS API.
"""
key = 'corepos_number'
supported_fields = [
'corepos_number',
'id',
'first_name',
'last_name',
'full_name',
'status',
]
def get_host_objects(self):
return self.api.get_employees()
def normalize_host_object(self, employee):
return {
'corepos_number': int(employee['emp_no']),
'id': int(employee['emp_no']),
'first_name': employee['FirstName'],
'last_name': employee['LastName'],
'full_name': normalize_full_name(employee['FirstName'], employee['LastName']),
'status': self.enum.EMPLOYEE_STATUS_CURRENT if employee['EmpActive'] else self.enum.EMPLOYEE_STATUS_FORMER,
}
class StoreImporter(FromCOREPOSAPI, corepos_importing.model.StoreImporter):
"""
Importer for store data from CORE POS API.