Sign in to SystemGroup per user on the orders page

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-11 14:53:44 +03:30
parent 1910e7560f
commit b8c4e703fb
11 changed files with 408 additions and 42 deletions

View File

@@ -401,6 +401,15 @@ form {
padding-right: 0;
}
#crm-login {
max-width: 22rem;
}
/* .form-stack { display: flex } would otherwise override the hidden attribute. */
#crm-login[hidden] {
display: none !important;
}
#add-custom-line {
align-self: flex-start;
}

View File

@@ -1,6 +1,7 @@
const seenOrderKeys = new Set();
let ordersLoaded = false;
let lastSince = '';
let lastUsername = '';
let lastRows = null;
let productCodes = new Set();
@@ -72,7 +73,35 @@ function renderSince() {
el.textContent = lastSince;
}
function loginForm() {
return document.getElementById('crm-login');
}
function showLogin() {
loginForm().hidden = false;
document.getElementById('crm-refresh').hidden = true;
document.getElementById('crm-forget').hidden = true;
document.getElementById('crm-account').hidden = true;
document.getElementById('crm-count').hidden = true;
lastRows = null;
lastUsername = '';
ordersLoaded = false;
}
function showSession() {
loginForm().hidden = true;
document.getElementById('crm-password').value = '';
document.getElementById('crm-refresh').hidden = false;
document.getElementById('crm-forget').hidden = false;
document.getElementById('crm-count').hidden = false;
const account = document.getElementById('crm-account');
account.hidden = !lastUsername;
account.textContent = lastUsername ? `${t('sg_signed_in')} ${lastUsername}` : '';
}
function renderChrome() {
if (!loginForm().hidden) setStatus(t('sg_need_login'));
if (lastUsername) showSession();
renderSince();
if (lastRows) renderOrders(lastRows, false);
}
@@ -138,32 +167,51 @@ function setStatus(message) {
status.textContent = message;
}
async function fetchProducts() {
const res = await fetch('/api/products');
if (!res.ok) return;
const products = await res.json().catch(() => []);
productCodes = new Set((products || []).map((p) => (p.code || '').trim()));
}
async function applyOrdersResponse(ordersRes) {
const data = await ordersRes.json().catch(() => ({}));
if (ordersRes.status === 401) {
const rejected = data.code === 'login_rejected';
seenOrderKeys.clear();
lastSince = '';
renderSince();
showLogin();
setStatus(t('sg_need_login'));
if (rejected) showToast(t('sg_rejected'), true);
return;
}
if (!ordersRes.ok) {
const message = data.detail || t('no_crm_orders');
if (!ordersLoaded) setStatus(message);
showToast(message, true);
return;
}
lastSince = data.since || '';
lastUsername = data.username || '';
lastRows = data.orders || [];
showSession();
renderSince();
const added = renderOrders(lastRows, ordersLoaded);
ordersLoaded = true;
if (added > 0) showToast(t('crm_new'));
}
async function loadCrmOrders() {
const button = document.getElementById('crm-refresh');
button.disabled = true;
if (!ordersLoaded) setStatus(t('crm_loading'));
try {
const [ordersRes, productsRes] = await Promise.all([
const [ordersRes] = await Promise.all([
fetch('/api/crm-orders', { cache: 'no-store' }),
fetch('/api/products'),
fetchProducts(),
]);
if (productsRes.ok) {
const products = await productsRes.json().catch(() => []);
productCodes = new Set((products || []).map((p) => (p.code || '').trim()));
}
const data = await ordersRes.json().catch(() => ({}));
if (!ordersRes.ok) {
const message = data.detail || t('no_crm_orders');
if (!ordersLoaded) setStatus(message);
showToast(message, true);
return;
}
lastSince = data.since || '';
lastRows = data.orders || [];
renderSince();
const added = renderOrders(lastRows, ordersLoaded);
ordersLoaded = true;
if (added > 0) showToast(t('crm_new'));
await applyOrdersResponse(ordersRes);
} catch (err) {
setStatus(t('no_crm_orders'));
showToast('Network error', true);
@@ -172,8 +220,58 @@ async function loadCrmOrders() {
}
}
async function signIn(event) {
event.preventDefault();
const button = document.getElementById('crm-sign-in');
button.disabled = true;
setStatus(t('crm_loading'));
try {
const [ordersRes] = await Promise.all([
fetch('/api/crm-orders/session', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
cache: 'no-store',
body: JSON.stringify({
username: document.getElementById('crm-username').value.trim(),
password: document.getElementById('crm-password').value,
}),
}),
fetchProducts(),
]);
await applyOrdersResponse(ordersRes);
} catch (err) {
setStatus(t('sg_need_login'));
showToast('Network error', true);
} finally {
button.disabled = false;
}
}
async function signOut() {
const button = document.getElementById('crm-forget');
button.disabled = true;
try {
const res = await fetch('/api/crm-orders/session', { method: 'DELETE' });
if (!res.ok) {
showToast('Network error', true);
return;
}
seenOrderKeys.clear();
lastSince = '';
renderSince();
showLogin();
setStatus(t('sg_need_login'));
} catch (err) {
showToast('Network error', true);
} finally {
button.disabled = false;
}
}
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('crm-refresh').addEventListener('click', loadCrmOrders);
document.getElementById('crm-forget').addEventListener('click', signOut);
loginForm().addEventListener('submit', signIn);
loadCrmOrders();
});

View File

@@ -57,6 +57,13 @@ const translations = {
'remaining_qty': 'Remaining',
'product_title': 'Title',
'crm_loading': 'Loading orders…',
'sg_username': 'Username',
'sg_password': 'Password',
'sg_sign_in': 'Sign in',
'sg_sign_out': 'Sign out',
'sg_signed_in': 'Signed in as',
'sg_need_login': 'Sign in to load orders.',
'sg_rejected': 'SystemGroup rejected this username or password.',
'no_crm_orders': 'No orders after this date',
'orders_after': 'Orders after',
'crm_new': 'New orders added',
@@ -129,6 +136,13 @@ const translations = {
'remaining_qty': 'مانده',
'product_title': 'عنوان',
'crm_loading': 'در حال دریافت سفارش‌ها…',
'sg_username': 'نام کاربری',
'sg_password': 'رمز عبور',
'sg_sign_in': 'ورود',
'sg_sign_out': 'خروج',
'sg_signed_in': 'کاربر',
'sg_need_login': 'برای دیدن حواله‌ها وارد شوید.',
'sg_rejected': 'نام کاربری یا رمز سامانه گروه نادرست است.',
'no_crm_orders': 'سفارشی بعد از این تاریخ نیست',
'orders_after': 'حواله‌های بعد از',
'crm_new': 'سفارش جدید اضافه شد',