diff --git a/ajax_jira.php b/ajax_jira.php index 6984c30..decf619 100644 --- a/ajax_jira.php +++ b/ajax_jira.php @@ -95,37 +95,66 @@ function jira_resolve_categorie_interne(array $post) return ''; } -function jira_submit_contact_ticket(array $post) +function jira_wait_for_account_id($email, $maxAttempts = 4) { - $tabRetour = array('success' => false, 'accountId' => ''); - $userOk = false; - - $result = jira_recherche_email($post['email']); - if ($result['success'] == true) { - $userOk = true; - $tabRetour['accountId'] = $result['jira']['accountId'] ?? ''; - } else { - $result = jira_creer_user($post['email'], $post['nom']); - if ($result['success'] == true) { - $userOk = true; - $tabRetour['accountId'] = $result['statut']['accountId'] ?? ''; + for ($i = 0; $i < $maxAttempts; $i++) { + if ($i > 0) { + usleep(500000); + } + $result = jira_recherche_email($email); + if (!empty($result['success']) && !empty($result['jira']['accountId'])) { + return $result['jira']['accountId']; } } - if (!$userOk) { - return $tabRetour; + return ''; +} + +function jira_submit_contact_ticket(array $post) +{ + $tabRetour = array('success' => false, 'accountId' => ''); + $email = trim((string)($post['email'] ?? '')); + $nom = trim((string)($post['nom'] ?? '')); + $userJustCreated = false; + + $result = jira_recherche_email($email); + $accountId = !empty($result['success']) ? ($result['jira']['accountId'] ?? '') : ''; + + if ($accountId === '') { + $result = jira_creer_user($email, $nom); + if (empty($result['success'])) { + return $tabRetour; + } + $userJustCreated = true; + if (is_array($result['statut'])) { + $accountId = $result['statut']['accountId'] ?? ''; + } + if ($accountId === '') { + $accountId = jira_wait_for_account_id($email); + } } + $tabRetour['accountId'] = $accountId; + $data = array( 'serviceDeskId' => 2, 'requestTypeId' => jira_resolve_request_type_id($post), 'summary' => $post['summary'] ?? '', 'description' => $post['description'] ?? '', 'categorie_interne' => jira_resolve_categorie_interne($post), - 'raiseOnBehalfOf' => $post['email'] ?? '', + 'raiseOnBehalfOf' => $accountId !== '' ? $accountId : $email, ); $ticket = jira_create_ticket($data); + if (empty($ticket['success']) && ($userJustCreated || $accountId === '')) { + $accountId = jira_wait_for_account_id($email, 3); + if ($accountId !== '') { + $tabRetour['accountId'] = $accountId; + $data['raiseOnBehalfOf'] = $accountId; + $ticket = jira_create_ticket($data); + } + } + $tabRetour['success'] = !empty($ticket['success']); return $tabRetour;