Reorder via customergroup.order (exact id list → sort 0…n-1); block renaming POS/Lieferanten while still allowing create/update of other groups by name.
34 lines
884 B
JavaScript
34 lines
884 B
JavaScript
import {
|
|
CREATE,
|
|
GET_BY_COMPANY_AND_NAME,
|
|
GET_NEXT_SORT,
|
|
} from '../../sql/customergroup.js';
|
|
import { SUPPLIER_GROUP_NAME } from '../../sql/entity.js';
|
|
|
|
/** Ensure company has customergroup "Lieferanten"; return its id. */
|
|
export default async function ensureLieferantenGroup(pool, companyId, tx) {
|
|
const req = () => (tx ?? pool).request();
|
|
|
|
const existing = await req()
|
|
.input('companyId', companyId)
|
|
.input('name', SUPPLIER_GROUP_NAME)
|
|
.query(GET_BY_COMPANY_AND_NAME);
|
|
|
|
if (existing.recordset[0]) {
|
|
return existing.recordset[0].id;
|
|
}
|
|
|
|
const next = await req()
|
|
.input('companyId', companyId)
|
|
.query(GET_NEXT_SORT);
|
|
const sort = next.recordset[0].sort;
|
|
|
|
const created = await req()
|
|
.input('companyId', companyId)
|
|
.input('name', SUPPLIER_GROUP_NAME)
|
|
.input('sort', sort)
|
|
.query(CREATE);
|
|
|
|
return created.recordset[0].id;
|
|
}
|