This commit is contained in:
seb
2026-09-25 12:30:45 +02:00
parent b5255bdd17
commit 607e6cc0ac
10 changed files with 98 additions and 124 deletions

View File

@@ -54,6 +54,7 @@ export default function createUnderParent(provider) {
let article;
try {
article = await runner.query(createArticleSql, {
companyId,
sku,
unitId,
unitamount: 1,

View File

@@ -157,7 +157,7 @@ export default function importRows(provider) {
continue;
}
const existing = await baseRunner.query(articleQueries.GET_ID_BY_SKU, { sku });
const existing = await baseRunner.query(articleQueries.GET_ID_BY_SKU, { sku, companyId });
if (existing.rows[0]) {
skipped.push({ sku, name, reason: 'duplicate sku' });
continue;
@@ -169,6 +169,7 @@ export default function importRows(provider) {
const slug = slugify(name);
const article = await runner.query(articleQueries.CREATE, {
companyId,
sku,
unitId,
unitamount: 1,
@@ -277,10 +278,10 @@ export default function importRows(provider) {
try {
await runInTx(async (runner) => {
const child = await runner.query(articleQueries.GET_ID_BY_SKU, { sku });
const child = await runner.query(articleQueries.GET_ID_BY_SKU, { sku, companyId });
const childId = child.rows[0]?.articleId;
if (childId) {
const father = await runner.query(articleQueries.GET_ID_BY_SKU, { sku: fatherSku });
const father = await runner.query(articleQueries.GET_ID_BY_SKU, { sku: fatherSku, companyId });
const fatherId = father.rows[0]?.articleId;
if (fatherId && fatherId !== childId) {
const childCount = await runner.query(getChildCountSql, {
@@ -289,6 +290,7 @@ export default function importRows(provider) {
const count = childCount.rows[0].count;
await runner.query(articleQueries.UPDATE_FATHER_BY_SKU, {
sku,
companyId,
fatherArticleId: fatherId,
fathersort: count,
});

View File

@@ -30,11 +30,12 @@ export default function setFatherBySku(provider) {
*/
return async (googleAuthId, sku, fatherSku, tx) => {
const runSet = async (runner) => {
const { articleId, languageId } = await resolve(googleAuthId, sku, runner);
const { articleId, languageId, companyId } = await resolve(googleAuthId, sku, runner);
if (fatherSku == null || fatherSku === '') {
await runner.query(queries.UPDATE_FATHER_BY_SKU, {
sku,
companyId,
fatherArticleId: null,
});
const unlinked = await load(articleId, languageId, runner);
@@ -44,7 +45,7 @@ export default function setFatherBySku(provider) {
return unlinked;
}
const father = await runner.query(queries.GET_ID_BY_SKU, { sku: fatherSku });
const father = await runner.query(queries.GET_ID_BY_SKU, { sku: fatherSku, companyId });
const fatherId = father.rows[0]?.articleId;
if (!fatherId) {
throw new Error('Father not found');
@@ -60,6 +61,7 @@ export default function setFatherBySku(provider) {
await runner.query(queries.UPDATE_FATHER_BY_SKU, {
sku,
companyId,
fatherArticleId: fatherId,
fathersort: count,
});

View File

@@ -1,8 +1,16 @@
import { LIST_BY_COMPANY as MSSQL_LIST_BY_COMPANY } from '../../providers/mssql/queries/numbers.js';
import { LIST_BY_COMPANY as SQLITE_LIST_BY_COMPANY } from '../../providers/sqlite3/queries/numbers.js';
import {
ENSURE_TYPE as MSSQL_ENSURE_TYPE,
LIST_BY_COMPANY as MSSQL_LIST_BY_COMPANY,
} from '../../providers/mssql/queries/numbers.js';
import {
ENSURE_TYPE as SQLITE_ENSURE_TYPE,
LIST_BY_COMPANY as SQLITE_LIST_BY_COMPANY,
} from '../../providers/sqlite3/queries/numbers.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
const DEFAULT_TYPES = ['sku', 'entity', 'delivery'];
function shape(row) {
return {
id: row.id,
@@ -15,13 +23,14 @@ function shape(row) {
export default function list(provider) {
const access = resolveSelectedCompanyAccess(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const ensureSql = isSqlite ? SQLITE_ENSURE_TYPE : MSSQL_ENSURE_TYPE;
const sql =
provider?.dialect === 'sqlite3'
? SQLITE_LIST_BY_COMPANY
: MSSQL_LIST_BY_COMPANY;
isSqlite ? SQLITE_LIST_BY_COMPANY : MSSQL_LIST_BY_COMPANY;
/**
* Lists the number sequences of the user's selected company.
* Ensures that default sequences ('sku', 'entity', 'delivery') exist.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} [tx] - Optional transaction to run within.
@@ -33,9 +42,14 @@ export default function list(provider) {
*/
return async (googleAuthId, tx) => {
const { companyId } = await access(googleAuthId, tx);
const runner = makeRunner(provider, tx);
const result = await makeRunner(provider, tx).query(sql, { companyId });
for (const type of DEFAULT_TYPES) {
await runner.query(ensureSql, { companyId, type });
}
const result = await runner.query(sql, { companyId });
return result.rows.map(shape);
};
}
}