u
This commit is contained in:
32
src/index.js
32
src/index.js
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
`;
|
||||
|
||||
@@ -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
|
||||
`;
|
||||
|
||||
@@ -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
16
src/sql/google_auth.js
Normal 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
15
src/sql/language.js
Normal 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
|
||||
`;
|
||||
14
src/store/category/create.js
Normal file
14
src/store/category/create.js
Normal 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;
|
||||
};
|
||||
}
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
13
src/store/company/create.js
Normal file
13
src/store/company/create.js
Normal 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;
|
||||
};
|
||||
}
|
||||
11
src/store/company/delete.js
Normal file
11
src/store/company/delete.js
Normal 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);
|
||||
};
|
||||
}
|
||||
@@ -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];
|
||||
};
|
||||
|
||||
8
src/store/company/list.js
Normal file
8
src/store/company/list.js
Normal 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;
|
||||
};
|
||||
}
|
||||
12
src/store/company/setDefaultLang.js
Normal file
12
src/store/company/setDefaultLang.js
Normal 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);
|
||||
};
|
||||
}
|
||||
12
src/store/company/setRootCategory.js
Normal file
12
src/store/company/setRootCategory.js
Normal 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);
|
||||
};
|
||||
}
|
||||
14
src/store/companyuser/add.js
Normal file
14
src/store/companyuser/add.js
Normal 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;
|
||||
};
|
||||
}
|
||||
@@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
13
src/store/google_auth/add.js
Normal file
13
src/store/google_auth/add.js
Normal 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;
|
||||
};
|
||||
}
|
||||
11
src/store/google_auth/del.js
Normal file
11
src/store/google_auth/del.js
Normal 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);
|
||||
};
|
||||
}
|
||||
8
src/store/google_auth/list.js
Normal file
8
src/store/google_auth/list.js
Normal 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
13
src/store/lang/create.js
Normal 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
11
src/store/lang/delete.js
Normal 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
8
src/store/lang/list.js
Normal 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;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user