customer.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /**
  6. * @api
  7. */
  8. define([
  9. 'jquery',
  10. 'ko',
  11. 'underscore',
  12. './address-list'
  13. ], function ($, ko, _, addressList) {
  14. 'use strict';
  15. var isLoggedIn = ko.observable(window.isCustomerLoggedIn),
  16. customerData = {};
  17. if (isLoggedIn()) {
  18. customerData = window.customerData;
  19. } else {
  20. customerData = {};
  21. }
  22. return {
  23. customerData: customerData,
  24. customerDetails: {},
  25. isLoggedIn: isLoggedIn,
  26. /**
  27. * @param {Boolean} flag
  28. */
  29. setIsLoggedIn: function (flag) {
  30. isLoggedIn(flag);
  31. },
  32. /**
  33. * @return {Array}
  34. */
  35. getBillingAddressList: function () {
  36. return addressList();
  37. },
  38. /**
  39. * @return {Array}
  40. */
  41. getShippingAddressList: function () {
  42. return addressList();
  43. },
  44. /**
  45. * @param {String} fieldName
  46. * @param {*} value
  47. */
  48. setDetails: function (fieldName, value) {
  49. if (fieldName) {
  50. this.customerDetails[fieldName] = value;
  51. }
  52. },
  53. /**
  54. * @param {String} fieldName
  55. * @return {*}
  56. */
  57. getDetails: function (fieldName) {
  58. if (fieldName) {
  59. if (this.customerDetails.hasOwnProperty(fieldName)) {
  60. return this.customerDetails[fieldName];
  61. }
  62. return undefined;
  63. }
  64. return this.customerDetails;
  65. },
  66. /**
  67. * @param {Array} address
  68. * @return {Number}
  69. */
  70. addCustomerAddress: function (address) {
  71. var fields = [
  72. 'customer_id', 'country_id', 'street', 'company', 'telephone', 'fax', 'postcode', 'city',
  73. 'firstname', 'lastname', 'middlename', 'prefix', 'suffix', 'vat_id', 'default_billing',
  74. 'default_shipping'
  75. ],
  76. customerAddress = {},
  77. hasAddress = 0,
  78. existingAddress;
  79. if (!this.customerData.addresses) {
  80. this.customerData.addresses = [];
  81. }
  82. customerAddress = _.pick(address, fields);
  83. if (address.hasOwnProperty('region_id')) {
  84. customerAddress.region = {
  85. 'region_id': address['region_id'],
  86. region: address.region
  87. };
  88. }
  89. for (existingAddress in this.customerData.addresses) {
  90. if (this.customerData.addresses.hasOwnProperty(existingAddress)) {
  91. if (_.isEqual(this.customerData.addresses[existingAddress], customerAddress)) { //eslint-disable-line
  92. hasAddress = existingAddress;
  93. break;
  94. }
  95. }
  96. }
  97. if (hasAddress === 0) {
  98. return this.customerData.addresses.push(customerAddress) - 1;
  99. }
  100. return hasAddress;
  101. },
  102. /**
  103. * @param {*} addressId
  104. * @return {Boolean}
  105. */
  106. setAddressAsDefaultBilling: function (addressId) {
  107. if (this.customerData.addresses[addressId]) {
  108. this.customerData.addresses[addressId]['default_billing'] = 1;
  109. return true;
  110. }
  111. return false;
  112. },
  113. /**
  114. * @param {*} addressId
  115. * @return {Boolean}
  116. */
  117. setAddressAsDefaultShipping: function (addressId) {
  118. if (this.customerData.addresses[addressId]) {
  119. this.customerData.addresses[addressId]['default_shipping'] = 1;
  120. return true;
  121. }
  122. return false;
  123. }
  124. };
  125. });