This commit is contained in:
seb
2026-10-09 10:41:17 +02:00
parent 760f074b4e
commit f3674edcb7
24 changed files with 1181 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
import { queriesFor, requireText } from '../logistics/helpers.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
function shape(row) {
return {
id: row.id,
companyId: row.company_id,
type: row.type,
name: row.name,
};
}
export default function create(provider) {
const access = resolveSelectedCompanyAccess(provider);
const q = queriesFor(provider);
/**
* Creates an account for the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} input - Account input.
* @param {string} input.type - Account type.
* @param {string} input.name - Account name.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{id: number, companyId: number, type: string, name: string}>}
* @throws {Error} If type or name is missing.
*
* @example
* const account = await store.account.create(googleAuthId, { type: 'bank', name: 'Main' });
*/
return async (googleAuthId, input = {}, tx) => {
const type = requireText(input.type, 'Account type');
const name = requireText(input.name, 'Account name');
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, runner);
const result = await runner.query(q.ACCOUNT_CREATE, { companyId, type, name });
const row = result.rows[0] ?? { id: result.insertId, company_id: companyId, type, name };
return shape(row);
};
}

View File

@@ -0,0 +1,27 @@
import { queriesFor, requireId } from '../logistics/helpers.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
/**
* Deletes an account of the user's selected company. Its payments are removed by cascade.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} id - The account id.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<void>}
* @throws {Error} If the account id is invalid.
*
* @example
* await store.account.delete(googleAuthId, 3);
*/
export default function del(provider) {
const access = resolveSelectedCompanyAccess(provider);
const q = queriesFor(provider);
return async (googleAuthId, id, tx) => {
const accountId = requireId(id, 'Account');
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, runner);
await runner.query(q.ACCOUNT_DELETE, { id: accountId, companyId });
};
}

30
src/store/account/list.js Normal file
View File

@@ -0,0 +1,30 @@
import { queriesFor } from '../logistics/helpers.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
/**
* Lists accounts for 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, companyId: number, type: string, name: string}>>}
*
* @example
* const accounts = await store.account.list(googleAuthId);
*/
export default function list(provider) {
const access = resolveSelectedCompanyAccess(provider);
const q = queriesFor(provider);
return async (googleAuthId, tx) => {
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, runner);
const result = await runner.query(q.ACCOUNT_LIST, { companyId });
return result.rows.map((row) => ({
id: row.id,
companyId: row.company_id,
type: row.type,
name: row.name,
}));
};
}

View File

@@ -0,0 +1,25 @@
import * as mssqlQueries from '../../providers/mssql/queries/logistics.js';
import * as sqliteQueries from '../../providers/sqlite3/queries/logistics.js';
/** Selects the dialect-specific logistics queries for a provider. */
export function queriesFor(provider) {
return provider?.dialect === 'sqlite3' ? sqliteQueries : mssqlQueries;
}
/** Trims a required string; throws `<label> required` when empty. */
export function requireText(value, label) {
const s = typeof value === 'string' ? value.trim() : '';
if (!s) {
throw new Error(`${label} required`);
}
return s;
}
/** Validates a positive integer id; throws `<label> not found` otherwise. */
export function requireId(value, label) {
const n = Number(value);
if (!Number.isInteger(n) || n <= 0) {
throw new Error(`${label} not found`);
}
return n;
}

View File

