This commit is contained in:
seb
2026-07-31 00:20:20 +02:00
parent 6bdac7e543
commit af8ef0a788
23 changed files with 421 additions and 123 deletions

View File

@@ -1,6 +1,19 @@
import companyuserGetByEmail from './store/companyuser/getByEmail.js';
import companyuserAdd from './store/companyuser/add.js';
import companyGetByCompanyUserId from './store/company/getByCompanyUserId.js';
import companyCreate from './store/company/create.js';
import companySetDefaultLang from './store/company/setDefaultLang.js';
import companySetRootCategory from './store/company/setRootCategory.js';
import companyDelete from './store/company/delete.js';
import companyList from './store/company/list.js';
import categoryGetTree from './store/category/getTree.js';
import categoryCreate from './store/category/create.js';
import langCreate from './store/lang/create.js';
import langDelete from './store/lang/delete.js';
import langList from './store/lang/list.js';
import googleAuthAdd from './store/google_auth/add.js';
import googleAuthList from './store/google_auth/list.js';
import googleAuthDel from './store/google_auth/del.js';
import storePurge from './store/purge.js';
import storeInitialize from './store/initialize.js';
@@ -10,14 +23,33 @@ export default class LibDashStore {
this.companyuser = {
getByEmail: companyuserGetByEmail(pool),
add: companyuserAdd(pool),
};
this.company = {
getByCompanyUserId: companyGetByCompanyUserId(pool),
create: companyCreate(pool),
setDefaultLang: companySetDefaultLang(pool),
setRootCategory: companySetRootCategory(pool),
delete: companyDelete(pool),
list: companyList(pool),
};
this.category = {
getTree: categoryGetTree(pool),
create: categoryCreate(pool),
};
this.lang = {
create: langCreate(pool),
delete: langDelete(pool),
list: langList(pool),
};
this.google_auth = {
add: googleAuthAdd(pool),
list: googleAuthList(pool),
del: googleAuthDel(pool),
};
this.purge = storePurge(pool);

View File

@@ -14,3 +14,9 @@ SELECT id, parent_id, sort
FROM tree
ORDER BY sort, id
`;
export const CREATE = `
INSERT INTO category (parent_id, sort)
OUTPUT INSERTED.id
VALUES (@parentId, @sort)
`;

View File

@@ -8,3 +8,31 @@ FROM company c
INNER JOIN companyuser cu ON cu.company_id = c.id
WHERE cu.id = @companyUserId
`;
export const CREATE = `
INSERT INTO company (name)
OUTPUT INSERTED.id
VALUES (@name)
`;
export const LIST = `
SELECT id, name, root_category_id, default_language_id
FROM company
`;
export const SET_DEFAULT_LANG = `
UPDATE company
SET default_language_id = @languageId
WHERE id = @companyId
`;
export const SET_ROOT_CATEGORY = `
UPDATE company
SET root_category_id = @categoryId
WHERE id = @companyId
`;
export const DELETE = `
DELETE FROM company
WHERE id = @companyId
`;

View File

@@ -4,3 +4,9 @@ FROM companyuser cu
INNER JOIN google_auth ga ON ga.id = cu.google_auth_id
WHERE ga.email = @email
`;
export const ADD = `
INSERT INTO companyuser (google_auth_id, company_id)
OUTPUT INSERTED.id
VALUES (@authId, @companyId)
`;

16
src/sql/google_auth.js Normal file
View File

@@ -0,0 +1,16 @@
export const ADD = `
INSERT INTO google_auth (email)
OUTPUT INSERTED.id
VALUES (@email)
`;
export const LIST = `
SELECT id, email, created_at
FROM google_auth
ORDER BY id
`;
export const DEL = `
DELETE FROM google_auth
WHERE id = @id
`;

15
src/sql/language.js Normal file
View File

@@ -0,0 +1,15 @@
export const CREATE = `
INSERT INTO language (iso639)
OUTPUT INSERTED.id
VALUES (@iso639)
`;
export const LIST = `
SELECT id, iso639
FROM language
`;
export const DELETE = `
DELETE FROM language
WHERE id = @id
`;

View File

@@ -0,0 +1,14 @@
import sql from 'mssql';
import { CREATE } from '../../sql/category.js';
export default function create(pool) {
return async ({ parent_id, sort }) => {
const result = await pool
.request()
.input('parentId', sql.Int, parent_id)
.input('sort', sql.Int, sort)
.query(CREATE);
return result.recordset[0].id;
};
}

View File

@@ -1,30 +1,6 @@
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
@@ -32,7 +8,6 @@ export default function getTree(pool) {
.input('rootId', sql.Int, rootId)
.query(GET_TREE);
if (result.recordset.length === 0) return null;
return buildTree(result.recordset, rootId);
return result.recordset;
};
}

View File

@@ -0,0 +1,13 @@
import sql from 'mssql';
import { CREATE } from '../../sql/company.js';
export default function create(pool) {
return async ({ name }) => {
const result = await pool
.request()
.input('name', sql.NVarChar(255), name)
.query(CREATE);
return result.recordset[0].id;
};
}

View File

@@ -0,0 +1,11 @@
import sql from 'mssql';
import { DELETE } from '../../sql/company.js';
export default function del(pool) {
return async (companyId) => {
await pool
.request()
.input('companyId', sql.Int, companyId)
.query(DELETE);
};
}

View File

@@ -9,7 +9,7 @@ export default function getByCompanyUserId(pool) {
.query(GET_BY_COMPANY_USER_ID);
const row = result.recordset[0];
if (!row) return null;
if (!row) return;
return [row.id, row.default_language_id, row.root_category_id, row.name];
};

View File

@@ -0,0 +1,8 @@
import { LIST } from '../../sql/company.js';
export default function list(pool) {
return async () => {
const result = await pool.request().query(LIST);
return result.recordset;
};
}

View File

@@ -0,0 +1,12 @@
import sql from 'mssql';
import { SET_DEFAULT_LANG } from '../../sql/company.js';
export default function setDefaultLang(pool) {
return async (companyId, languageId) => {
await pool
.request()
.input('companyId', sql.Int, companyId)
.input('languageId', sql.Int, languageId)
.query(SET_DEFAULT_LANG);
};
}

View File

@@ -0,0 +1,12 @@
import sql from 'mssql';
import { SET_ROOT_CATEGORY } from '../../sql/company.js';
export default function setRootCategory(pool) {
return async (companyId, categoryId) => {
await pool
.request()
.input('companyId', sql.Int, companyId)
.input('categoryId', sql.Int, categoryId)
.query(SET_ROOT_CATEGORY);
};
}

View File

@@ -0,0 +1,14 @@
import sql from 'mssql';
import { ADD } from '../../sql/companyuser.js';
export default function add(pool) {
return async (companyId, authId) => {
const result = await pool
.request()
.input('companyId', sql.Int, companyId)
.input('authId', sql.Int, authId)
.query(ADD);
return result.recordset[0].id;
};
}

View File

@@ -8,6 +8,6 @@ export default function getByEmail(pool) {
.input('email', sql.NVarChar(320), email)
.query(GET_BY_EMAIL);
return result.recordset[0]?.id ?? null;
return result.recordset.map((row) => row.id);
};
}

View File

@@ -0,0 +1,13 @@
import sql from 'mssql';
import { ADD } from '../../sql/google_auth.js';
export default function add(pool) {
return async (email) => {
const result = await pool
.request()
.input('email', sql.NVarChar(320), email)
.query(ADD);
return result.recordset[0].id;
};
}

View File

@@ -0,0 +1,11 @@
import sql from 'mssql';
import { DEL } from '../../sql/google_auth.js';
export default function del(pool) {
return async (id) => {
await pool
.request()
.input('id', sql.Int, id)
.query(DEL);
};
}

View File

@@ -0,0 +1,8 @@
import { LIST } from '../../sql/google_auth.js';
export default function list(pool) {
return async () => {
const result = await pool.request().query(LIST);
return result.recordset;
};
}

13
src/store/lang/create.js Normal file
View File

@@ -0,0 +1,13 @@
import sql from 'mssql';
import { CREATE } from '../../sql/language.js';
export default function create(pool) {
return async (iso639) => {
const result = await pool
.request()
.input('iso639', sql.NVarChar(10), iso639)
.query(CREATE);
return result.recordset[0].id;
};
}

11
src/store/lang/delete.js Normal file
View File

@@ -0,0 +1,11 @@
import sql from 'mssql';
import { DELETE } from '../../sql/language.js';
export default function del(pool) {
return async (id) => {
await pool
.request()
.input('id', sql.Int, id)
.query(DELETE);
};
}

8
src/store/lang/list.js Normal file
View File

@@ -0,0 +1,8 @@
import { LIST } from '../../sql/language.js';
export default function list(pool) {
return async () => {
const result = await pool.request().query(LIST);
return result.recordset;
};
}