Skip to content
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

Для прохождения тестового задания #403

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions ReturnOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

namespace NW\WebService\References\Operations\Notification;

use Exception;

class TsReturnOperation extends ReferencesOperation
{
public const TYPE_NEW = 1;
public const TYPE_CHANGE = 2;

/**
* @throws Exception
*/
public function doOperation(): array
{
$data = (array)$this->getRequest('data');
$data['differences']['to'] = (int)$data['differences']['to'];

$resellerId = (int)$data['resellerId'];
$notificationType = (int)$data['notificationType'];
$result = [
'notificationEmployeeByEmail' => false,
'notificationClientByEmail' => false,
'notificationClientBySms' => [
'isSent' => false,
'message' => '',
],
];

if (empty($resellerId)) {
throw new Exception('Empty resellerId', 400);
}

if (empty($notificationType)) {
throw new Exception('Empty notificationType', 400);
}

//$reseller, $client, $cr, $et никогда не будут раны null в данных условиях, т.к. объект инициализируется всегда,
//Если только не прописать дополнительное условие в static::getById();
$reseller = Seller::getById($resellerId);
if ($reseller === null) {
throw new Exception('Seller not found!', 400);
}

$client = Contractor::getById($data['clientId']);
if ($client === null || $client->type !== Contractor::TYPE_CUSTOMER || $client->Seller->id !== $resellerId) {
throw new Exception('сlient not found!', 400);
}

$cr = Employee::getById((int)$data['creatorId']);
if ($cr === null) {
throw new Exception('Creator not found!', 400);
}

$et = Employee::getById((int)$data['expertId']);
if ($et === null) {
throw new Exception('Expert not found!', 400);
}

$differences = '';
if ($notificationType === self::TYPE_NEW) {
//Судя по-всему __ - функция преобразования текста по шаблону, в задании не указана
$differences = __('NewPositionAdded', null, $resellerId);
} elseif ($notificationType === self::TYPE_CHANGE && !empty($data['differences'])) {
$differences = __('PositionStatusHasChanged', [
//Вообще от таких преобразований перед передачей в метод следует избавляться, это позволяет найти ошибки на уровне тестирования,
// нужно вводить доп методы проерки данных перед отправкой
'FROM' => Status::getName((int)$data['differences']['from']),
'TO' => Status::getName($data['differences']['to']),
], $resellerId);
}

$templateData = [
'COMPLAINT_ID' => (int)$data['complaintId'],
'COMPLAINT_NUMBER' => (string)$data['complaintNumber'],
'CREATOR_ID' => (int)$data['creatorId'],
'CREATOR_NAME' => $cr->getFullName(),
'EXPERT_ID' => (int)$data['expertId'],
'EXPERT_NAME' => $et->getFullName(),
'CLIENT_ID' => (int)$data['clientId'],
'CLIENT_NAME' => $client->getFullName(),
'CONSUMPTION_ID' => (int)$data['consumptionId'],
'CONSUMPTION_NUMBER' => (string)$data['consumptionNumber'],
'AGREEMENT_NUMBER' => (string)$data['agreementNumber'],
'DATE' => (string)$data['date'],
'DIFFERENCES' => $differences,
];

// Если хоть одна переменная для шаблона не задана, то не отправляем уведомления
foreach ($templateData as $key => $tempData) {
if (empty($tempData)) {
throw new Exception("Template Data ({$key}) is empty!", 500);
}
}

$emailFrom = getResellerEmailFrom($resellerId);
// Получаем email сотрудников из настроек
$emails = getEmailsByPermit($resellerId, 'tsGoodsReturn');
if (!empty($emailFrom) && count($emails)) {
foreach ($emails as $email) {
MessagesClient::sendMessage([
0 => [ // MessageTypes::EMAIL
'emailFrom' => $emailFrom,
'emailTo' => $email,
'subject' => __('complaintEmployeeEmailSubject', $templateData, $resellerId),
'message' => __('complaintEmployeeEmailBody', $templateData, $resellerId),
],
], $resellerId, NotificationEvents::CHANGE_RETURN_STATUS);
$result['notificationEmployeeByEmail'] = true;

}
}

// Шлём клиентское уведомление, только если произошла смена статуса
if ($notificationType === self::TYPE_CHANGE && !empty($data['differences']['to'])) {
if (!empty($emailFrom) && !empty($client->email)) {
MessagesClient::sendMessage([
0 => [ // MessageTypes::EMAIL
'emailFrom' => $emailFrom,
'emailTo' => $client->email,
'subject' => __('complaintClientEmailSubject', $templateData, $resellerId),
'message' => __('complaintClientEmailBody', $templateData, $resellerId),
],
], $resellerId, $client->id, NotificationEvents::CHANGE_RETURN_STATUS, $data['differences']['to']);
$result['notificationClientByEmail'] = true;
}

if (!empty($client->mobile)) {
$error = '';
$res = NotificationManager::send($resellerId, $client->id, NotificationEvents::CHANGE_RETURN_STATUS, $data['differences']['to'], $templateData, $error);
if ($res) {
$result['notificationClientBySms']['isSent'] = true;
}
if (!empty($error)) {
$result['notificationClientBySms']['message'] = $error;
}
}
}

return $result;
}
}
113 changes: 113 additions & 0 deletions others.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace NW\WebService\References\Operations\Notification;

/**
* @property Seller $Seller
*/
class Contractor
{
//Не указано динамичное свойство класса $resellerId и нет конструктора класса
const TYPE_CUSTOMER = 0;
public $id;
public $type;
public $name;

public static function getById(int $resellerId): self
{
return new static($resellerId); // fakes the getById method
}

public function getFullName(): string
{
return $this->id ? ($this->name . ' ' . $this->id) : $this->name;
}
}

class Seller extends Contractor
{
public function getFullName(): string
{
return $this->name . ' ' . $this->id;
}
}

class Employee extends Contractor
{
public function getFullName(): string
{
return $this->name . ' ' . $this->id;
}
}

class Status
{
public $id, $name;

public static function getName(int $id): string
{
$a = [
0 => 'Completed',
1 => 'Pending',
2 => 'Rejected',
];

return $a[$id];
}
}

abstract class ReferencesOperation
{
abstract public function doOperation(): array;

public function getRequest($pName)
{
return $_REQUEST[$pName];
}
}

function getResellerEmailFrom()
{
return 'contractor@example.com';
}

function getEmailsByPermit($resellerId, $event)
{
// fakes the method
return ['someemeil@example.com', 'someemeil2@example.com'];
}

class NotificationEvents
{
const CHANGE_RETURN_STATUS = 'changeReturnStatus';
const NEW_RETURN_STATUS = 'newReturnStatus';
}

class NotificationManager
{
public static function send(
$resellerId,
$clientid,
$event,
$notificationSubEvent,
$templateData,
&$errorText,
$locale = null
) {
// fakes the method
return true;
}
}

class MessagesClient
{
static function sendMessage(
$sendMessages,
$resellerId = 0,
$customerId = 0,
$notificationEvent = 0,
$notificationSubEvent = ''
) {
return '';
}
}
Loading