98 lines
2.8 KiB
JavaScript
Executable file
98 lines
2.8 KiB
JavaScript
Executable file
/**
|
|
* Executive Cockpit API Client V5.3 - Direct SQLite Bridge
|
|
* Secure token-based session management, real endpoints, no mocks.
|
|
*/
|
|
export class Api {
|
|
static getBaseUrl() {
|
|
return '/executive/api';
|
|
}
|
|
|
|
static getToken() {
|
|
return sessionStorage.getItem('exec_auth_token') || '';
|
|
}
|
|
|
|
static getHeaders() {
|
|
const headers = { 'Content-Type': 'application/json' };
|
|
const token = this.getToken();
|
|
if (token) {
|
|
headers['Authorization'] = `Bearer ${token}`;
|
|
}
|
|
return headers;
|
|
}
|
|
|
|
static async login(pin) {
|
|
const res = await fetch(`${this.getBaseUrl()}/auth`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ pin: pin.trim() })
|
|
});
|
|
const data = await res.json();
|
|
if (res.ok && data.success && data.token) {
|
|
sessionStorage.setItem('exec_auth_token', data.token);
|
|
sessionStorage.setItem('exec_role', data.role);
|
|
sessionStorage.setItem('exec_name', data.name);
|
|
return data;
|
|
}
|
|
throw new Error(data.error || 'کلید دسترسی نامعتبر است.');
|
|
}
|
|
|
|
static async getState() {
|
|
const res = await fetch(`${this.getBaseUrl()}/state`, {
|
|
headers: this.getHeaders()
|
|
});
|
|
if (!res.ok) throw new Error('خطا در دریافت وضعیت از سرور');
|
|
return await res.json();
|
|
}
|
|
|
|
static async updateTaskStatus(id, status, blockedReason = null) {
|
|
const res = await fetch(`${this.getBaseUrl()}/tasks`, {
|
|
method: 'POST',
|
|
headers: this.getHeaders(),
|
|
body: JSON.stringify({
|
|
action: 'update_status',
|
|
id,
|
|
status,
|
|
blocked_reason: blockedReason
|
|
})
|
|
});
|
|
if (!res.ok) throw new Error('خطا در بهروزرسانی وضعیت در دیتابیس');
|
|
return await res.json();
|
|
}
|
|
|
|
static async createTask(task) {
|
|
const res = await fetch(`${this.getBaseUrl()}/tasks`, {
|
|
method: 'POST',
|
|
headers: this.getHeaders(),
|
|
body: JSON.stringify({
|
|
action: 'create',
|
|
task
|
|
})
|
|
});
|
|
if (!res.ok) throw new Error('خطا در ثبت تسک در دیتابیس');
|
|
return await res.json();
|
|
}
|
|
|
|
static async smartNlpParse(text) {
|
|
const res = await fetch(`${this.getBaseUrl()}/smart-parse`, {
|
|
method: 'POST',
|
|
headers: this.getHeaders(),
|
|
body: JSON.stringify({ text })
|
|
});
|
|
if (!res.ok) throw new Error('خطا در پردازش هوشمند متن');
|
|
return await res.json();
|
|
}
|
|
|
|
static async getBriefing() {
|
|
const res = await fetch(`${this.getBaseUrl()}/briefing`, {
|
|
headers: this.getHeaders()
|
|
});
|
|
if (!res.ok) throw new Error('خطا در دریافت گزارش روزانه');
|
|
return await res.json();
|
|
}
|
|
|
|
static logout() {
|
|
sessionStorage.removeItem('exec_auth_token');
|
|
sessionStorage.removeItem('exec_role');
|
|
sessionStorage.removeItem('exec_name');
|
|
}
|
|
}
|