Skip to content

Instantly share code, notes, and snippets.

@shogochiai
Last active April 6, 2018 08:05
Show Gist options
  • Save shogochiai/5fa45042aaca90784ee3b4be392d74c1 to your computer and use it in GitHub Desktop.
Save shogochiai/5fa45042aaca90784ee3b4be392d74c1 to your computer and use it in GitHub Desktop.
/*
* This public doc will be uploaded on https://gist.github.com/shogochiai/5fa45042aaca90784ee3b4be392d74c1
* Requirement:
* - Higher than NodeJS-v8.0.0
* - Install command "npm i cryptr axios"
* - api_key and api_secret will be passed from VIPPLAZA
*/
const Cryptr = require('cryptr')
const axios = require('axios')
const api_key = '$2a$20$1cu/Xyu2vZpPj3eUkWkWCeqM0X5EIF/.3yApaEIy8GwBMWDmva5ZK'
const api_secret = '$2a$15$mvRcPX8np5czS2dW8tkz5u8HCY/o6f21iQxEBRh1/.OMlDJCMXMCS'
let http = axios.create({
baseURL: 'http://stocksync-public-api-intg.vipplaza.co.id/v1' // -intg is integration env, -prod is production
})
let getStock = async (from, to) => {
try {
const reply = await http.get(`/stock?from=${from}&to=${to}`, {headers: {'X-Vip-api_key' : api_key}})
return reply.data
} catch(err) {
return err
}
}
let updateStock = async (payload, product_id) => {
try {
// Salt/Seed is api_secret. Write action needs api_secret.
let cryptr = new Cryptr(api_secret);
// you must input what your change in params payload exp => payload = {qty: 10}
let body = cryptr.encrypt(JSON.stringify(payload))
// you must encrypt your payload to change your stock
let encryptedString = cryptr.encrypt( body + '*' + api_key)
// and you make changed in your stock like this.
const response = await http.put(`/stock/${product_id}`, {updateQty: encryptedString}, {headers: {'X-vip-client_id' : '-L93kd2pFTtf27phYNrH'}})
return response.data
} catch(err) {
return err
}
}
/*
* Main
*/
// 1. After order
// Get some signal of order_created
// For each product within order
updateStock([{sizeId: 'stkx-80ab8200-364f-11e8-b840-91b5dc91fbba', qty: 50}], 'prdx-7455b390-364f-11e8-bec8-997757c9f9d3')
.then(data=>{
// Success flow
console.log(data)
})
.catch(err=>{
console.error(err)
})
// 2. Every certain time frame (5min, 15min, 30min ... depends on customer OOS rate probabiliry)
getStock('2018-03-01T03:23:49.846Z', '2018-03-29T03:23:49.846Z')
.then(data=>{
// For each stock change on VIP side, do iteration and update stocks
console.log(data)
})
.catch(err=>{
console.error(err)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment