UNPKG

onlinepbx-moysklad

Version:

Модуль для CRM-адаптера к OnlinePBX

175 lines (158 loc) 4.8 kB
'use strict' const mc = require('minico') const path = require('path') const nock = require('nock') const test = require('blue-tape') const { Core } = require('scaleflow') const onlinepbxMoysklad = require('..') const { CONTACT_INFO, HTTP_ACTION, LIST_USERS } = require('onlinepbx-crm-adapter-actions') // Mock log const loggerInitializer = (_, { instance }) => Object.assign(instance, { log () {} }) const getScope = (type, query) => nock('https://online.moysklad.ru') .get(`/api/remap/1.1/entity/${type}`) .query(query) .replyWithFile(200, path.resolve(__dirname, `res/${type}.json`)) nock.emitter.on('no match', req => { console.log('no match:', req) }) test('onlinepbx-moysklad', mc(function * (t) { let scope let result let TestCore = Core.compose(onlinepbxMoysklad).init(loggerInitializer) let core = TestCore() // - // t.comment('CONTACT_INFO - company') scope = getScope('counterparty', { expand: 'contactpersons', search: '8922 609-07-05' }) result = yield core.dispatch({ type: CONTACT_INFO, payload: { phone: '8922 609-07-05' } }) t.ok(result) t.deepEqual(result, { contact: { email: 'mvv@vensi.me', id: 'd04f8c38-a83f-4117-9802-e420dc531555', name: 'Макеев Виталий Вячеславович (ФЛ) [4]', url: 'https://online.moysklad.ru/app/#company/view?id=' + 'd04f8c38-a83f-4117-9802-e420dc531555' } }) scope.done() // - // t.comment('CONTACT_INFO - contact in company') scope = getScope('counterparty', { expand: 'contactpersons', search: '3521782' }) result = yield core.dispatch({ type: CONTACT_INFO, payload: { phone: '3521782' } }) t.ok(result) t.deepEqual(result, { company: { email: 'mvv@vensi.me', id: 'd04f8c38-a83f-4117-9802-e420dc531555', title: 'Макеев Виталий Вячеславович (ФЛ) [4]', url: 'https://online.moysklad.ru/app/#company/view?' + 'id=d04f8c38-a83f-4117-9802-e420dc531555' }, contact: { email: 'mvv@vensi.me', id: '4e36970d-1f92-4b76-8ab2-f995d029f729', name: 'Виталий', url: 'https://online.moysklad.ru/app/#company/view?' + 'id=d04f8c38-a83f-4117-9802-e420dc531555' } }) scope.done() // - // t.comment('CONTACT_INFO - no result') scope = getScope('counterparty', { expand: 'contactpersons', search: '705' }) result = yield core.dispatch({ type: CONTACT_INFO, payload: { phone: '705' } }) t.equal(result, null) scope.done() t.comment('LIST_USERS') scope = getScope('employee', {}) result = yield core.dispatch({ type: LIST_USERS }) t.ok(result) t.deepEqual(result, [ { id: '005323cb-83ac-11e6-7a31-d0fd002ef703', name: 'Игнатьева Т. М.', phone: '100' } ]) scope.done() // - // t.comment('HTTP_ACTION - error if no action specified') result = yield core.dispatch({ type: HTTP_ACTION, payload: { caller_name: 'Rostelecom', caller_number: '83433521782' } }).catch(err => err.message) t.equal(result, 'Http command action not specified') // - // t.comment('HTTP_ACTION - get contact name') scope = getScope('counterparty', { expand: 'contactpersons', search: '83433521782' }) result = yield core.dispatch({ type: HTTP_ACTION, payload: { action: 'set_name', caller_name: 'Rostelecom', caller_number: '83433521782' } }) t.equal(result, 'set_name: "Макеев Виталий Вячеславович (ФЛ) [4]"') scope.done() // - // t.comment('HTTP_ACTION - no result #1') scope = getScope('counterparty', { expand: 'contactpersons', search: '456' }) result = yield core.dispatch({ type: HTTP_ACTION, payload: { action: 'set_name', caller_name: 'Rostelecom', caller_number: '456' } }) t.equal(result, 'set_name: "Rostelecom"') scope.done() // - // t.comment('HTTP_ACTION - no result #2') scope = getScope('counterparty', { expand: 'contactpersons', search: '456' }) result = yield core.dispatch({ type: HTTP_ACTION, payload: { action: 'set_name', caller_number: '456' } }) t.equal(result, 'set_name: "Unknown"') scope.done() // - // t.comment('HTTP_ACTION - get contact name (translit)') scope = getScope('counterparty', { expand: 'contactpersons', search: '83433521782' }) core.options.translitHttpSetNameCommand = true result = yield core.dispatch({ type: HTTP_ACTION, payload: { action: 'set_name', caller_name: 'Rostelecom', caller_number: '83433521782' } }) t.equal(result, 'set_name: "Makeev Vytalyi Viacheslavovych FL 4"') scope.done() delete core.options.translitHttpSetNameCommand }))