This commit is contained in:
seb
2026-10-04 09:33:27 +02:00
parent 3b74c12f4e
commit 89fe6e6451
14 changed files with 1836 additions and 1546 deletions

View File

@@ -5,6 +5,8 @@ import companyCreate from './store/company/create.js';
import companyUpdate from './store/company/update.js';
import companyDelete from './store/company/delete.js';
import companyList from './store/company/list.js';
import companyAddHistory from './store/company/addHistory.js';
import companyGetHistory from './store/company/getHistory.js';
import categoryGetTreeForCompany from './store/category/getTreeForCompany.js';
import categoryGetCategoryArticles from './store/category/getCategoryArticles.js';
import categoryCreateUnderParent from './store/category/createUnderParent.js';
@@ -234,6 +236,8 @@ export default class LibDashStore {
update: companyUpdate(provider),
delete: companyDelete(provider),
list: companyList(provider),
addHistory: companyAddHistory(provider),
getHistory: companyGetHistory(provider),
};
this.category = {

View File

@@ -34,6 +34,7 @@ export const TABLES = [
'articleean',
'articlehistory',
'entityhistory',
'companyhistory',
'entityaddress',
'entitycontact',
'article',

View File

@@ -0,0 +1,19 @@
export const INSERT = `
INSERT INTO companyhistory (company_id, companyuser_id, [text])
OUTPUT INSERTED.id, INSERTED.companyuser_id AS companyuserId, INSERTED.[date], INSERTED.[text]
VALUES (@companyId, @companyuserId, @text)
`;
export const LIST_BY_COMPANY_ID = `
SELECT id, companyuser_id AS companyuserId, [date], [text]
FROM companyhistory
WHERE company_id = @companyId
ORDER BY [date] DESC, id DESC
`;
export const LIST_BY_COMPANY_ID_LIMITED = `
SELECT TOP (@limit) id, companyuser_id AS companyuserId, [date], [text]
FROM companyhistory
WHERE company_id = @companyId
ORDER BY [date] DESC, id DESC
`;

View File

@@ -374,6 +374,20 @@ BEGIN
);
END
`;
export const INIT_COMPANYHISTORY = `
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'companyhistory')
BEGIN
CREATE TABLE companyhistory (
id INT IDENTITY(1,1) PRIMARY KEY,
company_id INT NOT NULL,
companyuser_id INT NULL,
[date] DATETIME2 NOT NULL CONSTRAINT DF_companyhistory_date DEFAULT SYSUTCDATETIME(),
[text] NVARCHAR(MAX) NOT NULL,
CONSTRAINT FK_companyhistory_company FOREIGN KEY (company_id) REFERENCES company(id) ON DELETE CASCADE,
CONSTRAINT FK_companyhistory_companyuser FOREIGN KEY (companyuser_id) REFERENCES companyuser(id) ON DELETE SET NULL
);
END
`;
export const INIT_ARTICLETOMBSTONE = `
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'articletombstone')
@@ -1147,6 +1161,7 @@ export const SCHEMA_SCRIPTS = [
INIT_ARTICLEHISTORY,
INIT_ARTICLETOMBSTONE,
INIT_ENTITYHISTORY,
INIT_COMPANYHISTORY,
INIT_SYNCPOS,
INIT_COMPANYSETTING,
INIT_POSTERMINAL,

View File

@@ -0,0 +1,13 @@
import * as source from '../../mssql/queries/companyhistory.js';
import { translateQueries } from './translate.js';
const queries = translateQueries(source, 'companyhistory');
export const { INSERT, LIST_BY_COMPANY_ID } = queries;
export const LIST_BY_COMPANY_ID_LIMITED = `
SELECT id, companyuser_id AS companyuserId, date, text
FROM companyhistory
WHERE company_id = @companyId
ORDER BY date DESC, id DESC
LIMIT @limit
`;

View File

@@ -536,6 +536,17 @@ CREATE TABLE IF NOT EXISTS articlehistory (
FOREIGN KEY (companyuser_id) REFERENCES companyuser(id) ON DELETE SET NULL
);
`;
export const INIT_COMPANYHISTORY = `
CREATE TABLE IF NOT EXISTS companyhistory (
id INTEGER PRIMARY KEY,
company_id INTEGER NOT NULL,
companyuser_id INTEGER,
"date" TEXT NOT NULL DEFAULT (${UTC_NOW}),
"text" TEXT NOT NULL,
FOREIGN KEY (company_id) REFERENCES company(id) ON DELETE CASCADE,
FOREIGN KEY (companyuser_id) REFERENCES companyuser(id) ON DELETE SET NULL
);
`;
export const INIT_ARTICLETOMBSTONE = `
CREATE TABLE IF NOT EXISTS articletombstone (
@@ -818,6 +829,7 @@ export const SCHEMA_SCRIPTS = [
INIT_ARTICLEHISTORY,
INIT_ARTICLETOMBSTONE,
INIT_ENTITYHISTORY,
INIT_COMPANYHISTORY,
INIT_SYNCPOS,
INIT_COMPANYSETTING,
INIT_POSTERMINAL,

View File

@@ -0,0 +1,54 @@
import { INSERT as MSSQL_INSERT } from '../../providers/mssql/queries/companyhistory.js';
import { INSERT as SQLITE_INSERT } from '../../providers/sqlite3/queries/companyhistory.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
export default function addHistory(provider) {
const access = resolveSelectedCompanyAccess(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const sql = isSqlite ? SQLITE_INSERT : MSSQL_INSERT;
/**
* Adds an entry to the company history for the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object|string} input - The history input object or text string.
* @param {string} [input.text] - The history entry text.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<{id: number, companyuserId: number|null, date: string, text: string}>} The inserted entry.
* @throws {Error} If text is missing or user is not authorized.
*
* @example
* const entry = await store.company.addHistory(42, { text: 'API suggestion: add barcode scanner endpoint' });
* // => { id, companyuserId, date, text }
*/
return async (googleAuthId, input, tx) => {
let rawText = typeof input === 'string' ? input : input?.text;
const text = typeof rawText === 'string' ? rawText.trim() : '';
if (!text) {
throw new Error('Text required');
}
let resolvedTx = tx;
if (input != null && typeof input === 'object' && input._isTx) {
resolvedTx = input;
}
const runner = makeRunner(provider, resolvedTx);
const { companyId, companyuserId } = await access(googleAuthId, runner);
const result = await runner.query(sql, {
companyId,
companyuserId: companyuserId ?? null,
text,
});
const row = result.rows[0];
return {
id: row.id,
companyuserId: row.companyuserId ?? null,
date: row.date,
text: row.text,
};
};
}

View File

@@ -0,0 +1,54 @@
import { LIST_BY_COMPANY_ID as MSSQL_QUERY, LIST_BY_COMPANY_ID_LIMITED as MSSQL_QUERY_LIMITED } from '../../providers/mssql/queries/companyhistory.js';
import { LIST_BY_COMPANY_ID as SQLITE_QUERY, LIST_BY_COMPANY_ID_LIMITED as SQLITE_QUERY_LIMITED } from '../../providers/sqlite3/queries/companyhistory.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
export default function getHistory(provider) {
const access = resolveSelectedCompanyAccess(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const sql = isSqlite ? SQLITE_QUERY : MSSQL_QUERY;
const sqlLimited = isSqlite ? SQLITE_QUERY_LIMITED : MSSQL_QUERY_LIMITED;
/**
* Returns the company history for the user's selected company, newest first.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} [opts] - Optional options.
* @param {number} [opts.limit] - Maximum number of entries to return (newest first).
* @param {object} [tx] - Optional transaction.
* @returns {Promise<Array<{id: number, companyuserId: number|null, date: string, text: string}>>} The company history entries.
*
* @example
* const history = await store.company.getHistory(42);
* // => [{ id, companyuserId, date, text }]
*
* const recent = await store.company.getHistory(42, { limit: 5 });
* // => up to 5 most recent entries
*/
return async (googleAuthId, opts, tx) => {
let limit = null;
let resolvedTx = tx;
if (opts != null && typeof opts === 'object' && !opts._isTx) {
limit = opts.limit != null ? Number(opts.limit) : null;
} else if (opts != null) {
resolvedTx = opts;
}
const runner = makeRunner(provider, resolvedTx);
const { companyId } = await access(googleAuthId, runner);
let result;
if (limit != null && Number.isInteger(limit) && limit > 0) {
result = await runner.query(sqlLimited, { companyId, limit });
} else {
result = await runner.query(sql, { companyId });
}
return result.rows.map((row) => ({
id: row.id,
companyuserId: row.companyuserId ?? null,
date: row.date,
text: row.text,
}));
};
}