From 1a600f4c61b2b31db1cf05bf48384eeed95da5f8 Mon Sep 17 00:00:00 2001 From: AntonyAntonio Date: Tue, 3 Jan 2017 19:36:26 -0300 Subject: [PATCH 1/3] Guillermo - path get-mail-templates [skip ci] --- server/controllers/system.php | 2 ++ .../controllers/system/edit-mail-templates.php | 18 ++++++++++++++++++ .../controllers/system/get-mail-templates.php | 18 ++++++++++++++++++ .../system/recover-mail-template.php | 18 ++++++++++++++++++ server/models/MailTemplate.php | 8 ++++++++ 5 files changed, 64 insertions(+) create mode 100644 server/controllers/system/edit-mail-templates.php create mode 100644 server/controllers/system/get-mail-templates.php create mode 100644 server/controllers/system/recover-mail-template.php diff --git a/server/controllers/system.php b/server/controllers/system.php index b815bca7..5e719e16 100644 --- a/server/controllers/system.php +++ b/server/controllers/system.php @@ -6,6 +6,7 @@ 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'; $systemControllerGroup = new ControllerGroup(); $systemControllerGroup->setGroupPath('/system'); @@ -17,5 +18,6 @@ $systemControllerGroup->addController(new AddDepartmentController); $systemControllerGroup->addController(new EditDepartmentController); $systemControllerGroup->addController(new DeleteDepartmentController); $systemControllerGroup->addController(new GetLogsController); +$systemControllerGroup->addController(new GetMailTemplatesController); $systemControllerGroup->finalize(); \ No newline at end of file diff --git a/server/controllers/system/edit-mail-templates.php b/server/controllers/system/edit-mail-templates.php new file mode 100644 index 00000000..a6e26b60 --- /dev/null +++ b/server/controllers/system/edit-mail-templates.php @@ -0,0 +1,18 @@ + 'staff_3', + 'requestData' => [] + ]; + } + + public function handler() { + Response::respondSuccess(); + + } +} \ No newline at end of file diff --git a/server/controllers/system/get-mail-templates.php b/server/controllers/system/get-mail-templates.php new file mode 100644 index 00000000..b0d0a46a --- /dev/null +++ b/server/controllers/system/get-mail-templates.php @@ -0,0 +1,18 @@ + 'staff_3', + 'requestData' => [] + ]; + } + + public function handler() { + Response::respondSuccess(MailTemplate::getAll()->toArray()); + + } +} \ No newline at end of file diff --git a/server/controllers/system/recover-mail-template.php b/server/controllers/system/recover-mail-template.php new file mode 100644 index 00000000..772a512d --- /dev/null +++ b/server/controllers/system/recover-mail-template.php @@ -0,0 +1,18 @@ + 'staff_3', + 'requestData' => [] + ]; + } + + public function handler() { + Response::respondSuccess(); + + } +} \ No newline at end of file diff --git a/server/models/MailTemplate.php b/server/models/MailTemplate.php index 55ad8a8e..a2931061 100644 --- a/server/models/MailTemplate.php +++ b/server/models/MailTemplate.php @@ -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, + ]; + } } \ No newline at end of file From 51357c2cd84a36410786021bc6b94dd0c5497801 Mon Sep 17 00:00:00 2001 From: AntonyAntonio Date: Tue, 3 Jan 2017 20:35:46 -0300 Subject: [PATCH 2/3] Guillermo - path edit/recover-mail-templates [skip ci] --- server/controllers/system.php | 4 ++ .../controllers/system/edit-mail-template.php | 49 +++++++++++++++++++ .../system/edit-mail-templates.php | 18 ------- .../system/recover-mail-template.php | 27 +++++++++- server/data/ERRORS.php | 3 ++ server/models/DataStore.php | 6 +++ 6 files changed, 88 insertions(+), 19 deletions(-) create mode 100644 server/controllers/system/edit-mail-template.php delete mode 100644 server/controllers/system/edit-mail-templates.php diff --git a/server/controllers/system.php b/server/controllers/system.php index 5e719e16..2cde4fe2 100644 --- a/server/controllers/system.php +++ b/server/controllers/system.php @@ -7,6 +7,8 @@ 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'); @@ -19,5 +21,7 @@ $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(); \ No newline at end of file diff --git a/server/controllers/system/edit-mail-template.php b/server/controllers/system/edit-mail-template.php new file mode 100644 index 00000000..cb916a86 --- /dev/null +++ b/server/controllers/system/edit-mail-template.php @@ -0,0 +1,49 @@ + '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(); + + } +} \ No newline at end of file diff --git a/server/controllers/system/edit-mail-templates.php b/server/controllers/system/edit-mail-templates.php deleted file mode 100644 index a6e26b60..00000000 --- a/server/controllers/system/edit-mail-templates.php +++ /dev/null @@ -1,18 +0,0 @@ - 'staff_3', - 'requestData' => [] - ]; - } - - public function handler() { - Response::respondSuccess(); - - } -} \ No newline at end of file diff --git a/server/controllers/system/recover-mail-template.php b/server/controllers/system/recover-mail-template.php index 772a512d..9fdff9be 100644 --- a/server/controllers/system/recover-mail-template.php +++ b/server/controllers/system/recover-mail-template.php @@ -7,11 +7,36 @@ class RecoverMailTemplateController extends Controller { public function validations() { return [ 'permission' => 'staff_3', - 'requestData' => [] + '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(); } diff --git a/server/data/ERRORS.php b/server/data/ERRORS.php index 2dbe9857..e0052543 100644 --- a/server/data/ERRORS.php +++ b/server/data/ERRORS.php @@ -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'; } diff --git a/server/models/DataStore.php b/server/models/DataStore.php index dbcd93ac..e280ac13 100644 --- a/server/models/DataStore.php +++ b/server/models/DataStore.php @@ -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; From d7cd5086bd1dae3e3be4bada1f859b9a894363b8 Mon Sep 17 00:00:00 2001 From: AntonyAntonio Date: Tue, 3 Jan 2017 21:06:45 -0300 Subject: [PATCH 3/3] Guillermo - tests get/edit/recover-mail-templates [skip ci] --- tests/init.rb | 3 +++ tests/system/edit-mail-template.rb | 22 ++++++++++++++++++++++ tests/system/get-mail-templates.rb | 15 +++++++++++++++ tests/system/recover-mail-template.rb | 19 +++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 tests/system/edit-mail-template.rb create mode 100644 tests/system/get-mail-templates.rb create mode 100644 tests/system/recover-mail-template.rb diff --git a/tests/init.rb b/tests/init.rb index a300a6fd..3eb51877 100644 --- a/tests/init.rb +++ b/tests/init.rb @@ -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' diff --git a/tests/system/edit-mail-template.rb b/tests/system/edit-mail-template.rb new file mode 100644 index 00000000..da539e53 --- /dev/null +++ b/tests/system/edit-mail-template.rb @@ -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 diff --git a/tests/system/get-mail-templates.rb b/tests/system/get-mail-templates.rb new file mode 100644 index 00000000..8e067655 --- /dev/null +++ b/tests/system/get-mail-templates.rb @@ -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 diff --git a/tests/system/recover-mail-template.rb b/tests/system/recover-mail-template.rb new file mode 100644 index 00000000..0335da96 --- /dev/null +++ b/tests/system/recover-mail-template.rb @@ -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