diff --git a/app/Models/MailSender.php b/app/Models/MailSender.php index 625baaf6..1dedd9ab 100644 --- a/app/Models/MailSender.php +++ b/app/Models/MailSender.php @@ -25,11 +25,6 @@ class MailSender extends Model return $this->belongsTo(Company::class); } - public function setSettingsAttribute($value) - { - $this->attributes['settings'] = json_encode($value); - } - public function scopeWhereOrder($query, $orderByField, $orderBy) { $query->orderBy($orderByField, $orderBy); diff --git a/database/migrations/2023_03_10_125202_create_mail_senders_table.php b/database/migrations/2023_03_10_125202_create_mail_senders_table.php index 4573a593..8e1cf725 100644 --- a/database/migrations/2023_03_10_125202_create_mail_senders_table.php +++ b/database/migrations/2023_03_10_125202_create_mail_senders_table.php @@ -32,62 +32,54 @@ class CreateMailSendersTable extends Migration $table->timestamps(); }); - $user = User::where('role', 'super admin')->first(); + $users = User::where('role', 'super admin')->get(); - if ($user) { - $users = User::where('role', 'super admin')->get(); + foreach ($users as $user) { + BouncerFacade::allow($user)->toManage(MailSender::class); + } - foreach ($users as $user) { - $user->allow('view-mail-sender'); - $user->allow('create-mail-sender'); - $user->allow('edit-mail-sender'); - $user->allow('delete-mail-sender'); - BouncerFacade::allow($user)->toOwn(MailSender::class); + $companies = Company::all(); + + $companies->map(function ($company) { + if (env('MAIL_DRIVER') == 'smtp') { + $settings = [ + 'MAIL_HOST' => env('MAIL_HOST'), + 'MAIL_PORT' => env('MAIL_PORT'), + 'MAIL_USERNAME' => env('MAIL_USERNAME'), + 'MAIL_PASSWORD' => env('MAIL_PASSWORD'), + 'MAIL_ENCRYPTION' => env('MAIL_ENCRYPTION') + ]; + $this->createSender($settings, $company->id); } - $companies = Company::all(); + if (env('MAIL_DRIVER') == 'mail' || env('MAIL_DRIVER') == 'sendmail') { + $this->createSender(null, $company->id); + } - $companies->map(function ($company) { - if (env('MAIL_DRIVER') == 'smtp') { - $settings = [ - 'MAIL_HOST' => env('MAIL_HOST'), - 'MAIL_PORT' => env('MAIL_PORT'), - 'MAIL_USERNAME' => env('MAIL_USERNAME'), - 'MAIL_PASSWORD' => env('MAIL_PASSWORD'), - 'MAIL_ENCRYPTION' => env('MAIL_ENCRYPTION') - ]; - $this->insertData($settings, $company->id); - } + if (env('MAIL_DRIVER') == 'mailgun') { + $settings = [ + 'MAILGUN_DOMAIN' => env('MAILGUN_DOMAIN'), + 'MAILGUN_SECRET' => env('MAILGUN_SECRET'), + 'MAILGUN_ENDPOINT' => env('MAILGUN_ENDPOINT'), + ]; + $this->createSender($settings, $company->id); + } - if (env('MAIL_DRIVER') == 'mail' || env('MAIL_DRIVER') == 'sendmail') { - $this->insertData(null, $company->id); - } - - if (env('MAIL_DRIVER') == 'mailgun') { - $settings = [ - 'MAILGUN_DOMAIN' => env('MAILGUN_DOMAIN'), - 'MAILGUN_SECRET' => env('MAILGUN_SECRET'), - 'MAILGUN_ENDPOINT' => env('MAILGUN_ENDPOINT'), - ]; - $this->insertData($settings, $company->id); - } - - if (env('MAIL_DRIVER') == 'ses') { - $settings = [ - 'MAIL_HOST' => env('MAIL_HOST'), - 'MAIL_PORT' => env('MAIL_PORT'), - 'MAIL_ENCRYPTION' => env('MAIL_ENCRYPTION'), - 'MAILGUN_DOMAIN' => env('MAILGUN_DOMAIN'), - 'SES_KEY' => env('SES_KEY'), - 'SES_SECRET' => env('SES_SECRET'), - ]; - $this->insertData($settings, $company->id); - } - }); - } + if (env('MAIL_DRIVER') == 'ses') { + $settings = [ + 'MAIL_HOST' => env('MAIL_HOST'), + 'MAIL_PORT' => env('MAIL_PORT'), + 'MAIL_ENCRYPTION' => env('MAIL_ENCRYPTION'), + 'MAILGUN_DOMAIN' => env('MAILGUN_DOMAIN'), + 'SES_KEY' => env('SES_KEY'), + 'SES_SECRET' => env('SES_SECRET'), + ]; + $this->createSender($settings, $company->id); + } + }); } - public function insertData($settings, $company_id) + public function createSender($settings, $company_id) { $data = [ 'name' => env('MAIL_DRIVER'), diff --git a/resources/scripts/admin/stores/mail-sender.js b/resources/scripts/admin/stores/mail-sender.js index cfaed799..eff7076d 100644 --- a/resources/scripts/admin/stores/mail-sender.js +++ b/resources/scripts/admin/stores/mail-sender.js @@ -68,7 +68,7 @@ export const useMailSenderStore = (useWindow = false) => { fetchMailSenders(params) { return new Promise((resolve, reject) => { axios - .get(`/api/v1/mail-sender`, { params }) + .get(`/api/v1/mail-senders`, { params }) .then((response) => { this.mailSenders = response.data.data resolve(response) @@ -83,7 +83,7 @@ export const useMailSenderStore = (useWindow = false) => { fetchMailSender(id) { return new Promise((resolve, reject) => { axios - .get(`/api/v1/mail-sender/${id}`) + .get(`/api/v1/mail-senders/${id}`) .then((response) => { this.currentMailSender = response.data.data if (response.data.data.settings) { @@ -116,7 +116,7 @@ export const useMailSenderStore = (useWindow = false) => { const notificationStore = useNotificationStore() return new Promise((resolve, reject) => { axios - .post('/api/v1/mail-sender', data) + .post('/api/v1/mail-senders', data) .then((response) => { this.mailSenders.push(response.data.data) notificationStore.showNotification({ @@ -136,7 +136,7 @@ export const useMailSenderStore = (useWindow = false) => { const notificationStore = useNotificationStore() return new Promise((resolve, reject) => { axios - .put(`/api/v1/mail-sender/${data.id}`, data) + .put(`/api/v1/mail-senders/${data.id}`, data) .then((response) => { if (response.data) { let pos = this.mailSenders.findIndex( @@ -160,7 +160,7 @@ export const useMailSenderStore = (useWindow = false) => { deleteMailSender(id) { return new Promise((resolve, reject) => { axios - .delete(`/api/v1/mail-sender/${id}`) + .delete(`/api/v1/mail-senders/${id}`) .then((response) => { if (response.data.success) { let index = this.mailSenders.findIndex( diff --git a/routes/api.php b/routes/api.php index 579636ae..1a057a9e 100644 --- a/routes/api.php +++ b/routes/api.php @@ -403,14 +403,12 @@ Route::prefix('/v1')->group(function () { // Mails //---------------------------------- - Route::apiResource('mail-sender', MailSenderController::class); + Route::apiResource('mail-senders', MailSenderController::class); Route::get('/mail-drivers', [MailConfigurationController::class, 'getMailDrivers']); Route::post('/mail-test', [MailConfigurationController::class, 'TestMailDriver']); - Route::get('mail-senders', GetAllMailSendersController::class); - Route::get('/company/mail/config', GetCompanyMailConfigurationController::class); Route::apiResource('notes', NotesController::class);