@@ -0,0 +1,93 @@
import { queriesFor, requireId, requireText } from '../logistics/helpers.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import nextNumber from '../numbers/nextNumber.js';
import makeRunner from '../runner.js';
function optionalText(value) {
if (value == null) return null;
const s = String(value).trim();
return s || null;
}
function shape(row) {
return {
id: row.id,
shippingId: row.shipping_id,
entityaddressId: row.entityaddress_id,
number: row.number,
type: row.type,
state: row.state,
details: row.details ?? null,
};
}
export default function create(provider) {
const access = resolveSelectedCompanyAccess(provider);
const allocateNumber = nextNumber(provider);
const q = queriesFor(provider);
/**
* Creates a package for a shipping. The number is allocated from the `package`
* numbers sequence (own prefix + counter). The package must target an entity address.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} input - Package input.
* @param {number} input.shippingId - The shipping id.
* @param {number} input.entityaddressId - The target entity address id (required).
* @param {string} input.type - Package type.
* @param {string} input.state - Package state.
* @param {string} [input.details] - Optional details.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{id: number, shippingId: number, entityaddressId: number, number: string, type: string, state: string, details: string|null}>}
* @throws {Error} If shipping or entity address is not found, or input is invalid.
*
* @example
* await store.package.create(googleAuthId, { shippingId: 9, entityaddressId: 3, type: 'box', state: 'packed' });
*/
return async (googleAuthId, input = {}, tx) => {
const shippingId = requireId(input.shippingId, 'Shipping');
const entityaddressId = requireId(input.entityaddressId, 'Entity address');
const type = requireText(input.type, 'Package type');
const state = requireText(input.state, 'Package state');
const details = optionalText(input.details);
const run = async (runner, innerTx) => {
const { companyId } = await access(googleAuthId, runner);
const shippingRes = await runner.query(q.CHECK_SHIPPING_COMPANY, { shippingId, companyId });
if (!shippingRes.rows[0]) {
throw new Error('Shipping not found');
}
const addressRes = await runner.query(q.CHECK_ENTITYADDRESS_COMPANY, { entityaddressId, companyId });
if (!addressRes.rows[0]) {
throw new Error('Entity address not found');
}
const number = await allocateNumber(companyId, 'package', innerTx);
const result = await runner.query(q.PACKAGE_CREATE, {
shippingId,
entityaddressId,
number,
type,
state,
details,
});
const row = result.rows[0] ?? {
id: result.insertId,
shipping_id: shippingId,
entityaddress_id: entityaddressId,
number,
type,
state,
details,
};
return shape(row);
};
if (typeof provider?.transaction === 'function') {
return provider.transaction((runner, innerTx) => run(runner, innerTx));
}
return run(makeRunner(provider, tx));
};
}

View File

@@ -0,0 +1,26 @@
import { queriesFor, requireId } from '../logistics/helpers.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
/**
* Deletes a package of the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} id - The package id.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<void>}
*
* @example
* await store.package.delete(googleAuthId, 12);
*/
export default function del(provider) {
const access = resolveSelectedCompanyAccess(provider);
const q = queriesFor(provider);
return async (googleAuthId, id, tx) => {
const packageId = requireId(id, 'Package');
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, runner);
await runner.query(q.PACKAGE_DELETE, { id: packageId, companyId });
};
}

View File

@@ -0,0 +1,41 @@
import { queriesFor, requireId } from '../logistics/helpers.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
/**
* Lists packages of a shipping in the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} shippingId - The shipping id.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<Array<{id: number, shippingId: number, entityaddressId: number, number: string, type: string, state: string, details: string|null}>>}
*
* @example
* const packages = await store.package.listByShippingId(googleAuthId, 9);
*/
export default function listByShippingId(provider) {
const access = resolveSelectedCompanyAccess(provider);
const q = queriesFor(provider);
return async (googleAuthId, shippingIdInput, tx) => {
const shippingId = requireId(shippingIdInput, 'Shipping');
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, runner);
const shippingRes = await runner.query(q.CHECK_SHIPPING_COMPANY, { shippingId, companyId });
if (!shippingRes.rows[0]) {
throw new Error('Shipping not found');
}
const result = await runner.query(q.PACKAGE_LIST_BY_SHIPPING, { shippingId });
return result.rows.map((row) => ({
id: row.id,
shippingId: row.shipping_id,
entityaddressId: row.entityaddress_id,
number: row.number,
type: row.type,
state: row.state,
details: row.details ?? null,
}));
};
}

