Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Current timetable parsing #1

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
54 changes: 54 additions & 0 deletions src/current-timetable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Lesson } from './types';
import { defined } from './utils';

interface CurrentTimetableCard {
type: 'card';
date: string;
uniperiod: string;
starttime: string;
endtime: string;
subjectid: string;
classids: string[]
groupnames: string[];
igroupid: string;
teacherids: string[];
classroomids: string[];
colors: string[];
cellSlices: string;
cellOrder: number;
removed?: boolean;
}

type CurrentTimetableItem = CurrentTimetableCard;

export function parseCurrentTimetable(json: string): {
lessons: Lesson[];
} {
const body = JSON.parse(json);
if (body === null || typeof body !== 'object') throw new Error('current timetable response should be an object');
if (body.r === null || typeof body.r !== 'object') {
throw new Error('current timetable response should have a field "r"');
}
if (!(body.r.ttitems instanceof Array)) throw new Error('current timetable response should have an array "r.ttitems"');
const items = body.r.ttitems as CurrentTimetableItem[];
const lessons = items.map((item): Lesson | undefined => {
if (item.type !== 'card') return undefined;
return {
type: 'lesson',
date: item.date,
periodId: item.uniperiod,
startTime: item.starttime,
endTime: item.endtime,
classIds: item.classids,
classroomIds: item.classroomids,
color: item.colors[0] ?? null,
groupNames: item.groupnames.filter((name) => name !== ''),
subjectId: item.subjectid,
teacherIds: item.teacherids,
removed: item.removed ?? false,
};
}).filter(defined);
return {
lessons,
};
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './substitutions';
export * from './maindbi';
export * from './current-timetable';
export * from './url';
export * from './types';
91 changes: 91 additions & 0 deletions src/maindbi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { MainDbi } from './types.js';

interface MainDbiTableRows {
teachers: {
id: string;
short: string;
};

subjects: {
id: string;
name: string;
short: string;
};

classrooms: {
id: string;
short: string;
};

classes: {
id: string;
name: string;
short: string;
};

periods: {
id: string;
name: string;
starttime: string;
endtime: string;
};
}

type MainDbiTable<ID extends keyof MainDbiTableRows = keyof MainDbiTableRows> = ID extends any ? {
id: ID;
data_rows: MainDbiTableRows[ID][];
} : never;

interface MainDbiResponse {
type: 'maindbi';
dbid: string;
tables: MainDbiTable[];
}

export function parseMainDbi(json: string): MainDbi {
const body = JSON.parse(json);
if (body === null || typeof body !== 'object') throw new Error('maindbi response should be an object');
if (body.r === null || typeof body.r !== 'object') throw new Error('maindbi response should have a field "r"');
if (body.r.type !== 'maindbi') throw new Error('"r.type" should be equal to "maindbi" in maindbi response');
const response = body.r as MainDbiResponse;
const result: MainDbi = {
classes: {},
classrooms: {},
subjects: {},
teachers: {},
periods: {},
};

const mappers: {
[ID in keyof MainDbiTableRows]: (row: MainDbiTableRows[ID]) => MainDbi[ID][string];
} = {
classes: (row) => ({
name: row.name,
short: row.short,
}),
teachers: (row) => ({
name: row.short,
}),
classrooms: (row) => ({
name: row.short,
}),
periods: (row) => ({
name: row.name,
startTime: row.starttime,
endTime: row.endtime,
}),
subjects: (row) => ({
name: row.name,
short: row.short,
}),
};
response.tables.forEach((table) => {
const resultTable = result[table.id];
const mapper = mappers[table.id];
table.data_rows.forEach((row) => {
resultTable[row.id] = mapper(row as any);
});
});

return result;
}
48 changes: 48 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,57 @@ export interface SubstitutionSection {
name: string;
changes: Substitution[];
}

export interface Substitutions {
date: string;
absentTeachers: Absence[];
absentClasses: Absence[];
sections: SubstitutionSection[];
}

interface Teacher {
name: string;
}

interface Subject {
name: string;
short: string;
}

interface Classroom {
name: string;
}

interface Class {
name: string;
short: string;
}

interface Period {
name: string;
startTime: string;
endTime: string;
}

export interface MainDbi {
teachers: Record<string, Teacher>;
subjects: Record<string, Subject>;
classrooms: Record<string, Classroom>;
classes: Record<string, Class>;
periods: Record<string, Period>;
}

export interface Lesson {
type: 'lesson';
date: string;
periodId: string;
startTime: string;
endTime: string;
subjectId: string;
classIds: string[];
groupNames: string[];
teacherIds: string[];
classroomIds: string[];
color: string | null;
removed: boolean;
}
48 changes: 46 additions & 2 deletions src/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,51 @@ export const getSubstitutionsUrl = () => '/substitution/server/viewer.js?__func=
export const getSubstitutionsBody = (date: string, teachersMode = false) => ({
__args: [
null,
{ date: date, mode: teachersMode ? 'teachers' : 'classes' }
{ date, mode: teachersMode ? 'teachers' : 'classes' },
],
__gsh: '00000000'
__gsh: '00000000',
});

export const getMainDbiBody = (schoolYear: number) => ({
__args: [
null,
schoolYear,
{},
{
op: 'fetch',
needed_part: {
teachers: ['short'],
subjects: ['short', 'name'],
classrooms: ['short'],
classes: ['short', 'name'],
periods: ['name', 'starttime', 'endtime'],
},
needed_combos: {},
},
],
__gsh: '00000000',
});

export const getCurrentTimetableBody = (
schoolYear: number,
dateFrom: string,
dateTo: string,
table: 'classes' | 'teachers' | 'classrooms' | 'subjects',
id: string,
) => ({
__args: [
null,
{
year: schoolYear,
datefrom: dateFrom,
dateto: dateTo,
table,
id,
showColors: true,
showIgroupsInClasses: false,
showOrig: true,
log_module: 'CurrentTTView',
},
],
__gsh: '00000000',
});
109 changes: 109 additions & 0 deletions test/expected/class-current-timetable-0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
"lessons": [
{
"type": "lesson",
"date": "2022-10-31",
"periodId": "1",
"startTime": "08:00",
"endTime": "09:45",
"subjectId": "-22",
"classIds": [
"-105"
],
"groupNames": [],
"teacherIds": [
"-48"
],
"classroomIds": [
"-28"
],
"color": "#60FFC0",
"removed": false
},
{
"type": "lesson",
"date": "2022-10-31",
"periodId": "3",
"startTime": "10:00",
"endTime": "10:45",
"subjectId": "-2",
"classIds": [
"-105"
],
"groupNames": [],
"teacherIds": [
"-161"
],
"classroomIds": [
"-16"
],
"color": null,
"removed": true
},
{
"type": "lesson",
"date": "2022-10-31",
"periodId": "4",
"startTime": "11:00",
"endTime": "11:45",
"subjectId": "-14",
"classIds": [
"-105"
],
"groupNames": [],
"teacherIds": [
"-37"
],
"classroomIds": [
"-12"
],
"color": "#66FFFF",
"removed": false
},
{
"type": "lesson",
"date": "2022-10-31",
"periodId": "5",
"startTime": "12:00",
"endTime": "13:45",
"subjectId": "-43",
"classIds": [
"-105"
],
"groupNames": [
"wychowanie fizyczne_ch"
],
"teacherIds": [
"-74"
],
"classroomIds": [
"-39"
],
"color": "#003399",
"removed": false
},
{
"type": "lesson",
"date": "2022-10-31",
"periodId": "5",
"startTime": "12:00",
"endTime": "13:45",
"subjectId": "-43",
"classIds": [
"-107",
"-105"
],
"groupNames": [
"wychowanie fizyczne_dz"
],
"teacherIds": [
"-70"
],
"classroomIds": [
"-64"
],
"color": "#FF8080",
"removed": false
}
]
}
3 changes: 3 additions & 0 deletions test/expected/class-current-timetable-event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"lessons": []
}
Loading