UNPKG

shopee-v2-api

Version:

1,301 lines (1,159 loc) 35 kB
/* -------------------------------------------------------------------------- */ /* Imports */ /* -------------------------------------------------------------------------- */ const _ = require('lodash'); /* -------------------------------------------------------------------------- */ /* Constants */ /* -------------------------------------------------------------------------- */ const { constants } = require('./constants'); /* -------------------------------------------------------------------------- */ /* Common / Auth */ /* -------------------------------------------------------------------------- */ const { buildSignKey, buildUrl, get, post, GPRefreshToken } = require('./auth'); /* -------------------------------------------------------------------------- */ /* API */ /* -------------------------------------------------------------------------- */ exports.GPItemLimit = async (userMerchant) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/get_global_item_limit'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id }); // request && response data const responseData = await get(url); if (responseData.response) { return responseData.response; } else { return null; } } catch (err) { throw err; } } /** * 상품 카테고리 목록 조회 * @param {*} userMerchant * @returns */ exports.GPGetCategories = async (userMerchant) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/get_category'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id, language: 'en' }); // request && response data const responseData = await get(url); if (responseData.response && responseData.response.category_list) { return responseData.response.category_list.map((category) => ({ category_id: category.category_id, parent_id: category.parent_category_id, category_name: category.original_category_name, has_children: category.has_children, country: userMerchant.country, marketPlace: userMerchant.marketPlace })); } else { return []; } } catch (err) { throw err; } } /** * 속성 정보 조회 * @param {*} userMerchant * @param {*} queries * @returns */ exports.GPGetAttributes = async (userMerchant, queries) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/get_attributes'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, _.assign({ partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id }, queries)); // request && response data const responseData = await get(url); if (responseData.response && responseData.response.attribute_list) { return responseData.response.attribute_list; } else { return []; } } catch (err) { throw err; } } /** * 브랜드 목록 * @param {*} userMerchant * @param {*} queries * @returns */ exports.GPGetBrandList = async (userMerchant, queries) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/get_brand_list'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); let brandAttribute = {}; let hasMore = true; let offset = 0; while (hasMore) { // url const url = buildUrl(uri, _.assign({ partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id }, _.assign( queries, { page_size: 100, status: 1, offset: offset } ))); // request && response data const responseData = await get(url); if (responseData.response && responseData.response.brand_list) { // 브랜드 추가 if (!brandAttribute.brand_list) { brandAttribute.brand_list = []; } brandAttribute.brand_list = brandAttribute.brand_list.concat(responseData.response.brand_list); brandAttribute = _.assign(brandAttribute, { is_mandatory: responseData.response.is_mandatory, input_type: responseData.response.input_type, }); hasMore = responseData.response.has_next_page; offset = responseData.response.next_offset; } else { hasMore = false; } } return brandAttribute; } catch (err) { throw err; } } /** * 추천 카테고리 * @param {*} userMerchant * @param {*} itemName * @returns */ exports.GPGetRecommendCats = async (userMerchant, itemName) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/category_recommend'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id, global_item_name: encodeURIComponent(itemName) }); // request && response data const responseData = await get(url); // 데이터 형식 변경 if (responseData.response && responseData.response.category_id) { return responseData.response.category_id; } else { return []; } } catch (err) { throw err; } } /** * GP 상품 등록 제한 조회 * @param {*} userMerchant * @param {*} category_id * @returns */ exports.GPGetItemLimit = async (userMerchant, category_id) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/get_global_item_limit'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id, category_id: category_id }); // request && response data const responseData = await get(url); // 데이터 형식 변경 if (responseData.response) { return responseData.response; } else { return null; } } catch (err) { throw err; } } /** * 사이즈 차트 지원 유무 * @param {*} userMerchant * @param {*} category_id * @returns */ exports.GPGetSupportSizeChart = async (userMerchant, category_id) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/support_size_chart'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id, category_id: category_id }); // request && response data const responseData = await get(url); // 데이터 형식 변경 if (responseData.response) { return responseData.response; } else { return null; } } catch (err) { throw err; } } /** * 사이즈 차트 업데이트 * @param {*} userMerchant * @param {*} item_id * @param {*} size_chart * @returns */ exports.GPUpdateSizeChart = async (userMerchant, itemId, size_chart) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/support_size_chart'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id, }); // request && response data const responseData = await post(url, JSON.stringify({ global_item_id: itemId, size_chart: size_chart })); // 결과 if (responseData.request_id) { return true; } else { return null; } } catch (err) { throw err; } } /** * 상품 등록 * @param {*} userMerchant * @param {*} item * @returns */ exports.GPAddItem = async (userMerchant, item) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/add_global_item'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id, }); // request && response data const responseData = await post(url, JSON.stringify(item)); // 데이터 형식 변경 if (responseData.response) { return responseData.response; } else { return responseData; } } catch (err) { throw err; } } /** * 상품 수정 * @param {*} userMerchant * @param {*} item * @returns */ exports.GPUpdateItem = async (userMerchant, item) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/update_global_item'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id }); // request && response data const responseData = await post(url, JSON.stringify(item)); return responseData; } catch (err) { throw err; } } /** * 상품 삭제 * @param {*} userMerchant * @param {*} item_id * @returns */ exports.GPDeleteItem = async (userMerchant, globalItemId) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/delete_global_item'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id, }); // request && response data const responseData = await post(url, JSON.stringify({ global_item_id: globalItemId })); return responseData; } catch (err) { throw err; } } /** * 가격 수정 * @param {*} userMerchant * @param {*} item_id * @param {*} priceList * @returns */ exports.GPUpdatePrice = async (userMerchant, globalItemId, priceList) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/update_price'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id, }); // request && response data const responseData = await post(url, JSON.stringify({ global_item_id: globalItemId, price_list: priceList // global_model_id, original_price })); return responseData } catch (err) { throw err; } } /** * 재고 수정 * @param {*} userMerchant * @param {*} item_id * @param {*} stockList * @returns */ exports.GPUpdateStock = async (userMerchant, globalItemId, stockList) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/update_stock'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id }); // request && response data const responseData = await post(url, JSON.stringify({ global_item_id: globalItemId, stock_list: stockList // global_model_id, normal_stock })); return responseData; } catch (err) { throw err; } } /** * 상품 목록 * @param {*} userMerchant * @param {*} offset * @param {*} itemPerPage * @returns */ exports.GPGetItemList = async (userMerchant, offset, itemPerPage) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/get_global_item_list'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id, offset: offset, page_size: itemPerPage, }); // request && response data const responseData = await get(url); if (responseData.response) { return responseData.response; } else { return null; } } catch (err) { throw err; } } /** * 상품 상세 정보 * @param {*} userMerchant * @returns */ exports.GPGetItemDetail = async (userMerchant, itemId) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/get_global_item_info'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id, global_item_id_list: [itemId] }); // request && response data const responseData = await get(url); // 데이터 형식 변경 if (responseData.response && responseData.response.global_item_list) { return responseData.response.global_item_list.map((item) => ({ global_item_id: item.global_item_id, category_id: item.category_id, name: item.global_item_name, description: item.description, item_sku: item.global_item_sku, status: item.global_item_status, create_time: item.create_time, update_time: item.update_time, price: item.price_info ? item.price_info[0].current_price : 0, stock: item.stock_info ? item.stock_info[0].normal_stock : null, images: item.image.image_url_list, has_variation: item.has_model, promotion_id: item.promotion_id, brand: item.brand, attributes: item.attribute_list, dimension: item.dimension, weight: item.weight, pre_order: item.pre_order, size_chart: item.size_chart, }))[0]; } else { return null; } } catch (err) { throw err; } } /** * 상품들 상세 정보 * @param {*} userMerchant * @returns */ exports.GPGetItemDetails = async (userMerchant, itemIds) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/get_global_item_info'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id, global_item_id_list: itemIds }); // request && response data const responseData = await get(url); // 데이터 형식 변경 if (responseData.response && responseData.response.global_item_list) { return responseData.response.global_item_list.map((item) => ({ global_item_id: item.global_item_id, category_id: item.category_id, name: item.global_item_name, description: item.description, item_sku: item.global_item_sku, status: item.global_item_status, create_time: item.create_time, update_time: item.update_time, price: item.price_info[0].current_price, stock: item.stock_info[0].normal_stock, images: item.image.image_url_list, has_variation: item.has_model, promotion_id: item.promotion_id }))[0]; } else { return null; } } catch (err) { throw err; } } /** * 옵션 조회 * @param {*} userMerchant * @param {*} itemId * @returns */ exports.GPGetVariations = async (userMerchant, globalItemId) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/get_global_model_list'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id, global_item_id: globalItemId }); // request && response data const responseData = await get(url); if (responseData.response && responseData.response.global_model) { return { tier_variation: responseData.response.tier_variation.map((tier) => ({ name: tier.name, options: tier.option_list.map((optionData) => optionData.option), images_url: tier.option_list.map((optionData) => optionData.image ? optionData.image.image_url : null) })), variations: responseData.response.global_model.map((model) => ({ global_variation_id: model.global_model_id, tier_index: model.tier_index, variation_sku: model.global_model_sku, stock: model.stock_info[0].normal_stock, price: model.price_info.original_price, })), standardise_tier_variation: responseData.response.standardise_tier_variation }; } else { return {}; } } catch (err) { throw err; } } /** * 옵션 삭제 * @param {*} userMerchant * @param {*} itemId * @param {*} modelId * @returns */ exports.GPDeleteVariation = async (userMerchant, globalItemId, globalModelId) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/delete_global_model'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id }); // request && response data const responseData = await post(url, JSON.stringify({ global_item_id: globalItemId, global_model_id: globalModelId })); return responseData; } catch (err) { throw err; } } /** * 옵션 초기화 * @param {*} userMerchant * @param {*} itemId * @param {*} tierVariation * @param {*} model * @returns */ exports.GPInitTierVariation = async (userMerchant, globalItemId, tierVariation, globalModel) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/init_tier_variation'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id }); // request && response data const responseData = await post(url, JSON.stringify({ global_item_id: globalItemId, tier_variation: tierVariation, global_model: globalModel })); return responseData; } catch (err) { throw err; } } /** * 옵션(tier_variation) 정렬 - 추가, 삭제, 수정 * @param {*} userMerchant * @param {*} itemId * @param {*} tierVariation * @returns */ exports.GPUpdateTierVariation = async (userMerchant, globalItemId, tierVariation) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/update_tier_variation'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id }); // request &&response data const responseData = await post(url, JSON.stringify({ global_item_id: globalItemId, tier_variation: tierVariation })); return responseData; } catch (err) { throw err; } } /** * 옵션 sku 수정 * @param {*} userMerchant * @param {*} itemId * @param {*} model * @returns */ exports.GPUpdateVariation = async (userMerchant, globalItemId, model) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/update_global_model'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id }); // request && response data const responseData = await post(url, JSON.stringify({ global_item_id: globalItemId, global_model: model })); return responseData; } catch (err) { throw err; } } /** * 옵션 추가 * @param {*} userMerchant * @param {*} itemId * @param {*} model * @returns */ exports.GPAddVariation = async (userMerchant, globalItemId, model) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/add_global_model'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id }); // request && response data const responseData = await post(url, JSON.stringify({ global_item_id: globalItemId, global_model: model })); return responseData; } catch (err) { throw err; } } /** * 배포 가능 샵 목록 조회 * @param {*} userMerchant * @returns */ exports.GetPublishableShop = async (userMerchant, globalItemId) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/get_publishable_shop'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id, global_item_id: globalItemId }); // request && response data const responseData = await get(url); if (responseData.response && responseData.response.publishable_shop) { return responseData.response.publishable_shop; } else { return []; } } catch (err) { throw err; } } /** * GP 상품 배포 * @param {*} userMerchant * @param {*} itemId * @param {*} shopId * @param {*} country * @param {*} item * @returns */ exports.GPCreatePublishTask = async (userMerchant, globalItemId, shopId, country, item) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/create_publish_task'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id }); // request &&response data const responseData = await post(url, JSON.stringify({ global_item_id: globalItemId, shop_id: shopId, shop_region: country, item: item })); return responseData; } catch (err) { throw err; } } /** * 배포 결과 조회 * @param {*} userMerchant * @param {*} publishTaskId * @returns */ exports.GPGetPublishTaskResult = async (userMerchant, publishTaskId) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/get_publish_task_result'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id, publish_task_id: publishTaskId }); // request && response data const responseData = await get(url); if (responseData.response) { return responseData.response; } else { return responseData; } } catch (err) { throw err; } } exports.GPGetPublishedList = async (userMerchant, globalItemId) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/get_published_list'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id, global_item_id: globalItemId }); // request && response data const responseData = await get(url); if (responseData.response) { return responseData.response; } else { return responseData; } } catch (err) { throw err; } } /** * GP 상품 ID 조회 * @param {*} userMerchant * @param {*} itemId * @returns */ exports.GPGetGlobalItemId = async (userMerchant, shopId, itemIds) => { try { // access token 유효 확인 let { merchant_id, accessToken } = await GPRefreshToken(userMerchant); // uri const uri = '/api/v2/global_product/get_global_item_id'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, merchant_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, merchant_id: merchant_id, shop_id: shopId, item_id_list: itemIds }); // request && response data const responseData = await get(url); return responseData.response; } catch (err) { throw err; } };