u
This commit is contained in:
@@ -1,26 +1,15 @@
|
||||
import { GET_CATEGORY_ID_BY_SKU } from '../../sql/article.js';
|
||||
import { ADD as ADD_ARTICLEPIC, GET_NEXT_SORT } from '../../sql/articlepic.js';
|
||||
import { CREATE as CREATE_PIC } from '../../sql/pic.js';
|
||||
import processAvifImage from '../../util/processAvifImage.js';
|
||||
import newFullHash from '../../util/newFullHash.js';
|
||||
import resolveCategoryAccess from '../category/resolveCategoryAccess.js';
|
||||
import request from '../request.js';
|
||||
import resolveArticleBySku from './resolveArticleBySku.js';
|
||||
|
||||
export default function addImage(pool) {
|
||||
const access = resolveCategoryAccess(pool);
|
||||
const resolve = resolveArticleBySku(pool);
|
||||
|
||||
return async (googleAuthId, sku, image) => {
|
||||
const categoryLookup = await pool
|
||||
.request()
|
||||
.input('sku', sku)
|
||||
.query(GET_CATEGORY_ID_BY_SKU);
|
||||
|
||||
const row = categoryLookup.recordset[0];
|
||||
if (!row?.categoryId || !row?.articleId) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
|
||||
await access(googleAuthId, row.categoryId);
|
||||
const { articleId } = await resolve(googleAuthId, sku);
|
||||
|
||||
const processed = await processAvifImage(image);
|
||||
|
||||
@@ -29,7 +18,7 @@ export default function addImage(pool) {
|
||||
|
||||
try {
|
||||
const sortResult = await request(pool, tx)
|
||||
.input('articleId', row.articleId)
|
||||
.input('articleId', articleId)
|
||||
.query(GET_NEXT_SORT);
|
||||
|
||||
const sort = sortResult.recordset[0].sort;
|
||||
@@ -51,7 +40,7 @@ export default function addImage(pool) {
|
||||
const picId = picResult.recordset[0].id;
|
||||
|
||||
await request(pool, tx)
|
||||
.input('articleId', row.articleId)
|
||||
.input('articleId', articleId)
|
||||
.input('picId', picId)
|
||||
.input('sort', sort)
|
||||
.query(ADD_ARTICLEPIC);
|
||||
|
||||
@@ -1,25 +1,18 @@
|
||||
import { GET_CATEGORY_ID_BY_SKU } from '../../sql/article.js';
|
||||
import resolveCategoryAccess from '../category/resolveCategoryAccess.js';
|
||||
import resolveArticleBySku from './resolveArticleBySku.js';
|
||||
import queryBySku from './queryBySku.js';
|
||||
|
||||
export default function getBySku(pool) {
|
||||
const access = resolveCategoryAccess(pool);
|
||||
const resolve = resolveArticleBySku(pool);
|
||||
const load = queryBySku(pool);
|
||||
|
||||
return async (googleAuthId, sku) => {
|
||||
const categoryLookup = await pool
|
||||
.request()
|
||||
.input('sku', sku)
|
||||
.query(GET_CATEGORY_ID_BY_SKU);
|
||||
let t0 = performance.now();
|
||||
const { articleId, languageId } = await resolve(googleAuthId, sku);
|
||||
console.log(`[getBySku] resolveArticleBySku ${(performance.now() - t0).toFixed(1)}ms`);
|
||||
|
||||
const categoryId = categoryLookup.recordset[0]?.categoryId;
|
||||
if (!categoryId) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
|
||||
const { languageId } = await access(googleAuthId, categoryId);
|
||||
|
||||
const article = await load(sku, languageId);
|
||||
t0 = performance.now();
|
||||
const article = await load(articleId, languageId);
|
||||
console.log(`[getBySku] queryBySku (parallel) ${(performance.now() - t0).toFixed(1)}ms`);
|
||||
if (!article) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {
|
||||
GET_CATEGORY_IDS_BY_ARTICLE_ID,
|
||||
GET_DETAIL_BY_SKU,
|
||||
GET_DETAIL_BY_ARTICLE_ID,
|
||||
GET_EANS_BY_ARTICLE_ID,
|
||||
GET_PHOTOS_BY_ARTICLE_ID,
|
||||
GET_PRICES_BY_ARTICLE_ID,
|
||||
@@ -36,25 +36,24 @@ function shapeArticleDetail(row, categoryIds, photos, eans, prices) {
|
||||
}
|
||||
|
||||
export default function queryBySku(pool) {
|
||||
return async (sku, languageId) => {
|
||||
const detail = await pool
|
||||
.request()
|
||||
.input('sku', sku)
|
||||
.input('languageId', languageId)
|
||||
.query(GET_DETAIL_BY_SKU);
|
||||
return async (articleId, languageId) => {
|
||||
const [detail, categories, photos, eans, prices] = await Promise.all([
|
||||
pool
|
||||
.request()
|
||||
.input('articleId', articleId)
|
||||
.input('languageId', languageId)
|
||||
.query(GET_DETAIL_BY_ARTICLE_ID),
|
||||
pool.request().input('articleId', articleId).query(GET_CATEGORY_IDS_BY_ARTICLE_ID),
|
||||
pool.request().input('articleId', articleId).query(GET_PHOTOS_BY_ARTICLE_ID),
|
||||
pool.request().input('articleId', articleId).query(GET_EANS_BY_ARTICLE_ID),
|
||||
pool.request().input('articleId', articleId).query(GET_PRICES_BY_ARTICLE_ID),
|
||||
]);
|
||||
|
||||
const row = detail.recordset[0];
|
||||
if (!row) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const [categories, photos, eans, prices] = await Promise.all([
|
||||
pool.request().input('articleId', row.id).query(GET_CATEGORY_IDS_BY_ARTICLE_ID),
|
||||
pool.request().input('articleId', row.id).query(GET_PHOTOS_BY_ARTICLE_ID),
|
||||
pool.request().input('articleId', row.id).query(GET_EANS_BY_ARTICLE_ID),
|
||||
pool.request().input('articleId', row.id).query(GET_PRICES_BY_ARTICLE_ID),
|
||||
]);
|
||||
|
||||
return shapeArticleDetail(
|
||||
row,
|
||||
categories.recordset,
|
||||
|
||||
@@ -1,26 +1,31 @@
|
||||
import { GET_CATEGORY_ID_BY_SKU } from '../../sql/article.js';
|
||||
import resolveCategoryAccess from '../category/resolveCategoryAccess.js';
|
||||
import { RESOLVE_ARTICLE_ACCESS_BY_SKU } from '../../sql/article.js';
|
||||
|
||||
export default function resolveArticleBySku(pool) {
|
||||
const access = resolveCategoryAccess(pool);
|
||||
|
||||
return async (googleAuthId, sku) => {
|
||||
const lookup = await pool
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('sku', sku)
|
||||
.query(GET_CATEGORY_ID_BY_SKU);
|
||||
.input('googleAuthId', googleAuthId)
|
||||
.query(RESOLVE_ARTICLE_ACCESS_BY_SKU);
|
||||
|
||||
const row = lookup.recordset[0];
|
||||
if (!row?.categoryId || !row?.articleId) {
|
||||
const row = result.recordset[0];
|
||||
if (!row?.articleId || !row?.categoryId) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
|
||||
const { languageId } = await access(googleAuthId, row.categoryId);
|
||||
if (!row.companyId || !row.rootCategoryId || !row.languageId) {
|
||||
throw new Error('Parent not found');
|
||||
}
|
||||
if (!row.companyuserId) {
|
||||
throw new Error('Not authorized');
|
||||
}
|
||||
|
||||
return {
|
||||
articleId: row.articleId,
|
||||
categoryId: row.categoryId,
|
||||
languageId,
|
||||
languageId: row.languageId,
|
||||
companyId: row.companyId,
|
||||
rootCategoryId: row.rootCategoryId,
|
||||
posCustomerGroupId: row.posCustomerGroupId ?? null,
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { GET_CATEGORY_ID_BY_SKU } from '../../sql/article.js';
|
||||
import { UPDATE_BY_ARTICLE_AND_LANGUAGE } from '../../sql/articletext.js';
|
||||
import resolveCategoryAccess from '../category/resolveCategoryAccess.js';
|
||||
import request from '../request.js';
|
||||
import queryBySku from './queryBySku.js';
|
||||
import resolveArticleBySku from './resolveArticleBySku.js';
|
||||
|
||||
function slugify(name) {
|
||||
return name
|
||||
@@ -15,7 +14,7 @@ function slugify(name) {
|
||||
}
|
||||
|
||||
export default function updateBySku(pool) {
|
||||
const access = resolveCategoryAccess(pool);
|
||||
const resolve = resolveArticleBySku(pool);
|
||||
const load = queryBySku(pool);
|
||||
|
||||
return async (googleAuthId, sku, { name }) => {
|
||||
@@ -23,17 +22,7 @@ export default function updateBySku(pool) {
|
||||
throw new Error('Name required');
|
||||
}
|
||||
|
||||
const categoryLookup = await pool
|
||||
.request()
|
||||
.input('sku', sku)
|
||||
.query(GET_CATEGORY_ID_BY_SKU);
|
||||
|
||||
const row = categoryLookup.recordset[0];
|
||||
if (!row?.categoryId || !row?.articleId) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
|
||||
const { languageId } = await access(googleAuthId, row.categoryId);
|
||||
const { articleId, languageId } = await resolve(googleAuthId, sku);
|
||||
|
||||
const slug = slugify(name);
|
||||
const tx = pool.transaction();
|
||||
@@ -41,7 +30,7 @@ export default function updateBySku(pool) {
|
||||
|
||||
try {
|
||||
const update = await request(pool, tx)
|
||||
.input('articleId', row.articleId)
|
||||
.input('articleId', articleId)
|
||||
.input('languageId', languageId)
|
||||
.input('name', name.trim())
|
||||
.input('slug', slug)
|
||||
@@ -53,7 +42,7 @@ export default function updateBySku(pool) {
|
||||
|
||||
await tx.commit();
|
||||
|
||||
const article = await load(sku, languageId);
|
||||
const article = await load(articleId, languageId);
|
||||
if (!article) {
|
||||
throw new Error('Article not found');
|
||||
}
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
import { CREATE } from '../../sql/company.js';
|
||||
import { CREATE as CREATE_CATEGORY } from '../../sql/category.js';
|
||||
import {
|
||||
CREATE_WITHOUT_COMPANY as CREATE_CUSTOMERGROUP,
|
||||
SET_COMPANY as SET_CUSTOMERGROUP_COMPANY,
|
||||
} from '../../sql/customergroup.js';
|
||||
import { ADD as ADD_COMPANYUSER } from '../../sql/companyuser.js';
|
||||
import getByName from './getByName.js';
|
||||
import langEnsure from '../lang/ensure.js';
|
||||
import taxEnsure from '../tax/ensure.js';
|
||||
import request from '../request.js';
|
||||
|
||||
const POS_CUSTOMERGROUP_NAME = 'POS';
|
||||
|
||||
export default function create(pool) {
|
||||
const nameTaken = getByName(pool);
|
||||
const ensureLang = langEnsure(pool);
|
||||
@@ -33,6 +39,14 @@ export default function create(pool) {
|
||||
|
||||
const rootCategoryId = category.recordset[0].id;
|
||||
|
||||
// Circular FK: create POS group first (company_id null), company with
|
||||
// poscustomergroup_id NOT NULL, then link the group back to the company.
|
||||
const posGroup = await request(pool, tx)
|
||||
.input('name', POS_CUSTOMERGROUP_NAME)
|
||||
.query(CREATE_CUSTOMERGROUP);
|
||||
|
||||
const posCustomerGroupId = posGroup.recordset[0].id;
|
||||
|
||||
const result = await request(pool, tx)
|
||||
.input('name', name)
|
||||
.input('rootCategoryId', rootCategoryId)
|
||||
@@ -40,10 +54,16 @@ export default function create(pool) {
|
||||
.input('taxrate', taxrateId)
|
||||
.input('reducedtaxrate', reducedtaxrateId)
|
||||
.input('taxfreerate', taxfreerateId)
|
||||
.input('posCustomerGroupId', posCustomerGroupId)
|
||||
.query(CREATE);
|
||||
|
||||
const companyId = result.recordset[0].id;
|
||||
|
||||
await request(pool, tx)
|
||||
.input('id', posCustomerGroupId)
|
||||
.input('companyId', companyId)
|
||||
.query(SET_CUSTOMERGROUP_COMPANY);
|
||||
|
||||
await request(pool, tx)
|
||||
.input('companyId', companyId)
|
||||
.input('googleAuthId', googleAuthId)
|
||||
@@ -58,6 +78,7 @@ export default function create(pool) {
|
||||
taxrateId,
|
||||
reducedtaxrateId,
|
||||
taxfreerateId,
|
||||
posCustomerGroupId,
|
||||
};
|
||||
} catch (err) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user