Commit 503cc251 by tdgiang

update code

parent 4bdc896a
...@@ -7,22 +7,26 @@ var tokenCache = { token: null, expiresAt: 0 }; ...@@ -7,22 +7,26 @@ var tokenCache = { token: null, expiresAt: 0 };
function login(callback) { function login(callback) {
var url = config.sobanle.base_url + "/login"; var url = config.sobanle.base_url + "/login";
ApiRequest.postOtherUrl(url, { login: config.sobanle.username, password: config.sobanle.password }, function (err, body) { ApiRequest.postOtherUrl(
if (err) { url,
return callback(err); { login: config.sobanle.username, password: config.sobanle.password },
} function (err, body) {
if (!body || !body.access_token) { if (err) {
return callback(new Error("SOBANLE_LOGIN_FAILED")); return callback(err);
} }
tokenCache.token = body.access_token; if (!body || !body.access_token) {
var expiresInMs = (body.expires_in || 0) * 1000; return callback(new Error("SOBANLE_LOGIN_FAILED"));
// Refresh a bit early to avoid racing a near-expiry token. Headroom scales }
// with the token's own TTL (capped at 60s) rather than a flat 60s, so a tokenCache.token = body.access_token;
// short-TTL token isn't already "expired" the instant it's cached. var expiresInMs = (body.expires_in || 0) * 1000;
var earlyRefreshMs = Math.min(60000, expiresInMs * 0.1); // Refresh a bit early to avoid racing a near-expiry token. Headroom scales
tokenCache.expiresAt = Date.now() + expiresInMs - earlyRefreshMs; // with the token's own TTL (capped at 60s) rather than a flat 60s, so a
callback(null, body.access_token); // short-TTL token isn't already "expired" the instant it's cached.
}); var earlyRefreshMs = Math.min(60000, expiresInMs * 0.1);
tokenCache.expiresAt = Date.now() + expiresInMs - earlyRefreshMs;
callback(null, body.access_token);
},
);
} }
exports.login = login; exports.login = login;
...@@ -78,67 +82,98 @@ function withAuthRetry(makeRequest, callback) { ...@@ -78,67 +82,98 @@ function withAuthRetry(makeRequest, callback) {
} }
function getProducts(callback) { function getProducts(callback) {
withAuthRetry(function (token, cb) { withAuthRetry(
var url = config.sobanle.base_url + "/products?page=1&is_active=true&per_page=1000&"; function (token, cb) {
ApiRequest.getOtherUrlWithHeader(url, {}, { Authorization: "Bearer " + token }, cb); var url =
}, function (err, body) { config.sobanle.base_url +
if (err) { "/products?page=1&is_active=true&per_page=1000&";
return callback(err); ApiRequest.getOtherUrlWithHeader(
} url,
if (!body || !Array.isArray(body.data)) { {},
return callback(new Error("SOBANLE_PRODUCTS_INVALID")); { Authorization: "Bearer " + token },
} cb,
callback(null, body.data); );
}); },
function (err, body) {
if (err) {
return callback(err);
}
if (!body || !Array.isArray(body.data)) {
return callback(new Error("SOBANLE_PRODUCTS_INVALID"));
}
callback(null, body.data);
},
);
} }
exports.getProducts = getProducts; exports.getProducts = getProducts;
function createSale(lines, customerData, callback) { function createSale(lines, customerData, callback) {
withAuthRetry(function (token, cb) { withAuthRetry(
var url = config.sobanle.base_url + "/sales"; function (token, cb) {
var payload = { var url = config.sobanle.base_url + "/sales";
warehouse_id: config.sobanle.warehouse_id, var payload = {
biller_id: config.sobanle.biller_id, warehouse_id: config.sobanle.warehouse_id,
account_id: config.sobanle.account_id, biller_id: config.sobanle.biller_id,
currency_id: config.sobanle.currency_id, account_id: config.sobanle.account_id,
exchange_rate: "1", currency_id: config.sobanle.currency_id,
reference_no: null, exchange_rate: "1",
is_internal_api: true, reference_no: null,
sale_status: 2, is_internal_api: true,
list_product: lines.map(function (line) { sale_status: 2,
return { product_id: String(line.product_id), qty: String(line.qty) }; list_product: lines.map(function (line) {
}), return { product_id: String(line.product_id), qty: String(line.qty) };
customer_data: customerData, }),
payment_receiver: "", customer_data: customerData,
payment_note: "", payment_receiver: "",
sale_note: "Tạo tự động từ payment-gate", payment_note: "",
staff_note: "", staff_note: "",
}; };
ApiRequest.postOtherUrlWithHeader(url, payload, { Authorization: "Bearer " + token }, cb); ApiRequest.postOtherUrlWithHeader(
}, function (err, body) { url,
if (err) { payload,
return callback(err); { Authorization: "Bearer " + token },
} cb,
if (!body || !body.data || typeof body.data.id === "undefined" || typeof body.data.grand_total === "undefined") { );
return callback(new Error("SOBANLE_CREATE_SALE_INVALID")); },
} function (err, body) {
var total = Math.round(Number(body.data.grand_total)); if (err) {
if (!isFinite(total) || total <= 0) { return callback(err);
return callback(new Error("SOBANLE_CREATE_SALE_INVALID")); }
} if (
callback(null, { orderId: body.data.id, orderTotal: total }); !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));
if (!isFinite(total) || total <= 0) {
return callback(new Error("SOBANLE_CREATE_SALE_INVALID"));
}
callback(null, { orderId: body.data.id, orderTotal: total });
},
);
} }
exports.createSale = createSale; 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ẻ). // 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) { function changeSaleStatus(orderId, saleStatus, callback) {
withAuthRetry(function (token, cb) { withAuthRetry(
var url = config.sobanle.base_url + "/sales/change-sale-status/" + orderId; function (token, cb) {
ApiRequest.patchOtherUrlWithHeader(url, { sale_status: saleStatus }, { Authorization: "Bearer " + token }, cb); var url =
}, function (err) { config.sobanle.base_url + "/sales/change-sale-status/" + orderId;
callback(err || null); ApiRequest.patchOtherUrlWithHeader(
}); url,
{ sale_status: saleStatus },
{ Authorization: "Bearer " + token },
cb,
);
},
function (err) {
callback(err || null);
},
);
} }
exports.changeSaleStatus = changeSaleStatus; exports.changeSaleStatus = changeSaleStatus;
...@@ -158,7 +193,12 @@ function createAutoOrder(customerData, targetAmount, callback) { ...@@ -158,7 +193,12 @@ function createAutoOrder(customerData, targetAmount, callback) {
if (order.orderTotal > targetAmount) { if (order.orderTotal > targetAmount) {
console.error( console.error(
"createAutoOrder: Sổ Bán Lẻ order total exceeds target (orphan order sobanleOrderId=" + "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")); 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