sqlite
This commit is contained in:
@@ -1,13 +1,22 @@
|
||||
import {
|
||||
GET_MANIFEST_HEADER,
|
||||
GET_MANIFEST_LINES,
|
||||
GET_MANIFEST_HEADER as MSSQL_GET_MANIFEST_HEADER,
|
||||
GET_MANIFEST_LINES as MSSQL_GET_MANIFEST_LINES,
|
||||
} from '../../providers/mssql/queries/manifestDoc.js';
|
||||
import {
|
||||
GET_CUSTOMER_BY_ID,
|
||||
POS_GROUP_NAME,
|
||||
SUPPLIER_GROUP_NAME,
|
||||
GET_MANIFEST_HEADER as SQLITE_GET_MANIFEST_HEADER,
|
||||
GET_MANIFEST_LINES as SQLITE_GET_MANIFEST_LINES,
|
||||
} from '../../providers/sqlite3/queries/manifestDoc.js';
|
||||
import {
|
||||
GET_CUSTOMER_BY_ID as MSSQL_GET_CUSTOMER_BY_ID,
|
||||
POS_GROUP_NAME as MSSQL_POS_GROUP_NAME,
|
||||
SUPPLIER_GROUP_NAME as MSSQL_SUPPLIER_GROUP_NAME,
|
||||
} from '../../providers/mssql/queries/entity.js';
|
||||
import request from '../request.js';
|
||||
import {
|
||||
GET_CUSTOMER_BY_ID as SQLITE_GET_CUSTOMER_BY_ID,
|
||||
POS_GROUP_NAME as SQLITE_POS_GROUP_NAME,
|
||||
SUPPLIER_GROUP_NAME as SQLITE_SUPPLIER_GROUP_NAME,
|
||||
} from '../../providers/sqlite3/queries/entity.js';
|
||||
import makeRunner from '../runner.js';
|
||||
import { shapeLine } from '../manifestLines.js';
|
||||
|
||||
export function shapeSummary(type, row) {
|
||||
@@ -21,7 +30,21 @@ export function shapeSummary(type, row) {
|
||||
};
|
||||
}
|
||||
|
||||
export async function assertCustomerEntity(pool, tx, companyId, entityId) {
|
||||
export async function assertCustomerEntity(runnerOrPool, txOrCompanyId, maybeCompanyId, maybeEntityId) {
|
||||
let runner;
|
||||
let companyId;
|
||||
let entityId;
|
||||
|
||||
if (maybeEntityId !== undefined) {
|
||||
runner = makeRunner(runnerOrPool, txOrCompanyId);
|
||||
companyId = maybeCompanyId;
|
||||
entityId = maybeEntityId;
|
||||
} else {
|
||||
runner = makeRunner(runnerOrPool);
|
||||
companyId = txOrCompanyId;
|
||||
entityId = maybeCompanyId;
|
||||
}
|
||||
|
||||
if (entityId == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -29,37 +52,58 @@ export async function assertCustomerEntity(pool, tx, companyId, entityId) {
|
||||
throw new Error('Entity not found');
|
||||
}
|
||||
|
||||
const result = await request(pool, tx)
|
||||
.input('id', entityId)
|
||||
.input('companyId', companyId)
|
||||
.input('supplierGroupName', SUPPLIER_GROUP_NAME)
|
||||
.input('posGroupName', POS_GROUP_NAME)
|
||||
.query(GET_CUSTOMER_BY_ID);
|
||||
const isSqlite = runner?.dialect === 'sqlite3';
|
||||
const getCustomerSql = isSqlite ? SQLITE_GET_CUSTOMER_BY_ID : MSSQL_GET_CUSTOMER_BY_ID;
|
||||
const supplierGroupName = isSqlite ? SQLITE_SUPPLIER_GROUP_NAME : MSSQL_SUPPLIER_GROUP_NAME;
|
||||
const posGroupName = isSqlite ? SQLITE_POS_GROUP_NAME : MSSQL_POS_GROUP_NAME;
|
||||
|
||||
if (!result.recordset[0]) {
|
||||
const result = await runner.query(getCustomerSql, {
|
||||
id: entityId,
|
||||
companyId,
|
||||
supplierGroupName,
|
||||
posGroupName,
|
||||
});
|
||||
|
||||
if (!result.rows[0]) {
|
||||
throw new Error('Entity not found');
|
||||
}
|
||||
return entityId;
|
||||
}
|
||||
|
||||
export async function loadDoc(pool, tx, { type, companyId, id }) {
|
||||
const header = await request(pool, tx)
|
||||
.input('id', id)
|
||||
.input('companyId', companyId)
|
||||
.input('type', type)
|
||||
.query(GET_MANIFEST_HEADER);
|
||||
export async function loadDoc(runnerOrPool, txOrParams, maybeParams) {
|
||||
let runner;
|
||||
let params;
|
||||
|
||||
const row = header.recordset[0];
|
||||
if (maybeParams !== undefined) {
|
||||
runner = makeRunner(runnerOrPool, txOrParams);
|
||||
params = maybeParams;
|
||||
} else {
|
||||
runner = makeRunner(runnerOrPool);
|
||||
params = txOrParams;
|
||||
}
|
||||
|
||||
const { type, companyId, id } = params;
|
||||
const isSqlite = runner?.dialect === 'sqlite3';
|
||||
const getHeaderSql = isSqlite ? SQLITE_GET_MANIFEST_HEADER : MSSQL_GET_MANIFEST_HEADER;
|
||||
const getLinesSql = isSqlite ? SQLITE_GET_MANIFEST_LINES : MSSQL_GET_MANIFEST_LINES;
|
||||
|
||||
const header = await runner.query(getHeaderSql, {
|
||||
id,
|
||||
companyId,
|
||||
type,
|
||||
});
|
||||
|
||||
const row = header.rows[0];
|
||||
if (!row) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const lines = await request(pool, tx)
|
||||
.input('manifestId', id)
|
||||
.query(GET_MANIFEST_LINES);
|
||||
const lines = await runner.query(getLinesSql, {
|
||||
manifestId: id,
|
||||
});
|
||||
|
||||
return {
|
||||
...shapeSummary(type, row),
|
||||
lines: lines.recordset.map(shapeLine),
|
||||
lines: lines.rows.map(shapeLine),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user