Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- define(['knockout', 'http', 'knockout-validation', 'text!./client-detail.html', './client-model', 'knockout-mapping', 'dialog-helper', 'underscore', 'dictionaries', 'constants', 'q', 'navigator', 'selectize','moment','clip'],
- function (ko, http, koValid, templateMarkup, ClientModel, koMap, dialogManager, _, dictionaries, constants, Q, navigator, selectize,moment,clip) {
- function ClientDetailViewModel(params) {
- var that = this;
- // #region Properties
- this.clientType = ko.observable(params.clientType);
- this.client = new ClientModel();
- this.clientWithValidation = ko.validatedObservable(that.client);
- this.clientFinancialWithValidation = ko.validatedObservable(that.client.ClientFinancialDetail);
- this.companyTypes = ko.observableArray([]);
- this.companySubTypes = ko.observableArray([]);
- this.paymentTerms = ko.observableArray([]);
- this.currencies = ko.observableArray([]);
- this.paymentStatus = ko.observableArray([]);
- this.QuestionnaireAnswers = ko.observableArray([]);
- this.questions = ko.observableArray([]);
- this.affiliateManagers = ko.observableArray([]);
- this.Countries = ko.observableArray([]);
- this.campaignList = null;
- this.linkWithoutCode = ko.observable();
- this.linkWithoutCode2 = ko.observable();
- this.isLinkActive = ko.observable(false);
- this.isLinkActive2 = ko.observable(false);
- this.pnTrackingLinkId = ko.observable();
- this.pnTrackingLinkId2 = ko.observable();
- this.token = ko.observable();
- this.token2 = ko.observable();
- this.smartLink = ko.observable();
- this.smartLink2 = ko.observable();
- this.smartLink3 = ko.observable();
- this.anLinkWithoutCode = ko.observable('');
- this.DCMLoginPassword = ko.observable();
- this.DCMConfirmPassword = ko.observable();
- this.FormattedCreatedAt = ko.observable();
- this.filters = ko.observable();
- this.clientStatusName = ko.observable();
- // #endregion
- // #region Form events
- this.approveUser = function () {
- if (!that.client.AffiliateManagerId()) {
- dialogManager.showWarningDialog("Please select an account manager for this client and try again!");
- return;
- }
- dialogManager.block();
- http.makeAjaxRequest('api/ClientsStatus',
- 'POST',
- { ClientId: that.client.ClientId(), NewClientStatusId: constants.enum_ClientStatus.APPROVED })
- .then(function (result) {
- if (result && !result.IsSuccess) {
- dialogManager.showErrorDialog(result.Errors[0]);
- return;
- }
- dialogManager.showSuccessDialog('E-mail sent successfully');
- })
- .fail(function(error) {
- dialogManager.showErrorDialog('An error occurred during e-mail sending');
- })
- .done(function() {
- dialogManager.unblock();
- });
- }
- this.back = function () {
- window.history.back();
- }
- this.save = function () {
- var country = $('#select-tara').selectize()[0].selectize.getValue();
- if(country != "")
- that.client.ClientAddress.CountryId(country);
- else
- that.client.ClientAddress.CountryId(null);
- if (!that.clientWithValidation.isValid()) {
- return _.each(that.clientWithValidation.errors(), function (err) { dialogManager.showWarningDialog(err); });
- }
- if (!that.clientFinancialWithValidation.isValid()) {
- return _.each(that.clientFinancialWithValidation.errors(), function (err) { dialogManager.showWarningDialog(err); });
- }
- if (that.DCMConfirmPassword() != that.DCMLoginPassword()) {
- dialogManager.showWarningDialog('DCypher Media login passwords mismatch');
- return;
- }
- //int parsing of Payout Settings
- that.client.ClientFinancialDetail.PayoutSettings(parseInt(that.client.ClientFinancialDetail.PayoutSettings()));
- dialogManager.block();
- that.client.ClientContact_FinancialContactId
- .Email(that.client.ClientContact_FinancialContactId.Email()
- ? that.client.ClientContact_FinancialContactId.Email()
- : null);
- that.client.ClientContact_MainContactId.Email(that.client.ClientContact_MainContactId.Email()
- ? that.client.ClientContact_MainContactId.Email()
- : null);
- if (!that.client.ClientId()) {
- that.client.QuestionnaireAnswers = [];
- http.odata.post("Clients" + (that.DCMLoginPassword() ? "?dcmLoginPassword=" + that.DCMLoginPassword() + "&dcmConfirmPassword=" + that.DCMConfirmPassword() : ''), koMap.toJS(that.client))
- .then(function (data) {
- if (data.data && typeof data.data == "string")
- {
- dialogManager.showErrorDialog(data.data);
- return;
- }
- //that.getClient(data.data.ClientId);
- navigator.navigateTo('clients/' + that.clientType() + '/inactive');
- dialogManager.showSuccessDialog('Client created successfully');
- })
- .fail(function (error) {
- dialogManager.showErrorDialog(error);
- })
- .done(function () {
- dialogManager.unblock();
- });
- } else {
- http.odata.post("Clients" + (that.DCMLoginPassword() ? "?dcmLoginPassword=" + that.DCMLoginPassword() + "&dcmConfirmPassword=" + that.DCMConfirmPassword() : ''), koMap.toJS(that.client), that.client.ClientId())
- .then(function (data) {
- if (data.data && typeof data.data == "string") {
- dialogManager.showErrorDialog(data.data);
- return;
- }
- navigator.navigateTo('clients/' + that.clientType() + '/' + that.clientStatusName());
- dialogManager.showSuccessDialog('Client updated successfully');
- })
- .fail(function (error) {
- dialogManager.showErrorDialog(error);
- })
- .done(function () {
- dialogManager.unblock();
- });
- }
- }
- // #endregion
- // #region Client entity
- this.getClient = function (clientId) {
- http.odata.find("Clients", clientId,
- { expand: 'ClientFinancialDetail,SysCompanyType_CompanyTypeId,ClientContact_FinancialContactId,ClientContact_MainContactId,ClientContact_TechnicalContactId,ClientAddress,SysCompanyType_CompanySubTypeId,QuestionnaireAnswers,SysClientStatus' })
- .then(function (data) {
- var mapping = { ignore: "@odata.context" };
- that.clientStatusName(data.data.SysClientStatus.ClientStatusName.split(' ')[0].toLowerCase());
- koMap.fromJS(data.data, mapping, that.client);
- that.FormattedCreatedAt(moment(that.client.CreatedAt()).format('DD/MM/YYYY HH:mm'));
- params.id = data.data.CompanySubTypeId;
- $('#select-tara').selectize()[0].selectize.setValue(that.client.ClientAddress.CountryId());
- if (data.data.QuestionnaireAnswers && data.data.QuestionnaireAnswers.length > 0) {
- _.each(that.questions(), function (question) {
- var matchedAnswer = _.find(data.data.QuestionnaireAnswers, function (answer) {
- return question.SysQuestionnaireQuestionId == answer.SysQuestionnaireQuestionId;
- });
- if (matchedAnswer) {
- question.answer(matchedAnswer.AnswerText);
- }
- });
- }
- })
- .fail(function (error) {
- dialogManager.showErrorDialog(error);
- })
- .done(function () {
- dialogManager.unblock();
- });
- }
- // #endregion
- // #region Dictionaries
- this.getCompanyTypes = function () {
- http.odata.get("SysCompanyTypes")
- .then(function (data2) {
- that.companyTypes(data2.data);
- })
- .fail(function (err) {
- alert(err);
- });
- }
- this.getCampaigns = function () {
- dialogManager.block();
- http.odata.get("Campaigns", { expand: 'Offer', filter: 'Offer/SysOfferTypeId eq ' +constants.enum_OfferTypes.AN + ' and SysCampaignStatusId eq ' + constants.enum_CampaignStatuses.ACTIVE })
- .then(function (data) {
- that.campaignList = _.map(data.data, (function (campaign) {
- return {
- id: campaign.CampaignId,
- name: campaign.Offer.OfferName
- }
- }));
- that.configCampaignSelector(that.campaignList, []);
- })
- .fail(function (error) {
- dialogManager.showErrorDialog(error);
- })
- .done(function () {
- dialogManager.unblock();
- });
- }
- this.loadCompanySubTypes = function (companyTypeId) {
- if (!companyTypeId) {
- that.companySubTypes.removeAll();
- return;
- }
- http.odata.find("SysCompanyTypes", companyTypeId)
- .then(function (data) {
- that.companySubTypes(data.data);
- if (params.id) {
- that.client.CompanySubTypeId(params.id);
- params.id = null;
- }
- }).
- fail(function (error) {
- dialogManager.showErrorDialog(error);
- });
- }
- // #endregion
- //#region Selectize Dropwdowns
- this.configCountrySelector = function (availableData, selectedData) {
- $('#select-tara').selectize()[0].selectize.destroy();
- var fromIndex = _.findIndex(availableData, { CountryName: "Worldwide" });
- var element = availableData[fromIndex];
- availableData.splice(fromIndex, 1);
- availableData.splice(0, 0, element);
- var selectedItems = _.map(selectedData, (function (d) {
- return d.CountryId();
- }));
- $('#select-tara').selectize({
- options: availableData,
- valueField: 'CountryId',
- labelField: 'CountryName',
- maxItems: 1,
- delimiter: ',',
- persist: false,
- searchField: 'CountryName',
- });
- $('#select-tara').selectize()[0].selectize.setValue(selectedItems);
- }
- this.configCampaignSelector = function (availableData, selectedData) {
- selectize.create("#select-campaign", { values: availableData, maxItems: 1 });
- }
- //#endregion
- //#region Misc
- this.populateNavigationProperties = function () {
- that.offer.SysCountries(_.filter(that.Countries(), function (rt) {
- return _.contains($('#select-tara').selectize()[0].selectize.items, rt.CountryId.toString());
- }));
- }
- //#endregion
- this.getAppSettings = function (key, param) {
- dialogManager.block();
- http.makeAjaxRequest('api/AppSettings', 'GET', { settingKey: key })
- .then(function (result) {
- param(result);
- }).fail(function (error) {
- dialogManager.showErrorDialog(error);
- })
- .done(function () {
- dialogManager.unblock();
- });
- }
- this.getLinkStatus = function (clientId) {
- http.odata.get("PNTrackingLinks?clientId="+clientId,
- {
- filter: 'SysVerticalTypeId eq ' + constants.enum_VerticalTypes.ADULT
- })
- .then(function (data) {
- if (data.data[0] == undefined) {
- that.isLinkActive(false);
- that.linkWithoutCode(that.smartLink());
- }
- if ((data.data[0] != undefined) && (data.data[0].WithCode == true)) {
- that.token(data.data[0].Token);
- that.pnTrackingLinkId(data.data[0].PNTrackingLinkId);
- }
- if ((data.data[0] != undefined) && (data.data[0].IsActive == true)) {
- that.isLinkActive(true);
- that.token(data.data[0].Token);
- that.linkWithoutCode(that.smartLink() + '/?tracking_id=' + that.token());
- that.pnTrackingLinkId(data.data[0].PNTrackingLinkId);
- }
- }
- )
- .fail(function (error) {
- dialogManager.showErrorDialog(error);
- })
- .done(function () {
- dialogManager.unblock();
- });
- }
- this.getLinkStatus2 = function (clientId) {
- http.odata.get("PNTrackingLinks?clientId=" + clientId,
- {
- filter: 'SysVerticalTypeId eq ' + constants.enum_VerticalTypes.MAIN
- })
- .then(function (data) {
- if (data.data[0] == undefined) {
- that.isLinkActive2(false);
- that.linkWithoutCode2(that.smartLink2());
- }
- if ((data.data[0] != undefined) && (data.data[0].WithCode == true)) {
- that.token2(data.data[0].Token);
- that.pnTrackingLinkId2(data.data[0].PNTrackingLinkId);
- }
- if ((data.data[0] != undefined) && (data.data[0].IsActive == true)) {
- that.isLinkActive2(true);
- that.token2(data.data[0].Token);
- that.linkWithoutCode2(that.smartLink2() + '/?tracking_id=' + that.token2());
- that.pnTrackingLinkId2(data.data[0].PNTrackingLinkId);
- }
- }
- )
- .fail(function (error) {
- dialogManager.showErrorDialog(error);
- })
- .done(function () {
- dialogManager.unblock();
- });
- }
- ///////////////////////////////
- this.onActivateLink = function (data) {
- dialogManager.block();
- http.odata.post("PNTrackingLinks", {
- ClientId: that.client.ClientId(),
- SysVerticalTypeId: parseInt(constants.enum_VerticalTypes.ADULT),
- IsActive: true,
- WithCode: false
- })
- .then(function (data) {
- dialogManager.showSuccessDialog('Link activated successfully');
- that.getLinkStatus(that.client.ClientId());
- })
- .fail(function (error) {
- dialogManager.showErrorDialog(error);
- })
- .done(function () {
- dialogManager.unblock();
- });
- }
- this.onInactivateLink = function (data) {
- dialogManager.block();
- http.odata.post("PNTrackingLinks", {
- ClientId: that.client.ClientId(),
- PNTrackingLinkId: parseInt(that.pnTrackingLinkId()),
- SysVerticalTypeId: parseInt(constants.enum_VerticalTypes.ADULT),
- IsActive: false,
- WithCode: false,
- })
- .then(function (data) {
- dialogManager.showSuccessDialog('Link inactivated successfully');
- that.getAppSettings('PNTrackingLinkRootMainstreamUrl', that.smartLink);
- that.linkWithoutCode(that.smartLink());
- that.isLinkActive(false);
- that.getLinkStatus(that.client.ClientId());
- })
- .fail(function (error) {
- dialogManager.showErrorDialog(error);
- })
- .done(function () {
- dialogManager.unblock();
- });
- }
- ///////////////////
- this.onActivateLink2 = function (data) {
- dialogManager.block();
- http.odata.post("PNTrackingLinks", {
- ClientId: that.client.ClientId(),
- SysVerticalTypeId: parseInt(constants.enum_VerticalTypes.MAIN),
- IsActive: true,
- WithCode: false
- })
- .then(function (data) {
- dialogManager.showSuccessDialog('Link activated successfully');
- that.getLinkStatus2(that.client.ClientId());
- })
- .fail(function (error) {
- dialogManager.showErrorDialog(error);
- })
- .done(function () {
- dialogManager.unblock();
- });
- }
- this.onInactivateLink2 = function (data) {
- dialogManager.block();
- http.odata.post("PNTrackingLinks", {
- ClientId: that.client.ClientId(),
- PNTrackingLinkId: parseInt(that.pnTrackingLinkId2()),
- SysVerticalTypeId: parseInt(constants.enum_VerticalTypes.MAIN),
- IsActive: false,
- WithCode: false,
- })
- .then(function (data) {
- dialogManager.showSuccessDialog('Link inactivated successfully');
- that.getAppSettings('PNTrackingLinkRootMainstreamUrl', that.smartLink2);
- that.linkWithoutCode2(that.smartLink2());
- that.isLinkActive2(false);
- that.getLinkStatus2(that.client.ClientId());
- })
- .fail(function (error) {
- dialogManager.showErrorDialog(error);
- })
- .done(function () {
- dialogManager.unblock();
- });
- }
- this.onGenerateLink = function () {
- var campaignId = $('#select-campaign').selectize()[0].selectize.getValue();
- if (!campaignId)
- {
- dialogManager.showErrorDialog('Please select a campaign!');
- return;
- }
- that.getAppSettings('PublisherCampaignUrl', that.smartLink3);
- dialogManager.block();
- http.odata.post("CampaignsXClient?campaignId=" + campaignId + "&clientId=" + that.client.ClientId())
- .then(function(data) {
- if (!data) {
- return;
- }
- dialogManager.showSuccessDialog('Link activated successfully');
- that.anLinkWithoutCode(that.smartLink3().replace("{tracking_id}", data.data));
- })
- .fail(function(error) {
- dialogManager.showErrorDialog(error);
- })
- .done(function() {
- dialogManager.unblock();
- });
- }
- this.init = function () {
- clip.create('.btn');
- dialogManager.block();
- that.configCountrySelector([], []);
- that.configCampaignSelector([], []);
- Q.all([
- this.getCompanyTypes(),
- http.getAffiliateManagers(that.affiliateManagers),
- dictionaries.get(constants.enum_Dictionaries.PAYMENT_TERMS)
- .then(function (data) { that.paymentTerms(data.data) }),
- dictionaries.get(constants.enum_Dictionaries.CURRENCIES_TYPES)
- .then(function (data) { that.currencies(data.data) }),
- dictionaries.get(constants.enum_Dictionaries.PAYMENT_STATUS)
- .then(function (data) { that.paymentStatus(data.data) }),
- dictionaries.get(constants.enum_Dictionaries.GET_COUNTRIES)
- .then(function (data) { that.Countries(data.data) }),
- dictionaries.get(constants.enum_Dictionaries.REG_QUESTIONS)
- .then(function (data) {
- _.each(data.data, function (question) {
- question.answer = ko.observable('');
- })
- that.questions(data.data)
- }),
- this.client.CompanyTypeId.subscribe(that.loadCompanySubTypes, this)
- ])
- .then(function () {
- that.configCountrySelector(that.Countries(), []);
- if (params.id) {
- that.getClient(params.id);
- Q.all([
- that.getAppSettings('PNTrackingLinkRootAdultUrl', that.smartLink),
- that.getAppSettings('PNTrackingLinkRootMainstreamUrl', that.smartLink2)
- ])
- .then(function () {
- that.getLinkStatus(params.id);
- that.getLinkStatus2(params.id);
- });
- that.getCampaigns();
- } else {
- dialogManager.unblock();
- }
- })
- .fail(function (err) {
- dialogManager.unblock();
- dialogManager.showErrorDialog(err);
- });
- }
- that.init();
- }
- return { viewModel: ClientDetailViewModel, template: templateMarkup }
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement