u
This commit is contained in:
35
src/queries/customer-count.js
Normal file
35
src/queries/customer-count.js
Normal file
@@ -0,0 +1,35 @@
|
||||
import sql from 'mssql';
|
||||
import { isDemoMode } from '../demo/mode.js';
|
||||
import { getDemoCustomerCount } from '../demo/store.js';
|
||||
import { getPool } from '../db.js';
|
||||
import { getActiveShopId, getActiveShopSubshopId } from '../shop.js';
|
||||
|
||||
const CUSTOMER_COUNT_SQL = `
|
||||
SELECT COUNT(DISTINCT k.kKunde) AS CustomerCount
|
||||
FROM dbo.tkunde k
|
||||
LEFT JOIN dbo.tInetKundeShop iks
|
||||
ON iks.kKunde = k.kKunde
|
||||
AND iks.kShop = @kShop
|
||||
AND iks.kSubShop = @SubShopId
|
||||
WHERE k.kKundenGruppe = 1
|
||||
AND (@kShop = 0 OR EXISTS (
|
||||
SELECT 1 FROM dbo.tInetKundeShop x
|
||||
WHERE x.kKunde = k.kKunde AND x.kShop = @kShop
|
||||
))
|
||||
AND CONVERT(BIGINT, k.bRowversion) > @cursor;
|
||||
`;
|
||||
|
||||
export async function getCustomerCount({ cursor = 0 } = {}) {
|
||||
if (isDemoMode()) {
|
||||
return getDemoCustomerCount({ cursor });
|
||||
}
|
||||
|
||||
const result = await getPool()
|
||||
.request()
|
||||
.input('kShop', sql.Int, getActiveShopId())
|
||||
.input('SubShopId', sql.Int, getActiveShopSubshopId())
|
||||
.input('cursor', sql.BigInt, cursor)
|
||||
.query(CUSTOMER_COUNT_SQL);
|
||||
|
||||
return result.recordset[0]?.CustomerCount ?? 0;
|
||||
}
|
||||
Reference in New Issue
Block a user