env-config

This commit is contained in:
paulwer 2023-12-14 08:32:15 +01:00
parent 1125f57a7f
commit b065542020
4 changed files with 58 additions and 28 deletions

View File

@ -24,9 +24,9 @@ class IncomingMailHandler
{
private $server;
public $connection;
public function __construct(string $server, string $user, string $password)
public function __construct(string $server, string $port, string $user, string $password)
{
$this->server = new Server($server);
$this->server = new Server($server, $port == '' ? null : $port);
$this->connection = $this->server->authenticate($user, $password);
}

View File

@ -46,32 +46,26 @@ use Turbo124\Beacon\Facades\LightLogs;
/*Multi Mailer implemented*/
class ExpenseImportJob implements ShouldQueue
class InboundExpensesJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesHash;
public $tries = 4; //number of retries
public $deleteWhenMissingModels = true;
/** @var null|\App\Models\Company $company **/
public Company $company;
private array $imap_companies;
private array $imap_credentials;
private $expense_repo;
public function __construct()
{
$this->credentials = [];
$this->getImapCredentials();
$this->expense_repo = new ExpenseRepository();
}
public function backoff()
{
// return [5, 10, 30, 240];
return [rand(5, 10), rand(30, 40), rand(60, 79), rand(160, 400)];
}
public function handle()
{
@ -81,8 +75,8 @@ class ExpenseImportJob implements ShouldQueue
nlog("importing expenses from imap-servers");
$a = Account::with('companies')->cursor()->each(function ($account) {
$account->companies()->where('expense_import', true)->whereNotNull('expense_mailbox_imap_host')->whereNotNull('expense_mailbox_imap_user')->whereNotNull('expense_mailbox_imap_password')->cursor()->each(function ($company) {
Account::with('companies')->cursor()->each(function ($account) {
$account->companies()->whereIn('id', $this->imap_companies)->cursor()->each(function ($company) {
$this->handleCompanyImap($company);
});
});
@ -90,9 +84,33 @@ class ExpenseImportJob implements ShouldQueue
}
private function getImapCredentials()
{
$servers = explode(",", config('ninja.imap_inbound_expense.servers'));
$ports = explode(",", config('ninja.imap_inbound_expense.servers'));
$users = explode(",", config('ninja.imap_inbound_expense.servers'));
$passwords = explode(",", config('ninja.imap_inbound_expense.servers'));
$companies = explode(",", config('ninja.imap_inbound_expense.servers'));
if (sizeOf($servers) != sizeOf($ports) || sizeOf($servers) != sizeOf($users) || sizeOf($servers) != sizeOf($passwords) || sizeOf($servers) != sizeOf($companies))
throw new \Exception('invalid configuration imap_inbound_expenses (wrong element-count)');
foreach ($companies as $index => $companyId) {
$this->imap_credentials[$companyId] = [
"server" => $servers[$index],
"port" => $servers[$index],
"user" => $servers[$index],
"password" => $servers[$index],
];
$this->imap_companies[] = $companyId;
}
}
private function handleCompanyImap(Company $company)
{
$incommingMails = new IncomingMailHandler($company->expense_mailbox_imap_host, $company->company->expense_mailbox_imap_user, $company->company->expense_mailbox_imap_password);
$credentials = $this->imap_credentials[$company->id];
$incommingMails = new IncomingMailHandler($credentials->server, $credentials->port, $credentials->user, $credentials->password);
$emails = $incommingMails->getUnprocessedEmails();
@ -100,18 +118,19 @@ class ExpenseImportJob implements ShouldQueue
$sender = $mail->getSender();
$vendor = Vendor::where('expense_sender_email', $sender)->orWhere($sender, 'LIKE', "CONCAT('%',expense_sender_email)")->first();
$vendor = Vendor::where('expense_sender_email', $sender)->orWhere($sender, 'LIKE', "CONCAT('%',expense_sender_domain)")->first();
if ($vendor !== null)
$vendor = Vendor::where("email", $sender)->first();
// TODO: check email for existing vendor?!
$documents = []; // TODO: $mail->getAttachments() + save email as document (.html)
$data = [
"vendor_id" => $vendor !== null ? $vendor->id : null,
"date" => $mail->getDate(),
"public_notes" => $mail->getSubject(),
"private_notes" => $mail->getCompleteBodyText(),
"documents" => $mail->getAttachments(), // FIXME: https://github.com/ddeboer/imap?tab=readme-ov-file#message-attachments
"documents" => $documents, // FIXME: https://github.com/ddeboer/imap?tab=readme-ov-file#message-attachments
];
$expense = $this->expense_repo->save($data, ExpenseFactory::create($company->company->id, $company->company->owner()->id)); // TODO: dont assign a new number at beginning
@ -128,4 +147,11 @@ class ExpenseImportJob implements ShouldQueue
}
}
public function backoff()
{
// return [5, 10, 30, 240];
return [rand(5, 10), rand(30, 40), rand(60, 79), rand(160, 400)];
}
}

View File

@ -196,7 +196,7 @@ return [
'ninja_default_company_id' => env('NINJA_COMPANY_ID', null),
'ninja_default_company_gateway_id' => env('NINJA_COMPANY_GATEWAY_ID', null),
'ninja_hosted_secret' => env('NINJA_HOSTED_SECRET', ''),
'ninja_hosted_header' =>env('NINJA_HEADER', ''),
'ninja_hosted_header' => env('NINJA_HEADER', ''),
'ninja_connect_secret' => env('NINJA_CONNECT_SECRET', ''),
'internal_queue_enabled' => env('INTERNAL_QUEUE_ENABLED', true),
'ninja_apple_api_key' => env('APPLE_API_KEY', false),
@ -227,5 +227,12 @@ return [
'paypal' => [
'secret' => env('PAYPAL_SECRET', null),
'client_id' => env('PAYPAL_CLIENT_ID', null),
],
'imap_inbound_expense' => [
'servers' => env('IMAP_INBOUND_EXPENSE_SERVERS', ''),
'ports' => env('IMAP_INBOUND_EXPENSE_PORTS', ''),
'users' => env('IMAP_INBOUND_EXPENSE_USERS', ''),
'passwords' => env('IMAP_INBOUND_EXPENSE_PASSWORDS', ''),
'companies' => env('IMAP_INBOUND_EXPENSE_COMPANIES', ''),
]
];

View File

@ -11,14 +11,11 @@ return new class extends Migration {
public function up(): void
{
Schema::table('company', function (Blueprint $table) {
$table->string("expense_mailbox_imap_host")->nullable();
$table->string("expense_mailbox_imap_port")->nullable();
$table->string("expense_mailbox_imap_user")->nullable();
$table->string("expense_mailbox_imap_password")->nullable();
$table->string("expense_import")->default(true);
});
Schema::table('vendor', function (Blueprint $table) {
$table->string("expense_sender_email")->nullable();
$table->string("expense_sender_url")->nullable();
$table->string("expense_sender_domain")->nullable();
});
}