Skip to content

Commit

Permalink
refactor(groups): change db structure and remove group locales
Browse files Browse the repository at this point in the history
Reverts some changes and implements other tweaks.
Grades are now stored as a separate table (rather than JSON).
Groups will add all columns to the OxGroup state.
Removed default groups.
  • Loading branch information
thelindat committed May 29, 2024
1 parent 1cca426 commit 529fd26
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 47 deletions.
12 changes: 1 addition & 11 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,5 @@
"non_binary": "Non-Binary",
"date_of_birth": "Date of birth",
"withdraw": "Withdraw",
"deposit": "Deposit",
"groups": {
"police": {
"name": "Los Santos Police Department",
"grades": ["Cadet", "Officer", "Sergeant", "Captain", "Commander", "Chief"]
},
"dispatch": {
"name": "Police Dispatch",
"grades": ["Dispatcher"]
}
}
"deposit": "Deposit"
}
17 changes: 14 additions & 3 deletions server/groups/db.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { db } from 'db';

export type GroupsTable = { name: string; grades: number; colour?: number };
import type { DbGroups } from 'types';

export function SelectGroups() {
return db.query<GroupsTable>('SELECT * FROM ox_groups');
return db.query<DbGroups>(`
SELECT
ox_groups.*,
JSON_ARRAYAGG(ox_group_grades.label ORDER BY ox_group_grades.grade) AS grades
FROM
ox_groups
JOIN
ox_group_grades
ON
ox_groups.name = ox_group_grades.group
GROUP BY
ox_groups.name;
`);
}

export async function AddCharacterGroup(charId: number, name: string, grade: number) {
Expand Down
20 changes: 7 additions & 13 deletions server/groups/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { addAce, addCommand, addPrincipal, locale, removeAce, removePrincipal }
import { SelectGroups } from './db';
import { OxPlayer } from 'player/class';
import type { Dict, OxGroup } from 'types';
import type { GroupsTable } from './db';
import type { GroupsResult } from './db';

Check failure on line 5 in server/groups/index.ts

View workflow job for this annotation

GitHub Actions / Build and Create Tagged release

Module '"./db"' has no exported member 'GroupsResult'.
import { GetGroupPermissions } from '../../common';

const groups: Dict<OxGroup> = {};
Expand All @@ -29,23 +29,17 @@ export function RemoveGroupPermission(groupName: string, grade: number, permissi
GlobalState[`group.${groupName}:permissions`] = permissions;
}

async function CreateGroup({ name, grades, colour }: GroupsTable) {
async function CreateGroup(data: GroupsResult) {
const group: OxGroup = {
name,
colour,
label: locale(`groups.${name}.name`),
principal: `group.${name}`,
grades: [],
...data,
grades: JSON.parse(data.grades),
principal: `group.${data.name}`,
};

for (let index = 0; index < grades; index++) {
group.grades[index] = locale(`groups.${name}.grades.${index}`);
}

GlobalState[group.principal] = group;
GlobalState[`${group.name}:count`] = 0;

groups[name] = group;
groups[group.name] = group;
group.grades = group.grades.reduce(
(acc, value, index) => {
acc[index + 1] = value;
Expand Down Expand Up @@ -126,4 +120,4 @@ addCommand<{ target: string; group: string; grade?: number }>(
);

exports('SetGroupPermission', SetGroupPermission);
exports('RemoveGroupPermission', RemoveGroupPermission)
exports('RemoveGroupPermission', RemoveGroupPermission);
26 changes: 8 additions & 18 deletions sql/install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,18 @@ CREATE TABLE
CREATE TABLE
IF NOT EXISTS `ox_groups` (
`name` VARCHAR(20) NOT NULL,
`grades` LONGTEXT NOT NULL,
`label` VARCHAR(50) NOT NULL,
`colour` TINYINT UNSIGNED DEFAULT NULL,
PRIMARY KEY (`name`) USING BTREE
);

INSERT INTO
`ox_groups` (
`name`,
`grades`,
`colour`
)
VALUES
(
'police',
6,
6
),
(
'dispatch',
1,
1
);
CREATE TABLE `ox_group_grades` (
`group` VARCHAR(20) NOT NULL,
`grade` TINYINT(3) UNSIGNED NOT NULL DEFAULT '1',
`label` VARCHAR(50) NOT NULL,
PRIMARY KEY (`group`, `grade`) USING BTREE,
CONSTRAINT `FK_ox_group_grades_ox_groups` FOREIGN KEY (`group`) REFERENCES `ox_groups` (`name`) ON UPDATE CASCADE ON DELETE CASCADE
);

CREATE TABLE
IF NOT EXISTS `character_groups` (
Expand Down
8 changes: 6 additions & 2 deletions types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,18 @@ export interface OxAccount {
role: 'contributor' | 'manager' | 'owner';
}

export interface OxGroup {
export interface DbGroups {
name: string;
label: string;
grades: string[];
principal: string;
colour?: number;
}

export interface OxGroup extends DbGroups {
grades: string[];
principal: string;
}

export interface OxGroupPermissions {
[grade: string]: { [permission: string]: boolean };
}

0 comments on commit 529fd26

Please sign in to comment.