Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions server/groups/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export async function InsertGroup({ name, label, type, colour, hasAccount, grade
const insertedGrades = (await conn.batch(
'INSERT INTO `ox_group_grades` (`group`, `grade`, `label`, `accountRole`) VALUES (?, ?, ?, ?)',
grades.map((gradeLabel, index) => [name, index + 1, gradeLabel, accountRoles[index + 1]]),
)) as UpsertResult;
)) as UpsertResult[];

return insertedGrades.affectedRows > 0;
return insertedGrades.reduce((acc, curr) => acc + curr.affectedRows, 0) > 0
}

export function RemoveGroup(groupName: string) {
Expand Down
13 changes: 12 additions & 1 deletion server/groups/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,14 @@ function SetupGroup(data: DbGroup) {
export async function CreateGroup(data: CreateGroupProperties) {
if (groups[data.name]) throw new Error(`Cannot create OxGroup<${data.name}> (group already exists with that name)`);

const grades = data.grades.map((grade) => grade.label);
if (data.label.length > 50) {
throw new Error(`Cannot create OxGroup<${data.name}> (label is too long)`)
}

const grades = data.grades
.filter((grade) => grade.label)
.map((grade) => grade.label);

const accountRoles = data.grades.reduce(
(acc, grade, index) => {
if (grade.accountRole) acc[index + 1] = grade.accountRole;
Expand All @@ -112,6 +119,10 @@ export async function CreateGroup(data: CreateGroupProperties) {
{} as Dict<OxAccountRole>,
);

if (grades.length === 0) {
throw new Error(`Cannot create OxGroup<${data.name}> (missing at least one grade)`)
}

const group: DbGroup = {
...data,
grades: grades,
Expand Down