Skip to content

Instantly share code, notes, and snippets.

@guilhermemarconi
Last active June 20, 2024 02:11
Show Gist options
  • Save guilhermemarconi/2af015a8c3528cdb5cc4f50b7b3170a3 to your computer and use it in GitHub Desktop.
Save guilhermemarconi/2af015a8c3528cdb5cc4f50b7b3170a3 to your computer and use it in GitHub Desktop.
Deal with User Profile and User Addresses in VTEX
fetch(`/no-cache/postalcode/address/delete/${addressName}`, {
credentials: 'same-origin',
})
.then(res => res.json())
.then(addressData => {
// ...
})
fetch(`/no-cache/postalcode/address/${postalCode /* e.g.:24310-090 */}`, {
credentials: 'same-origin',
})
.then(res => res.json())
.then(addressData => {
// ...
})
fetch(`/no-cache/account/address/detail/${addressName}`, {
credentials: 'same-origin',
})
.then(res => res.json())
.then(addressData => {
// ...
})
/**
* Pre-requisites
*
* 1) This fields should be public to read in AD data entity:
* addressName, addressType, city, complement, country, neighborhood, number, postalCode,
* receiverName, reference, state, street
*
* 2) userId need to be a filter and also public to filter.
*
* 3) For security reasons, the AD data entity must not allow requests without filter.
*/
vtexjs.checkout.getOrderForm(['userProfileId'])
.done(orderForm => {
let fields = ['addressName', 'addressType', 'city', 'complement', 'country', 'neighborhood', 'number', 'postalCode', 'receiverName', 'reference', 'state', 'street']
fetch(`/api/dataentities/AD/search/?_fields=${fields.join(',')}&userId=${orderForm.userProfileId}`)
.then(res => res.json())
.then(addressesData => {
// ...
})
})
/**
* Pre-requisites
*
* 1) This fields should be public to read in CL data entity:
* firstName, lastName, nickName, document, birthDate, gender, corporateName, businessDocument,
* stateRegistration, homePhone, phone, businessPhone, isCorporate
*
* 2) userId need to be a filter and also public to filter.
*
* 3) For security reasons, the CL data entity must not allow requests without filter.
*/
vtexjs.checkout.getOrderForm(['userProfileId'])
.done(orderForm => {
let fields = ['firstName', 'lastName', 'nickName', 'document', 'birthDate', 'gender', 'corporateName', 'businessDocument', 'stateRegistration', 'homePhone', 'phone', 'businessPhone', 'isCorporate']
fetch(`/api/dataentities/CL/search/?_fields=${fields.join(',')}&userId=${orderForm.userProfileId}`)
.then(res => res.json())
.then(userData => {
// ...
})
})
let formData = $('#form-profile-address').serialize()
fetch('/no-cache/account/address/save', {
credentials: 'same-origin',
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
},
body: formData,
})
.then(res => {
// ...
})
let formData = $('#form-profile-data').serialize()
fetch('/no-cache/account/profile/save', {
credentials: 'same-origin',
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
},
body: formData,
})
.then(res => {
// ...
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment