-
Notifications
You must be signed in to change notification settings - Fork 1
Implement Bitrix24Partners support (issue #70) #71
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
Draft
mesilov
wants to merge
4
commits into
dev
Choose a base branch
from
claude/bitrix24-issue-70-01PSzKghSeJKibfs1z3wb8sX
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
66ac18e
Implement Bitrix24Partners support (issue #70)
claude 4bc8ae0
Replace direct curl calls with Symfony HttpClient
claude 53a6924
Refactor Bitrix24Partner entity and improve robustness
claude 3b346ef
Improve CSV handling, deprecate setBitrix24PartnerId, and add tests
claude 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
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
config/xml/Bitrix24.Lib.Bitrix24Partners.Entity.Bitrix24Partner.dcm.xml
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,35 @@ | ||
| <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" | ||
| xmlns:xs="https://www.w3.org/2001/XMLSchema" | ||
| xmlns:orm="https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> | ||
| <entity name="Bitrix24\Lib\Bitrix24Partners\Entity\Bitrix24Partner" table="bitrix24partner"> | ||
| <id name="id" type="uuid" column="id"> | ||
|
|
||
| </id> | ||
|
|
||
| <field name="title" type="string" column="title" nullable="false"/> | ||
|
|
||
| <field name="site" type="string" column="site" nullable="true"/> | ||
|
|
||
| <field name="phone" type="phone_number" column="phone" nullable="true"/> | ||
|
|
||
| <field name="email" type="string" column="email" nullable="true"/> | ||
|
|
||
| <field name="bitrix24PartnerId" type="integer" column="b24_partner_id" nullable="true"> | ||
mesilov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <options> | ||
| <option name="unique">true</option> | ||
| </options> | ||
mesilov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </field> | ||
|
|
||
| <field name="openLineId" type="string" column="open_line_id" nullable="true"/> | ||
|
|
||
| <field name="externalId" type="string" column="external_id" nullable="true"/> | ||
|
|
||
| <field name="status" enum-type="string" column="status" nullable="false"/> | ||
|
|
||
| <field name="comment" type="text" column="comment" nullable="true"/> | ||
|
|
||
| <field name="createdAt" type="carbon_immutable" column="created_at_utc" precision="3" nullable="false"/> | ||
|
|
||
| <field name="updatedAt" type="carbon_immutable" column="updated_at_utc" precision="3" nullable="false"/> | ||
| </entity> | ||
| </doctrine-mapping> | ||
189 changes: 189 additions & 0 deletions
189
src/Bitrix24Partners/Console/ImportPartnersCsvCommand.php
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,189 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Bitrix24\Lib\Bitrix24Partners\Console; | ||
|
|
||
| use Bitrix24\Lib\Bitrix24Partners\UseCase\Create\Command as CreateCommand; | ||
| use Bitrix24\Lib\Bitrix24Partners\UseCase\Create\Handler as CreateHandler; | ||
| use libphonenumber\NumberParseException; | ||
| use libphonenumber\PhoneNumberUtil; | ||
| use Symfony\Component\Console\Attribute\AsCommand; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Input\InputArgument; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
| use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
|
||
| #[AsCommand( | ||
| name: 'bitrix24:partners:import', | ||
| description: 'Import partners from CSV file into database' | ||
| )] | ||
| class ImportPartnersCsvCommand extends Command | ||
| { | ||
| public function __construct( | ||
| private readonly CreateHandler $createHandler | ||
| ) { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| #[\Override] | ||
| protected function configure(): void | ||
| { | ||
| $this | ||
| ->addArgument( | ||
| 'file', | ||
| InputArgument::REQUIRED, | ||
| 'Path to CSV file to import' | ||
| ) | ||
| ->addOption( | ||
| 'skip-errors', | ||
| 's', | ||
| InputOption::VALUE_NONE, | ||
| 'Skip rows with errors and continue processing' | ||
| ); | ||
| } | ||
|
|
||
| #[\Override] | ||
| protected function execute(InputInterface $input, OutputInterface $output): int | ||
| { | ||
| $io = new SymfonyStyle($input, $output); | ||
|
|
||
| $file = $input->getArgument('file'); | ||
| $skipErrors = $input->getOption('skip-errors'); | ||
|
|
||
| if (!file_exists($file)) { | ||
| $io->error(sprintf('File not found: %s', $file)); | ||
|
|
||
| return Command::FAILURE; | ||
| } | ||
|
|
||
| $io->title('Importing Bitrix24 Partners from CSV'); | ||
| $io->info(sprintf('Reading file: %s', $file)); | ||
|
|
||
| try { | ||
| $imported = $this->importFromCsv($file, $skipErrors, $io); | ||
|
|
||
| $io->success(sprintf('Successfully imported %d partners', $imported)); | ||
|
|
||
| return Command::SUCCESS; | ||
| } catch (\Exception $e) { | ||
| $io->error(sprintf('Error: %s', $e->getMessage())); | ||
|
|
||
| return Command::FAILURE; | ||
| } | ||
| } | ||
|
|
||
| private function importFromCsv(string $file, bool $skipErrors, SymfonyStyle $io): int | ||
| { | ||
| $fp = fopen($file, 'r'); | ||
| if (false === $fp) { | ||
| throw new \RuntimeException(sprintf('Cannot open file: %s', $file)); | ||
| } | ||
|
|
||
| $phoneUtil = PhoneNumberUtil::getInstance(); | ||
| $imported = 0; | ||
| $skipped = 0; | ||
| $lineNumber = 0; | ||
|
|
||
| // Read header | ||
| $header = fgetcsv($fp); | ||
| if (false === $header) { | ||
| fclose($fp); | ||
| throw new \RuntimeException('CSV file is empty'); | ||
| } | ||
|
|
||
| $lineNumber++; | ||
|
|
||
| // Validate header | ||
| $expectedHeaders = ['title', 'site', 'phone', 'email', 'bitrix24_partner_id', 'open_line_id', 'external_id']; | ||
| if ($header !== $expectedHeaders) { | ||
| $io->warning(sprintf( | ||
| 'CSV header mismatch. Expected: %s, Got: %s', | ||
| implode(', ', $expectedHeaders), | ||
| implode(', ', $header) | ||
| )); | ||
| } | ||
|
|
||
| // Process rows | ||
| while (false !== ($row = fgetcsv($fp))) { | ||
| $lineNumber++; | ||
|
|
||
| try { | ||
| // Skip empty rows | ||
| if (empty(array_filter($row))) { | ||
| continue; | ||
| } | ||
|
|
||
| // Parse row data | ||
| $title = trim($row[0] ?? ''); | ||
| $site = !empty($row[1] ?? '') ? trim($row[1]) : null; | ||
| $phoneString = !empty($row[2] ?? '') ? trim($row[2]) : null; | ||
| $email = !empty($row[3] ?? '') ? trim($row[3]) : null; | ||
| $bitrix24PartnerId = !empty($row[4] ?? '') ? (int) $row[4] : null; | ||
| $openLineId = !empty($row[5] ?? '') ? trim($row[5]) : null; | ||
| $externalId = !empty($row[6] ?? '') ? trim($row[6]) : null; | ||
mesilov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Validate required fields | ||
| if (empty($title)) { | ||
| throw new \InvalidArgumentException('Title is required'); | ||
| } | ||
|
|
||
| // Parse phone number | ||
| $phone = null; | ||
| if (null !== $phoneString) { | ||
| try { | ||
| $phone = $phoneUtil->parse($phoneString, 'RU'); | ||
| } catch (NumberParseException $e) { | ||
| if (!$skipErrors) { | ||
| throw new \InvalidArgumentException( | ||
| sprintf('Invalid phone number: %s', $phoneString), | ||
| 0, | ||
| $e | ||
| ); | ||
| } | ||
| $io->warning(sprintf('Line %d: Invalid phone number "%s", skipping phone', $lineNumber, $phoneString)); | ||
| $phone = null; | ||
| } | ||
| } | ||
|
|
||
| // Create partner | ||
| $command = new CreateCommand( | ||
| $title, | ||
| $site, | ||
| $phone, | ||
| $email, | ||
| $bitrix24PartnerId, | ||
| $openLineId, | ||
| $externalId | ||
| ); | ||
|
|
||
| $this->createHandler->handle($command); | ||
| $imported++; | ||
|
|
||
| $io->writeln(sprintf('Imported: %s', $title)); | ||
| } catch (\Exception $e) { | ||
| if (!$skipErrors) { | ||
| fclose($fp); | ||
| throw new \RuntimeException( | ||
| sprintf('Error on line %d: %s', $lineNumber, $e->getMessage()), | ||
| 0, | ||
| $e | ||
| ); | ||
| } | ||
|
|
||
| $skipped++; | ||
| $io->warning(sprintf('Line %d: Skipped due to error: %s', $lineNumber, $e->getMessage())); | ||
| } | ||
| } | ||
|
|
||
| fclose($fp); | ||
|
|
||
| if ($skipped > 0) { | ||
| $io->note(sprintf('Skipped %d rows due to errors', $skipped)); | ||
| } | ||
|
|
||
| return $imported; | ||
| } | ||
| } | ||
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.