Data4Autos-Shopify-Backend/fulfillmentService_bak.js
2026-04-13 05:23:25 +00:00

175 lines
5.3 KiB
JavaScript
Executable File
Raw Permalink Blame History

// const axios = require('axios');
// const { log } = require('./logger');
// /**
// * Logs messages with a shop context.
// * @param {string} shop - Shopify store name.
// * @param {string} message - Log message.
// */
// /**
// * Create a Shopify fulfillment service.
// * @param {string} shop - The Shopify store domain (e.g., myshop.myshopify.com).
// * @param {string} accessToken - The Shopify Admin API access token.
// * @returns {Promise<object>} The created fulfillment service or error details.
// */
// async function createFulfillmentService(shop, accessToken) {
// const client = axios.create({
// baseURL: `https://${shop}/admin/api/2024-01/graphql.json`,
// headers: {
// 'X-Shopify-Access-Token': accessToken,
// 'Content-Type': 'application/json',
// },
// });
// // const mutation = `
// // mutation {
// // fulfillmentServiceCreate(
// // name: "Data4Autos Distribution",
// // callbackUrl: "https://backend.data4autos.com/fulfillment"
// // ) {
// // fulfillmentService {
// // id
// // serviceName
// // callbackUrl
// // handle
// // location {
// // id
// // }
// // }
// // userErrors {
// // field
// // message
// // }
// // }
// // }
// // `;
// const mutation = `
// mutation {
// fulfillmentServiceCreate(
// name: "Data4Autos Distribution",
// callbackUrl: "https://backend.data4autos.com/fulfillment",
// fulfillmentOrdersOptIn: true,
// inventoryManagement: true,
// trackingSupport: true,
// requiresShippingMethod: true,
// permitsSkuSharing: true
// ) {
// fulfillmentService {
// id
// serviceName
// callbackUrl
// handle
// location {
// id
// }
// }
// userErrors {
// field
// message
// }
// }
// }
// `;
// try {
// log(shop, `🚀 Creating fulfillment service...`);
// const response = await client.post('', { query: mutation });
// const data = response.data.data.fulfillmentServiceCreate;
// if (data.userErrors && data.userErrors.length > 0) {
// log(shop, `â<>Œ User errors: ${JSON.stringify(data.userErrors)}`);
// return { success: false, errors: data.userErrors };
// }
// log(shop, `✅ Fulfillment Service created: ${JSON.stringify(data.fulfillmentService)}`);
// return { success: true, fulfillmentService: data.fulfillmentService };
// } catch (error) {
// log(shop, `💥 Request failed: ${error.response ? JSON.stringify(error.response.data) : error.message}`);
// return { success: false, error: error.response ? error.response.data : error.message };
// }
// }
// module.exports = { createFulfillmentService };
const axios = require('axios');
const { log } = require('./logger');
/**
* Create a Shopify fulfillment service.
* @param {string} shop - The Shopify store domain (e.g., myshop.myshopify.com).
* @param {string} accessToken - The Shopify Admin API access token.
* @returns {Promise<object>} The created fulfillment service or error details.
*/
async function createFulfillmentService(shop, accessToken) {
const client = axios.create({
baseURL: `https://${shop}/admin/api/2024-01/graphql.json`,
headers: {
'X-Shopify-Access-Token': accessToken,
'Content-Type': 'application/json',
},
});
const mutation = `
mutation {
fulfillmentServiceCreate(
name: "Data4Autos Distribution",
callbackUrl: "https://backend.data4autos.com/fulfillment",
fulfillmentOrdersOptIn: true,
inventoryManagement: true,
trackingSupport: true,
permitsSkuSharing: true
) {
fulfillmentService {
id
serviceName
callbackUrl
handle
location {
id
}
}
userErrors {
field
message
}
}
}
`;
try {
log(shop, `🚀 Creating fulfillment service...`);
const response = await client.post('', { query: mutation });
console.log(`Response from Shopify: ${JSON.stringify(response.data, null, 2)}`);
const data = response.data.data.fulfillmentServiceCreate;
if (data.userErrors && data.userErrors.length > 0) {
console.log(shop, `â<EFBFBD>Œ User errors: ${JSON.stringify(data.userErrors)}`);
return { success: false, errors: data.userErrors };
}
log(shop, `✅ Fulfillment Service created: ${JSON.stringify(data.fulfillmentService)}`);
return { success: true, fulfillmentService: data.fulfillmentService };
} catch (error) {
log(shop, `💥 Request failed: ${error.response ? JSON.stringify(error.response.data) : error.message}`);
return { success: false, error: error.response ? error.response.data : error.message };
}
}
module.exports = { createFulfillmentService };
// -------------------------------
// Test call (comment out later)
// -------------------------------
// (async () => {
// const shop = "veloxautomotive.myshopify.com"; // Replace with your shop domain
// const accessToken = "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"; // Replace with your token
// const result = await createFulfillmentService(shop, accessToken);
// console.log("Result:", result);
// })();