Merged in OS-137-mail-template-paths (pull request #108)

OS-137 mail template paths
This commit is contained in:
Ivan Diaz 2017-01-04 17:41:42 -03:00
commit f9194b1b03
11 changed files with 192 additions and 0 deletions

View File

@ -6,6 +6,9 @@ require_once 'system/add-department.php';
require_once 'system/edit-department.php';
require_once 'system/delete-department.php';
require_once 'system/get-logs.php';
require_once 'system/get-mail-templates.php';
require_once 'system/edit-mail-template.php';
require_once 'system/recover-mail-template.php';
$systemControllerGroup = new ControllerGroup();
$systemControllerGroup->setGroupPath('/system');
@ -17,5 +20,8 @@ $systemControllerGroup->addController(new AddDepartmentController);
$systemControllerGroup->addController(new EditDepartmentController);
$systemControllerGroup->addController(new DeleteDepartmentController);
$systemControllerGroup->addController(new GetLogsController);
$systemControllerGroup->addController(new GetMailTemplatesController);
$systemControllerGroup->addController(new EditMailTemplateController);
$systemControllerGroup->addController(new RecoverMailTemplateController);
$systemControllerGroup->finalize();

View File

@ -0,0 +1,49 @@
<?php
use Respect\Validation\Validator as DataValidator;
class EditMailTemplateController extends Controller {
const PATH = '/edit-mail-template';
public function validations() {
return [
'permission' => 'staff_3',
'requestData' => [
'templateType' => [
'validation' => DataValidator::length(4),
'error' => ERRORS::INVALID_TEMPLATE
],
'language' => [
'validation' => DataValidator::length(2, 2),
'error' => ERRORS::INVALID_LANGUAGE
],
'subject' => [
'validation' => DataValidator::length(4),
'error' => ERRORS::INVALID_SUBJECT
],
'body' => [
'validation' => DataValidator::length(4),
'error' => ERRORS::INVALID_BODY
]
]
];
}
public function handler() {
$language = Controller::request('language');
$templateType = Controller::request('templateType');
$subject = Controller::request('subject');
$body = Controller::request('body');
$mailTemplate = MailTemplate::findOne(' language = ? AND type = ?', [$language, $templateType]);
if($mailTemplate->isNull()) {
Response::respondError(ERRORS::INVALID_TEMPLATE);
return;
}
$mailTemplate->subject = $subject;
$mailTemplate->body = $body;
$mailTemplate->store();
Response::respondSuccess();
}
}

View File

@ -0,0 +1,18 @@
<?php
use Respect\Validation\Validator as DataValidator;
class GetMailTemplatesController extends Controller {
const PATH = '/get-mail-templates';
public function validations() {
return [
'permission' => 'staff_3',
'requestData' => []
];
}
public function handler() {
Response::respondSuccess(MailTemplate::getAll()->toArray());
}
}

View File

@ -0,0 +1,43 @@
<?php
use Respect\Validation\Validator as DataValidator;
class RecoverMailTemplateController extends Controller {
const PATH = '/recover-mail-template';
public function validations() {
return [
'permission' => 'staff_3',
'requestData' => [
'templateType' => [
'validation' => DataValidator::length(4),
'error' => ERRORS::INVALID_TEMPLATE
],
'language' => [
'validation' => DataValidator::length(2, 2),
'error' => ERRORS::INVALID_LANGUAGE
],
]
];
}
public function handler() {
$type = Controller::request('templateType');
$language = Controller::request('language');
$mailTemplate = MailTemplate::findOne(' language = ? AND type = ?', [$language, $type]);
if($mailTemplate->isNull()) {
Response::respondError(ERRORS::INVALID_TEMPLATE);
return;
}
$defaultTemplates = InitialMails::retrieve();
$mailTemplate->body = $defaultTemplates[$type][$language]['body'] ;
$mailTemplate->subject = $defaultTemplates[$type][$language]['subject'] ;
$mailTemplate->store();
Response::respondSuccess();
}
}

View File

@ -32,4 +32,7 @@ class ERRORS {
const SAME_DEPARTMENT = 'SAME_DEPARTMENT';
const INVALID_TOKEN = 'INVALID_TOKEN';
const UNVERIFIED_USER = 'UNVERIFIED_USER';
const INVALID_TEMPLATE = 'INVALID_TEMPLATE';
const INVALID_SUBJECT = 'INVALID_SUBJECT';
const INVALID_BODY = 'INVALID_BODY';
}

View File

@ -36,6 +36,12 @@ abstract class DataStore {
return DataStoreList::getList(ucfirst(static::TABLE), $beanList);
}
public static function findOne($query = '', $matches = []) {
$bean = RedBean::findOne(static::TABLE, $query, $matches);
return ($bean) ? new static($bean) : new NullDataStore();
}
private static function validateProp($propToValidate) {
$validProp = false;

View File

@ -46,4 +46,12 @@ class MailTemplate extends DataStore {
return $compiledString;
}
public function toArray() {
return [
'type' => $this->type,
'subject' => $this->subject,
'language' => $this->language,
'body' => $this->body,
];
}
}

View File

@ -48,3 +48,6 @@ require './system/add-department.rb'
require './system/edit-department.rb'
require './system/delete-department.rb'
require './staff/last-events.rb'
require './system/get-mail-templates.rb'
require './system/edit-mail-template.rb'
require './system/recover-mail-template.rb'

View File

@ -0,0 +1,22 @@
describe'system/edit-mail-template' do
request('/user/logout')
Scripts.login($staff[:email], $staff[:password], true)
it 'should edit mail template' do
result= request('/system/edit-mail-template', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token,
language: 'en',
templateType: 'USER_SIGNUP',
subject: 'new subject',
body: 'new message'
})
(result['status']).should.equal('success')
row = $database.getRow('mailtemplate', 1, 'id')
(row['subject']).should.equal('new subject')
(row['body']).should.equal('new message')
end
end

View File

@ -0,0 +1,15 @@
describe'system/get-mail-templates' do
request('/user/logout')
Scripts.login($staff[:email], $staff[:password], true)
it 'should get mail templates' do
result= request('/system/get-mail-templates', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token
})
(result['status']).should.equal('success')
(result['data'].size).should.equal(10)
end
end

View File

@ -0,0 +1,19 @@
describe'system/recover-mail-template' do
request('/user/logout')
Scripts.login($staff[:email], $staff[:password], true)
it 'should recover mail template' do
result= request('/system/recover-mail-template', {
csrf_userid: $csrf_userid,
csrf_token: $csrf_token,
language: 'en',
templateType: 'USER_SIGNUP',
})
(result['status']).should.equal('success')
row = $database.getRow('mailtemplate', 1, 'id')
(row['subject']).should.equal('Signup {{to}} - OpenSupports')
end
end