Advertisement
Guest User

Untitled

a guest
Oct 4th, 2016
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.92 KB | None | 0 0
  1. define(['knockout', 'http', 'knockout-validation', 'text!./client-detail.html', './client-model', 'knockout-mapping', 'dialog-helper', 'underscore', 'dictionaries', 'constants', 'q', 'navigator', 'selectize','moment','clip'],
  2. function (ko, http, koValid, templateMarkup, ClientModel, koMap, dialogManager, _, dictionaries, constants, Q, navigator, selectize,moment,clip) {
  3.  
  4. function ClientDetailViewModel(params) {
  5.  
  6. var that = this;
  7.  
  8. // #region Properties
  9.  
  10. this.clientType = ko.observable(params.clientType);
  11.  
  12. this.client = new ClientModel();
  13. this.clientWithValidation = ko.validatedObservable(that.client);
  14. this.clientFinancialWithValidation = ko.validatedObservable(that.client.ClientFinancialDetail);
  15. this.companyTypes = ko.observableArray([]);
  16. this.companySubTypes = ko.observableArray([]);
  17. this.paymentTerms = ko.observableArray([]);
  18. this.currencies = ko.observableArray([]);
  19. this.paymentStatus = ko.observableArray([]);
  20.  
  21. this.QuestionnaireAnswers = ko.observableArray([]);
  22. this.questions = ko.observableArray([]);
  23. this.affiliateManagers = ko.observableArray([]);
  24. this.Countries = ko.observableArray([]);
  25. this.campaignList = null;
  26.  
  27. this.linkWithoutCode = ko.observable();
  28. this.linkWithoutCode2 = ko.observable();
  29. this.isLinkActive = ko.observable(false);
  30. this.isLinkActive2 = ko.observable(false);
  31. this.pnTrackingLinkId = ko.observable();
  32. this.pnTrackingLinkId2 = ko.observable();
  33. this.token = ko.observable();
  34. this.token2 = ko.observable();
  35. this.smartLink = ko.observable();
  36. this.smartLink2 = ko.observable();
  37. this.smartLink3 = ko.observable();
  38. this.anLinkWithoutCode = ko.observable('');
  39. this.DCMLoginPassword = ko.observable();
  40. this.DCMConfirmPassword = ko.observable();
  41. this.FormattedCreatedAt = ko.observable();
  42.  
  43. this.filters = ko.observable();
  44. this.clientStatusName = ko.observable();
  45.  
  46. // #endregion
  47.  
  48.  
  49.  
  50. // #region Form events
  51.  
  52.  
  53. this.approveUser = function () {
  54.  
  55. if (!that.client.AffiliateManagerId()) {
  56. dialogManager.showWarningDialog("Please select an account manager for this client and try again!");
  57. return;
  58. }
  59.  
  60. dialogManager.block();
  61.  
  62. http.makeAjaxRequest('api/ClientsStatus',
  63. 'POST',
  64. { ClientId: that.client.ClientId(), NewClientStatusId: constants.enum_ClientStatus.APPROVED })
  65. .then(function (result) {
  66. if (result && !result.IsSuccess) {
  67. dialogManager.showErrorDialog(result.Errors[0]);
  68. return;
  69. }
  70.  
  71. dialogManager.showSuccessDialog('E-mail sent successfully');
  72. })
  73. .fail(function(error) {
  74. dialogManager.showErrorDialog('An error occurred during e-mail sending');
  75. })
  76. .done(function() {
  77. dialogManager.unblock();
  78. });
  79. }
  80.  
  81.  
  82. this.back = function () {
  83. window.history.back();
  84. }
  85.  
  86. this.save = function () {
  87.  
  88.  
  89. var country = $('#select-tara').selectize()[0].selectize.getValue();
  90.  
  91. if(country != "")
  92. that.client.ClientAddress.CountryId(country);
  93. else
  94. that.client.ClientAddress.CountryId(null);
  95.  
  96. if (!that.clientWithValidation.isValid()) {
  97. return _.each(that.clientWithValidation.errors(), function (err) { dialogManager.showWarningDialog(err); });
  98.  
  99. }
  100.  
  101. if (!that.clientFinancialWithValidation.isValid()) {
  102. return _.each(that.clientFinancialWithValidation.errors(), function (err) { dialogManager.showWarningDialog(err); });
  103.  
  104. }
  105.  
  106. if (that.DCMConfirmPassword() != that.DCMLoginPassword()) {
  107. dialogManager.showWarningDialog('DCypher Media login passwords mismatch');
  108. return;
  109. }
  110.  
  111. //int parsing of Payout Settings
  112. that.client.ClientFinancialDetail.PayoutSettings(parseInt(that.client.ClientFinancialDetail.PayoutSettings()));
  113.  
  114. dialogManager.block();
  115.  
  116. that.client.ClientContact_FinancialContactId
  117. .Email(that.client.ClientContact_FinancialContactId.Email()
  118. ? that.client.ClientContact_FinancialContactId.Email()
  119. : null);
  120.  
  121. that.client.ClientContact_MainContactId.Email(that.client.ClientContact_MainContactId.Email()
  122. ? that.client.ClientContact_MainContactId.Email()
  123. : null);
  124.  
  125. if (!that.client.ClientId()) {
  126. that.client.QuestionnaireAnswers = [];
  127.  
  128. http.odata.post("Clients" + (that.DCMLoginPassword() ? "?dcmLoginPassword=" + that.DCMLoginPassword() + "&dcmConfirmPassword=" + that.DCMConfirmPassword() : ''), koMap.toJS(that.client))
  129. .then(function (data) {
  130.  
  131. if (data.data && typeof data.data == "string")
  132. {
  133. dialogManager.showErrorDialog(data.data);
  134. return;
  135. }
  136.  
  137. //that.getClient(data.data.ClientId);
  138. navigator.navigateTo('clients/' + that.clientType() + '/inactive');
  139. dialogManager.showSuccessDialog('Client created successfully');
  140.  
  141. })
  142. .fail(function (error) {
  143. dialogManager.showErrorDialog(error);
  144. })
  145. .done(function () {
  146. dialogManager.unblock();
  147. });
  148. } else {
  149.  
  150.  
  151.  
  152. http.odata.post("Clients" + (that.DCMLoginPassword() ? "?dcmLoginPassword=" + that.DCMLoginPassword() + "&dcmConfirmPassword=" + that.DCMConfirmPassword() : ''), koMap.toJS(that.client), that.client.ClientId())
  153. .then(function (data) {
  154. if (data.data && typeof data.data == "string") {
  155. dialogManager.showErrorDialog(data.data);
  156. return;
  157. }
  158.  
  159. navigator.navigateTo('clients/' + that.clientType() + '/' + that.clientStatusName());
  160. dialogManager.showSuccessDialog('Client updated successfully');
  161. })
  162. .fail(function (error) {
  163. dialogManager.showErrorDialog(error);
  164. })
  165. .done(function () {
  166. dialogManager.unblock();
  167. });
  168. }
  169. }
  170.  
  171. // #endregion
  172.  
  173. // #region Client entity
  174.  
  175. this.getClient = function (clientId) {
  176.  
  177. http.odata.find("Clients", clientId,
  178. { expand: 'ClientFinancialDetail,SysCompanyType_CompanyTypeId,ClientContact_FinancialContactId,ClientContact_MainContactId,ClientContact_TechnicalContactId,ClientAddress,SysCompanyType_CompanySubTypeId,QuestionnaireAnswers,SysClientStatus' })
  179. .then(function (data) {
  180.  
  181. var mapping = { ignore: "@odata.context" };
  182.  
  183. that.clientStatusName(data.data.SysClientStatus.ClientStatusName.split(' ')[0].toLowerCase());
  184.  
  185. koMap.fromJS(data.data, mapping, that.client);
  186.  
  187. that.FormattedCreatedAt(moment(that.client.CreatedAt()).format('DD/MM/YYYY HH:mm'));
  188. params.id = data.data.CompanySubTypeId;
  189.  
  190. $('#select-tara').selectize()[0].selectize.setValue(that.client.ClientAddress.CountryId());
  191.  
  192.  
  193. if (data.data.QuestionnaireAnswers && data.data.QuestionnaireAnswers.length > 0) {
  194.  
  195. _.each(that.questions(), function (question) {
  196.  
  197. var matchedAnswer = _.find(data.data.QuestionnaireAnswers, function (answer) {
  198.  
  199. return question.SysQuestionnaireQuestionId == answer.SysQuestionnaireQuestionId;
  200. });
  201.  
  202. if (matchedAnswer) {
  203. question.answer(matchedAnswer.AnswerText);
  204. }
  205. });
  206. }
  207.  
  208. })
  209. .fail(function (error) {
  210. dialogManager.showErrorDialog(error);
  211. })
  212. .done(function () {
  213.  
  214. dialogManager.unblock();
  215. });
  216. }
  217.  
  218. // #endregion
  219.  
  220. // #region Dictionaries
  221.  
  222. this.getCompanyTypes = function () {
  223.  
  224. http.odata.get("SysCompanyTypes")
  225. .then(function (data2) {
  226. that.companyTypes(data2.data);
  227. })
  228. .fail(function (err) {
  229. alert(err);
  230. });
  231. }
  232.  
  233. this.getCampaigns = function () {
  234.  
  235. dialogManager.block();
  236.  
  237. http.odata.get("Campaigns", { expand: 'Offer', filter: 'Offer/SysOfferTypeId eq ' +constants.enum_OfferTypes.AN + ' and SysCampaignStatusId eq ' + constants.enum_CampaignStatuses.ACTIVE })
  238. .then(function (data) {
  239.  
  240. that.campaignList = _.map(data.data, (function (campaign) {
  241. return {
  242. id: campaign.CampaignId,
  243. name: campaign.Offer.OfferName
  244. }
  245. }));
  246. that.configCampaignSelector(that.campaignList, []);
  247. })
  248. .fail(function (error) {
  249. dialogManager.showErrorDialog(error);
  250. })
  251. .done(function () {
  252. dialogManager.unblock();
  253. });
  254. }
  255.  
  256. this.loadCompanySubTypes = function (companyTypeId) {
  257.  
  258. if (!companyTypeId) {
  259. that.companySubTypes.removeAll();
  260. return;
  261. }
  262.  
  263. http.odata.find("SysCompanyTypes", companyTypeId)
  264. .then(function (data) {
  265. that.companySubTypes(data.data);
  266. if (params.id) {
  267. that.client.CompanySubTypeId(params.id);
  268. params.id = null;
  269. }
  270. }).
  271. fail(function (error) {
  272. dialogManager.showErrorDialog(error);
  273. });
  274. }
  275.  
  276. // #endregion
  277.  
  278. //#region Selectize Dropwdowns
  279.  
  280. this.configCountrySelector = function (availableData, selectedData) {
  281.  
  282. $('#select-tara').selectize()[0].selectize.destroy();
  283.  
  284. var fromIndex = _.findIndex(availableData, { CountryName: "Worldwide" });
  285. var element = availableData[fromIndex];
  286. availableData.splice(fromIndex, 1);
  287. availableData.splice(0, 0, element);
  288. var selectedItems = _.map(selectedData, (function (d) {
  289. return d.CountryId();
  290. }));
  291.  
  292. $('#select-tara').selectize({
  293. options: availableData,
  294. valueField: 'CountryId',
  295. labelField: 'CountryName',
  296. maxItems: 1,
  297. delimiter: ',',
  298. persist: false,
  299. searchField: 'CountryName',
  300. });
  301.  
  302. $('#select-tara').selectize()[0].selectize.setValue(selectedItems);
  303.  
  304. }
  305.  
  306. this.configCampaignSelector = function (availableData, selectedData) {
  307.  
  308. selectize.create("#select-campaign", { values: availableData, maxItems: 1 });
  309.  
  310. }
  311.  
  312. //#endregion
  313.  
  314. //#region Misc
  315.  
  316. this.populateNavigationProperties = function () {
  317.  
  318.  
  319. that.offer.SysCountries(_.filter(that.Countries(), function (rt) {
  320. return _.contains($('#select-tara').selectize()[0].selectize.items, rt.CountryId.toString());
  321. }));
  322. }
  323.  
  324. //#endregion
  325.  
  326. this.getAppSettings = function (key, param) {
  327.  
  328. dialogManager.block();
  329. http.makeAjaxRequest('api/AppSettings', 'GET', { settingKey: key })
  330. .then(function (result) {
  331. param(result);
  332. }).fail(function (error) {
  333. dialogManager.showErrorDialog(error);
  334. })
  335. .done(function () {
  336. dialogManager.unblock();
  337. });
  338. }
  339.  
  340.  
  341. this.getLinkStatus = function (clientId) {
  342. http.odata.get("PNTrackingLinks?clientId="+clientId,
  343. {
  344. filter: 'SysVerticalTypeId eq ' + constants.enum_VerticalTypes.ADULT
  345.  
  346. })
  347.  
  348. .then(function (data) {
  349.  
  350. if (data.data[0] == undefined) {
  351. that.isLinkActive(false);
  352. that.linkWithoutCode(that.smartLink());
  353. }
  354. if ((data.data[0] != undefined) && (data.data[0].WithCode == true)) {
  355. that.token(data.data[0].Token);
  356. that.pnTrackingLinkId(data.data[0].PNTrackingLinkId);
  357. }
  358. if ((data.data[0] != undefined) && (data.data[0].IsActive == true)) {
  359. that.isLinkActive(true);
  360. that.token(data.data[0].Token);
  361. that.linkWithoutCode(that.smartLink() + '/?tracking_id=' + that.token());
  362. that.pnTrackingLinkId(data.data[0].PNTrackingLinkId);
  363. }
  364.  
  365. }
  366.  
  367. )
  368. .fail(function (error) {
  369. dialogManager.showErrorDialog(error);
  370. })
  371. .done(function () {
  372. dialogManager.unblock();
  373. });
  374. }
  375.  
  376.  
  377. this.getLinkStatus2 = function (clientId) {
  378. http.odata.get("PNTrackingLinks?clientId=" + clientId,
  379. {
  380. filter: 'SysVerticalTypeId eq ' + constants.enum_VerticalTypes.MAIN
  381.  
  382. })
  383.  
  384. .then(function (data) {
  385.  
  386. if (data.data[0] == undefined) {
  387. that.isLinkActive2(false);
  388. that.linkWithoutCode2(that.smartLink2());
  389. }
  390. if ((data.data[0] != undefined) && (data.data[0].WithCode == true)) {
  391. that.token2(data.data[0].Token);
  392. that.pnTrackingLinkId2(data.data[0].PNTrackingLinkId);
  393. }
  394. if ((data.data[0] != undefined) && (data.data[0].IsActive == true)) {
  395. that.isLinkActive2(true);
  396. that.token2(data.data[0].Token);
  397. that.linkWithoutCode2(that.smartLink2() + '/?tracking_id=' + that.token2());
  398. that.pnTrackingLinkId2(data.data[0].PNTrackingLinkId);
  399. }
  400.  
  401. }
  402.  
  403. )
  404. .fail(function (error) {
  405. dialogManager.showErrorDialog(error);
  406. })
  407. .done(function () {
  408. dialogManager.unblock();
  409. });
  410. }
  411.  
  412. ///////////////////////////////
  413. this.onActivateLink = function (data) {
  414. dialogManager.block();
  415.  
  416. http.odata.post("PNTrackingLinks", {
  417. ClientId: that.client.ClientId(),
  418. SysVerticalTypeId: parseInt(constants.enum_VerticalTypes.ADULT),
  419. IsActive: true,
  420. WithCode: false
  421. })
  422. .then(function (data) {
  423. dialogManager.showSuccessDialog('Link activated successfully');
  424. that.getLinkStatus(that.client.ClientId());
  425. })
  426. .fail(function (error) {
  427. dialogManager.showErrorDialog(error);
  428. })
  429. .done(function () {
  430. dialogManager.unblock();
  431. });
  432. }
  433. this.onInactivateLink = function (data) {
  434. dialogManager.block();
  435.  
  436. http.odata.post("PNTrackingLinks", {
  437. ClientId: that.client.ClientId(),
  438. PNTrackingLinkId: parseInt(that.pnTrackingLinkId()),
  439. SysVerticalTypeId: parseInt(constants.enum_VerticalTypes.ADULT),
  440. IsActive: false,
  441. WithCode: false,
  442. })
  443. .then(function (data) {
  444. dialogManager.showSuccessDialog('Link inactivated successfully');
  445. that.getAppSettings('PNTrackingLinkRootMainstreamUrl', that.smartLink);
  446. that.linkWithoutCode(that.smartLink());
  447. that.isLinkActive(false);
  448.  
  449. that.getLinkStatus(that.client.ClientId());
  450. })
  451. .fail(function (error) {
  452. dialogManager.showErrorDialog(error);
  453. })
  454. .done(function () {
  455. dialogManager.unblock();
  456.  
  457. });
  458. }
  459.  
  460.  
  461. ///////////////////
  462. this.onActivateLink2 = function (data) {
  463. dialogManager.block();
  464.  
  465. http.odata.post("PNTrackingLinks", {
  466. ClientId: that.client.ClientId(),
  467. SysVerticalTypeId: parseInt(constants.enum_VerticalTypes.MAIN),
  468. IsActive: true,
  469. WithCode: false
  470. })
  471. .then(function (data) {
  472. dialogManager.showSuccessDialog('Link activated successfully');
  473. that.getLinkStatus2(that.client.ClientId());
  474. })
  475. .fail(function (error) {
  476. dialogManager.showErrorDialog(error);
  477. })
  478. .done(function () {
  479. dialogManager.unblock();
  480. });
  481. }
  482.  
  483. this.onInactivateLink2 = function (data) {
  484. dialogManager.block();
  485.  
  486. http.odata.post("PNTrackingLinks", {
  487. ClientId: that.client.ClientId(),
  488. PNTrackingLinkId: parseInt(that.pnTrackingLinkId2()),
  489. SysVerticalTypeId: parseInt(constants.enum_VerticalTypes.MAIN),
  490. IsActive: false,
  491. WithCode: false,
  492. })
  493. .then(function (data) {
  494. dialogManager.showSuccessDialog('Link inactivated successfully');
  495. that.getAppSettings('PNTrackingLinkRootMainstreamUrl', that.smartLink2);
  496. that.linkWithoutCode2(that.smartLink2());
  497. that.isLinkActive2(false);
  498.  
  499. that.getLinkStatus2(that.client.ClientId());
  500. })
  501. .fail(function (error) {
  502. dialogManager.showErrorDialog(error);
  503. })
  504. .done(function () {
  505. dialogManager.unblock();
  506.  
  507. });
  508. }
  509.  
  510. this.onGenerateLink = function () {
  511.  
  512. var campaignId = $('#select-campaign').selectize()[0].selectize.getValue();
  513.  
  514. if (!campaignId)
  515. {
  516. dialogManager.showErrorDialog('Please select a campaign!');
  517. return;
  518. }
  519. that.getAppSettings('PublisherCampaignUrl', that.smartLink3);
  520.  
  521. dialogManager.block();
  522.  
  523. http.odata.post("CampaignsXClient?campaignId=" + campaignId + "&clientId=" + that.client.ClientId())
  524. .then(function(data) {
  525.  
  526. if (!data) {
  527. return;
  528. }
  529.  
  530. dialogManager.showSuccessDialog('Link activated successfully');
  531.  
  532. that.anLinkWithoutCode(that.smartLink3().replace("{tracking_id}", data.data));
  533. })
  534. .fail(function(error) {
  535. dialogManager.showErrorDialog(error);
  536. })
  537. .done(function() {
  538. dialogManager.unblock();
  539. });
  540. }
  541.  
  542.  
  543. this.init = function () {
  544.  
  545. clip.create('.btn');
  546.  
  547. dialogManager.block();
  548.  
  549. that.configCountrySelector([], []);
  550. that.configCampaignSelector([], []);
  551.  
  552. Q.all([
  553. this.getCompanyTypes(),
  554. http.getAffiliateManagers(that.affiliateManagers),
  555. dictionaries.get(constants.enum_Dictionaries.PAYMENT_TERMS)
  556. .then(function (data) { that.paymentTerms(data.data) }),
  557. dictionaries.get(constants.enum_Dictionaries.CURRENCIES_TYPES)
  558. .then(function (data) { that.currencies(data.data) }),
  559. dictionaries.get(constants.enum_Dictionaries.PAYMENT_STATUS)
  560. .then(function (data) { that.paymentStatus(data.data) }),
  561. dictionaries.get(constants.enum_Dictionaries.GET_COUNTRIES)
  562. .then(function (data) { that.Countries(data.data) }),
  563. dictionaries.get(constants.enum_Dictionaries.REG_QUESTIONS)
  564. .then(function (data) {
  565. _.each(data.data, function (question) {
  566. question.answer = ko.observable('');
  567.  
  568. })
  569. that.questions(data.data)
  570. }),
  571. this.client.CompanyTypeId.subscribe(that.loadCompanySubTypes, this)
  572. ])
  573. .then(function () {
  574. that.configCountrySelector(that.Countries(), []);
  575. if (params.id) {
  576. that.getClient(params.id);
  577.  
  578.  
  579. Q.all([
  580. that.getAppSettings('PNTrackingLinkRootAdultUrl', that.smartLink),
  581. that.getAppSettings('PNTrackingLinkRootMainstreamUrl', that.smartLink2)
  582. ])
  583. .then(function () {
  584. that.getLinkStatus(params.id);
  585. that.getLinkStatus2(params.id);
  586. });
  587.  
  588. that.getCampaigns();
  589. } else {
  590.  
  591. dialogManager.unblock();
  592. }
  593.  
  594. })
  595. .fail(function (err) {
  596. dialogManager.unblock();
  597. dialogManager.showErrorDialog(err);
  598. });
  599. }
  600.  
  601. that.init();
  602. }
  603.  
  604. return { viewModel: ClientDetailViewModel, template: templateMarkup }
  605.  
  606. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement