sqlite
This commit is contained in:
@@ -1,6 +1,14 @@
|
||||
import { CREATE, GET_NEXT_SORT } from '../../providers/mssql/queries/entityaddress.js';
|
||||
import {
|
||||
CREATE as MSSQL_CREATE,
|
||||
GET_NEXT_SORT as MSSQL_GET_NEXT_SORT,
|
||||
} from '../../providers/mssql/queries/entityaddress.js';
|
||||
import {
|
||||
CREATE as SQLITE_CREATE,
|
||||
GET_NEXT_SORT as SQLITE_GET_NEXT_SORT,
|
||||
} from '../../providers/sqlite3/queries/entityaddress.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import resolveEntityAccess from '../entity/resolveEntityAccess.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
function shape(row) {
|
||||
return {
|
||||
@@ -24,9 +32,12 @@ function optionalString(value) {
|
||||
return s || null;
|
||||
}
|
||||
|
||||
export default function create(pool) {
|
||||
const access = resolveSelectedCompanyAccess(pool);
|
||||
const entityAccess = resolveEntityAccess(pool);
|
||||
export default function create(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const entityAccess = resolveEntityAccess(provider);
|
||||
const isSqlite = provider?.dialect === 'sqlite3';
|
||||
const getSortSql = isSqlite ? SQLITE_GET_NEXT_SORT : MSSQL_GET_NEXT_SORT;
|
||||
const createSql = isSqlite ? SQLITE_CREATE : MSSQL_CREATE;
|
||||
|
||||
/**
|
||||
* Creates a new address for an entity.
|
||||
@@ -43,6 +54,7 @@ export default function create(pool) {
|
||||
* @param {string} [input.city] - The city.
|
||||
* @param {string} [input.county] - The county.
|
||||
* @param {number} [input.sort] - Optional sort order.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<{id: number, entityId: number, firstName: string|null, name: string|null, street: string|null, streetNumber: string|null, zip: string|null, city: string|null, county: string|null, addressType: string, sort: number}>} The created address.
|
||||
* @throws {Error} If the address type is missing.
|
||||
*
|
||||
@@ -50,7 +62,7 @@ export default function create(pool) {
|
||||
* const address = await store.entityaddress.create(42, 5, { addressType: 'billing', street: 'Main St', city: 'Berlin' });
|
||||
* // => { id, entityId, firstName, name, street, streetNumber, zip, city, county, addressType, sort }
|
||||
*/
|
||||
return async (googleAuthId, entityId, input = {}) => {
|
||||
return async (googleAuthId, entityId, input = {}, tx) => {
|
||||
const addressType = input.addressType?.trim?.()
|
||||
? input.addressType.trim()
|
||||
: null;
|
||||
@@ -58,34 +70,45 @@ export default function create(pool) {
|
||||
throw new Error('Address type required');
|
||||
}
|
||||
|
||||
const { companyId } = await access(googleAuthId);
|
||||
await entityAccess(companyId, entityId);
|
||||
const runner = makeRunner(provider, tx);
|
||||
const { companyId } = await access(googleAuthId, tx);
|
||||
await entityAccess(companyId, entityId, tx);
|
||||
|
||||
let sort = input.sort;
|
||||
if (sort === undefined || sort === null) {
|
||||
const next = await pool
|
||||
.request()
|
||||
.input('entityId', entityId)
|
||||
.query(GET_NEXT_SORT);
|
||||
sort = next.recordset[0].sort;
|
||||
const next = await runner.query(getSortSql, { entityId });
|
||||
sort = next.rows[0].sort;
|
||||
} else if (!Number.isFinite(sort)) {
|
||||
throw new Error('Invalid sort');
|
||||
}
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('entityId', entityId)
|
||||
.input('firstName', optionalString(input.firstName))
|
||||
.input('name', optionalString(input.name))
|
||||
.input('street', optionalString(input.street))
|
||||
.input('streetNumber', optionalString(input.streetNumber))
|
||||
.input('zip', optionalString(input.zip))
|
||||
.input('city', optionalString(input.city))
|
||||
.input('county', optionalString(input.county))
|
||||
.input('addressType', addressType)
|
||||
.input('sort', sort)
|
||||
.query(CREATE);
|
||||
const result = await runner.query(createSql, {
|
||||
entityId,
|
||||
firstName: optionalString(input.firstName),
|
||||
name: optionalString(input.name),
|
||||
street: optionalString(input.street),
|
||||
streetNumber: optionalString(input.streetNumber),
|
||||
zip: optionalString(input.zip),
|
||||
city: optionalString(input.city),
|
||||
county: optionalString(input.county),
|
||||
addressType,
|
||||
sort,
|
||||
});
|
||||
|
||||
return shape(result.recordset[0]);
|
||||
const row = result.rows[0] ?? {
|
||||
id: result.insertId,
|
||||
entityId,
|
||||
firstName: optionalString(input.firstName),
|
||||
name: optionalString(input.name),
|
||||
street: optionalString(input.street),
|
||||
streetNumber: optionalString(input.streetNumber),
|
||||
zip: optionalString(input.zip),
|
||||
city: optionalString(input.city),
|
||||
county: optionalString(input.county),
|
||||
addressType,
|
||||
sort,
|
||||
};
|
||||
|
||||
return shape(row);
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user