View File

@@ -0,0 +1,75 @@
import { queriesFor, requireId, requireText } from '../logistics/helpers.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import resolveManifestAccess from '../manifestattachment/resolveManifestAccess.js';
import makeRunner from '../runner.js';
function shape(row) {
return {
id: row.id,
accountId: row.account_id,
manifestId: row.manifest_id,
type: row.type,
amount: row.amount,
};
}
export default function create(provider) {
const access = resolveSelectedCompanyAccess(provider);
const manifestAccess = resolveManifestAccess(provider);
const q = queriesFor(provider);
/**
* Adds a payment to a manifest (order) using an account of the selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} input - Payment input.
* @param {number} input.accountId - The account id.
* @param {number} input.manifestId - The manifest (order) id.
* @param {string} input.type - Payment type.
* @param {number} input.amount - Payment amount.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{id: number, accountId: number, manifestId: number, type: string, amount: number}>}
* @throws {Error} If account or manifest is not found, or input is invalid.
*
* @example
* await store.payment.create(googleAuthId, { accountId: 1, manifestId: 42, type: 'cash', amount: 19.9 });
*/
return async (googleAuthId, input = {}, tx) => {
const accountId = requireId(input.accountId, 'Account');
const manifestId = requireId(input.manifestId, 'Manifest');
const type = requireText(input.type, 'Payment type');
if (!Number.isFinite(input.amount)) {
throw new Error('Invalid payment amount');
}
const run = async (runner, innerTx) => {
const { companyId } = await access(googleAuthId, runner);
const accountRes = await runner.query(q.CHECK_ACCOUNT_COMPANY, { accountId, companyId });
if (!accountRes.rows[0]) {
throw new Error('Account not found');
}
await manifestAccess(companyId, manifestId, innerTx);
const result = await runner.query(q.PAYMENT_CREATE, {
accountId,
manifestId,
type,
amount: input.amount,
});
const row = result.rows[0] ?? {
id: result.insertId,
account_id: accountId,
manifest_id: manifestId,
type,
amount: input.amount,
};
return shape(row);
};
if (typeof provider?.transaction === 'function') {
return provider.transaction((runner, innerTx) => run(runner, innerTx));
}
return run(makeRunner(provider, tx));
};
}

View File

@@ -0,0 +1,26 @@
import { queriesFor, requireId } from '../logistics/helpers.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
/**
* Deletes a payment belonging to a manifest of the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} id - The payment id.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<void>}
*
* @example
* await store.payment.delete(googleAuthId, 7);
*/
export default function del(provider) {
const access = resolveSelectedCompanyAccess(provider);
const q = queriesFor(provider);
return async (googleAuthId, id, tx) => {
const paymentId = requireId(id, 'Payment');
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, runner);
await runner.query(q.PAYMENT_DELETE, { id: paymentId, companyId });
};
}

View File

@@ -0,0 +1,37 @@
import { queriesFor, requireId } from '../logistics/helpers.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import resolveManifestAccess from '../manifestattachment/resolveManifestAccess.js';
import makeRunner from '../runner.js';
/**
* Lists payments of a manifest (order) in the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} manifestId - The manifest id.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<Array<{id: number, accountId: number, manifestId: number, type: string, amount: number}>>}
*
* @example
* const payments = await store.payment.listByManifestId(googleAuthId, 42);
*/
export default function listByManifestId(provider) {
const access = resolveSelectedCompanyAccess(provider);
const manifestAccess = resolveManifestAccess(provider);
const q = queriesFor(provider);
return async (googleAuthId, manifestIdInput, tx) => {
const manifestId = requireId(manifestIdInput, 'Manifest');
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, runner);
await manifestAccess(companyId, manifestId, tx);
const result = await runner.query(q.PAYMENT_LIST_BY_MANIFEST, { manifestId });
return result.rows.map((row) => ({
id: row.id,
accountId: row.account_id,
manifestId: row.manifest_id,
type: row.type,
amount: row.amount,
}));
};
}

