synch correction and db update
This commit is contained in:
@@ -49,17 +49,17 @@ export async function deleteSubCategory(id) {
|
||||
* ═══════════════════════════════════════════════════════════════════════════ */
|
||||
|
||||
export async function listSeverities() {
|
||||
return query('SELECT id, value FROM admin_severities ORDER BY value ASC');
|
||||
return query('SELECT id, value, color FROM admin_severities ORDER BY value ASC');
|
||||
}
|
||||
|
||||
export async function createSeverity(value) {
|
||||
const result = await query('INSERT INTO admin_severities (value) VALUES (?)', [value]);
|
||||
return { id: Number(result.insertId), value };
|
||||
export async function createSeverity(value, color = null) {
|
||||
const result = await query('INSERT INTO admin_severities (value, color) VALUES (?, ?)', [value, color || null]);
|
||||
return { id: Number(result.insertId), value, color: color || null };
|
||||
}
|
||||
|
||||
export async function updateSeverity(id, value) {
|
||||
await query('UPDATE admin_severities SET value = ? WHERE id = ?', [value, id]);
|
||||
return { id, value };
|
||||
export async function updateSeverity(id, value, color = null) {
|
||||
await query('UPDATE admin_severities SET value = ?, color = ? WHERE id = ?', [value, color || null, id]);
|
||||
return { id, value, color: color || null };
|
||||
}
|
||||
|
||||
export async function deleteSeverity(id) {
|
||||
@@ -71,18 +71,18 @@ export async function deleteSeverity(id) {
|
||||
* ═══════════════════════════════════════════════════════════════════════════ */
|
||||
|
||||
export async function listStatuses() {
|
||||
const rows = await query('SELECT id, value, require_handled_by, require_comment FROM admin_statuses ORDER BY value ASC');
|
||||
return rows.map(r => ({ id: r.id, value: r.value, requireHandledBy: !!r.require_handled_by, requireComment: !!r.require_comment }));
|
||||
const rows = await query('SELECT id, value, require_handled_by, require_comment, color FROM admin_statuses ORDER BY value ASC');
|
||||
return rows.map(r => ({ id: r.id, value: r.value, requireHandledBy: !!r.require_handled_by, requireComment: !!r.require_comment, color: r.color || null }));
|
||||
}
|
||||
|
||||
export async function createStatus(value, requireHandledBy = false, requireComment = false) {
|
||||
const result = await query('INSERT INTO admin_statuses (value, require_handled_by, require_comment) VALUES (?, ?, ?)', [value, requireHandledBy ? 1 : 0, requireComment ? 1 : 0]);
|
||||
return { id: Number(result.insertId), value, requireHandledBy, requireComment };
|
||||
export async function createStatus(value, requireHandledBy = false, requireComment = false, color = null) {
|
||||
const result = await query('INSERT INTO admin_statuses (value, require_handled_by, require_comment, color) VALUES (?, ?, ?, ?)', [value, requireHandledBy ? 1 : 0, requireComment ? 1 : 0, color || null]);
|
||||
return { id: Number(result.insertId), value, requireHandledBy, requireComment, color: color || null };
|
||||
}
|
||||
|
||||
export async function updateStatus(id, value, requireHandledBy = false, requireComment = false) {
|
||||
await query('UPDATE admin_statuses SET value = ?, require_handled_by = ?, require_comment = ? WHERE id = ?', [value, requireHandledBy ? 1 : 0, requireComment ? 1 : 0, id]);
|
||||
return { id, value, requireHandledBy, requireComment };
|
||||
export async function updateStatus(id, value, requireHandledBy = false, requireComment = false, color = null) {
|
||||
await query('UPDATE admin_statuses SET value = ?, require_handled_by = ?, require_comment = ?, color = ? WHERE id = ?', [value, requireHandledBy ? 1 : 0, requireComment ? 1 : 0, color || null, id]);
|
||||
return { id, value, requireHandledBy, requireComment, color: color || null };
|
||||
}
|
||||
|
||||
export async function deleteStatus(id) {
|
||||
@@ -191,24 +191,33 @@ export async function deleteUser(id) {
|
||||
* ═══════════════════════════════════════════════════════════════════════════ */
|
||||
|
||||
export async function listSites() {
|
||||
return query(`
|
||||
SELECT id, site_code AS siteCode, host, obe_site_code AS obeSiteCode, pxs_site_code AS pxsSiteCode
|
||||
const rows = await query(`
|
||||
SELECT id, site_code AS siteCode, host, obe_site_code AS obeSiteCode, pxs_site_code AS pxsSiteCode,
|
||||
latitude, longitude
|
||||
FROM admin_sites ORDER BY site_code ASC
|
||||
`);
|
||||
/* MariaDB returns DECIMAL columns as strings — convert to JS numbers. */
|
||||
return rows.map(r => ({
|
||||
...r,
|
||||
latitude: r.latitude != null ? parseFloat(r.latitude) : null,
|
||||
longitude: r.longitude != null ? parseFloat(r.longitude) : null
|
||||
}));
|
||||
}
|
||||
|
||||
export async function createSite(data) {
|
||||
const result = await query(
|
||||
'INSERT INTO admin_sites (site_code, host, obe_site_code, pxs_site_code) VALUES (?, ?, ?, ?)',
|
||||
[data.siteCode, data.host || '', data.obeSiteCode || '', data.pxsSiteCode || '']
|
||||
'INSERT INTO admin_sites (site_code, host, obe_site_code, pxs_site_code, latitude, longitude) VALUES (?, ?, ?, ?, ?, ?)',
|
||||
[data.siteCode, data.host || '', data.obeSiteCode || '', data.pxsSiteCode || '',
|
||||
data.latitude != null ? data.latitude : null, data.longitude != null ? data.longitude : null]
|
||||
);
|
||||
return { id: Number(result.insertId), ...data };
|
||||
}
|
||||
|
||||
export async function updateSite(id, data) {
|
||||
await query(
|
||||
'UPDATE admin_sites SET site_code = ?, host = ?, obe_site_code = ?, pxs_site_code = ? WHERE id = ?',
|
||||
[data.siteCode, data.host || '', data.obeSiteCode || '', data.pxsSiteCode || '', id]
|
||||
'UPDATE admin_sites SET site_code = ?, host = ?, obe_site_code = ?, pxs_site_code = ?, latitude = ?, longitude = ? WHERE id = ?',
|
||||
[data.siteCode, data.host || '', data.obeSiteCode || '', data.pxsSiteCode || '',
|
||||
data.latitude != null ? data.latitude : null, data.longitude != null ? data.longitude : null, id]
|
||||
);
|
||||
return { id, ...data };
|
||||
}
|
||||
@@ -223,21 +232,29 @@ export async function deleteSite(id) {
|
||||
|
||||
export async function listClRecords() {
|
||||
const records = await query(`
|
||||
SELECT id, sort_order AS sort, category, sub_category AS subCategory, severity,
|
||||
image_required AS imageRequired, description_en AS descriptionEN,
|
||||
description_fr AS descriptionFR, description_nl AS descriptionNL
|
||||
FROM admin_cl_records ORDER BY sort_order ASC
|
||||
SELECT r.id, r.sort_order AS sort,
|
||||
r.category_id AS categoryId, COALESCE(c.value, '') AS category,
|
||||
r.sub_category_id AS subCategoryId, COALESCE(sc.value, '') AS subCategory,
|
||||
r.severity_id AS severityId, COALESCE(sv.value, '') AS severity,
|
||||
r.image_required AS imageRequired,
|
||||
r.description_en AS descriptionEN,
|
||||
r.description_fr AS descriptionFR,
|
||||
r.description_nl AS descriptionNL
|
||||
FROM admin_cl_records r
|
||||
LEFT JOIN admin_categories c ON c.id = r.category_id
|
||||
LEFT JOIN admin_sub_categories sc ON sc.id = r.sub_category_id
|
||||
LEFT JOIN admin_severities sv ON sv.id = r.severity_id
|
||||
ORDER BY r.sort_order ASC
|
||||
`);
|
||||
/* Convert tinyint to boolean */
|
||||
for (const r of records) r.imageRequired = !!r.imageRequired;
|
||||
return records;
|
||||
}
|
||||
|
||||
export async function createClRecord(data) {
|
||||
const result = await query(
|
||||
`INSERT INTO admin_cl_records (sort_order, category, sub_category, severity, image_required, description_en, description_fr, description_nl)
|
||||
`INSERT INTO admin_cl_records (sort_order, category_id, sub_category_id, severity_id, image_required, description_en, description_fr, description_nl)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
[data.sort, data.category || '', data.subCategory || '', data.severity || '',
|
||||
[data.sort, data.categoryId || null, data.subCategoryId || null, data.severityId || null,
|
||||
data.imageRequired ? 1 : 0, data.descriptionEN || '', data.descriptionFR || '', data.descriptionNL || '']
|
||||
);
|
||||
return { id: Number(result.insertId), ...data };
|
||||
@@ -245,9 +262,9 @@ export async function createClRecord(data) {
|
||||
|
||||
export async function updateClRecord(id, data) {
|
||||
await query(
|
||||
`UPDATE admin_cl_records SET sort_order = ?, category = ?, sub_category = ?, severity = ?,
|
||||
`UPDATE admin_cl_records SET sort_order = ?, category_id = ?, sub_category_id = ?, severity_id = ?,
|
||||
image_required = ?, description_en = ?, description_fr = ?, description_nl = ? WHERE id = ?`,
|
||||
[data.sort, data.category || '', data.subCategory || '', data.severity || '',
|
||||
[data.sort, data.categoryId || null, data.subCategoryId || null, data.severityId || null,
|
||||
data.imageRequired ? 1 : 0, data.descriptionEN || '', data.descriptionFR || '', data.descriptionNL || '', id]
|
||||
);
|
||||
return { id, ...data };
|
||||
@@ -329,24 +346,29 @@ export async function deleteClTemplate(id) {
|
||||
|
||||
export async function listTasks() {
|
||||
return query(`
|
||||
SELECT id, site_id AS siteId, user_id AS userId, template_id AS templateId,
|
||||
project, process, status, created_at AS createdAt
|
||||
FROM admin_tasks ORDER BY created_at DESC
|
||||
SELECT t.id, t.site_id AS siteId, t.user_id AS userId, t.template_id AS templateId,
|
||||
t.project_id AS projectId, COALESCE(p.value, '') AS project,
|
||||
t.process_id AS processId, COALESCE(pr.value, '') AS process,
|
||||
t.status, t.created_at AS createdAt
|
||||
FROM admin_tasks t
|
||||
LEFT JOIN admin_projects p ON p.id = t.project_id
|
||||
LEFT JOIN admin_processes pr ON pr.id = t.process_id
|
||||
ORDER BY t.created_at DESC
|
||||
`);
|
||||
}
|
||||
|
||||
export async function createTask(data) {
|
||||
const result = await query(
|
||||
'INSERT INTO admin_tasks (site_id, user_id, template_id, project, process, status) VALUES (?, ?, ?, ?, ?, ?)',
|
||||
[data.siteId, data.userId, data.templateId, data.project || '', data.process || '', data.status || 'pending']
|
||||
'INSERT INTO admin_tasks (site_id, user_id, template_id, project_id, process_id, status) VALUES (?, ?, ?, ?, ?, ?)',
|
||||
[data.siteId, data.userId, data.templateId, data.projectId || null, data.processId || null, data.status || 'pending']
|
||||
);
|
||||
return { id: Number(result.insertId), ...data };
|
||||
}
|
||||
|
||||
export async function updateTask(id, data) {
|
||||
await query(
|
||||
'UPDATE admin_tasks SET site_id = ?, user_id = ?, template_id = ?, project = ?, process = ?, status = ? WHERE id = ?',
|
||||
[data.siteId, data.userId, data.templateId, data.project || '', data.process || '', data.status || 'pending', id]
|
||||
'UPDATE admin_tasks SET site_id = ?, user_id = ?, template_id = ?, project_id = ?, process_id = ?, status = ? WHERE id = ?',
|
||||
[data.siteId, data.userId, data.templateId, data.projectId || null, data.processId || null, data.status || 'pending', id]
|
||||
);
|
||||
return { id, ...data };
|
||||
}
|
||||
@@ -355,12 +377,36 @@ export async function deleteTask(id) {
|
||||
await query('DELETE FROM admin_tasks WHERE id = ?', [id]);
|
||||
}
|
||||
|
||||
/* ═══════════════════════════════════════════════════════════════════════════
|
||||
* APP CONFIG (key-value settings store)
|
||||
* ═══════════════════════════════════════════════════════════════════════════ */
|
||||
|
||||
export async function getAppConfig(key) {
|
||||
const rows = await query('SELECT config_value FROM admin_app_config WHERE config_key = ?', [key]);
|
||||
return rows.length ? rows[0].config_value : null;
|
||||
}
|
||||
|
||||
export async function setAppConfig(key, value) {
|
||||
await query(
|
||||
'INSERT INTO admin_app_config (config_key, config_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE config_value = ?',
|
||||
[key, String(value), String(value)]
|
||||
);
|
||||
return { key, value: String(value) };
|
||||
}
|
||||
|
||||
export async function getAllAppConfig() {
|
||||
const rows = await query('SELECT config_key AS `key`, config_value AS value FROM admin_app_config');
|
||||
const obj = {};
|
||||
rows.forEach(r => { obj[r.key] = r.value; });
|
||||
return obj;
|
||||
}
|
||||
|
||||
/* ═══════════════════════════════════════════════════════════════════════════
|
||||
* BULK LOAD — returns all admin data in a single response
|
||||
* ═══════════════════════════════════════════════════════════════════════════ */
|
||||
|
||||
export async function loadAllAdminData() {
|
||||
const [categories, subCategories, severities, statuses, handledBy, projects, processes, users, sites, clRecords, clTemplates, tasks] =
|
||||
const [categories, subCategories, severities, statuses, handledBy, projects, processes, users, sites, clRecords, clTemplates, tasks, appConfig] =
|
||||
await Promise.all([
|
||||
listCategories(),
|
||||
listSubCategories(),
|
||||
@@ -373,7 +419,8 @@ export async function loadAllAdminData() {
|
||||
listSites(),
|
||||
listClRecords(),
|
||||
listClTemplates(),
|
||||
listTasks()
|
||||
listTasks(),
|
||||
getAllAppConfig()
|
||||
]);
|
||||
|
||||
return {
|
||||
@@ -383,6 +430,7 @@ export async function loadAllAdminData() {
|
||||
sites,
|
||||
clRecords,
|
||||
clTemplates,
|
||||
tasks
|
||||
tasks,
|
||||
appConfig
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user