Commit 503cc251 by tdgiang

update code

parent 4bdc896a
......@@ -7,7 +7,10 @@ var tokenCache = { token: null, expiresAt: 0 };
function login(callback) {
var url = config.sobanle.base_url + "/login";
ApiRequest.postOtherUrl(url, { login: config.sobanle.username, password: config.sobanle.password }, function (err, body) {
ApiRequest.postOtherUrl(
url,
{ login: config.sobanle.username, password: config.sobanle.password },
function (err, body) {
if (err) {
return callback(err);
}
......@@ -22,7 +25,8 @@ function login(callback) {
var earlyRefreshMs = Math.min(60000, expiresInMs * 0.1);
tokenCache.expiresAt = Date.now() + expiresInMs - earlyRefreshMs;
callback(null, body.access_token);
});
},
);
}
exports.login = login;
......@@ -78,10 +82,19 @@ function withAuthRetry(makeRequest, callback) {
}
function getProducts(callback) {
withAuthRetry(function (token, cb) {
var url = config.sobanle.base_url + "/products?page=1&is_active=true&per_page=1000&";
ApiRequest.getOtherUrlWithHeader(url, {}, { Authorization: "Bearer " + token }, cb);
}, function (err, body) {
withAuthRetry(
function (token, cb) {
var url =
config.sobanle.base_url +
"/products?page=1&is_active=true&per_page=1000&";
ApiRequest.getOtherUrlWithHeader(
url,
{},
{ Authorization: "Bearer " + token },
cb,
);
},
function (err, body) {
if (err) {
return callback(err);
}
......@@ -89,12 +102,14 @@ function getProducts(callback) {
return callback(new Error("SOBANLE_PRODUCTS_INVALID"));
}
callback(null, body.data);
});
},
);
}
exports.getProducts = getProducts;
function createSale(lines, customerData, callback) {
withAuthRetry(function (token, cb) {
withAuthRetry(
function (token, cb) {
var url = config.sobanle.base_url + "/sales";
var payload = {
warehouse_id: config.sobanle.warehouse_id,
......@@ -111,15 +126,25 @@ function createSale(lines, customerData, callback) {
customer_data: customerData,
payment_receiver: "",
payment_note: "",
sale_note: "Tạo tự động từ payment-gate",
staff_note: "",
};
ApiRequest.postOtherUrlWithHeader(url, payload, { Authorization: "Bearer " + token }, cb);
}, function (err, body) {
ApiRequest.postOtherUrlWithHeader(
url,
payload,
{ Authorization: "Bearer " + token },
cb,
);
},
function (err, body) {
if (err) {
return callback(err);
}
if (!body || !body.data || typeof body.data.id === "undefined" || typeof body.data.grand_total === "undefined") {
if (
!body ||
!body.data ||
typeof body.data.id === "undefined" ||
typeof body.data.grand_total === "undefined"
) {
return callback(new Error("SOBANLE_CREATE_SALE_INVALID"));
}
var total = Math.round(Number(body.data.grand_total));
......@@ -127,18 +152,28 @@ function createSale(lines, customerData, callback) {
return callback(new Error("SOBANLE_CREATE_SALE_INVALID"));
}
callback(null, { orderId: body.data.id, orderTotal: total });
});
},
);
}
exports.createSale = createSale;
// saleStatus: 1 = giao dịch thành công, 4 = giao dịch thất bại (theo Sổ Bán Lẻ).
function changeSaleStatus(orderId, saleStatus, callback) {
withAuthRetry(function (token, cb) {
var url = config.sobanle.base_url + "/sales/change-sale-status/" + orderId;
ApiRequest.patchOtherUrlWithHeader(url, { sale_status: saleStatus }, { Authorization: "Bearer " + token }, cb);
}, function (err) {
withAuthRetry(
function (token, cb) {
var url =
config.sobanle.base_url + "/sales/change-sale-status/" + orderId;
ApiRequest.patchOtherUrlWithHeader(
url,
{ sale_status: saleStatus },
{ Authorization: "Bearer " + token },
cb,
);
},
function (err) {
callback(err || null);
});
},
);
}
exports.changeSaleStatus = changeSaleStatus;
......@@ -158,7 +193,12 @@ function createAutoOrder(customerData, targetAmount, callback) {
if (order.orderTotal > targetAmount) {
console.error(
"createAutoOrder: Sổ Bán Lẻ order total exceeds target (orphan order sobanleOrderId=" +
order.orderId + " orderTotal=" + order.orderTotal + " targetAmount=" + targetAmount + ")"
order.orderId +
" orderTotal=" +
order.orderTotal +
" targetAmount=" +
targetAmount +
")",
);
return callback(new Error("SOBANLE_TOTAL_EXCEEDS_TARGET"));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment