Update Process for VTEX Account in VTEX-Commerce
npm install @janiscommerce/vtex-account-processes
❌ Wrong:
const { VtexAccountProcesses } = require('@janiscommerce/vtex-account-processes');
const vtexAccountProcess = new VtexAccountProcesses();
✔️ Good:
const { VtexAccountProcesses } = require('@janiscommerce/vtex-account-processes');
const { ApiSession } = require('@janiscommerce/api-session');
const vtexAccountProcess = session.getSessionInstance(VtexAccountProcesses);
send(accountId, processName, status, content, options)
- Async
- Description: Update
processName
foraccountId
in VTEX-Commerce withstatus
. - Parameters:
accountId
: OBJECT-ID VTEX Account ID in VTEX-CommerceprocessName
: STRING name of the processstatus
: STRING new Status for that processcontent
: OBJECT, OPTIONAL, Extra Data you want to add for that process, whatever you want to save, for example a message, or an error stack, etc.options
: OBJECT, OPTIONAL, To add the Start Date or an End Date
- Returns: OBJECT
statusCode
: HTTP Status code of the call to VTEX-Commercebody
: Body response
You can get the valid Statuses using:
statuses
- static getter
- Returns: OBJECT
pending
processing
success
error
This is used to keep an extra information in Account Process API, like a log.
In the process:
await vtexAccountProcess.send(
'5dea9fc691240d00084083f8',
'import-readme',
VtexAccountProcesses.statuses.pending,
{ message: 'Start Importing Categories from VTEX' } // CONTENT
);
In Vtex-Commerce:
Now, there are 2 options
startDate
: BOOLEAN, to add an Date-Now ISO-String, to indicate the start of the processendDate
: BOOLEAN, to add an Date-Now ISO-String, to indicate the end of the process
This is use to set in Account-Process API these properties.
In the process:
// Start the process in 31/12/1969 21:00hs
await vtexAccountProcess.send(
'5dea9fc691240d00084083f8',
'import-readme',
VtexAccountProcesses.statuses.pending,
null,
{ startDate: true }
);
// Finish the process in 05/08/2020 11:57hs
await vtexAccountProcess.send(
'5dea9fc691240d00084083f8',
'import-readme',
VtexAccountProcesses.statuses.success,
null,
{ endDate: true }
);
In Vtex-Commerce:
- Send with minimal data, and pending status, and create a process in VTEX-Commerce
const response = await vtexAccountProcess.send(
'5dea9fc691240d00084083f8',
'import-readme',
VtexAccountProcesses.statuses.pending
)
/*
Response: {
statusCode: 200,
body: {
id: '5dea9fc691240d0008408000'
}
}
*/
- Send with content, and processing status, and Account is not found in VTEX-Commerce
const response = await vtexAccountProcess.send(
'5dea9fc691240d00084083f8',
'import-readme',
VtexAccountProcesses.statuses.processing,
{ itemsImported: 10, itemsNotModified: 1 }
);
/*
Response: {
statusCode: 404,
body: {
message: 'Account not found'
}
}
*/
- Send with a Start Date, and error status, and VTEX-Commerce is failing
const response = await vtexAccountProcess.send(
'5dea9fc691240d00084083f8',
'import-readme',
VtexAccountProcesses.statuses.error,
null // No Content,
{ startDate: true }
);
/*
Response: {
statusCode: 503,
body: {
message: 'Timeout'
}
}
*/
- Send with an End Date, and success status, and update an existing process in VTEX-Commerce
const response = await vtexAccountProcess.send(
'5dea9fc691240d00084083f8',
'import-readme',
VtexAccountProcesses.statuses.success,
null // No Content,
{ endDate: true }
);
/*
Response: {
statusCode: 200,
body: {
id: '5dea9fc691240d0008408000'
}
}
*/
The errors are informed with a VtexAccountProcessesError
.
This object has a code that can be useful for a debugging or error handling.
The codes are the following:
Code | Description |
---|---|
1 | No Session |
2 | Invalid Account Id |
3 | Invalid Process Name |
4 | Invalid Status |
5 | Invalid Content |
6 | Invalid Options |