diff --git a/server/groups/db.ts b/server/groups/db.ts index 70761cb6..7e3804f8 100644 --- a/server/groups/db.ts +++ b/server/groups/db.ts @@ -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) { diff --git a/server/groups/index.ts b/server/groups/index.ts index 85af73f1..a146264e 100644 --- a/server/groups/index.ts +++ b/server/groups/index.ts @@ -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; @@ -112,6 +119,10 @@ export async function CreateGroup(data: CreateGroupProperties) { {} as Dict, ); + if (grades.length === 0) { + throw new Error(`Cannot create OxGroup<${data.name}> (missing at least one grade)`) + } + const group: DbGroup = { ...data, grades: grades,