Restructured services to support more... services
This commit is contained in:
57
api/src/services/db/rolesService.ts
Normal file
57
api/src/services/db/rolesService.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { MemberLight } from '@app/shared/types/member';
|
||||
import pool from '../../db';
|
||||
import { Role, RoleSummary } from '@app/shared/types/roles'
|
||||
|
||||
export async function assignUserGroup(userID: number, roleID: number) {
|
||||
const sql = `INSERT INTO members_roles (member_id, role_id) VALUES (?, ?);`;
|
||||
const params = [userID, roleID];
|
||||
|
||||
return await pool.query(sql, params);
|
||||
}
|
||||
|
||||
export async function createGroup(name: string, color: string, description: string) {
|
||||
const sql = `INSERT INTO roles (name, color, description) VALUES (?, ?, ?)`;
|
||||
const params = [name, color, description];
|
||||
|
||||
const result = await pool.query(sql, params);
|
||||
return { id: result.insertId, name, color, description };
|
||||
}
|
||||
|
||||
export async function getUserRoles(userID: number): Promise<Role[]> {
|
||||
const sql = `SELECT r.id, r.name
|
||||
FROM members_roles mr
|
||||
INNER JOIN roles r ON mr.role_id = r.id
|
||||
WHERE mr.member_id = ?;`;
|
||||
|
||||
return await pool.query(sql, [userID]);
|
||||
}
|
||||
|
||||
export async function getRole(id: number): Promise<Role> {
|
||||
let res = await pool.query(`SELECT * FROM roles WHERE id = ?`, [id])
|
||||
return res[0] as Role;
|
||||
}
|
||||
|
||||
export async function getAllRoles(): Promise<RoleSummary> {
|
||||
return await pool.query(`SELECT id, name, color FROM roles`);
|
||||
}
|
||||
|
||||
export async function getUsersWithRole(roleId: number): Promise<MemberLight[]> {
|
||||
const out = await pool.query(
|
||||
`
|
||||
SELECT
|
||||
m.member_id AS id,
|
||||
m.member_name AS username,
|
||||
m.displayName,
|
||||
u.color
|
||||
FROM members_roles mr
|
||||
JOIN view_member_rank_unit_status_latest m
|
||||
ON m.member_id = mr.member_id
|
||||
LEFT JOIN units u
|
||||
ON u.name = m.unit
|
||||
WHERE mr.role_id = ?
|
||||
`,
|
||||
[roleId]
|
||||
)
|
||||
|
||||
return out as MemberLight[]
|
||||
}
|
||||
Reference in New Issue
Block a user