This commit is contained in:
seb
2026-07-31 05:34:45 +02:00
parent 806d077d57
commit 09c7a8bc74
19 changed files with 405 additions and 30 deletions

View File

@@ -1,23 +1,56 @@
import sql from 'mssql';
import { CREATE } from '../../sql/company.js';
import { CREATE as CREATE_CATEGORY } from '../../sql/category.js';
import getByName from './getByName.js';
import langEnsure from '../lang/ensure.js';
import taxEnsure from '../tax/ensure.js';
import request from '../request.js';
export default function create(pool) {
return async ({ name }) => {
const category = await pool
.request()
.input('parentId', sql.Int, null)
.input('sort', sql.Int, 0)
.query(CREATE_CATEGORY);
const nameTaken = getByName(pool);
const ensureLang = langEnsure(pool);
const ensureTax = taxEnsure(pool);
const rootCategoryId = category.recordset[0].id;
return async ({ iso639, name, taxrate, reducedtaxrate }) => {
const tx = new sql.Transaction(pool);
await tx.begin();
const result = await pool
.request()
.input('name', sql.NVarChar(255), name)
.input('rootCategoryId', sql.Int, rootCategoryId)
.query(CREATE);
try {
const existing = await nameTaken(name, tx);
if (existing) {
throw new Error('Company name not available');
}
return result.recordset[0].id;
const languageId = await ensureLang(iso639, tx);
const taxrateId = await ensureTax(taxrate, tx);
const reducedtaxrateId = await ensureTax(reducedtaxrate, tx);
const taxfreerateId = await ensureTax(0, tx);
const category = await request(pool, tx)
.input('parentId', sql.Int, null)
.input('sort', sql.Int, 0)
.query(CREATE_CATEGORY);
const rootCategoryId = category.recordset[0].id;
const result = await request(pool, tx)
.input('name', sql.NVarChar(255), name)
.input('rootCategoryId', sql.Int, rootCategoryId)
.input('languageId', sql.Int, languageId)
.input('taxrate', sql.Int, taxrateId)
.input('reducedtaxrate', sql.Int, reducedtaxrateId)
.input('taxfreerate', sql.Int, taxfreerateId)
.query(CREATE);
await tx.commit();
return result.recordset[0].id;
} catch (err) {
try {
await tx.rollback();
} catch {
// ignore
}
throw err;
}
};
}

View File

@@ -0,0 +1,13 @@
import sql from 'mssql';
import { GET_BY_ID } from '../../sql/company.js';
import request from '../request.js';
export default function getById(pool) {
return async (id, tx) => {
const result = await request(pool, tx)
.input('id', sql.Int, id)
.query(GET_BY_ID);
return result.recordset[0];
};
}

View File

@@ -0,0 +1,13 @@
import sql from 'mssql';
import { GET_BY_NAME } from '../../sql/company.js';
import request from '../request.js';
export default function getByName(pool) {
return async (name, tx) => {
const result = await request(pool, tx)
.input('name', sql.NVarChar(255), name)
.query(GET_BY_NAME);
return result.recordset[0];
};
}

View File

@@ -0,0 +1,54 @@
import sql from 'mssql';
import { UPDATE } from '../../sql/company.js';
import getById from './getById.js';
import getByName from './getByName.js';
import langEnsure from '../lang/ensure.js';
import taxEnsure from '../tax/ensure.js';
import request from '../request.js';
export default function update(pool) {
const byId = getById(pool);
const byName = getByName(pool);
const ensureLang = langEnsure(pool);
const ensureTax = taxEnsure(pool);
return async (id, { iso639, name, taxrate, reducedtaxrate }) => {
const tx = new sql.Transaction(pool);
await tx.begin();
try {
const company = await byId(id, tx);
if (!company) {
throw new Error('Company not found');
}
if (name !== company.name) {
const taken = await byName(name, tx);
if (taken) {
throw new Error('Company name not available');
}
}
const languageId = await ensureLang(iso639, tx);
const taxrateId = await ensureTax(taxrate, tx);
const reducedtaxrateId = await ensureTax(reducedtaxrate, tx);
await request(pool, tx)
.input('id', sql.Int, id)
.input('name', sql.NVarChar(255), name)
.input('languageId', sql.Int, languageId)
.input('taxrate', sql.Int, taxrateId)
.input('reducedtaxrate', sql.Int, reducedtaxrateId)
.query(UPDATE);
await tx.commit();
} catch (err) {
try {
await tx.rollback();
} catch {
// ignore
}
throw err;
}
};
}

View File

@@ -0,0 +1,12 @@
import sql from 'mssql';
import { SET_SELECTED_COMPANY } from '../../sql/google_auth.js';
export default function setSelectedCompany(pool) {
return async (id, companyId) => {
await pool
.request()
.input('id', sql.Int, id)
.input('companyId', sql.Int, companyId)
.query(SET_SELECTED_COMPANY);
};
}

View File

@@ -1,10 +1,10 @@
import sql from 'mssql';
import { CREATE } from '../../sql/language.js';
import request from '../request.js';
export default function create(pool) {
return async (iso639) => {
const result = await pool
.request()
return async (iso639, tx) => {
const result = await request(pool, tx)
.input('iso639', sql.NVarChar(10), iso639)
.query(CREATE);

12
src/store/lang/ensure.js Normal file
View File

@@ -0,0 +1,12 @@
import getByIso639 from './getByIso639.js';
import create from './create.js';
export default function ensure(pool) {
const get = getByIso639(pool);
const add = create(pool);
return async (iso639, tx) => {
const row = await get(iso639, tx);
if (row) return row.id;
return add(iso639, tx);
};
}

View File

@@ -0,0 +1,13 @@
import sql from 'mssql';
import { GET_BY_ISO639 } from '../../sql/language.js';
import request from '../request.js';
export default function getByIso639(pool) {
return async (iso639, tx) => {
const result = await request(pool, tx)
.input('iso639', sql.NVarChar(10), iso639)
.query(GET_BY_ISO639);
return result.recordset[0];
};
}

5
src/store/request.js Normal file
View File

@@ -0,0 +1,5 @@
import sql from 'mssql';
export default function request(pool, tx) {
return tx ? new sql.Request(tx) : pool.request();
}

13
src/store/tax/create.js Normal file
View File

@@ -0,0 +1,13 @@
import sql from 'mssql';
import { CREATE } from '../../sql/tax.js';
import request from '../request.js';
export default function create(pool) {
return async (rate, tx) => {
const result = await request(pool, tx)
.input('rate', sql.Decimal(18, 4), rate)
.query(CREATE);
return result.recordset[0].id;
};
}

12
src/store/tax/ensure.js Normal file
View File

@@ -0,0 +1,12 @@
import getByRate from './getByRate.js';
import create from './create.js';
export default function ensure(pool) {
const get = getByRate(pool);
const add = create(pool);
return async (rate, tx) => {
const row = await get(rate, tx);
if (row) return row.id;
return add(rate, tx);
};
}

View File

@@ -0,0 +1,13 @@
import sql from 'mssql';
import { GET_BY_RATE } from '../../sql/tax.js';
import request from '../request.js';
export default function getByRate(pool) {
return async (rate, tx) => {
const result = await request(pool, tx)
.input('rate', sql.Decimal(18, 4), rate)
.query(GET_BY_RATE);
return result.recordset[0];
};
}

8
src/store/tax/list.js Normal file
View File

@@ -0,0 +1,8 @@
import { LIST } from '../../sql/tax.js';
export default function list(pool) {
return async () => {
const result = await pool.request().query(LIST);
return result.recordset;
};
}