def run()

in jobs/eam-integrations/scripts/workday_netsuite_integration.py [0:0]


    def run(self, max_limit):

        """Run all the steps of the integration"""
        operations = [
                Operations.update_employee,
                Operations.rehired,
                Operations.add_new_manager,
                Operations.add_new_hire,
                Operations.international_transfer,
                ]
        # ========================================================
        # Step 1: Getting Workday Data
        # ========================================================
        try:
            self.logger.info("Step 1: Getting Workday Data")
            wd_workers, workers_dict, wd_comp = self.workday.get_listing_of_workers()
 
            self.logger.info(f"Number of Worday employees {len(wd_workers)}.")
        except (APIAdaptorException, Exception) as e:
            self.logger.error(str(e))
            self.logger.critical("Failed on Step 1: Getting Workday Data")
            sys.exit(1)

        # ========================================================
        # Step 2: Getting NetSuite Data
        # ========================================================
        try:
            self.logger.info("Step 2: Getting NetSuite Data")
            ns_workers, ns_comp = self.netsuite.get_employees()
            self.logger.info(f"Number of Worday employees {len(ns_workers)}.")
        except (APIAdaptorException, Exception) as e:
            self.logger.error(str(e))
            self.logger.critical("Failed on Step 2: Getting NetSuite Data")
            sys.exit(1)

        # ========================================================
        # Step 3: Compare Workday and Netsuite data
        # ========================================================
        self.logger.info("Step 3: Compare Workday and Netsuite data")
        add_list, upd_list, del_list = self.netsuite.compare_users(wd_comp=wd_comp, ns_comp=ns_comp)
        self.logger.critical(f"Diff list {del_list}.")
        # remove terminated employees from add_list
        terminated = [x.Employee_ID for x in wd_workers if x.Employee_Status=='2']
        add_list = [x for x in add_list if x not in terminated]

        # add terminated records to update
        upd_list = upd_list + terminated
        # ========================================================
        #  Step 4: Add rehires
        # ========================================================
        
        try:
            # diff_hire_dates that are in the add_list
            rehires = [x for x in wd_workers if self.compare_dates(x.Most_Recent_Hire_Date, x.Original_Hire_Date)
                        and x.Employee_ID in add_list and x.Employee_Status=='1']           
            
            if Operations.rehired in operations:
                self.logger.critical("Step 4: Add rehires")
                self.netsuite.update(wd_workers=rehires,
                                     max_limit=max_limit,
                                    workers_dict=workers_dict,                                    
                                    newEmployee=False,
                                    reHire=True,
                                    ns_workers=ns_workers,
                                    wd_comp=wd_comp, 
                                    ns_comp=ns_comp,
                                    operation=Operations.rehired
                                    )      
                self.netsuite.post_error_report( "Adding rehires")
        except (APIAdaptorException, Exception) as e:
            self.logger.error(str(e))
            self.logger.critical("Failed on Step 4: Add rehires")
             
        # ========================================================
        # Step 5: Add new employees
        # ========================================================
        try:
            
            # remove the rehires from the add_list
            add_list = [x for x in add_list if x not in [x.Employee_ID for x in rehires]]
            wd_workers_add = [x for x in wd_workers if x.Employee_ID in add_list and
                                                    x.Employee_Status =='1']
            # Add managers first
            wd_workers_add_managers = [x for x in wd_workers_add if x.Employee_ID 
                                        in [x.Manager_ID for x in wd_workers_add]]    
            if Operations.add_new_manager in operations:
                self.logger.info("Step 4: Add new employees")
                self.netsuite.update(wd_workers=wd_workers_add_managers,
                                                max_limit=max_limit,
                                                workers_dict=workers_dict,                                                
                                                newEmployee=True,
                                                ns_workers=ns_workers,
                                                operation=Operations.add_new_manager                                            
                                                )
                self.netsuite.post_error_report("Adding new managers")  

            # Adding non managers
            wd_workers_add = [x for x in wd_workers_add if x.Employee_ID 
                            not in [x.Manager_ID for x in wd_workers_add]]            
            
            if Operations.add_new_hire in operations:
                self.netsuite.update(wd_workers=wd_workers_add,
                                     max_limit=max_limit,
                                    workers_dict=workers_dict,                                    
                                    newEmployee=True,
                                    ns_workers=ns_workers,
                                    operation=Operations.add_new_hire                               
                                    )
                self.netsuite.post_error_report("Adding new employees")
                
        except (APIAdaptorException, Exception) as e:
            self.logger.error(str(e))
            self.logger.critical("Failed on Step 5: Add new employees")

        # ========================================================
        # Step 6: International Transfers
        # ========================================================
        try:            
            ret = self.workday.get_international_transfers(ns_workers, workers_dict)
            wd_workers_upd = [x for x in wd_workers if x.Employee_ID in [x.Employee_ID for x in ret]]
            
            if Operations.international_transfer in operations:
                self.logger.info("Step 6: International Transfers")
                self.netsuite.update(wd_workers=wd_workers_upd,
                                    newEmployee=False,
                                    max_limit=max_limit,
                                    workers_dict=workers_dict,  
                                    internationalTransfer=True,
                                    ns_workers=ns_workers,
                                    wd_comp=wd_comp, 
                                    ns_comp=ns_comp,
                                    operation=Operations.international_transfer            
                                    )
                self.netsuite.post_error_report("International Transfers")
            
        except (APIAdaptorException, Exception) as e:
            self.logger.error(str(e))
            self.logger.critical("Failed on Step 6: International Transfers")

        # ========================================================
        # Step 7: Update employees
        # ========================================================
        try:
            # compare_and_save_data(wd_workers, upd_list, terminated, wd_comp, ns_comp)
            wd_workers_upd = [x for x in wd_workers if x.Employee_ID in upd_list] 
            # wd_workers_upd = [x for x in wd_workers_upd if x.Employee_ID == '205032']
            if Operations.update_employee in operations:
                self.logger.info("Step 7: Update employees")
                self.netsuite.update(wd_workers=wd_workers_upd,
                                     max_limit=max_limit,
                                    workers_dict=workers_dict,                                    
                                    newEmployee=False,
                                    ns_workers=ns_workers,
                                    wd_comp=wd_comp, 
                                    ns_comp=ns_comp,
                                    operation=Operations.update_employee
                                    )
                self.netsuite.post_error_report("Updating employes")
            
        except (APIAdaptorException, Exception) as e:
            self.logger.error(str(e))
            self.logger.critical("Failed on Step 7: Update employees")
        
        
        self.logger.info("End of Integration.")