u
This commit is contained in:
38
src/store/category/getTree.js
Normal file
38
src/store/category/getTree.js
Normal file
@@ -0,0 +1,38 @@
|
||||
import sql from 'mssql';
|
||||
import { GET_TREE } from '../../sql/category.js';
|
||||
|
||||
function buildTree(rows, rootId) {
|
||||
const byId = new Map();
|
||||
for (const row of rows) {
|
||||
byId.set(row.id, {
|
||||
id: row.id,
|
||||
parent_id: row.parent_id,
|
||||
sort: row.sort,
|
||||
children: [],
|
||||
});
|
||||
}
|
||||
|
||||
let root = null;
|
||||
for (const node of byId.values()) {
|
||||
if (node.id === rootId) {
|
||||
root = node;
|
||||
continue;
|
||||
}
|
||||
const parent = byId.get(node.parent_id);
|
||||
if (parent) parent.children.push(node);
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
export default function getTree(pool) {
|
||||
return async (rootId) => {
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('rootId', sql.Int, rootId)
|
||||
.query(GET_TREE);
|
||||
|
||||
if (result.recordset.length === 0) return null;
|
||||
return buildTree(result.recordset, rootId);
|
||||
};
|
||||
}
|
||||
16
src/store/company/getByCompanyUserId.js
Normal file
16
src/store/company/getByCompanyUserId.js
Normal file
@@ -0,0 +1,16 @@
|
||||
import sql from 'mssql';
|
||||
import { GET_BY_COMPANY_USER_ID } from '../../sql/company.js';
|
||||
|
||||
export default function getByCompanyUserId(pool) {
|
||||
return async (companyUserId) => {
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('companyUserId', sql.Int, companyUserId)
|
||||
.query(GET_BY_COMPANY_USER_ID);
|
||||
|
||||
const row = result.recordset[0];
|
||||
if (!row) return null;
|
||||
|
||||
return [row.id, row.default_language_id, row.root_category_id, row.name];
|
||||
};
|
||||
}
|
||||
13
src/store/companyuser/getByEmail.js
Normal file
13
src/store/companyuser/getByEmail.js
Normal file
@@ -0,0 +1,13 @@
|
||||
import sql from 'mssql';
|
||||
import { GET_BY_EMAIL } from '../../sql/companyuser.js';
|
||||
|
||||
export default function getByEmail(pool) {
|
||||
return async (email) => {
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('email', sql.NVarChar(320), email)
|
||||
.query(GET_BY_EMAIL);
|
||||
|
||||
return result.recordset[0]?.id ?? null;
|
||||
};
|
||||
}
|
||||
12
src/store/initialize.js
Normal file
12
src/store/initialize.js
Normal file
@@ -0,0 +1,12 @@
|
||||
import { SCHEMA_SCRIPTS } from '../schema.js';
|
||||
import purge from './purge.js';
|
||||
|
||||
export default function initialize(pool) {
|
||||
const doPurge = purge(pool);
|
||||
return async () => {
|
||||
await doPurge();
|
||||
for (const script of SCHEMA_SCRIPTS) {
|
||||
await pool.request().query(script);
|
||||
}
|
||||
};
|
||||
}
|
||||
7
src/store/purge.js
Normal file
7
src/store/purge.js
Normal file
@@ -0,0 +1,7 @@
|
||||
import { buildPurgeSql } from '../sql/purge.js';
|
||||
|
||||
export default function purge(pool) {
|
||||
return async () => {
|
||||
await pool.request().query(buildPurgeSql());
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user