Skip to content

Commit

Permalink
DisciplineConfig checking
Browse files Browse the repository at this point in the history
  • Loading branch information
disturm committed Jun 9, 2021
1 parent c730b0d commit 5bccf32
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/helpers/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ export function groupBy<TItem>(items: TItem[], key: keyof TItem) {
}, reducer);
}

export function getKeys<T>(obj: T): (keyof T)[] {
const keys = Object.keys(obj) as (keyof T)[];
return keys;
}

export function filterNull<T>(items: (T | null)[]): T[] {
const result: T[] = [];
for (const item of items) {
if (item !== null) {
result.push(item);
}
}
return result;
}

export function pluralize(
count: number,
version1: string,
Expand Down
37 changes: 35 additions & 2 deletions src/managers/SpreadsheetManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { StudentFailure, TermType } from "../apis/BrsApi";
import { ControlActionConfig } from "./MarksManager";
import * as googleApi from "../apis/GoogleApi";
import { compareNormalized, normalizeString } from "../helpers/tools";
import {
compareNormalized,
getKeys,
filterNull,
normalizeString,
} from "../helpers/tools";
import { parseStudentFailure } from "../helpers/brsHelpers";

export interface ActualStudent {
Expand All @@ -27,6 +32,10 @@ export interface DisciplineConfig {
defaultStudentFailure: StudentFailure;
}

type DisciplineConfigErrors = {
[key in keyof DisciplineConfig]: string | null;
};

export default class SpreadsheetManager {
private readonly spreadsheetId: string;

Expand Down Expand Up @@ -219,61 +228,85 @@ function buildDisciplineConfig(rows: string[][], indices: Indices) {
isModule: false,
defaultStudentFailure: StudentFailure.NoFailure,
};
const errors: DisciplineConfigErrors = {
name: "Дисциплина",
year: "Учебный год",
termType: "Семестр",
course: "Курс",
isModule: "ИТС",
defaultStudentFailure: "Причина отсутствия по умолчанию",
};

for (let i = 0; i < rows.length; i++) {
const key = rows[i][indices.disciplineKeyColumn]?.trim();
if (!key) {
break;
}
const value = rows[i][indices.disciplineValueColumn]?.trim();
addDisciplineConfigParameter(result, key, value);
addDisciplineConfigParameter(result, errors, key, value);
}

const errorNames = filterNull(getKeys(errors).map((k) => errors[k]));
if (errorNames.length > 0) {
const errorNamesString = errorNames.map(n => ${n}»`).join(", ");
throw new Error(`Следующие параметры дисциплины не заданы: ${errorNamesString}`);
}

return result;
}

function addDisciplineConfigParameter(
config: DisciplineConfig,
errors: DisciplineConfigErrors,
key: string,
value: string
) {
if (compareNormalized(key, "Дисциплина")) {
if (value) {
config.name = value;
errors.name = null;
}
} else if (compareNormalized(key, "ИТС")) {
if (value) {
config.isModule = value.toLowerCase() === "да";
errors.isModule = null;
}
} else if (compareNormalized(key, "Год")) {
if (value) {
config.year = parseInt(value.toLowerCase(), 10);
errors.year = null;
}
} else if (compareNormalized(key, "Учебный год")) {
if (value) {
const yearParts = value.toLowerCase().split("/");
config.year = parseInt(yearParts[0], 10);
errors.year = null;
}
} else if (compareNormalized(key, "Семестр")) {
if (value) {
if (value.toLowerCase() === "осенний") {
config.termType = TermType.Fall;
errors.termType = null;
} else if (value.toLowerCase() === "весенний") {
config.termType = TermType.Spring;
errors.termType = null;
}
}
} else if (compareNormalized(key, "Курс")) {
if (value) {
const lowerValue = value.toLowerCase().trim();
if (lowerValue === "все курсы") {
config.course = 0;
errors.course = null;
} else {
config.course = parseInt(value.toLowerCase(), 10);
errors.course = null;
}
}
} else if (compareNormalized(key, "Причина отсутствия по умолчанию")) {
config.defaultStudentFailure =
parseStudentFailure(value) ?? StudentFailure.NoFailure;
errors.defaultStudentFailure = null;
}
}

Expand Down

0 comments on commit 5bccf32

Please sign in to comment.