Repository navigation
add Yemot transportation late arrival reporting #77
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,133 @@ | ||
| import { Injectable } from '@nestjs/common'; | ||
| import { BaseYemotHandlerService } from '@shared/utils/yemot/v2/yemot-router.service'; | ||
|
|
||
| import { Student } from './db/entities/Student.entity'; | ||
| import { Transportation } from './db/entities/Transportation.entity'; | ||
| import { KnownAbsence } from './db/entities/KnownAbsence.entity'; | ||
| import { StudentKlass } from './db/entities/StudentKlass.entity'; | ||
| import { Between } from 'typeorm'; | ||
| /** | ||
| * Yemot Handler Service for processing incoming Yemot calls | ||
| * Currently returns a maintenance mode message | ||
| */ | ||
|
|
||
| @Injectable() | ||
| export class YemotHandlerService extends BaseYemotHandlerService { | ||
|
|
||
| override async processCall(): Promise<void> { | ||
| await this.getUserByDidPhone(); | ||
| this.logger.log(`Processing call with ID: ${this.call.callId} from phone: ${this.call.phone}`); | ||
|
|
||
| // Return maintenance mode message | ||
| this.hangupWithMessage('המערכת במצב תחזוקה, אנא נסה שוב מאוחר יותר'); | ||
| const student = await this.getStudentByPhone() | ||
| if (!student) return; | ||
| const alreadyReported = await this.hasReportedToday(student.id); | ||
| if (alreadyReported) { | ||
| await this.hangupWithMessage("כבר דיווחת היום, לא ניתן לדווח פעמיים."); | ||
| return; | ||
| } | ||
| const transportation = await this.getTransportByInput(); | ||
| if (!transportation) return; | ||
| const isValid = await this.isDepartureTimeValid(transportation); | ||
| if (isValid) { | ||
| await this.createAbsenceRecord(student, transportation); | ||
| await this.hangupWithMessage("דווח בהצלחה") | ||
| } | ||
| else { | ||
| await this.hangupWithMessage("יצאת מאוחר מידי המשך יום טוב"); | ||
| } | ||
| } | ||
|
|
||
| private async getStudentByPhone(): Promise<Student> { | ||
| const student = await this.dataSource.getRepository(Student).findOneBy({ | ||
| userId: this.user.id, | ||
| phone: this.call.phone, | ||
| }); | ||
| if (!student) { | ||
| await this.hangupWithMessage('הטלפון לא קיים במערכת'); | ||
| return null | ||
| } | ||
| return student; | ||
| } | ||
|
|
||
| private async hasReportedToday(studentId: number): Promise<boolean> { | ||
| const absenceRepo = this.dataSource.getRepository(KnownAbsence); | ||
|
|
||
| const startOfDay = new Date(); | ||
| startOfDay.setHours(0, 0, 0, 0); | ||
|
|
||
| const endOfDay = new Date(); | ||
| endOfDay.setHours(23, 59, 59, 999); | ||
|
|
||
| const existingReport = await absenceRepo.findOne({ | ||
| where: { | ||
| userId: this.user.id, | ||
| studentReferenceId: studentId, | ||
| reportDate: Between(startOfDay, endOfDay) | ||
| } | ||
| }); | ||
| return !!existingReport; | ||
| } | ||
|
|
||
| private async getTransportByInput(): Promise<Transportation> { | ||
| let transportation = null; | ||
|
|
||
| while (!transportation) { | ||
| transportation = await this.getTransportByNum(); | ||
|
|
||
| if (!transportation) { | ||
| await this.sendMessage(' מספר הסעה לא תקין, נסי שוב'); | ||
| } | ||
| } | ||
|
|
||
| return transportation; | ||
| } | ||
|
|
||
| private async getTransportByNum(): Promise<Transportation> { | ||
| const num = await this.askForInput('הקישי מספר הסעה'); | ||
| const transportation = await this.dataSource.getRepository(Transportation).findOneBy({ | ||
| userId: this.user.id, | ||
| key: Number(num), | ||
| }); | ||
| return transportation; | ||
| } | ||
|
|
||
| private async isDepartureTimeValid(transportation): Promise<boolean> { | ||
| let valid: boolean | null = null; | ||
| while (valid === null) { | ||
| const message = `האם יצאת לדרך לפני השעה ${transportation.departureTime}? הקישי 1 - כן, 2 - לא`; | ||
| const userInput = await this.askForInput(message); | ||
| if (userInput === '1') { | ||
| valid = true; | ||
| } else if (userInput === '2') { | ||
| valid = false; | ||
| } else { | ||
| await this.sendMessage('הקשה לא תקינה, נא הקישי 1 או 2'); | ||
| } | ||
| } | ||
| return valid; | ||
| } | ||
|
|
||
| private async createAbsenceRecord(student: Student, transportation: Transportation) { | ||
| const absenceRepo = this.dataSource.getRepository(KnownAbsence); | ||
| const studentKlass = await this.dataSource.getRepository(StudentKlass).findOneBy({ | ||
| userId: this.user.id, | ||
| studentReferenceId: student.id, | ||
| studentTz: student.tz | ||
| }); | ||
| if (!studentKlass) { | ||
| await this.hangupWithMessage(' התלמידה אינה משויכת לכיתה.'); | ||
| return; | ||
| } | ||
| const newAbsence = absenceRepo.create({ | ||
| userId: this.user.id, | ||
| studentTz: student.tz, | ||
| studentReferenceId: student.id, | ||
| reportDate: new Date(), | ||
| absnceCount: 1, | ||
| isApproved: true, | ||
| comment: `איחור של הסעה ${transportation.key}`, | ||
| klassReferenceId: studentKlass.klassReferenceId, | ||
| }); | ||
| await absenceRepo.save(newAbsence); | ||
| } | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
תלמידה יכולה להיות משויכת ליותר מכיתה אחת, תבדקי מול הסמינר מה ירצו לעשות (ייתכן ויבחרו להשתמש רק בכיתת הבסיס)