sqlite
This commit is contained in:
@@ -1,11 +1,18 @@
|
||||
import { CREATE } from '../../providers/mssql/queries/entity.js';
|
||||
import { INSERT as INSERT_HISTORY } from '../../providers/mssql/queries/entityhistory.js';
|
||||
import request from '../request.js';
|
||||
import { CREATE as MSSQL_CREATE } from '../../providers/mssql/queries/entity.js';
|
||||
import { CREATE as SQLITE_CREATE } from '../../providers/sqlite3/queries/entity.js';
|
||||
import { INSERT as MSSQL_INSERT_HISTORY } from '../../providers/mssql/queries/entityhistory.js';
|
||||
import { INSERT as SQLITE_INSERT_HISTORY } from '../../providers/sqlite3/queries/entityhistory.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import ensureLieferantenGroup from './ensureLieferantenGroup.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export default function create(pool) {
|
||||
const access = resolveSelectedCompanyAccess(pool);
|
||||
export default function create(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const isSqlite = provider?.dialect === 'sqlite3';
|
||||
const createSql = isSqlite ? SQLITE_CREATE : MSSQL_CREATE;
|
||||
const insertHistorySql = isSqlite
|
||||
? SQLITE_INSERT_HISTORY
|
||||
: MSSQL_INSERT_HISTORY;
|
||||
|
||||
/**
|
||||
* Creates a new supplier for the user's selected company, recording history.
|
||||
@@ -13,6 +20,7 @@ export default function create(pool) {
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {object} input - The supplier input.
|
||||
* @param {string} input.name - The supplier name.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<{id: number, name: string, createdAt: string, updatedAt: string}>} The created supplier.
|
||||
* @throws {Error} If the name is empty.
|
||||
*
|
||||
@@ -20,36 +28,35 @@ export default function create(pool) {
|
||||
* const supplier = await store.supplier.create(42, { name: 'Acme Supplies' });
|
||||
* // => { id, name, createdAt, updatedAt }
|
||||
*/
|
||||
return async (googleAuthId, { name } = {}) => {
|
||||
return async (googleAuthId, { name } = {}, tx) => {
|
||||
const cleaned = name?.trim();
|
||||
if (!cleaned) {
|
||||
throw new Error('Name required');
|
||||
}
|
||||
|
||||
const { companyId, companyuserId } = await access(googleAuthId);
|
||||
const runCreate = async (runner) => {
|
||||
const { companyId, companyuserId } = await access(googleAuthId, runner);
|
||||
const customergroupId = await ensureLieferantenGroup(provider, companyId, runner);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
const created = await runner.query(createSql, {
|
||||
customergroupId,
|
||||
firstName: null,
|
||||
name: cleaned,
|
||||
number: null,
|
||||
});
|
||||
|
||||
try {
|
||||
const customergroupId = await ensureLieferantenGroup(pool, companyId, tx);
|
||||
const row = created.rows[0] ?? {
|
||||
id: created.insertId,
|
||||
name: cleaned,
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
};
|
||||
|
||||
const created = await request(pool, tx)
|
||||
.input('customergroupId', customergroupId)
|
||||
.input('firstName', null)
|
||||
.input('name', cleaned)
|
||||
.input('number', null)
|
||||
.query(CREATE);
|
||||
|
||||
const row = created.recordset[0];
|
||||
|
||||
await request(pool, tx)
|
||||
.input('entityId', row.id)
|
||||
.input('companyuserId', companyuserId)
|
||||
.input('text', 'Erstellt')
|
||||
.query(INSERT_HISTORY);
|
||||
|
||||
await tx.commit();
|
||||
await runner.query(insertHistorySql, {
|
||||
entityId: row.id,
|
||||
companyuserId,
|
||||
text: 'Erstellt',
|
||||
});
|
||||
|
||||
return {
|
||||
id: row.id,
|
||||
@@ -57,13 +64,14 @@ export default function create(pool) {
|
||||
createdAt: row.createdAt,
|
||||
updatedAt: row.updatedAt,
|
||||
};
|
||||
} catch (err) {
|
||||
try {
|
||||
await tx.rollback();
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
throw err;
|
||||
};
|
||||
|
||||
if (tx) {
|
||||
return runCreate(makeRunner(provider, tx));
|
||||
}
|
||||
if (typeof provider?.transaction === 'function') {
|
||||
return provider.transaction(runCreate);
|
||||
}
|
||||
return runCreate(makeRunner(provider));
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,35 +1,42 @@
|
||||
import { DELETE_BY_ID, SUPPLIER_GROUP_NAME } from '../../providers/mssql/queries/entity.js';
|
||||
import {
|
||||
DELETE_BY_ID as MSSQL_DELETE_BY_ID,
|
||||
SUPPLIER_GROUP_NAME,
|
||||
} from '../../providers/mssql/queries/entity.js';
|
||||
import { DELETE_BY_ID as SQLITE_DELETE_BY_ID } from '../../providers/sqlite3/queries/entity.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export default function del(pool) {
|
||||
const access = resolveSelectedCompanyAccess(pool);
|
||||
export default function del(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const sql = provider?.dialect === 'sqlite3' ? SQLITE_DELETE_BY_ID : MSSQL_DELETE_BY_ID;
|
||||
|
||||
/**
|
||||
* Deletes a supplier by its id within the user's selected company.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {number} id - The id of the supplier to delete.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<void>}
|
||||
* @throws {Error} If the supplier is not found.
|
||||
*
|
||||
* @example
|
||||
* await store.supplier.delete(42, 5);
|
||||
*/
|
||||
return async (googleAuthId, id) => {
|
||||
return async (googleAuthId, id, tx) => {
|
||||
if (!Number.isFinite(id)) {
|
||||
throw new Error('Supplier not found');
|
||||
}
|
||||
|
||||
const { companyId } = await access(googleAuthId);
|
||||
const runner = makeRunner(provider, tx);
|
||||
const { companyId } = await access(googleAuthId, tx);
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('id', id)
|
||||
.input('companyId', companyId)
|
||||
.input('groupName', SUPPLIER_GROUP_NAME)
|
||||
.query(DELETE_BY_ID);
|
||||
const result = await runner.query(sql, {
|
||||
id,
|
||||
companyId,
|
||||
groupName: SUPPLIER_GROUP_NAME,
|
||||
});
|
||||
|
||||
if (!result.rowsAffected[0]) {
|
||||
if (!result.rowsAffected) {
|
||||
throw new Error('Supplier not found');
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,33 +1,44 @@
|
||||
import {
|
||||
CREATE,
|
||||
GET_BY_COMPANY_AND_NAME,
|
||||
GET_NEXT_SORT,
|
||||
CREATE as MSSQL_CREATE,
|
||||
GET_BY_COMPANY_AND_NAME as MSSQL_GET_BY_COMPANY_AND_NAME,
|
||||
GET_NEXT_SORT as MSSQL_GET_NEXT_SORT,
|
||||
} from '../../providers/mssql/queries/customergroup.js';
|
||||
import {
|
||||
CREATE as SQLITE_CREATE,
|
||||
GET_BY_COMPANY_AND_NAME as SQLITE_GET_BY_COMPANY_AND_NAME,
|
||||
GET_NEXT_SORT as SQLITE_GET_NEXT_SORT,
|
||||
} from '../../providers/sqlite3/queries/customergroup.js';
|
||||
import { SUPPLIER_GROUP_NAME } from '../../providers/mssql/queries/entity.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
/** Ensure company has customergroup "Lieferanten"; return its id. */
|
||||
export default async function ensureLieferantenGroup(pool, companyId, tx) {
|
||||
const req = () => (tx ?? pool).request();
|
||||
export default async function ensureLieferantenGroup(provider, companyId, tx) {
|
||||
const runner = makeRunner(provider, tx);
|
||||
const isSqlite = runner?.dialect === 'sqlite3';
|
||||
|
||||
const existing = await req()
|
||||
.input('companyId', companyId)
|
||||
.input('name', SUPPLIER_GROUP_NAME)
|
||||
.query(GET_BY_COMPANY_AND_NAME);
|
||||
const getByNameSql = isSqlite
|
||||
? SQLITE_GET_BY_COMPANY_AND_NAME
|
||||
: MSSQL_GET_BY_COMPANY_AND_NAME;
|
||||
const getSortSql = isSqlite ? SQLITE_GET_NEXT_SORT : MSSQL_GET_NEXT_SORT;
|
||||
const createSql = isSqlite ? SQLITE_CREATE : MSSQL_CREATE;
|
||||
|
||||
if (existing.recordset[0]) {
|
||||
return existing.recordset[0].id;
|
||||
const existing = await runner.query(getByNameSql, {
|
||||
companyId,
|
||||
name: SUPPLIER_GROUP_NAME,
|
||||
});
|
||||
|
||||
if (existing.rows[0]) {
|
||||
return existing.rows[0].id;
|
||||
}
|
||||
|
||||
const next = await req()
|
||||
.input('companyId', companyId)
|
||||
.query(GET_NEXT_SORT);
|
||||
const sort = next.recordset[0].sort;
|
||||
const next = await runner.query(getSortSql, { companyId });
|
||||
const sort = next.rows[0].sort;
|
||||
|
||||
const created = await req()
|
||||
.input('companyId', companyId)
|
||||
.input('name', SUPPLIER_GROUP_NAME)
|
||||
.input('sort', sort)
|
||||
.query(CREATE);
|
||||
const created = await runner.query(createSql, {
|
||||
companyId,
|
||||
name: SUPPLIER_GROUP_NAME,
|
||||
sort,
|
||||
});
|
||||
|
||||
return created.recordset[0].id;
|
||||
return created.rows[0]?.id ?? created.insertId;
|
||||
}
|
||||
|
||||
@@ -1,29 +1,35 @@
|
||||
import { LIST_SUPPLIERS_BY_COMPANY, SUPPLIER_GROUP_NAME } from '../../providers/mssql/queries/entity.js';
|
||||
import {
|
||||
LIST_SUPPLIERS_BY_COMPANY as MSSQL_LIST,
|
||||
SUPPLIER_GROUP_NAME,
|
||||
} from '../../providers/mssql/queries/entity.js';
|
||||
import { LIST_SUPPLIERS_BY_COMPANY as SQLITE_LIST } from '../../providers/sqlite3/queries/entity.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export default function list(pool) {
|
||||
const access = resolveSelectedCompanyAccess(pool);
|
||||
export default function list(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const sql = provider?.dialect === 'sqlite3' ? SQLITE_LIST : MSSQL_LIST;
|
||||
|
||||
/**
|
||||
* Lists the suppliers of the user's selected company.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<Array<{id: number, name: string, createdAt: string, updatedAt: string}>>} The suppliers.
|
||||
*
|
||||
* @example
|
||||
* const suppliers = await store.supplier.list(42);
|
||||
* // => [{ id, name, createdAt, updatedAt }]
|
||||
*/
|
||||
return async (googleAuthId) => {
|
||||
const { companyId } = await access(googleAuthId);
|
||||
return async (googleAuthId, tx) => {
|
||||
const { companyId } = await access(googleAuthId, tx);
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('companyId', companyId)
|
||||
.input('groupName', SUPPLIER_GROUP_NAME)
|
||||
.query(LIST_SUPPLIERS_BY_COMPANY);
|
||||
const result = await makeRunner(provider, tx).query(sql, {
|
||||
companyId,
|
||||
groupName: SUPPLIER_GROUP_NAME,
|
||||
});
|
||||
|
||||
return result.recordset.map((row) => ({
|
||||
return result.rows.map((row) => ({
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
createdAt: row.createdAt,
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
import {
|
||||
GET_GROUP_ID_FOR_SUPPLIER,
|
||||
GET_SUPPLIER_BY_ID,
|
||||
GET_GROUP_ID_FOR_SUPPLIER as MSSQL_GET_GROUP_ID_FOR_SUPPLIER,
|
||||
GET_SUPPLIER_BY_ID as MSSQL_GET_SUPPLIER_BY_ID,
|
||||
SUPPLIER_GROUP_NAME,
|
||||
UPDATE_NAME,
|
||||
UPDATE_NAME as MSSQL_UPDATE_NAME,
|
||||
} from '../../providers/mssql/queries/entity.js';
|
||||
import { INSERT as INSERT_HISTORY } from '../../providers/mssql/queries/entityhistory.js';
|
||||
import request from '../request.js';
|
||||
import {
|
||||
GET_GROUP_ID_FOR_SUPPLIER as SQLITE_GET_GROUP_ID_FOR_SUPPLIER,
|
||||
GET_SUPPLIER_BY_ID as SQLITE_GET_SUPPLIER_BY_ID,
|
||||
UPDATE_NAME as SQLITE_UPDATE_NAME,
|
||||
} from '../../providers/sqlite3/queries/entity.js';
|
||||
import { INSERT as MSSQL_INSERT_HISTORY } from '../../providers/mssql/queries/entityhistory.js';
|
||||
import { INSERT as SQLITE_INSERT_HISTORY } from '../../providers/sqlite3/queries/entityhistory.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
function shape(row) {
|
||||
return {
|
||||
@@ -17,8 +23,20 @@ function shape(row) {
|
||||
};
|
||||
}
|
||||
|
||||
export default function update(pool) {
|
||||
const access = resolveSelectedCompanyAccess(pool);
|
||||
export default function update(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const isSqlite = provider?.dialect === 'sqlite3';
|
||||
|
||||
const getGroupIdSql = isSqlite
|
||||
? SQLITE_GET_GROUP_ID_FOR_SUPPLIER
|
||||
: MSSQL_GET_GROUP_ID_FOR_SUPPLIER;
|
||||
const getSupplierByIdSql = isSqlite
|
||||
? SQLITE_GET_SUPPLIER_BY_ID
|
||||
: MSSQL_GET_SUPPLIER_BY_ID;
|
||||
const updateNameSql = isSqlite ? SQLITE_UPDATE_NAME : MSSQL_UPDATE_NAME;
|
||||
const insertHistorySql = isSqlite
|
||||
? SQLITE_INSERT_HISTORY
|
||||
: MSSQL_INSERT_HISTORY;
|
||||
|
||||
/**
|
||||
* Updates the name of a supplier, recording history.
|
||||
@@ -27,6 +45,7 @@ export default function update(pool) {
|
||||
* @param {number} id - The id of the supplier to update.
|
||||
* @param {object} input - The update input.
|
||||
* @param {string} input.name - The new supplier name.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<{id: number, name: string, createdAt: string, updatedAt: string}>} The updated supplier.
|
||||
* @throws {Error} If the supplier is not found or the name is empty.
|
||||
*
|
||||
@@ -34,7 +53,7 @@ export default function update(pool) {
|
||||
* const supplier = await store.supplier.update(42, 5, { name: 'Acme GmbH' });
|
||||
* // => { id, name, createdAt, updatedAt }
|
||||
*/
|
||||
return async (googleAuthId, id, { name } = {}) => {
|
||||
return async (googleAuthId, id, { name } = {}, tx) => {
|
||||
if (!Number.isFinite(id)) {
|
||||
throw new Error('Supplier not found');
|
||||
}
|
||||
@@ -44,64 +63,55 @@ export default function update(pool) {
|
||||
throw new Error('Name required');
|
||||
}
|
||||
|
||||
const { companyId, companyuserId } = await access(googleAuthId);
|
||||
const runUpdate = async (runner) => {
|
||||
const { companyId, companyuserId } = await access(googleAuthId, runner);
|
||||
|
||||
const existing = await pool
|
||||
.request()
|
||||
.input('id', id)
|
||||
.input('companyId', companyId)
|
||||
.input('groupName', SUPPLIER_GROUP_NAME)
|
||||
.query(GET_GROUP_ID_FOR_SUPPLIER);
|
||||
const existing = await runner.query(getGroupIdSql, {
|
||||
id,
|
||||
companyId,
|
||||
groupName: SUPPLIER_GROUP_NAME,
|
||||
});
|
||||
|
||||
const current = existing.recordset[0];
|
||||
if (!current) {
|
||||
throw new Error('Supplier not found');
|
||||
}
|
||||
|
||||
if (current.name === cleaned) {
|
||||
const same = await pool
|
||||
.request()
|
||||
.input('id', id)
|
||||
.input('companyId', companyId)
|
||||
.input('groupName', SUPPLIER_GROUP_NAME)
|
||||
.query(GET_SUPPLIER_BY_ID);
|
||||
return shape(same.recordset[0]);
|
||||
}
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
|
||||
try {
|
||||
const updated = await request(pool, tx)
|
||||
.input('id', id)
|
||||
.input('customergroupId', current.customergroupId)
|
||||
.input('name', cleaned)
|
||||
.query(UPDATE_NAME);
|
||||
|
||||
const row = updated.recordset[0];
|
||||
if (!row) {
|
||||
const current = existing.rows[0];
|
||||
if (!current) {
|
||||
throw new Error('Supplier not found');
|
||||
}
|
||||
|
||||
await request(pool, tx)
|
||||
.input('entityId', id)
|
||||
.input('companyuserId', companyuserId)
|
||||
.input(
|
||||
'text',
|
||||
`Name geändert von "${current.name}" auf "${cleaned}"`,
|
||||
)
|
||||
.query(INSERT_HISTORY);
|
||||
if (current.name === cleaned) {
|
||||
const same = await runner.query(getSupplierByIdSql, {
|
||||
id,
|
||||
companyId,
|
||||
groupName: SUPPLIER_GROUP_NAME,
|
||||
});
|
||||
return shape(same.rows[0]);
|
||||
}
|
||||
|
||||
await tx.commit();
|
||||
const updated = await runner.query(updateNameSql, {
|
||||
id,
|
||||
customergroupId: current.customergroupId,
|
||||
name: cleaned,
|
||||
});
|
||||
|
||||
const row = updated.rows[0] ?? {
|
||||
...current,
|
||||
name: cleaned,
|
||||
};
|
||||
|
||||
await runner.query(insertHistorySql, {
|
||||
entityId: id,
|
||||
companyuserId,
|
||||
text: `Name geändert von "${current.name}" auf "${cleaned}"`,
|
||||
});
|
||||
|
||||
return shape(row);
|
||||
} catch (err) {
|
||||
try {
|
||||
await tx.rollback();
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
throw err;
|
||||
};
|
||||
|
||||
if (tx) {
|
||||
return runUpdate(makeRunner(provider, tx));
|
||||
}
|
||||
if (typeof provider?.transaction === 'function') {
|
||||
return provider.transaction(runUpdate);
|
||||
}
|
||||
return runUpdate(makeRunner(provider));
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user