Код: Выделить всё
function set_contact_request_number_sent(
$status, // Status of the request (success/error)
$request_note, // Notes related to the request
$uuid, // Unique identifier for the request
$task_id, // ID of the task related to the request
$platform_of_request, // Platform from which the request is made (e.g., web, mobile)
$device_id, // ID of the device making the request
$org_id, // Organization ID related to the user
$szif_task_id, // ID for the specific task
$typ_pozadavku, // Type of the request
$tel_cislo, // Phone number related to the request
$datum, // Date related to the request
$user_id, // ID of the user making the request
$errors, // Array to hold error messages
$notification_token, // Token for notifications
$now_timestamp // Current timestamp
) {
// Check if the user has a valid organization
try {
// Select the organization ID for the user from user_alias table
$result = dibi::select('org_id')
->from('user_alias')
->where('id = %s', $user_id) // Filter by user ID
->where('org_id IS NOT NULL') // Ensure organization ID is not null
->fetch(); // Fetch the result
// If no result is found, set status to error and add error message
if (!$result) {
$status = 'error'; // Set status to error
$request_note .= 'Error: ' . ERROR_MSG['access_denied'] . '. '; // Append error note
$errors[] = ERROR_MSG['access_denied']; // Add error message to errors array
} else {
// If organization ID is not provided, use the fetched one
$org_id = $org_id ?: $result->org_id; // Use existing org_id if not provided
}
} catch (Exception $e) {
// Catch any exceptions and set status to error
$status = 'error'; // Set status to error
$request_note .= 'Exception: ' . $e->getMessage() . '. '; // Append exception message
$errors[] = 'Failed to check user alias: ' . $e->getMessage(); // Add error message to errors array
}
// Check task validity
try {
// Select task details from task table
$task_result = dibi::select('id, status, flg_deleted, org_id, szif_task_id')
->from('task')
->where('id = %i', $task_id) // Filter by task ID
->where('flg_deleted = %i', 0) // Ensure task is not deleted
->where('szif_task_id', $szif_task_id) // Filter by specific task ID
->where('(status = %s OR status = %s OR status = %s)', 'new', 'open', 'returned') // Check valid statuses
->fetch(); // Fetch the result
// If task is not found, set status to error and add error message
if (!$task_result) {
if ($status !== 'error') { // If status is not already an error
$status = 'error'; // Set status to error
}
$request_note .= 'Error: ' . ERROR_MSG['request_unavailable'] . '. '; // Append error note
$errors[] = ERROR_MSG['request_unavailable']; // Add error message to errors array
}
} catch (Exception $e) {
// Catch any exceptions and set status to error
if ($status !== 'error') {
$status = 'error'; // Set status to error
}
$request_note .= 'Exception: ' . $e->getMessage() . '. '; // Append exception message
$errors[] = 'Failed to check task validity: ' . $e->getMessage(); // Add error message to errors array
}
// Check for a maximum of 2 requests for this task
try {
// Select existing requests that meet certain criteria
$requests = dibi::select('request_id')
->from('contact_requests')
->where('org_id = %i', $org_id) // Filter by organization ID
->where('task_id = %i', $task_id) // Filter by task ID
->where('request_status = %s', 'ok') // Ensure request status is 'ok'
->where('szif_task_id = %i', $szif_task_id) // Filter by specific task ID
->where('number_sent < 2') // Limit to requests sent less than 2 times
->fetchAll(); // Fetch all matching records
// Count the number of requests found
$countRequests = count($requests); // Count the number of requests
// Handle the result
if ($countRequests >= 2) { // If there are 2 or more requests
if ($status !== 'error') {
$status = 'error'; // Set status to error if not already
}
$request_note .= 'Error: ' . ERROR_MSG['max_requests_exceeded'] . '. '; // Append error note
$errors[] = ERROR_MSG['max_requests_exceeded']; // Add error message to errors array
}
} catch (Exception $e) {
// Catch any exceptions and set status to error
if ($status !== 'error') {
$status = 'error'; // Set status to error
}
$request_note .= 'Exception: ' . $e->getMessage() . '. '; // Append exception message
$errors[] = 'Failed to check maximum requests: ' . $e->getMessage(); // Add error message to errors array
}
// Check for requests made in the last 24 hours
$countRecent = dibi::select('COUNT(request_id) AS pocet') // Count requests
->from('contact_requests') // From contact_requests table
->where('task_id = %i', $task_id) // Filter by task ID
->where('last_time_sent > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 DAY)') // Ensure requests are within the last 24 hours
->where('request_status = %s', 'ok') // Ensure request status is 'ok'
->fetchSingle(); // Fetch single value
// Return error if count of requests is 2 or more
if ($countRequests >= 2) {
return [ // Return error response
'status' => 'error',
'message' => 'Maximum number of requests for this task has been reached.',
];
}
// Return error if there has been a request in the last 24 hours
if ($countRecent >= 1) {
return [ // Return error response
'status' => 'error',
'message' => 'A request has already been made in the last 24 hours.',
];
}
// Define maximum number of retries for IS_MACH request
define('MAX_RETRY_COUNT', 3); // Set the maximum number of retry attempts
// Begin transaction for contact request processing
dibi::begin(); // Start a new transaction
try {
// Track updated records for potential rollback
$updated_request_ids = []; // Initialize an array to track updated request IDs
// Check for existing records with the same org_id, task_id, and szif_task_id
$existing_record = dibi::select('*') // Select all fields
->from('contact_requests') // From contact_requests table
->where('org_id = %i', $org_id) // Filter by organization ID
->where('task_id = %i', $task_id) // Filter by task ID
->where('szif_task_id = %i', $szif_task_id) // Filter by specific task ID
->where('request_status = %s', 'ok') // Ensure request status is 'ok'
->fetch(); // Fetch the result
// Set number_sent based on existing records
$number_sent = $existing_record ? ($existing_record->number_sent + 1) : 1; // Increment count if existing record found
// Insert the new request into the contact_requests table
$new_record_id = dibi::insert('contact_requests', [ // Insert a new record
'org_id' => $org_id, // Set organization ID
'task_id' => $task_id, // Set task ID
'typ_pozadavku' => $typ_pozadavku, // Set type of request
'tel_cislo' => $tel_cislo, // Set phone number
'datum' => $datum, // Set date
'last_time_sent' => $now_timestamp, // Set last sent time
'number_sent' => $number_sent, // Set number sent
'sent_at' => $now_timestamp, // Set sent timestamp
'platform_of_request' => $platform_of_request, // Set platform of request
'uuid' => $uuid, // Set UUID
'request_status' => $status, // Set request status
'request_note' => $request_note, // Set request note
'szif_task_id' => $szif_task_id, // Set specific task ID
])->execute(); // Execute the insert
// If a new record was successfully created
if ($new_record_id) {
$updated_request_ids[] = $new_record_id; // Add new record ID to updated request IDs
$device_id = $device_id ?: 'unknown'; // Use provided device ID or set as 'unknown'
$notification_token = $notification_token ?: 'not_provided'; // Use provided token or set as 'not_provided'
}
// Commit the transaction if everything is successful
dibi::commit(); // Commit the transaction
// Return success response with relevant IDs
return [ // Return success response
'status' => 'success',
'device_id' => $device_id, // Include device ID
'notification_token' => $notification_token, // Include notification token
];
} catch (Exception $e) {
// Rollback transaction in case of an error
dibi::rollback(); // Rollback the transaction
// Return error response
return [ // Return error response
'status' => 'error',
'message' => 'Failed to set contact request number sent: ' . $e->getMessage(), // Append error message
];
}
}
Код: Выделить всё
Подробнее здесь: [url]https://stackoverflow.com/questions/79031509/how-to-handle-interface-communications-and-db-relationships[/url]