17 private const ERROR_CODE_ADDRESSEE_IS_NOT_SPECIFIED =
'ADDRESSEE_IS_NOT_SPECIFIED';
18 private const ERROR_CODE_ADDRESSEE_UNEXPECTED_VALUE =
'ADDRESSEE_UNEXPECTED_VALUE';
19 private const ERROR_CODE_MESSAGE_NOT_SPECIFIED =
'MESSAGE_NOT_SPECIFIED';
20 private const ERROR_CODE_MESSAGE_STATUS_NOT_SPECIFIED =
'MESSAGE_STATUS_NOT_SPECIFIED';
21 private const ERROR_CODE_MESSAGE_STATUS_SEMANTIC_NOT_SPECIFIED =
'MESSAGE_STATUS_SEMANTIC_NOT_SPECIFIED';
22 private const ERROR_CODE_UNEXPECTED_MESSAGE_STATUS_SEMANTIC =
'UNEXPECTED_MESSAGE_STATUS_SEMANTIC';
23 private const ERROR_CODE_DELIVERY_ID_NOT_SPECIFIED =
'DELIVERY_ID_NOT_SPECIFIED';
24 private const ERROR_CODE_DELIVERY_NOT_FOUND =
'DELIVERY_NOT_FOUND';
25 private const ERROR_CODE_REQUEST_ID_NOT_SPECIFIED =
'REQUEST_ID_NOT_SPECIFIED';
26 private const ERROR_CODE_REQUEST_NOT_FOUND =
'REQUEST_NOT_FOUND';
27 private const ERROR_CODE_REQUEST_SHIPMENT_NOT_FOUND =
'REQUEST_SHIPMENT_NOT_FOUND';
28 private const ERROR_CODE_DELETE_REQUEST_INTERNAL_ERROR =
'DELETE_REQUEST_INTERNAL_ERROR';
29 private const ERROR_CODE_PROPERTIES_UNEXPECTED_FORMAT =
'PROPERTIES_UNEXPECTED_FORMAT';
30 private const ERROR_CODE_PROPERTY_VALUE_UNEXPECTED_FORMAT =
'PROPERTY_VALUE_UNEXPECTED_FORMAT';
31 private const ERROR_CODE_PROPERTY_VALUE_TAGS_UNEXPECTED_FORMAT =
'PROPERTY_VALUE_TAGS_UNEXPECTED_FORMAT';
32 private const ERROR_CODE_PROPERTY_VALUE_TAG_UNEXPECTED_FORMAT =
'PROPERTY_VALUE_TAG_UNEXPECTED_FORMAT';
33 private const ERROR_CODE_UNEXPECTED_REQUEST_FINALIZE_INDICATOR_VALUE =
'UNEXPECTED_REQUEST_FINALIZE_INDICATOR_VALUE';
34 private const ERROR_CODE_UNEXPECTED_OVERWRITE_PROPERTIES_VALUE =
'UNEXPECTED_OVERWRITE_PROPERTIES_VALUE';
35 private const ERROR_CODE_EMPTY_UPDATE_PAYLOAD =
'EMPTY_UPDATE_PAYLOAD';
36 private const ERROR_CODE_UPDATE_REQUEST_INTERNAL_ERROR =
'UPDATE_REQUEST_INTERNAL_ERROR';
37 private const ERROR_CODE_STATUS_UNEXPECTED_FORMAT =
'STATUS_UNEXPECTED_FORMAT';
38 private const ERROR_CODE_STATUS_TEXT_NOT_SPECIFIED =
'STATUS_TEXT_NOT_SPECIFIED';
39 private const ERROR_CODE_STATUS_SEMANTIC_NOT_SPECIFIED =
'STATUS_SEMANTIC_NOT_SPECIFIED';
40 private const ERROR_CODE_DATE_VALUE_UNEXPECTED_FORMAT =
'DATE_VALUE_UNEXPECTED_FORMAT';
49 public static function updateRequest($query, $n, \CRestServer $server): bool
54 $requestId = self::getRequestId(
55 self::getDeliveryId($params,
'DELIVERY_ID'),
62 if (isset($params[
'FINALIZE']))
64 if (!in_array($params[
'FINALIZE'], [
'Y',
'N'],
true))
67 'Unexpected parameter FINALIZE value: Y, N expected',
68 self::ERROR_CODE_UNEXPECTED_OVERWRITE_PROPERTIES_VALUE
72 if ($params[
'FINALIZE'] ===
'Y')
74 $fields[
'STATUS'] = Requests\Manager::STATUS_PROCESSED;
78 $requestStatus = self::getRequestStatus($params,
'STATUS');
79 if (!is_null($requestStatus))
81 $fields[
'EXTERNAL_STATUS'] = $requestStatus[
'TEXT'];
82 $fields[
'EXTERNAL_STATUS_SEMANTIC'] = $requestStatus[
'SEMANTIC'];
85 $properties = self::getRequestProperties($params,
'PROPERTIES');
86 if (!is_null($properties))
88 $fields[
'EXTERNAL_PROPERTIES'] = $properties;
91 $overwriteProperties =
false;
92 if (isset($params[
'OVERWRITE_PROPERTIES']))
94 if (!in_array($params[
'OVERWRITE_PROPERTIES'], [
'Y',
'N'],
true))
97 'Unexpected parameter OVERWRITE_PROPERTIES value: Y, N expected',
98 self::ERROR_CODE_UNEXPECTED_OVERWRITE_PROPERTIES_VALUE
101 $overwriteProperties = $params[
'OVERWRITE_PROPERTIES'] ===
'Y';
107 'Empty update payload',
108 self::ERROR_CODE_EMPTY_UPDATE_PAYLOAD
112 $updateResult = Requests\Manager::updateDeliveryRequest(
117 if (!$updateResult->isSuccess())
119 throw new RestException(
'Internal error', self::ERROR_CODE_UPDATE_REQUEST_INTERNAL_ERROR);
131 private static function getRequestStatus(array $params,
string $key): ?array
133 if (!isset($params[$key]))
138 if (!is_array($params[$key]))
141 sprintf(
'Unexpected status (%s) format: array expected', $key),
142 self::ERROR_CODE_STATUS_UNEXPECTED_FORMAT
146 if (empty($params[$key][
'TEXT']))
149 'Status text has not been specified',
150 self::ERROR_CODE_STATUS_TEXT_NOT_SPECIFIED
154 if (empty($params[$key][
'SEMANTIC']))
156 throw new RestException(
157 'Status semantic has not been specified',
158 self::ERROR_CODE_STATUS_SEMANTIC_NOT_SPECIFIED
162 if (!in_array($params[$key][
'SEMANTIC'], Requests\Manager::getRequestStatusSemantics(),
true))
164 throw new RestException(
165 sprintf(
'Unexpected request status semantic: %s', $params[$key][
'SEMANTIC']),
166 self::ERROR_CODE_STATUS_SEMANTIC_NOT_SPECIFIED
171 'TEXT' => $params[$key][
'TEXT'],
172 'SEMANTIC' => $params[$key][
'SEMANTIC'],
182 private static function getRequestProperties(array $params,
string $key): ?array
184 if (!isset($params[$key]))
189 if (!is_array($params[$key]))
191 throw new RestException(
192 sprintf(
'Unexpected properties (%s) format: array expected', $key),
193 self::ERROR_CODE_PROPERTIES_UNEXPECTED_FORMAT
198 foreach ($params[$key] as $propertyKey => $propertyValue)
200 $isExpectedFormat = (
201 is_array($propertyValue)
202 && isset($propertyValue[
'NAME'])
203 && is_string($propertyValue[
'NAME'])
204 && !empty($propertyValue[
'NAME'])
205 && isset($propertyValue[
'VALUE'])
206 && is_string($propertyValue[
'VALUE'])
208 if (!$isExpectedFormat)
210 throw new RestException(
211 sprintf(
'Unexpected property value (%s.%s) format', $key, $propertyKey),
212 self::ERROR_CODE_PROPERTY_VALUE_UNEXPECTED_FORMAT
216 'NAME' => $propertyValue[
'NAME'],
217 'VALUE' => $propertyValue[
'VALUE'],
220 if (isset($propertyValue[
'TAGS']))
222 if (!is_array($propertyValue[
'TAGS']))
224 throw new RestException(
226 'Unexpected property value\'s tags format (%s.%s) format: array expected',
230 self::ERROR_CODE_PROPERTY_VALUE_TAGS_UNEXPECTED_FORMAT
234 foreach ($propertyValue[
'TAGS'] as $tag)
236 if (!is_string($tag))
238 throw new RestException(
240 'Property value (%s.%s) tag must be of string type',
244 self::ERROR_CODE_PROPERTY_VALUE_TAG_UNEXPECTED_FORMAT
249 $resultItem[
'TAGS'] = $propertyValue[
'TAGS'];
252 $result[] = $resultItem;
265 public static function deleteRequest($query, $n, \CRestServer $server): bool
270 $deliveryId = self::getDeliveryId($params,
'DELIVERY_ID');
271 $requestId = self::getRequestId($deliveryId, $params,
'REQUEST_ID');
273 $deleteResult = Requests\Manager::deleteDeliveryRequest($requestId);
274 if (!$deleteResult->isSuccess())
276 throw new RestException(
'Internal error', self::ERROR_CODE_DELETE_REQUEST_INTERNAL_ERROR);
289 public static function sendMessage($query, $n, \CRestServer $server): bool
294 $delivery = self::getDelivery($params,
'DELIVERY_ID');
296 $requestId = self::getRequestId($delivery->getId(), $params,
'REQUEST_ID');
298 Requests\Manager::sendMessage(
299 self::getAddressee($params,
'ADDRESSEE'),
300 self::getMessage($params,
'MESSAGE')->setCurrency($delivery->getCurrency()),
302 self::getShipmentId($requestId, $params,
'SHIPMENT_REQUEST_ID')
313 private static function getDeliveryId(array $params,
string $key): int
315 $delivery = self::getDelivery($params, $key);
317 return $delivery->getId();
326 private static function getDelivery(array $params,
string $key):
Delivery\Services\
Base
328 if (!isset($params[$key]))
331 sprintf(
'Parameter %s is not specified', $key),
332 self::ERROR_CODE_DELIVERY_ID_NOT_SPECIFIED
337 $delivery = Delivery\Services\Manager::getObjectById((
int)$params[$key]);
341 'Delivery service has not been found',
342 self::ERROR_CODE_DELIVERY_NOT_FOUND
355 private static function getAddressee(array $params,
string $key): string
357 if (empty($params[$key]))
359 throw new RestException(
360 sprintf(
'Parameter %s is not specified', $key),
361 self::ERROR_CODE_ADDRESSEE_IS_NOT_SPECIFIED
365 if (!in_array($params[$key], Requests\Manager::getMessageAddressees(),
true))
367 throw new RestException(
368 sprintf(
'Unexpected %s parameter value', $key),
369 self::ERROR_CODE_ADDRESSEE_UNEXPECTED_VALUE
373 return $params[$key];
383 private static function getRequestId(
int $deliveryId, array $params,
string $key): int
385 if (empty($params[$key]))
387 throw new RestException(
388 sprintf(
'Parameter %s is not specified', $key),
389 self::ERROR_CODE_REQUEST_ID_NOT_SPECIFIED
393 $requestList = Requests\RequestTable::getList([
395 '=DELIVERY_ID' => $deliveryId,
396 '=EXTERNAL_ID' => $params[$key],
399 $request = $requestList->fetch();
403 throw new RestException(
404 'Request has not been found',
405 self::ERROR_CODE_REQUEST_NOT_FOUND
409 return (
int)$request[
'ID'];
419 private static function getShipmentId(
int $requestId, array $params,
string $key): int
421 $requestShipmentFilter = [
'=REQUEST_ID' => $requestId];
422 if (isset($params[$key]))
424 $requestShipmentFilter[
'=EXTERNAL_ID'] = (int)$params[$key];
426 $requestShipment = Requests\ShipmentTable::getList([
'filter' => $requestShipmentFilter])->fetch();
427 if (!$requestShipment)
429 throw new RestException(
430 'Shipment has not been found',
431 self::ERROR_CODE_REQUEST_SHIPMENT_NOT_FOUND
435 return (
int)$requestShipment[
'SHIPMENT_ID'];
444 private static function getMessage(array $params,
string $key): Requests\Message\Message
447 !isset($params[$key])
448 || !is_array($params[$key])
450 (!is_string($params[$key][
'SUBJECT']) || empty($params[$key][
'SUBJECT']))
451 && (!is_string($params[$key][
'BODY']) || empty($params[$key][
'BODY']))
455 throw new RestException(
456 sprintf(
'Parameter %s is not specified', $key),
457 self::ERROR_CODE_MESSAGE_NOT_SPECIFIED
461 $message =
new Requests\Message\Message();
463 if (!empty($params[$key][
'SUBJECT']))
465 $message->setSubject($params[$key][
'SUBJECT']);
467 if (!empty($params[$key][
'BODY']))
469 $message->setBody($params[$key][
'BODY']);
472 if (isset($params[$key][
'MONEY_VALUES']) && is_array($params[$key][
'MONEY_VALUES']))
474 foreach ($params[$key][
'MONEY_VALUES'] as $key => $moneyValue)
476 $message->addMoneyValue(
483 if (isset($params[$key][
'DATE_VALUES']) && is_array($params[$key][
'DATE_VALUES']))
485 foreach ($params[$key][
'DATE_VALUES'] as $key => $dateValue)
487 if (!isset($dateValue[
'VALUE']) || !isset($dateValue[
'FORMAT']))
489 throw new RestException(
490 'Unexpected date value format',
491 self::ERROR_CODE_DATE_VALUE_UNEXPECTED_FORMAT
495 $message->addDateValue(
497 (
int)$dateValue[
'VALUE'],
498 (
string)$dateValue[
'FORMAT']
503 if (isset($params[$key][
'STATUS']))
505 if (!is_string($params[$key][
'STATUS'][
'MESSAGE']) || empty($params[$key][
'STATUS'][
'MESSAGE']))
507 throw new RestException(
508 'Status message is not specified',
509 self::ERROR_CODE_MESSAGE_STATUS_NOT_SPECIFIED
513 if (!is_string($params[$key][
'STATUS'][
'SEMANTIC']) || empty($params[
'MESSAGE'][
'STATUS'][
'SEMANTIC']))
515 throw new RestException(
516 'Message status semantic is not specified',
517 self::ERROR_CODE_MESSAGE_STATUS_SEMANTIC_NOT_SPECIFIED
521 if (!in_array($params[$key][
'STATUS'][
'SEMANTIC'], Requests\Message\Status::getAvailableSemantics(),
true))
523 throw new RestException(
524 sprintf(
'Unexpected message status semantic: %s', $params[
'MESSAGE'][
'STATUS'][
'SEMANTIC']),
525 self::ERROR_CODE_UNEXPECTED_MESSAGE_STATUS_SEMANTIC
530 new Requests\Message\Status(
531 $params[$key][
'STATUS'][
'MESSAGE'],
532 $params[$key][
'STATUS'][
'SEMANTIC']