54 $this->factory = $factory;
55 $this->mapperFactory = ServiceLocator::getInstance()->get(
'calendar.service.mappers.factory');
70 $mainResult =
new Result();
72 $factory = $this->factory;
79 return $mainResult->setData([$factory->
getConnection()->getVendor()->getCode() => [
81 'message' =>
'Section link not found',
85 $eventLink = $this->getEventConnection($event)
86 ?? (
new EventConnection())
88 ->setConnection($sectionLink->getConnection());
89 if ($sectionLink->isActive())
93 $result = $manager->create($event, $context);
94 if ($result->isSuccess())
96 $resultData = $result->getData();
99 ->setVendorEventId($resultData[
'event'][
'id'])
100 ->setEntityTag($resultData[
'event'][
'etag'])
101 ->setVendorVersionId($resultData[
'event'][
'version'] ??
null)
103 ->setLastSyncStatus($status);
104 if (!empty($result->getData()[
'data']))
106 $eventLink->setData($result->getData()[
'data']);
112 $eventLink->setLastSyncStatus($status);
119 catch (NotFoundException $e)
122 $sectionLink->setActive(
false)->setSyncToken(
null)->setPageToken(
null);
123 $this->mapperFactory->getSectionConnection()->update($sectionLink);
127 if (!$sectionLink->isActive())
130 $eventLink->setLastSyncStatus($status)->setVendorEventId(
'');
137 $eventLink = $eventLink->getId()
138 ? $this->mapperFactory->getEventConnection()->update($eventLink)
139 : $this->mapperFactory->getEventConnection()->create($eventLink);
141 $syncResult[
'eventConnectionId'] = $eventLink->getId();
142 $data[
'result'] = $syncResult;
144 return $mainResult->setData($data);
160 $mainResult =
new Result();
162 $factory = $this->factory;
168 $mainResult->addError(
new Error(
'Section connection not found'));
174 if ($eventLink && $eventLink->getVendorEventId() && $sectionLink->isActive())
176 if ((
int)$eventLink->getEventVersion() !== $event->
getVersion())
180 $result = $manager->update($event, $context);
181 if ($result->isSuccess())
185 ->setEntityTag($result->getData()[
'event'][
'etag'])
186 ->setVendorVersionId($result->getData()[
'event'][
'version'] ??
null)
193 $eventLink->setLastSyncStatus($status);
195 $this->mapperFactory->getEventConnection()->update($eventLink);
199 $this->mapperFactory->getEventConnection()->delete($eventLink);
201 return $this->createEvent($event, $context);
207 $eventLink->setLastSyncStatus($status);
208 $this->mapperFactory->getEventConnection()->update($eventLink);
212 elseif ($eventLink && !$sectionLink->isActive())
217 ->setLastSyncStatus($status)
219 $this->mapperFactory->getEventConnection()->update($eventLink);
226 ->setConnection($sectionLink->getConnection())
227 ->setLastSyncStatus($status)
230 $this->mapperFactory->getEventConnection()->create($eventLink);
236 $resultData[$factory->
getConnection()->getVendor()->getCode()] = [
240 return $mainResult->setData($resultData);
258 $mainResult =
new Result();
260 $factory = $this->factory;
266 $mainResult->addError(
new Error(
'Section connection not found'));
272 if ($eventLink && $eventLink->getVendorEventId() && $sectionLink->isActive())
276 $result = $manager->delete($event, $context);
277 if ($result->isSuccess())
282 $childIds = EventConnectionTable::query()
283 ->setSelect([
'ID',
'EVENT'])
284 ->where(
'EVENT.RECURRENCE_ID', $event->
getParentId())
285 ->where(
'EVENT.OWNER_ID', $event->
getOwner()->getId())
288 while ($child = $childIds->fetch())
290 $childToDelete[] = $child[
'ID'];
294 $DB->Query(
"DELETE FROM b_calendar_event_connection
295 WHERE ID IN (" . implode(
',', $childToDelete) .
")
296 AND CONNECTION_ID = '{$factory->getConnection()->getId()}';
303 $this->mapperFactory->getEventConnection()->delete($eventLink);
308 $eventLink->setLastSyncStatus($status);
309 $this->mapperFactory->getEventConnection()->update($eventLink);
315 $this->mapperFactory->getEventConnection()->delete($eventLink);
318 elseif ($eventLink && !$sectionLink->isActive())
321 $eventLink->setLastSyncStatus($status);
322 $this->mapperFactory->getEventConnection()->update($eventLink);
328 $resultData[$factory->
getConnection()->getVendor()->getCode()] = [
332 return $mainResult->setData($resultData);
350 $factory = $this->factory;
356 $result->addError(
new Error(
'Section connection not found'));
362 if ($masterEvent = $this->getMasterEvent($event))
365 $masterLink = $this->getEventConnection($masterEvent);
366 if ($masterLink ===
null)
369 $result->addError(
new Error(
'Series master event does not have connection with vendor'));
375 $result = $manager->createInstance($event, $context);
377 if ($result->isSuccess())
379 $resultData = $result->getData();
381 if ($factory->getCode() === Icloud\Helper::ACCOUNT_TYPE)
384 ->setVendorEventId($result->getData()[
'event'][
'id'])
385 ->setEntityTag($resultData[
'event'][
'etag'])
386 ->setLastSyncStatus($status)
387 ->setVersion($masterEvent->getVersion())
389 $this->mapperFactory->getEventConnection()->update($masterLink);
392 $eventLink = (
new EventConnection())
394 ->setConnection($sectionLink->getConnection())
395 ->setVendorEventId($result->getData()[
'event'][
'id'])
396 ->setVendorVersionId($result->getData()[
'event'][
'version'] ??
null)
397 ->setRecurrenceId($result->getData()[
'event'][
'recurrence'] ??
null)
398 ->setData($result->getData()[
'event'][
'data'] ??
null)
399 ->setLastSyncStatus($status)
402 if ($factory->getCode() !== Icloud\Helper::ACCOUNT_TYPE)
404 $eventLink->setEntityTag($result->getData()[
'event'][
'etag'] ??
null);
407 $this->mapperFactory->getEventConnection()->create($eventLink);
413 $this->mapperFactory->getEventConnection()->update($masterLink);
419 $result->addError(
new Error(
'Master event not found'));
422 $resultData[$factory->
getConnection()->getVendor()->getCode()] = [
427 return (
new Result())->setData($resultData);
445 $factory = $this->factory;
450 $result->addError(
new Error(
'Section connection not found'));
456 $result->addError(
new Error(
'Instance connection not found'));
461 if ($masterEvent = $this->getMasterEvent($event))
464 $masterLink = $this->getEventConnection($masterEvent);
466 if ($masterLink ===
null)
469 $result->addError(
new Error(
'Series master event does not have connection with vendor'));
476 ->add(
'sync',
'instanceLink', $eventLink)
479 $result = $manager->updateInstance($event, $context);
480 if ($result->isSuccess())
484 ->setLastSyncStatus($status)
486 ->setVendorVersionId($result->getData()[
'event'][
'version'] ??
null)
488 if (!empty($result->getData()[
'event'][
'id']))
490 $eventLink->setVendorEventId($result->getData()[
'event'][
'id']);
492 if ($factory->getCode() !== Icloud\Helper::ACCOUNT_TYPE)
494 $eventLink->setEntityTag($result->getData()[
'event'][
'etag'] ??
null);
496 $this->mapperFactory->getEventConnection()->update($eventLink);
498 if ($factory->getCode() === Icloud\Helper::ACCOUNT_TYPE)
501 ->setEntityTag($result->getData()[
'event'][
'etag'])
502 ->setLastSyncStatus($status)
503 ->setVersion($masterEvent->getVersion())
505 $this->mapperFactory->getEventConnection()->update($masterLink);
512 $this->mapperFactory->getEventConnection()->update($masterLink);
518 $result->addError(
new Error(
'Master event not found'));
521 $resultData[$factory->
getConnection()->getVendor()->getCode()] = [
526 return (
new Result())->setData($resultData);
538 $mainResult =
new Result();
541 $factory = $this->factory;
542 $excludeDate = $context->sync[
'excludeDate'] ??
null;
546 $mainResult->addError(
new Error(
'Not found info about exclude date'));
552 if ($masterLink && $sectionLink)
555 $result = $manager->deleteInstance($event, $context);
556 if ($result->isSuccess())
560 ->setEntityTag($result->getData()[
'event'][
'etag'] ??
null)
561 ->setLastSyncStatus($status)
562 ->setVersion($masterLink->getEvent()->getVersion())
571 $this->mapperFactory->getEventConnection()->update($masterLink);
576 $result->addError(
new Error(
'Link not found'));
579 $resultData[$factory->
getConnection()->getVendor()->getCode()] = [
584 return $result->setData($resultData);
596 $mainResult =
new Result();
598 $factory = $this->factory;
604 $mainResult->addError(
new Error(
'Section connection not found'));
608 $context->
add(
'sync',
'vendorSectionId', $sectionLink->getVendorSectionId());
610 $recurrenceEvent->getEvent()->setUid(
null);
612 $result = $manager->createRecurrence($recurrenceEvent, $sectionLink, $context);
614 if ($result->isSuccess())
616 $masterResult = $this->createEventLink($recurrenceEvent, $this->factory->getConnection()->getId());
619 foreach ($recurrenceEvent->getInstanceMap()->getCollection() as $instance)
621 $instanceResult[] = $this->createEventLink($instance, $this->factory->getConnection()->getId());
632 $resultData[$factory->
getConnection()->getVendor()->getCode()] = [
635 'linkMasterResult' => $masterResult ??
null,
636 'linkInstancesResult' => $instanceResult ??
null,
639 return $mainResult->setData($resultData);
649 public function updateRecurrence(SyncEvent $recurrenceEvent, EventContext $context): Result
651 $mainResult =
new Result();
653 $factory = $this->factory;
656 $sectionLink = $context->getSectionConnection();
659 $mainResult->addError(
new Error(
'Section connection not found'));
663 $context->add(
'sync',
'vendorSectionId', $sectionLink->getVendorSectionId());
665 $recurrenceEvent->getEvent()->setUid(
666 $recurrenceEvent->getEventConnection()->getVendorEventId()
670 (
int)$recurrenceEvent->getEventConnection()->getEventVersion()
671 === $recurrenceEvent->getEvent()->getVersion()
674 $result =
new Result();
677 'id' => $recurrenceEvent->getEventConnection()->getVendorEventId(),
678 'etag' => $recurrenceEvent->getEventConnection()->getEntityTag(),
687 $result = $manager->updateRecurrence($recurrenceEvent, $sectionLink, $context);
688 if ($result->isSuccess())
690 $masterResult = $this->updateEventLink($recurrenceEvent);
691 $instanceResult = [];
693 foreach ($recurrenceEvent->getInstanceMap()->getCollection() as $instance)
695 if ($instance->getEventConnection() && $instance->getEventConnection()->getId())
697 $instanceResult[] = $this->updateEventLink($instance);
701 $instanceResult[] = $this->createEventLink(
703 $this->factory->getConnection()->getId()
715 catch (NotFoundException $e)
717 $recurrenceEvent->getEvent()->setUid(
null);
718 return $this->createRecurrence($recurrenceEvent, $context);
722 $resultData[$factory->
getConnection()->getVendor()->getCode()] = [
725 'linkMasterResult' => $masterResult ??
null,
726 'linkInstancesResult' => $instanceResult ??
null,
729 return $mainResult->setData($resultData);
746 $mainResult =
new Result();
748 $factory = $this->factory;
754 $resultData[
'sectionConnection'] = $sectionLink;
764 $originalName = $section->getName();
769 $result = $manager->
create($section, $context);
775 $section->setName($originalName .
" ($counter)");
781 $section->setName($originalName);
786 $result = $safeCreate($section, $context);
787 if ($result->isSuccess())
791 ->setSection($section)
793 ->setVendorSectionId($result->getData()[
'id'])
794 ->setVersionId($result->getData()[
'version'])
796 ->setLastSyncStatus($status)
797 ->setLastSyncDate(
new Core\
Base\
Date())
799 $this->mapperFactory->getSectionConnection()->create($sectionLink);
801 $resultData[
'sectionConnection'] = $sectionLink;
805 $mainResult->addErrors($result->getErrors());
810 $resultData[$factory->
getConnection()->getVendor()->getCode()] = [
814 return $mainResult->setData($resultData);
827 $mainResult =
new Result();
829 $factory = $this->factory;
834 if ($sectionLink->isActive())
838 $result = $manager->update($section, $context);
839 if ($result->isSuccess())
842 $sectionLink->setVersionId($result->getData()[
'version']);
847 $mainResult->addErrors($result->getErrors());
852 $sectionLink->setActive(
false);
857 ->setLastSyncStatus($status)
858 ->setLastSyncDate(
new Core\
Base\
Date());
859 $this->mapperFactory->getSectionConnection()->update($sectionLink);
876 'error' => $result->getData()[
'error'] ??
'',
879 return $mainResult->setData($resultData);
890 $mainResult =
new Result();
892 $factory = $this->factory;
897 $sectionLink->setSection($section);
898 $result = $manager->delete($section, $context);
899 if ($result->isSuccess())
906 $mainResult->addErrors($result->getErrors());
908 ->setLastSyncStatus($status)
909 ->setLastSyncDate(
new Core\
Base\
Date())
911 $this->mapperFactory->getSectionConnection()->update($sectionLink);
919 $resultData[$factory->
getConnection()->getVendor()->getCode()] = [
923 return $mainResult->setData($resultData);
941 $link = EventConnectionTable::query()
943 ->addFilter(
'CONNECTION_ID', $connection->getId())
944 ->addFilter(
'EVENT_ID', $event->
getId())
951 EventConnectionTable::update($link->getId(), [
953 'VERSION' => $version,
964 return $this->factory->canSubscribeSection();
972 return $this->factory->canSubscribeConnection();
982 return $this->factory->getPushManager()->addSectionPush($link);
990 return $this->factory->getPushManager()->addConnectionPush($this->factory->getConnection());
1000 if ($manager = $this->factory->getPushManager())
1002 return $manager->renewPush($push);
1015 return $this->factory->getPushManager()->deletePush($push);
1025 return $this->factory->getPushManager()->deletePush($push);
1038 return $this->mapperFactory->getEventConnection()->
getMap([
1039 '=EVENT_ID' => $event->
getId(),
1040 '=CONNECTION_ID' => $this->factory->getConnection()->getId(),
1052 private function getMasterEvent(
Event $event): ?
Event
1054 return $this->mapperFactory->getEvent()->getMap([
1055 '=PARENT_ID' => $event->getRecurrenceId(),
1056 '=OWNER_ID' => $event->getOwner()->getId(),
1067 private function createEventLink(SyncEvent $syncEvent,
int $connectionId): \
Bitrix\Main\ORM\Data\
AddResult
1071 return EventConnectionTable::add([
1072 'EVENT_ID' => $syncEvent->getEvent()->getId(),
1073 'CONNECTION_ID' => $connectionId,
1074 'VENDOR_EVENT_ID' => $syncEvent->getEventConnection()
1075 ? $syncEvent->getEventConnection()->getVendorEventId()
1078 'SYNC_STATUS' =>
Dictionary::SYNC_STATUS[
'success'],
1079 'VERSION' => $syncEvent->getEvent()->getVersion(),
1080 'ENTITY_TAG' => $syncEvent->getEventConnection()
1081 ? $syncEvent->getEventConnection()->getEntityTag()
1093 private function updateEventLink(SyncEvent $syncEvent): Result
1095 $newLink = $this->mapperFactory->getEventConnection()->update($syncEvent->getEventConnection());
1097 return (
new Result())->setData([
'eventConnection' => $newLink]);