Commit 503cc251 by tdgiang

update code

parent 4bdc896a
......@@ -7,22 +7,26 @@ 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) {
if (err) {
return callback(err);
}
if (!body || !body.access_token) {
return callback(new Error("SOBANLE_LOGIN_FAILED"));
}
tokenCache.token = body.access_token;
var expiresInMs = (body.expires_in || 0) * 1000;
// 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
// 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);
});
ApiRequest.postOtherUrl(
url,
{ login: config.sobanle.username, password: config.sobanle.password },
function (err, body) {
if (err) {
return callback(err);
}
if (!body || !body.access_token) {
return callback(new Error("SOBANLE_LOGIN_FAILED"));
}
tokenCache.token = body.access_token;
var expiresInMs = (body.expires_in || 0) * 1000;
// 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
// 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;
......@@ -78,67 +82,98 @@ 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) {
if (err) {
return callback(err);
}
if (!body || !Array.isArray(body.data)) {
return callback(new Error("SOBANLE_PRODUCTS_INVALID"));
}
callback(null, body.data);
});
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);
}
if (!body || !Array.isArray(body.data)) {
return callback(new Error("SOBANLE_PRODUCTS_INVALID"));
}
callback(null, body.data);
},
);
}
exports.getProducts = getProducts;
function createSale(lines, customerData, callback) {
withAuthRetry(function (token, cb) {
var url = config.sobanle.base_url + "/sales";
var payload = {
warehouse_id: config.sobanle.warehouse_id,
biller_id: config.sobanle.biller_id,
account_id: config.sobanle.account_id,
currency_id: config.sobanle.currency_id,
exchange_rate: "1",
reference_no: null,
is_internal_api: true,
sale_status: 2,
list_product: lines.map(function (line) {
return { product_id: String(line.product_id), qty: String(line.qty) };
}),
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) {
if (err) {
return callback(err);
}
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));
if (!isFinite(total) || total <= 0) {
return callback(new Error("SOBANLE_CREATE_SALE_INVALID"));
}
callback(null, { orderId: body.data.id, orderTotal: total });
});
withAuthRetry(
function (token, cb) {
var url = config.sobanle.base_url + "/sales";
var payload = {
warehouse_id: config.sobanle.warehouse_id,
biller_id: config.sobanle.biller_id,
account_id: config.sobanle.account_id,
currency_id: config.sobanle.currency_id,
exchange_rate: "1",
reference_no: null,
is_internal_api: true,
sale_status: 2,
list_product: lines.map(function (line) {
return { product_id: String(line.product_id), qty: String(line.qty) };
}),
customer_data: customerData,
payment_receiver: "",
payment_note: "",
staff_note: "",
};
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"
) {
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;
// 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) {
callback(err || null);
});
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