UNPKG

dynamicsmobile

Version:

Allows development of off-line mobile and web business apps over the Dynamics Mobile platform. More info on https://www.dynamicsmobile.com

281 lines 13.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NotificationService = void 0; const tslib_1 = require("tslib"); const application_context_service_1 = require("./application-context-service"); const dms_platform_bridge_factory_1 = require("../platform/dms-platform-bridge-factory"); const injectable_1 = require("../ioc/injectable"); const dms_api_wrapper_1 = require("../platform-web/dms-api-wrapper"); let NotificationService = exports.NotificationService = class NotificationService { constructor(dms) { this.dms = dms; } sendSms(recipient, content) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const appArea = yield (0, dms_platform_bridge_factory_1.PlatformBridgeFactory)(this.dms).execute("getApparea", null); const token = yield (0, dms_platform_bridge_factory_1.PlatformBridgeFactory)(this.dms).execute("getSessionKey", null); if (!recipient) { throw new Error('NotificationService.sendSms requires recipient argument'); } // const re = /^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/ // if(!re.test(String(recipient).toLowerCase())){ // throw new Error('NotificationService.sendSms requires valid phone number for recipient argument'); // } if (!content) { throw new Error('NotificationService.sendsms requires content argument'); } if (content.length > 240) { throw new Error('NotificationService.sendsms requires content with length below 240'); } const message = { recipient: recipient, message: { content: Buffer.from(content).toString('base64'), }, channel: 'sms' }; return new Promise((resolve, reject) => { let apiUrl = `${dms_api_wrapper_1.DmsAPIWrapper._baseAPIUrl}/storage/sysentity/Notification/null/funcs/send`; $.ajax({ type: "POST", headers: { "Authorization": `apparea:${appArea};${token}` }, url: apiUrl, contentType: "application/json", data: JSON.stringify(message) }).done(function (data) { resolve(data); }).fail(function (err) { reject(err); }); }); }); } sendEmail(recipient, subject, content, attachments, sender) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const appArea = yield (0, dms_platform_bridge_factory_1.PlatformBridgeFactory)(this.dms).execute("getApparea", null); const token = yield (0, dms_platform_bridge_factory_1.PlatformBridgeFactory)(this.dms).execute("getSessionKey", null); if (!recipient) { throw new Error('NotificationService.sendEmail requires recipient argument'); } const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; if (!re.test(String(recipient).toLowerCase())) { throw new Error('NotificationService.sendEmail requires valid email for recipient argument'); } if (sender) { if (!re.test(String(sender).toLowerCase())) { throw new Error('NotificationService.sendEmail requires valid email for sender argument'); } } if (!subject) { throw new Error('NotificationService.sendEmail requires subject argument'); } if (!content) { throw new Error('NotificationService.sendEmail requires content argument'); } const message = { recipient: recipient, sender: sender, message: { subject: Buffer.from(subject).toString('base64'), content: Buffer.from(content).toString('base64'), }, channel: 'email' }; return new Promise((resolve, reject) => { let apiUrl = `${dms_api_wrapper_1.DmsAPIWrapper._baseAPIUrl}/storage/sysentity/Notification/null/funcs/send`; $.ajax({ type: "POST", headers: { "Authorization": `apparea:${appArea};${token}` }, url: apiUrl, contentType: "application/json", data: JSON.stringify(message) }).done(function (data) { resolve(data); }).fail(function (err) { reject(err); }); }); }); } sendViber(appCode, recipient, type, content) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const appArea = yield (0, dms_platform_bridge_factory_1.PlatformBridgeFactory)(this.dms).execute("getApparea", null); const token = yield (0, dms_platform_bridge_factory_1.PlatformBridgeFactory)(this.dms).execute("getSessionKey", null); if (!appCode) { throw new Error('NotificationService.sendViber requires appCode argument'); } if (!recipient) { throw new Error('NotificationService.sendViber requires recipient argument'); } if (!content) { throw new Error('NotificationService.sendViber requires content argument'); } // if(content.length>120){ // throw new Error('NotificationService.sendsms requires content with length below 120'); // } const message = { recipient: recipient, appCode: appCode, message: { type: type, content: Buffer.from(content).toString('base64'), }, channel: 'viber' }; return new Promise((resolve, reject) => { let apiUrl = `${dms_api_wrapper_1.DmsAPIWrapper._baseAPIUrl}/storage/sysentity/Notification/null/funcs/send`; $.ajax({ type: "POST", headers: { "Authorization": `apparea:${appArea};${token}` }, url: apiUrl, contentType: "application/json", data: JSON.stringify(message) }).done(function (data) { resolve(data); }).fail(function (err) { reject(err); }); }); }); } sendWhatsApp(appCode, recipient, type, content) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const appArea = yield (0, dms_platform_bridge_factory_1.PlatformBridgeFactory)(this.dms).execute("getApparea", null); const token = yield (0, dms_platform_bridge_factory_1.PlatformBridgeFactory)(this.dms).execute("getSessionKey", null); if (!appCode) { throw new Error('NotificationService.sendWhatsApp requires appCode argument'); } if (!recipient) { throw new Error('NotificationService.sendWhatsApp requires recipient argument'); } if (!content) { throw new Error('NotificationService.sendWhatsApp requires content argument'); } // if(content.length>120){ // throw new Error('NotificationService.sendsms requires content with length below 120'); // } const message = { recipient: recipient, appCode: appCode, message: { type: type, content: Buffer.from(typeof (content) == 'object' ? JSON.stringify(content) : content).toString('base64'), }, channel: 'whatsapp' }; return new Promise((resolve, reject) => { let apiUrl = `${dms_api_wrapper_1.DmsAPIWrapper._baseAPIUrl}/storage/sysentity/Notification/null/funcs/send`; $.ajax({ type: "POST", headers: { "Authorization": `apparea:${appArea};${token}` }, url: apiUrl, contentType: "application/json", data: JSON.stringify(message) }).done(function (data) { resolve(data); }).fail(function (err) { reject(err); }); }); }); } sendMessenger(appCode, recipient, type, content) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const appArea = yield (0, dms_platform_bridge_factory_1.PlatformBridgeFactory)(this.dms).execute("getApparea", null); const token = yield (0, dms_platform_bridge_factory_1.PlatformBridgeFactory)(this.dms).execute("getSessionKey", null); if (!appCode) { throw new Error('NotificationService.sendMessenger requires appCode argument'); } if (!recipient) { throw new Error('NotificationService.sendMessenger requires recipient argument'); } if (!content) { throw new Error('NotificationService.sendMessenger requires content argument'); } // if(content.length>120){ // throw new Error('NotificationService.sendsms requires content with length below 120'); // } const message = { recipient: recipient, appCode: appCode, message: { type: type, content: Buffer.from(content).toString('base64'), }, channel: 'messenger' }; return new Promise((resolve, reject) => { let apiUrl = `${dms_api_wrapper_1.DmsAPIWrapper._baseAPIUrl}/storage/sysentity/Notification/null/funcs/send`; $.ajax({ type: "POST", headers: { "Authorization": `apparea:${appArea};${token}` }, url: apiUrl, contentType: "application/json", data: JSON.stringify(message) }).done(function (data) { resolve(data); }).fail(function (err) { reject(err); }); }); }); } sendDevicePush(recipient, type, content) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const appArea = yield (0, dms_platform_bridge_factory_1.PlatformBridgeFactory)(this.dms).execute("getApparea", null); const token = yield (0, dms_platform_bridge_factory_1.PlatformBridgeFactory)(this.dms).execute("getSessionKey", null); if (!recipient) { throw new Error('NotificationService.sendDevicePush requires recipient argument'); } const types = ['custommessage', 'sync', 'wipeout']; if (types.indexOf(type) < 0) { throw new Error(`${types.join(',')}`); } if (!content) { throw new Error('NotificationService.sendDevicePush requires content argument'); } if (content.length > 1000) { throw new Error('NotificationService.sendDevicePush requires content with length below 1000'); } const message = { recipient: recipient, message: { type: type, content: Buffer.from(content).toString('base64'), }, channel: 'push' }; return new Promise((resolve, reject) => { let apiUrl = `${dms_api_wrapper_1.DmsAPIWrapper._baseAPIUrl}/storage/sysentity/Notification/null/funcs/send`; $.ajax({ type: "POST", headers: { "Authorization": `apparea:${appArea};${token}` }, url: apiUrl, contentType: "application/json", data: JSON.stringify(message) }).done(function (data) { resolve(data); }).fail(function (err) { reject(err); }); }); }); } }; exports.NotificationService = NotificationService = tslib_1.__decorate([ (0, injectable_1.Injectable)(), tslib_1.__metadata("design:paramtypes", [application_context_service_1.DmsApplicationService]) ], NotificationService); //# sourceMappingURL=notification-service.js.map