-
Notifications
You must be signed in to change notification settings - Fork 1
feat(integrations): add WP ERP action integration #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b7eada6
feat: wp wrp action init
RishadAlam aa3a5b3
feat: wp erp utilities added
RishadAlam 272f24c
refactor: wp erp action frontend
RishadAlam e4a6695
Merge branch 'main' into feat/wp-erp
RishadAlam d857ec1
refactor: remove employee and delete actions from WpErp integration c…
RishadAlam a033cdc
fix: enhance response handling and improve button visibility in WpErp…
RishadAlam 3d6aa2d
fix: enhance button disable logic in WpErp integration component
RishadAlam 5038c47
chore: merge main into feat/wp-erp and resolve conflicts
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * WP ERP Record Api | ||
| */ | ||
|
|
||
| namespace BitApps\Integrations\Actions\WpErp; | ||
|
|
||
| use BitApps\Integrations\Config; | ||
| use BitApps\Integrations\Core\Util\Common; | ||
| use BitApps\Integrations\Core\Util\Hooks; | ||
| use BitApps\Integrations\Log\LogHandler; | ||
|
|
||
| class RecordApiHelper | ||
| { | ||
| private $_integrationID; | ||
|
|
||
| private $_integrationDetails; | ||
|
|
||
| public function __construct($integrationDetails, $integId) | ||
| { | ||
| $this->_integrationDetails = $integrationDetails; | ||
| $this->_integrationID = $integId; | ||
| } | ||
|
|
||
| public function execute($fieldValues, $fieldMap, $utilities) | ||
| { | ||
| if (!\function_exists('erp_insert_people')) { | ||
| return [ | ||
| 'success' => false, | ||
| 'message' => __('WP ERP is not installed or activated', 'bit-integrations') | ||
| ]; | ||
| } | ||
|
|
||
| $fieldData = static::generateReqDataFromFieldMap($fieldMap, $fieldValues); | ||
| $mainAction = $this->_integrationDetails->mainAction ?? ''; | ||
|
|
||
| $defaultResponse = [ | ||
| 'success' => false, | ||
| // translators: %s: Plugin name | ||
| 'message' => wp_sprintf(__('%s plugin is not installed or activated', 'bit-integrations'), 'Bit Integrations Pro') | ||
| ]; | ||
|
|
||
| $hookMap = [ | ||
| 'createContact' => ['hook' => 'wperp_create_contact', 'type' => 'contact'], | ||
| 'updateContact' => ['hook' => 'wperp_update_contact', 'type' => 'contact'], | ||
| 'createCompany' => ['hook' => 'wperp_create_company', 'type' => 'company'], | ||
| 'updateCompany' => ['hook' => 'wperp_update_company', 'type' => 'company'], | ||
| 'createContactGroup' => ['hook' => 'wperp_create_contact_group', 'type' => 'contact_group'], | ||
| 'addContactToGroup' => ['hook' => 'wperp_add_contact_to_group', 'type' => 'contact_group'], | ||
| 'removeContactFromGroup' => ['hook' => 'wperp_remove_contact_from_group', 'type' => 'contact_group'], | ||
| 'addNote' => ['hook' => 'wperp_add_note', 'type' => 'note'], | ||
| 'createTask' => ['hook' => 'wperp_create_task', 'type' => 'task'], | ||
| 'createDepartment' => ['hook' => 'wperp_create_department', 'type' => 'department'], | ||
| 'createDesignation' => ['hook' => 'wperp_create_designation', 'type' => 'designation'], | ||
| 'createHoliday' => ['hook' => 'wperp_create_holiday', 'type' => 'holiday'], | ||
| 'createExpense' => ['hook' => 'wperp_create_expense', 'type' => 'expense'], | ||
| 'createPayment' => ['hook' => 'wperp_create_payment', 'type' => 'payment'], | ||
| ]; | ||
|
|
||
| if (!isset($hookMap[$mainAction])) { | ||
| $response = [ | ||
| 'success' => false, | ||
| 'message' => __('Invalid action', 'bit-integrations') | ||
| ]; | ||
| $type = 'WpErp'; | ||
| $actionType = 'unknown'; | ||
| } else { | ||
| $entry = $hookMap[$mainAction]; | ||
| $response = Hooks::apply( | ||
| Config::withPrefix($entry['hook']), | ||
| $defaultResponse, | ||
| $fieldData, | ||
| $utilities, | ||
| ); | ||
| $type = $entry['type']; | ||
| $actionType = $mainAction; | ||
| } | ||
|
|
||
| $responseType = (!is_wp_error($response) && isset($response['success']) && $response['success']) ? 'success' : 'error'; | ||
| LogHandler::save($this->_integrationID, ['type' => $type, 'type_name' => $actionType], $responseType, $response); | ||
|
|
||
| return $response; | ||
| } | ||
|
|
||
| private static function generateReqDataFromFieldMap($fieldMap, $fieldValues) | ||
| { | ||
| $dataFinal = []; | ||
| foreach ($fieldMap as $item) { | ||
| $triggerValue = $item->formField; | ||
| $actionValue = $item->wpErpField; | ||
|
|
||
| if (empty($actionValue)) { | ||
| continue; | ||
| } | ||
|
|
||
| $dataFinal[$actionValue] = $triggerValue === 'custom' && isset($item->customValue) | ||
| ? Common::replaceFieldWithValue($item->customValue, $fieldValues) | ||
| : ($fieldValues[$triggerValue] ?? ''); | ||
| } | ||
|
|
||
| return $dataFinal; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?php | ||
|
|
||
| if (!defined('ABSPATH')) { | ||
| exit; | ||
| } | ||
|
|
||
| use BitApps\Integrations\Actions\WpErp\WpErpController; | ||
| use BitApps\Integrations\Core\Util\Route; | ||
|
|
||
| Route::post('wp_erp_authorize', [WpErpController::class, 'wpErpAuthorize']); | ||
| Route::post('refresh_wp_erp_contact_groups', [WpErpController::class, 'refreshContactGroups']); | ||
| Route::post('refresh_wp_erp_life_stages', [WpErpController::class, 'refreshLifeStages']); | ||
| Route::post('refresh_wp_erp_departments', [WpErpController::class, 'refreshDepartments']); | ||
| Route::post('refresh_wp_erp_designations', [WpErpController::class, 'refreshDesignations']); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * WP ERP Integration | ||
| */ | ||
|
|
||
| namespace BitApps\Integrations\Actions\WpErp; | ||
|
|
||
| use WP_Error; | ||
|
|
||
| class WpErpController | ||
| { | ||
| public static function isExists() | ||
| { | ||
| if (!\function_exists('erp_insert_people')) { | ||
| wp_send_json_error(__('WP ERP is not activated or not installed', 'bit-integrations'), 400); | ||
| } | ||
| } | ||
|
|
||
| public static function wpErpAuthorize() | ||
| { | ||
| self::isExists(); | ||
| wp_send_json_success(true); | ||
| } | ||
|
|
||
| public function refreshContactGroups() | ||
| { | ||
| self::isExists(); | ||
|
|
||
| $groups = []; | ||
|
|
||
| if (\function_exists('erp_crm_get_contact_groups')) { | ||
| $groups = array_map( | ||
| function ($group) { | ||
| $group = (array) $group; | ||
|
|
||
| return (object) [ | ||
| 'value' => $group['id'] ?? '', | ||
| 'label' => $group['name'] ?? '', | ||
| ]; | ||
| }, | ||
| (array) erp_crm_get_contact_groups(['number' => -1]) | ||
| ); | ||
| } | ||
|
|
||
| wp_send_json_success(['groups' => $groups], 200); | ||
| } | ||
|
|
||
| public function refreshLifeStages() | ||
| { | ||
| self::isExists(); | ||
|
|
||
| $stages = []; | ||
|
|
||
| if (\function_exists('erp_crm_get_life_stages_dropdown_raw')) { | ||
| foreach ((array) erp_crm_get_life_stages_dropdown_raw() as $value => $label) { | ||
| $stages[] = (object) ['value' => $value, 'label' => $label]; | ||
| } | ||
| } | ||
|
|
||
| wp_send_json_success(['stages' => $stages], 200); | ||
| } | ||
|
|
||
| public function refreshDepartments() | ||
| { | ||
| self::isExists(); | ||
|
|
||
| $out = []; | ||
|
|
||
| if (\function_exists('erp_hr_get_departments')) { | ||
| foreach ((array) erp_hr_get_departments(['number' => -1, 'no_object' => true]) as $dept) { | ||
| $dept = (array) $dept; | ||
| $out[] = (object) [ | ||
| 'value' => $dept['id'] ?? '', | ||
| 'label' => $dept['title'] ?? '', | ||
| ]; | ||
| } | ||
| } | ||
|
|
||
| wp_send_json_success(['departments' => $out], 200); | ||
| } | ||
|
|
||
| public function refreshDesignations() | ||
| { | ||
| self::isExists(); | ||
|
|
||
| $out = []; | ||
|
|
||
| if (\function_exists('erp_hr_get_designations')) { | ||
| foreach ((array) erp_hr_get_designations(['number' => -1, 'no_object' => true]) as $designation) { | ||
| $designation = (array) $designation; | ||
| $out[] = (object) [ | ||
| 'value' => $designation['id'] ?? '', | ||
| 'label' => $designation['title'] ?? '', | ||
| ]; | ||
| } | ||
| } | ||
|
|
||
| wp_send_json_success(['designations' => $out], 200); | ||
| } | ||
|
|
||
| public function execute($integrationData, $fieldValues) | ||
| { | ||
| $integrationDetails = $integrationData->flow_details; | ||
| $integId = $integrationData->id; | ||
| $fieldMap = $integrationDetails->field_map; | ||
| $utilities = $integrationDetails->utilities ?? []; | ||
|
|
||
| if (empty($fieldMap)) { | ||
| return new WP_Error('field_map_empty', __('Field map is empty', 'bit-integrations')); | ||
| } | ||
|
|
||
| $recordApiHelper = new RecordApiHelper($integrationDetails, $integId); | ||
| $wpErpResponse = $recordApiHelper->execute($fieldValues, $fieldMap, $utilities); | ||
|
|
||
| if (is_wp_error($wpErpResponse)) { | ||
| return $wpErpResponse; | ||
| } | ||
|
|
||
| return $wpErpResponse; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
frontend/src/components/AllIntegrations/WpErp/EditWpErp.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { useState } from 'react' | ||
| import { useNavigate, useParams } from 'react-router' | ||
| import { useRecoilState, useRecoilValue } from 'recoil' | ||
| import { $actionConf, $formFields, $newFlow } from '../../../GlobalStates' | ||
| import { __ } from '../../../Utils/i18nwrap' | ||
| import SnackMsg from '../../Utilities/SnackMsg' | ||
| import { saveActionConf } from '../IntegrationHelpers/IntegrationHelpers' | ||
| import IntegrationStepThree from '../IntegrationHelpers/IntegrationStepThree' | ||
| import SetEditIntegComponents from '../IntegrationHelpers/SetEditIntegComponents' | ||
| import { checkMappedFields, handleInput } from './WpErpCommonFunc' | ||
| import WpErpIntegLayout from './WpErpIntegLayout' | ||
|
|
||
| export default function EditWpErp({ allIntegURL }) { | ||
| const navigate = useNavigate() | ||
| const { id, formID } = useParams() | ||
|
|
||
| const [wpErpConf, setWpErpConf] = useRecoilState($actionConf) | ||
| const [flow, setFlow] = useRecoilState($newFlow) | ||
| const formFields = useRecoilValue($formFields) | ||
| const [isLoading, setIsLoading] = useState(false) | ||
| const [snack, setSnackbar] = useState({ show: false }) | ||
|
|
||
| return ( | ||
| <div style={{ width: 900 }}> | ||
| <SnackMsg snack={snack} setSnackbar={setSnackbar} /> | ||
|
|
||
| <div className="flx mt-3"> | ||
| <b className="wdt-200 d-in-b">{__('Integration Name:', 'bit-integrations')}</b> | ||
| <input | ||
| className="btcd-paper-inp w-5" | ||
| onChange={e => handleInput(e, wpErpConf, setWpErpConf)} | ||
| name="name" | ||
| value={wpErpConf.name} | ||
| type="text" | ||
| placeholder={__('Integration Name...', 'bit-integrations')} | ||
| /> | ||
| </div> | ||
| <br /> | ||
|
|
||
| <SetEditIntegComponents entity={flow.triggered_entity} setSnackbar={setSnackbar} /> | ||
|
|
||
| <WpErpIntegLayout | ||
| formID={formID} | ||
| formFields={formFields} | ||
| wpErpConf={wpErpConf} | ||
| setWpErpConf={setWpErpConf} | ||
| setSnackbar={setSnackbar} | ||
| setIsLoading={setIsLoading} | ||
| isLoading={isLoading} | ||
| /> | ||
|
|
||
| <IntegrationStepThree | ||
| edit | ||
| saveConfig={() => | ||
| saveActionConf({ | ||
| flow, | ||
| setFlow, | ||
| allIntegURL, | ||
| conf: wpErpConf, | ||
| navigate, | ||
| id, | ||
| edit: 1, | ||
| setIsLoading, | ||
| setSnackbar | ||
| }) | ||
| } | ||
| disabled={!checkMappedFields(wpErpConf)} | ||
| isLoading={isLoading} | ||
| dataConf={wpErpConf} | ||
| setDataConf={setWpErpConf} | ||
| formFields={formFields} | ||
| /> | ||
| <br /> | ||
| </div> | ||
| ) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.