2021-06-30 19:31:49 +00:00
|
|
|
const { getTestConfig } = require( './test-config' );
|
|
|
|
const { HTTPClientFactory } = require('@woocommerce/api');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uses the WordPress API to update the Ready page's status.
|
|
|
|
*
|
|
|
|
* @param {string} status | Status to update the page to. One of: publish, future, draft, pending, private
|
|
|
|
*/
|
|
|
|
const updateReadyPageStatus = async ( status ) => {
|
|
|
|
const testConfig = getTestConfig();
|
|
|
|
|
|
|
|
const apiUrl = testConfig.url;
|
|
|
|
const wpPagesEndpoint = '/wp/v2/pages';
|
|
|
|
const adminUsername = testConfig.users.admin.username ? testConfig.users.admin.username : 'admin';
|
|
|
|
const adminPassword = testConfig.users.admin.password ? testConfig.users.admin.password : 'password';
|
2021-08-26 13:07:13 +00:00
|
|
|
const client = HTTPClientFactory.build( apiUrl )
|
|
|
|
.withBasicAuth( adminUsername, adminPassword )
|
2021-06-30 19:31:49 +00:00
|
|
|
.create();
|
|
|
|
|
|
|
|
// As the default status filter in the API is `publish`, we need to
|
|
|
|
// filter based on the supplied status otherwise no results are returned.
|
|
|
|
let statusFilter = 'publish';
|
|
|
|
if ( 'publish' === status ) {
|
|
|
|
// The page will be in a draft state, so we need to filter on that status
|
|
|
|
statusFilter = 'draft';
|
|
|
|
}
|
|
|
|
|
2021-08-26 13:07:13 +00:00
|
|
|
const getPostsResponse = await client.get( `${wpPagesEndpoint}?search=ready&status=${statusFilter}` );
|
2021-08-24 12:18:45 +00:00
|
|
|
if ( getPostsResponse.data && getPostsResponse.data.length > 0 ) {
|
2021-07-06 17:19:09 +00:00
|
|
|
const pageId = getPostsResponse.data[0].id;
|
|
|
|
// Update the page to the new status
|
2021-08-26 13:07:13 +00:00
|
|
|
await client.post( `${wpPagesEndpoint}/${pageId}`, { 'status': status } );
|
2021-07-06 17:19:09 +00:00
|
|
|
}
|
2021-06-30 19:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = updateReadyPageStatus;
|