2020-03-28 12:34:04 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-03-28 12:34:04 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2020-03-29 14:22:14 +02:00
|
|
|
use App\Helpers\Email\InvoiceEmail;
|
2020-03-28 12:34:04 +01:00
|
|
|
use App\Http\Requests\Email\SendEmailRequest;
|
2020-03-29 14:22:14 +02:00
|
|
|
use App\Jobs\Invoice\EmailInvoice;
|
2020-05-20 08:59:29 +02:00
|
|
|
use App\Jobs\Mail\EntitySentMailer;
|
2020-03-29 14:53:00 +02:00
|
|
|
use App\Models\Credit;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Quote;
|
2020-03-29 14:22:14 +02:00
|
|
|
use App\Notifications\SendGenericNotification;
|
2020-03-29 14:53:00 +02:00
|
|
|
use App\Transformers\CreditTransformer;
|
|
|
|
use App\Transformers\InvoiceTransformer;
|
|
|
|
use App\Transformers\QuoteTransformer;
|
2020-03-28 12:34:04 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
|
|
|
|
class EmailController extends BaseController
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
protected $entity_type = Invoice::class;
|
2020-03-29 14:53:00 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
protected $entity_transformer = InvoiceTransformer::class;
|
2020-03-29 14:53:00 +02:00
|
|
|
|
2020-03-28 12:34:04 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Returns a template filled with entity variables.
|
2020-03-28 12:34:04 +01:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*
|
|
|
|
* @OA\Post(
|
|
|
|
* path="/api/v1/emails",
|
|
|
|
* operationId="sendEmailTemplate",
|
|
|
|
* tags={"emails"},
|
|
|
|
* summary="Sends an email for an entity",
|
|
|
|
* description="Sends an email for an entity",
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\RequestBody(
|
|
|
|
* description="The template subject and body",
|
|
|
|
* required=true,
|
|
|
|
* @OA\MediaType(
|
|
|
|
* mediaType="application/json",
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="object",
|
|
|
|
* @OA\Property(
|
|
|
|
* property="subject",
|
|
|
|
* description="The email subject",
|
|
|
|
* type="string",
|
|
|
|
* ),
|
|
|
|
* @OA\Property(
|
|
|
|
* property="body",
|
|
|
|
* description="The email body",
|
|
|
|
* type="string",
|
|
|
|
* ),
|
|
|
|
* @OA\Property(
|
|
|
|
* property="entity",
|
|
|
|
* description="The entity name",
|
|
|
|
* type="string",
|
|
|
|
* ),
|
|
|
|
* @OA\Property(
|
|
|
|
* property="entity_id",
|
|
|
|
* description="The entity_id",
|
|
|
|
* type="string",
|
2020-04-04 12:32:42 +02:00
|
|
|
* ),
|
2020-03-28 12:34:04 +01:00
|
|
|
* @OA\Property(
|
|
|
|
* property="template",
|
|
|
|
* description="The template required",
|
|
|
|
* type="string",
|
2020-04-04 12:32:42 +02:00
|
|
|
* ),
|
2020-03-28 12:34:04 +01:00
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="success",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2020-03-28 12:34:04 +01:00
|
|
|
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
|
|
|
|
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Template"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public function send(SendEmailRequest $request)
|
|
|
|
{
|
2020-03-29 14:22:14 +02:00
|
|
|
$entity = $request->input('entity');
|
|
|
|
$entity_obj = $entity::find($request->input('entity_id'));
|
|
|
|
$subject = $request->input('subject');
|
|
|
|
$body = $request->input('body');
|
|
|
|
$entity_string = strtolower(class_basename($entity_obj));
|
|
|
|
|
2020-04-04 12:32:42 +02:00
|
|
|
$entity_obj->invitations->each(function ($invitation) use ($subject, $body, $entity_string, $entity_obj) {
|
2020-03-29 14:22:14 +02:00
|
|
|
if ($invitation->contact->send_email && $invitation->contact->email) {
|
|
|
|
$when = now()->addSeconds(1);
|
|
|
|
|
|
|
|
$invitation->contact->notify((new SendGenericNotification($invitation, $entity_string, $subject, $body))->delay($when));
|
|
|
|
}
|
|
|
|
});
|
2020-07-14 11:55:28 +02:00
|
|
|
|
2020-10-20 02:53:54 +02:00
|
|
|
$entity_obj->last_sent_date = now();
|
|
|
|
$entity_obj->save();
|
|
|
|
|
2020-07-14 11:55:28 +02:00
|
|
|
/*Only notify the admin ONCE, not once per contact/invite*/
|
|
|
|
$invitation = $entity_obj->invitations->first();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
EntitySentMailer::dispatch($invitation, $entity_string, $entity_obj->user, $invitation->company);
|
2020-07-14 11:55:28 +02:00
|
|
|
|
2020-03-29 14:53:00 +02:00
|
|
|
if ($this instanceof Invoice) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->entity_type = Invoice::class;
|
|
|
|
$this->entity_transformer = InvoiceTransformer::class;
|
2020-03-29 14:53:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this instanceof Quote) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->entity_type = Quote::class;
|
|
|
|
$this->entity_transformer = QuoteTransformer::class;
|
2020-03-29 14:53:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this instanceof Credit) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->entity_type = Credit::class;
|
|
|
|
$this->entity_transformer = CreditTransformer::class;
|
2020-03-29 14:53:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$entity_obj->service()->markSent()->save();
|
|
|
|
|
|
|
|
return $this->itemResponse($entity_obj);
|
2020-03-28 12:34:04 +01:00
|
|
|
}
|
|
|
|
}
|