UNPKG

shopee-v2-api

Version:

429 lines (380 loc) 11 kB
/* -------------------------------------------------------------------------- */ /* Imports */ /* -------------------------------------------------------------------------- */ const _ = require('lodash'); /* -------------------------------------------------------------------------- */ /* Constants */ /* -------------------------------------------------------------------------- */ const { constants } = require('./constants'); /* -------------------------------------------------------------------------- */ /* Common / Auth */ /* -------------------------------------------------------------------------- */ const { buildSignKey, buildUrl, get, post, RefreshToken } = require('./auth'); /* -------------------------------------------------------------------------- */ /* API */ /* -------------------------------------------------------------------------- */ /* --------------------------- Discount Promotion --------------------------- */ /** * discount 프로모션 목록 조회 * @param {*} userShop * @param {*} offset * @param {*} pageSize * @returns */ exports.GetDiscountsList = async (userShop, offset, pageSize) => { try { // access token 유효 확인 let { shop_id, accessToken } = await RefreshToken(userShop); // uri const uri = '/api/v2/discount/get_discount_list'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, shop_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, shop_id: shop_id, discount_status: 'all', page_no: offset, page_size: pageSize }); // request && response data const responseData = await get(url); return responseData.response; } catch (err) { throw err; } } /** * discount 상품 목록 조회 * @param {*} userShop * @param {*} discountId * @param {*} offset * @param {*} pageSize * @returns */ exports.GetDiscount = async (userShop, discountId, offset, pageSize) => { try { // access token 유효 확인 let { shop_id, accessToken } = await RefreshToken(userShop); // uri const uri = '/api/v2/discount/get_discount'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, shop_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, shop_id: shop_id, discount_id: discountId, page_no: offset, page_size: pageSize }); // request && response data const responseData = await get(url); return responseData.response; } catch (err) { throw err; } } /** * discount 추가 * @param {*} userShop * @param {*} discountName * @param {*} startTime * @param {*} endTime * @returns */ exports.AddDiscount = async (userShop, discountName, startTime, endTime) => { try { // access token 유효 확인 let { shop_id, accessToken } = await RefreshToken(userShop); // uri const uri = '/api/v2/discount/add_discount'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, shop_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, shop_id: shop_id }); // request && response data const responseData = await post(url, JSON.stringify({ discount_name: discountName, start_time: startTime, end_time: endTime })); return responseData; } catch (err) { throw err; } } /** * discount 프로모션에 상품 등록 * @param {*} userShop * @param {*} discountId * @param {*} itemList * @returns */ exports.AddDiscountItem = async (userShop, discountId, itemList) => { try { // access token 유효 확인 let { shop_id, accessToken } = await RefreshToken(userShop); // uri const uri = '/api/v2/discount/add_discount_item'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, shop_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, shop_id: shop_id }); // request && response data const responseData = await post(url, JSON.stringify({ discount_id: discountId, item_list: itemList })); return responseData; } catch (err) { throw err; } } /** * discount 프로모션에서 상품 삭제 * @param {*} userShop * @param {*} discountId * @param {*} itemId * @param {*} modelId * @returns */ exports.DeleteDiscountItem = async (userShop, discountId, itemId, modelId = null) => { try { // access token 유효 확인 let { shop_id, accessToken } = await RefreshToken(userShop); // uri const uri = '/api/v2/discount/delete_discount_item'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, shop_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, shop_id: shop_id }); // request && response data const responseData = await post(url, JSON.stringify({ discount_id: discountId, item_id: itemId, model_id: modelId })); return responseData; } catch (err) { throw err; } } /** * discount 삭제 * @param {*} userShop * @param {*} discountId * @returns */ exports.DeleteDiscount = async (userShop, discountId) => { try { // access token 유효 확인 let { shop_id, accessToken } = await RefreshToken(userShop); // uri const uri = '/api/v2/discount/delete_discount'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, shop_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, shop_id: shop_id }); // request && response data const responseData = await post(url, JSON.stringify({ discount_id: discountId, })); return responseData; } catch (err) { throw err; } } /** * discount 수정 * @param {*} userShop * @param {*} discountId * @param {*} discountName * @param {*} startTime * @param {*} endTime * @returns */ exports.UpdateDiscount = async (userShop, discountId, discountName, startTime, endTime) => { try { // access token 유효 확인 let { shop_id, accessToken } = await RefreshToken(userShop); // uri const uri = '/api/v2/discount/update_discount'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, shop_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, shop_id: shop_id }); // request && response data const responseData = await post(url, JSON.stringify({ discount_id: discountId, discount_name: discountName, start_time: startTime, end_time: endTime })); return responseData; } catch (err) { throw err; } } /** * discount 상품 목록 수정 * @param {*} userShop * @param {*} discountId * @param {*} itemList * @returns */ exports.UpdateDiscountItem = async (userShop, discountId, itemList) => { try { // access token 유효 확인 let { shop_id, accessToken } = await RefreshToken(userShop); // uri const uri = '/api/v2/discount/update_discount_item'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, shop_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, shop_id: shop_id }); // request && response data const responseData = await post(url, JSON.stringify({ discount_id: discountId, item_list: itemList })); return responseData; } catch (err) { throw err; } } /** * discount 중료 * @param {*} userShop * @param {*} discountId * @returns */ exports.EndDiscount = async (userShop, discountId) => { try { // access token 유효 확인 let { shop_id, accessToken } = await RefreshToken(userShop); // uri const uri = '/api/v2/discount/end_discount'; // sign key const { signKey, currentTimeStamp } = buildSignKey(uri, accessToken, shop_id); // url const url = buildUrl(uri, { partner_id: constants.partnerId, timestamp: currentTimeStamp, sign: signKey, access_token: accessToken, shop_id: shop_id }); // request && response data const responseData = await post(url, JSON.stringify({ discount_id: discountId, })); return responseData; } catch (err) { throw err; } }