3 var VoiceContext = require('../../models').VoiceContext;
4 var fs = require('fs');
5 var util = require('util');
6 var path = require('path');
7 var config = require('../../config/environment');
9 exports.register = function(ami) {
10 VoiceContext.beforeUpdate(function(updatedContext) {
11 if (updatedContext.changed('name')) {
12 throw new Error("You can't modify a context name");
14 if (updatedContext.defaultEntry) {
15 throw new Error("You can't modify a default context");
19 VoiceContext.beforeDelete(function(context) {
20 if (context.defaultEntry) {
21 throw new Error("You can't delete a default context");
25 VoiceContext.afterCreate(function(doc) {
26 rewriteContexts(doc, ami, false);
29 VoiceContext.afterDelete(function(doc) {
30 rewriteContexts(doc, ami, true);
34 function rewriteContexts(doc, ami, deleteContext) {
35 var contextValues = '';
37 contextValues = util.format('[%s]\nswitch => Realtime\n', doc.name);
47 .then(function(contexts) {
48 contexts.forEach(function(element) {
49 contextValues += util.format('[%s]\nswitch => Realtime\n', element.name);
51 fs.writeFile(path.join(config.root, 'server/files/asterisk/sip_xcally_contexts.conf'), contextValues, {
53 mode: parseInt('0777', 8)
56 console.log("Contexts file saved!");
59 }, function(err, res) {
68 .catch(function(err) {