View File

@@ -0,0 +1,71 @@
import { queriesFor, requireId, requireText } from '../logistics/helpers.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import resolveManifestAccess from '../manifestattachment/resolveManifestAccess.js';
import nextNumber from '../numbers/nextNumber.js';
import makeRunner from '../runner.js';
function shape(row) {
return {
id: row.id,
manifestId: row.manifest_id,
number: row.number,
type: row.type,
state: row.state,
};
}
export default function create(provider) {
const access = resolveSelectedCompanyAccess(provider);
const manifestAccess = resolveManifestAccess(provider);
const allocateNumber = nextNumber(provider);
const q = queriesFor(provider);
/**
* Creates a shipping for a manifest (order). The number is allocated from the
* `shipping` numbers sequence (own prefix + counter).
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} input - Shipping input.
* @param {number} input.manifestId - The manifest (order) id.
* @param {string} input.type - Shipping type.
* @param {string} input.state - Shipping state.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{id: number, manifestId: number, number: string, type: string, state: string}>}
* @throws {Error} If manifest is not found or input is invalid.
*
* @example
* await store.shipping.create(googleAuthId, { manifestId: 42, type: 'parcel', state: 'open' });
*/
return async (googleAuthId, input = {}, tx) => {
const manifestId = requireId(input.manifestId, 'Manifest');
const type = requireText(input.type, 'Shipping type');
const state = requireText(input.state, 'Shipping state');
const run = async (runner, innerTx) => {
const { companyId } = await access(googleAuthId, runner);
await manifestAccess(companyId, manifestId, innerTx);
const number = await allocateNumber(companyId, 'shipping', innerTx);
const result = await runner.query(q.SHIPPING_CREATE, {
manifestId,
number,
type,
state,
});
const row = result.rows[0] ?? {
id: result.insertId,
manifest_id: manifestId,
number,
type,
state,
};
return shape(row);
};
if (typeof provider?.transaction === 'function') {
return provider.transaction((runner, innerTx) => run(runner, innerTx));
}
return run(makeRunner(provider, tx));
};
}

View File

@@ -0,0 +1,26 @@
import { queriesFor, requireId } from '../logistics/helpers.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
/**
* Deletes a shipping of the user's selected company. Its packages are removed by cascade.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} id - The shipping id.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<void>}
*
* @example
* await store.shipping.delete(googleAuthId, 9);
*/
export default function del(provider) {
const access = resolveSelectedCompanyAccess(provider);
const q = queriesFor(provider);
return async (googleAuthId, id, tx) => {
const shippingId = requireId(id, 'Shipping');
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, runner);
await runner.query(q.SHIPPING_DELETE, { id: shippingId, companyId });
};
}

View File

@@ -0,0 +1,37 @@
import { queriesFor, requireId } from '../logistics/helpers.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import resolveManifestAccess from '../manifestattachment/resolveManifestAccess.js';
import makeRunner from '../runner.js';
/**
* Lists shippings of a manifest (order) in the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} manifestId - The manifest id.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<Array<{id: number, manifestId: number, number: string, type: string, state: string}>>}
*
* @example
* const shippings = await store.shipping.listByManifestId(googleAuthId, 42);
*/
export default function listByManifestId(provider) {
const access = resolveSelectedCompanyAccess(provider);
const manifestAccess = resolveManifestAccess(provider);
const q = queriesFor(provider);
return async (googleAuthId, manifestIdInput, tx) => {
const manifestId = requireId(manifestIdInput, 'Manifest');
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, runner);
await manifestAccess(companyId, manifestId, tx);
const result = await runner.query(q.SHIPPING_LIST_BY_MANIFEST, { manifestId });
return result.rows.map((row) => ({
id: row.id,
manifestId: row.manifest_id,
number: row.number,
type: row.type,
state: row.state,
}));
};
}