<?php
namespace EADPlataforma\Controller\Admin;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use EADPlataforma\Entity\Webhook;
use EADPlataforma\Entity\WebhookQueue;
use EADPlataforma\Entity\User;
use EADPlataforma\Enum\WebhookEnum;
use EADPlataforma\Enum\ErrorEnum;
/**
* @Route(
* path = "/admin/webhook",
* schemes = {"http|https"}
* )
* @Cache(
* maxage = "0",
* smaxage = "0",
* expires = "now",
* public = false
* )
*/
class WebhookController extends AbstractController {
public function getEntityClass(){
return Webhook::class;
}
/**
* @Route(
* path = "/list/paginate",
* methods = {"GET"},
* )
*/
public function getWebhookPaginate(Request $request) {
$permission = $this->userPermissionUtil->getPermission("appStore", "webhook", "see");
if($this->userPermissionUtil->isLow($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$this->requestUtil->setRequest($request)->setData();
$columns = [
"wh.id",
"wh.name",
"wh.type",
"wh.url",
"wh.token",
"DATE_FORMAT(wh.dateDelete, '%Y-%m-%d %H:%i:%s') AS dateDelete",
"ud.name AS userDelete"
];
$userClass = User::class;
$joins = [
"{$userClass} AS ud" => [ "LEFT", "ud.id = wh.userDelete" ],
];
$type = $this->requestUtil->getField('type');
$orderParam = $this->requestUtil->getField('order');
$searchText = $this->requestUtil->getField('searchText');
$limit = $this->requestUtil->getField('limit');
$offset = $this->requestUtil->getField('offset');
$filter = [];
$filterDelete = $this->requestUtil->getDeletedParam();
if(!empty($type)){
$filter["wh.type"] = $type;
}
$order = [ "wh.id" => "ASC" ];
if(!empty($orderParam)){
$order = json_decode($orderParam, true);
}
$webhooks = $this->repository->paginate(
"wh",
$searchText,
$columns,
$joins,
$filter,
$order,
$limit,
$offset,
$filterDelete
);
return $this->eadResponse($webhooks);
}
/**
* @Route(
* path = "/detail/{id}",
* methods = {"GET"},
* requirements = { "id" = "\d+" }
* )
*/
public function getWebhook(Request $request) {
$permission = $this->userPermissionUtil->getPermission("appStore", "webhook", "see");
if($this->userPermissionUtil->isLow($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$webhookId = $request->get('id');
$webhook = $this->repository->findOneBy([
"id" => $webhookId
]);
if (!$webhook) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
return $this->eadResponse($webhook->toReturn());
}
/**
* @Route(
* path = "/register",
* methods = {"POST"},
* )
*/
public function registerWebhook(Request $request) {
$permission = $this->userPermissionUtil->getPermission("appStore", "webhook", "create");
if($this->userPermissionUtil->isLow($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$this->requestUtil->setRequest($request)->setData();
$webhook = new Webhook();
if($this->requestUtil->issetField('name')){
$webhook->setName($this->requestUtil->getField('name'));
}
if($this->requestUtil->issetField('type')){
$webhook->setType((int)$this->requestUtil->getField('type'));
}
if($this->requestUtil->issetField('url')){
$webhook->setUrl($this->requestUtil->getField('url'));
}
if($this->requestUtil->issetField('token')){
$webhook->setToken($this->requestUtil->getField('token'));
}
$errors = $this->validateEntity($webhook);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
try{
$cronService = $this->generalService->getService('Aws\\AwsEventBridge');
$cronService->createByTypeApiDestination(WebhookEnum::CRON_WEBHOOK_QUEUE);
$this->em->persist($webhook);
$this->em->flush();
$data = $webhook->toReturn();
$this->userLogService->logInsert("webhook", $webhook->getId(), $data);
$domain = $this->configuration->getClient()->getDomainPrimary();
$domain = $this->configuration->getClient()->getDomainPrimary();
return $this->eadResponse($data);
}catch(\Exception $e){
return $this->eadResponse([ "message" => $e->getMessage() ], ErrorEnum::INTERNAL_ERROR);
}
return $this->eadResponse([ "success" => 1 ]);
}
/**
* @Route(
* path = "/edit/{id}",
* methods = {"PUT"},
* requirements = { "id" = "\d+" }
* )
*/
public function editWebhook(Request $request) {
$permission = $this->userPermissionUtil->getPermission("appStore", "webhook", "edit");
if($this->userPermissionUtil->isLow($permission)){
return $this->eadResponse(null, ErrorEnum::PERMISSION);
}
$webhookId = $request->get('id');
$webhook = $this->repository->findOneBy([
"id" => $webhookId,
"deleted" => WebhookEnum::ITEM_NO_DELETED
]);
if (!$webhook) {
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
$this->requestUtil->setRequest($request)->setData();
if($this->requestUtil->issetField('name')){
$webhook->setName($this->requestUtil->getField('name'));
}
/*if($this->requestUtil->issetField('type')){
$webhook->setType((int)$this->requestUtil->getField('type'));
}*/
if($this->requestUtil->issetField('url')){
$webhook->setUrl($this->requestUtil->getField('url'));
}
if($this->requestUtil->issetField('token')){
$webhook->setToken($this->requestUtil->getField('token'));
}
$errors = $this->validateEntity($webhook);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
$this->em->flush();
$data = $webhook->toReturn();
$this->userLogService->logUpdate("webhook", $webhook->getId(), $data);
return $this->eadResponse($data);
}
}