new-customer-address.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /**
  6. * @api
  7. */
  8. define([
  9. 'underscore',
  10. 'Magento_Checkout/js/model/default-post-code-resolver'
  11. ], function (_, DefaultPostCodeResolver) {
  12. 'use strict';
  13. /**
  14. * @param {Object} addressData
  15. * Returns new address object
  16. */
  17. return function (addressData) {
  18. var identifier = Date.now(),
  19. countryId = addressData['country_id'] || addressData.countryId || window.checkoutConfig.defaultCountryId,
  20. regionId;
  21. if (addressData.region && addressData.region['region_id']) {
  22. regionId = addressData.region['region_id'];
  23. } else if (!addressData['region_id']) {
  24. regionId = undefined;
  25. } else if (
  26. /* eslint-disable */
  27. addressData['country_id'] && addressData['country_id'] == window.checkoutConfig.defaultCountryId ||
  28. !addressData['country_id'] && countryId == window.checkoutConfig.defaultCountryId
  29. /* eslint-enable */
  30. ) {
  31. regionId = window.checkoutConfig.defaultRegionId || undefined;
  32. }
  33. return {
  34. email: addressData.email,
  35. countryId: countryId,
  36. regionId: regionId || addressData.regionId,
  37. regionCode: addressData.region ? addressData.region['region_code'] : null,
  38. region: addressData.region ? addressData.region.region : null,
  39. customerId: addressData['customer_id'] || addressData.customerId,
  40. street: addressData.street ? _.compact(addressData.street) : addressData.street,
  41. company: addressData.company,
  42. telephone: addressData.telephone,
  43. fax: addressData.fax,
  44. postcode: addressData.postcode ? addressData.postcode : DefaultPostCodeResolver.resolve(),
  45. city: addressData.city,
  46. firstname: addressData.firstname,
  47. lastname: addressData.lastname,
  48. middlename: addressData.middlename,
  49. prefix: addressData.prefix,
  50. suffix: addressData.suffix,
  51. vatId: addressData['vat_id'],
  52. saveInAddressBook: addressData['save_in_address_book'],
  53. customAttributes: addressData['custom_attributes'],
  54. /**
  55. * @return {*}
  56. */
  57. isDefaultShipping: function () {
  58. return addressData['default_shipping'];
  59. },
  60. /**
  61. * @return {*}
  62. */
  63. isDefaultBilling: function () {
  64. return addressData['default_billing'];
  65. },
  66. /**
  67. * @return {String}
  68. */
  69. getType: function () {
  70. return 'new-customer-address';
  71. },
  72. /**
  73. * @return {String}
  74. */
  75. getKey: function () {
  76. return this.getType();
  77. },
  78. /**
  79. * @return {String}
  80. */
  81. getCacheKey: function () {
  82. return this.getType() + identifier;
  83. },
  84. /**
  85. * @return {Boolean}
  86. */
  87. isEditable: function () {
  88. return true;
  89. },
  90. /**
  91. * @return {Boolean}
  92. */
  93. canUseForBilling: function () {
  94. return true;
  95. }
  96. };
  97. };
  98. });