Skip to content

Commit

Permalink
Merge pull request #133 from Dudrie/fix-several-things
Browse files Browse the repository at this point in the history
Fixes passwords & attendance sheet generation
  • Loading branch information
Dudrie authored Oct 23, 2019
2 parents 2453ce2 + 7f47dcc commit 99cdd13
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
11 changes: 9 additions & 2 deletions client/src/components/forms/ChangePasswordForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,19 @@ function ChangePasswordForm({ onSubmit, onCancel, className, ...other }: Props):
const onFormSubmit: FormikSubmitCallback<ChangePasswordFormState> = async (values, actions) => {
const { password, repeatedPassword } = values;

if (password.includes(':')) {
setError('Passwörter dürfen keinen Doppelpunkt enthalten.');
actions.setSubmitting(false);
return;
}

if (password !== repeatedPassword) {
setError('Passwörter stimmen nicht überein.');
actions.setSubmitting(false);
} else {
return onSubmit(values, actions);
return;
}

onSubmit(values, actions);
};

return (
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"private": true,
"version": "1.5.0",
"version": "1.5.3",
"workspaces": [
"client",
"server",
Expand Down
33 changes: 33 additions & 0 deletions server/src/model/documents/TutorialDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { StudentDocument } from './StudentDocument';
import { TeamDocument, TeamSchema } from './TeamDocument';
import { UserDocument } from './UserDocument';
import { getIdOfDocumentRef } from '../../helpers/documentHelpers';
import studentService from '../../services/student-service/StudentService.class';
import Logger from '../../helpers/Logger';

export class TutorialSchema extends Typegoose
implements Omit<Tutorial, 'id' | 'tutor' | 'correctors' | 'students' | 'teams' | 'substitutes'> {
Expand Down Expand Up @@ -78,6 +80,37 @@ export class TutorialSchema extends Typegoose

return false;
}

@instanceMethod
async getStudents(this: InstanceType<TutorialDocument>): Promise<StudentDocument[]> {
const studentDocs: StudentDocument[] = [];
const studentsToRemove: string[] = [];

await Promise.all(
this.students.map(student =>
studentService
.getDocumentWithId(getIdOfDocumentRef(student))
.then(doc => studentDocs.push(doc))
.catch(() => {
Logger.error(
`[TutorialDocument] Student with ID ${getIdOfDocumentRef(
student
)} does not exist in the DB (anymore). It gets removed from the tutorial.`
);

studentsToRemove.push(getIdOfDocumentRef(student));
})
)
);

if (studentsToRemove.length > 0) {
this.students = this.students.filter(s => !studentsToRemove.includes(getIdOfDocumentRef(s)));

await this.save();
}

return studentDocs;
}
}

export interface TutorialDocument extends TutorialSchema, Document {}
Expand Down
15 changes: 8 additions & 7 deletions server/src/services/pdf-service/PdfService.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import puppeteer from 'puppeteer';
import { PointMap } from 'shared/dist/model/Points';
import { ScheincriteriaSummaryByStudents } from 'shared/dist/model/ScheinCriteria';
import { Student } from 'shared/dist/model/Student';
import { Tutorial } from 'shared/dist/model/Tutorial';
import { User } from 'shared/dist/model/User';
import { getNameOfEntity, sortByName } from 'shared/dist/util/helpers';
import showdown, { ShowdownExtension } from 'showdown';
import { getIdOfDocumentRef } from '../../helpers/documentHelpers';
import Logger from '../../helpers/Logger';
import { StudentDocument } from '../../model/documents/StudentDocument';
import { TeamDocument } from '../../model/documents/TeamDocument';
import { TutorialDocument } from '../../model/documents/TutorialDocument';
import { BadRequestError } from '../../model/Errors';
import scheincriteriaService from '../scheincriteria-service/ScheincriteriaService.class';
import sheetService from '../sheet-service/SheetService.class';
Expand Down Expand Up @@ -56,7 +58,7 @@ class PdfService {
public generateAttendancePDF(tutorialId: string, date: Date): Promise<Buffer> {
return new Promise(async (resolve, reject) => {
try {
const tutorial = await tutorialService.getTutorialWithID(tutorialId);
const tutorial = await tutorialService.getDocumentWithID(tutorialId);

const body: string = await this.generateAttendanceHTML(tutorial, date);
const html = this.putBodyInHtml(body);
Expand Down Expand Up @@ -228,7 +230,7 @@ class PdfService {
}
}

private async generateAttendanceHTML(tutorial: Tutorial, date: Date): Promise<string> {
private async generateAttendanceHTML(tutorial: TutorialDocument, date: Date): Promise<string> {
if (!tutorial.tutor) {
throw new BadRequestError(
'Tutorial which attendance list should be generated does NOT have a tutor assigned.'
Expand All @@ -237,10 +239,9 @@ class PdfService {

const template = this.getAttendanceTemplate();

const tutor = await userService.getUserWithId(tutorial.tutor);
const students: Student[] = await Promise.all(
tutorial.students.map(student => studentService.getStudentWithId(student))
);
const tutor = await userService.getUserWithId(getIdOfDocumentRef(tutorial.tutor));
const students: StudentDocument[] = await tutorial.getStudents();

students.sort(sortByName);
// const substitutePart = isSubstituteTutor(tutorial, userData)
// ? `, Ersatztutor: ${getNameOfEntity(userData)}`
Expand Down
2 changes: 2 additions & 0 deletions server/src/services/student-service/StudentService.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ class StudentService {
);

tutorial.students = tutorial.students.filter(stud => getIdOfDocumentRef(stud) !== student.id);

tutorial.markModified('students');
await tutorial.save();

return this.getStudentOrReject(await student.remove());
Expand Down

0 comments on commit 99cdd13

Please sign in to comment.