Skip to content
This repository has been archived by the owner on Jan 25, 2023. It is now read-only.

Commit

Permalink
Notify about changes in timetable, prototype
Browse files Browse the repository at this point in the history
Prototyping #45
  • Loading branch information
kpostekk committed Mar 27, 2022
1 parent 138f6c2 commit f3d3f29
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@
"@nestjs/swagger": "^5.2.0",
"@nestjs/throttler": "^2.0.1",
"@nestjs/websockets": "^8.4.0",
"@sendgrid/mail": "^7.6.2",
"@types/luxon": "^2.0.9",
"cache-manager": "^3.6.0",
"chance": "^1.1.8",
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
"dotenv": "^16.0.0",
"ics": "^2.35.0",
"jsdom": "^19.0.0",
"liquidjs": "^9.34.0",
Expand All @@ -49,7 +51,8 @@
"rimraf": "^3.0.2",
"rxjs": "^7.2.0",
"socket.io": "^4.4.1",
"swagger-ui-express": "^4.3.0"
"swagger-ui-express": "^4.3.0",
"yaml": "^1.10.2"
},
"devDependencies": {
"@nestjs/cli": "^8.0.0",
Expand All @@ -64,6 +67,7 @@
"@types/node": "17",
"@types/passport-local": "^1.0.34",
"@types/supertest": "^2.0.11",
"@types/yaml": "^1.9.7",
"@typescript-eslint/eslint-plugin": "^5.16.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^8.11.0",
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { PublicTimetableModule } from './public-timetable/public-timetable.modul
import { RedocModule, RedocOptions } from 'nestjs-redoc'
import { VersioningType } from '@nestjs/common'
import { HypervisorModule } from './hypervisor/hypervisor.module'
import 'dotenv/config'

async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
Expand Down
44 changes: 40 additions & 4 deletions src/public-timetable/public-timetable.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { GroupsAvailableDto } from './dto/groups-available.dto'
import { TutorsAvailableDto } from './dto/tutors-available.dto'
import { Alarm, createEvents, EventAttributes } from 'ics'
import { createHash } from 'crypto'
import { difference, differenceWith, isEqual } from 'lodash'
import { MailService } from '@sendgrid/mail'
import * as YAML from 'yaml'

type ScheduleOptionalFilters = {
groups?: string[]
Expand All @@ -16,28 +19,61 @@ type ScheduleOptionalFilters = {

@Injectable()
export class PublicTimetableService {
private readonly log = new Logger('Public timetables')
private readonly logger = new Logger('Public timetables')
private readonly sendgridMail = new MailService()

constructor(
@InjectModel(Timetable.name)
private timetableModel: Model<TimetableDocument>,
) {}
) {
this.sendgridMail.setApiKey(process.env.SENDGRID_API_KEY)
}

async create(timetable: ScheduleEntryDto): Promise<TimetableDocument> {
const createdTimetable = new this.timetableModel(timetable)
return createdTimetable.save()
}

async updateOneEntry(htmlId: string, changeHash: string, entry: ScheduleEntryDto) {
return await this.timetableModel.findOneAndUpdate(
const previous = await this.timetableModel.findOneAndUpdate(
{
'entry.groups': entry.groups,
'entry.begin': entry.begin,
'entry.code': entry.code,
},
{ $set: { htmlId, changeHash, entry } },
{ new: true, upsert: true },
{ new: false, upsert: true },
)

if (previous.changeHash !== changeHash) {
this.logger.warn(`Received new hash, ${previous.changeHash} became ${changeHash}`)
// get delta
const delta = differenceWith(
Object.entries(entry),
Object.entries(previous.entry),
isEqual,
)
if (delta.length > 0) {
this.logger.warn(`Delta: ${JSON.stringify(Object.fromEntries(delta))}`)
this.sendgridMail.send({
from: 'schedule-changes@kpostek.dev',
templateId: 'd-9f3f6c1d58f44e51832818998666d406',
to: 's25290@pjwstk.edu.pl',
dynamicTemplateData: {
outdated: YAML.stringify(previous.entry),
updated: YAML.stringify(entry),
delta: YAML.stringify(Object.fromEntries(delta)),
},
})
} else {
this.logger.log(
'Delta returened 0 changes! Probably hashing fuction related change...',
)
}
return await this.timetableModel.findOne({ changeHash })
}

return previous
}

async timetableForDay(date: DateTime, optionalFilters: ScheduleOptionalFilters) {
Expand Down
39 changes: 37 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,29 @@
consola "^2.15.0"
node-fetch "^2.6.1"

"@sendgrid/client@^7.6.2":
version "7.6.2"
resolved "https://registry.yarnpkg.com/@sendgrid/client/-/client-7.6.2.tgz#5d08949120dad679f34260f1b875b4f57a8d688e"
integrity sha512-Yw3i3vPBBwfiIi+4i7+1f1rwQoLlLsu3qW16d1UuRp6RgX6H6yHYb2/PfqwNyCC0qzqIWGUKPWwYe5ggcr5Guw==
dependencies:
"@sendgrid/helpers" "^7.6.2"
axios "^0.26.0"

"@sendgrid/helpers@^7.6.2":
version "7.6.2"
resolved "https://registry.yarnpkg.com/@sendgrid/helpers/-/helpers-7.6.2.tgz#e4abdd4e259611ed549ae8e0f4a46cd4f587e5d1"
integrity sha512-kGW0kM2AOHfXjcvB6Lgwa/nMv8IALu0KyNY9X4HSa3MtLohymuhbG9HgjrOh66+BkbsfA03H3bcT0+sPVJ0GKQ==
dependencies:
deepmerge "^4.2.2"

"@sendgrid/mail@^7.6.2":
version "7.6.2"
resolved "https://registry.yarnpkg.com/@sendgrid/mail/-/mail-7.6.2.tgz#118650afbb58be82e3178fa172869d929d937118"
integrity sha512-IHHZFvgU95aqb11AevQvAfautj2pb8iW8UCiUJ2ae9pRF37e6EkBmU9NgdFjbQ/8Xhhm+KDVDzn/JLxDN/GiBw==
dependencies:
"@sendgrid/client" "^7.6.2"
"@sendgrid/helpers" "^7.6.2"

"@sinonjs/commons@^1.7.0":
version "1.8.3"
resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-1.8.3.tgz#3802ddd21a50a949b6721ddd72da36e67e7f1b2d"
Expand Down Expand Up @@ -1150,6 +1173,13 @@
"@types/node" "*"
"@types/webidl-conversions" "*"

"@types/yaml@^1.9.7":
version "1.9.7"
resolved "https://registry.yarnpkg.com/@types/yaml/-/yaml-1.9.7.tgz#2331f36e0aac91311a63d33eb026c21687729679"
integrity sha512-8WMXRDD1D+wCohjfslHDgICd2JtMATZU8CkhH8LVJqcJs6dyYj5TGptzP8wApbmEullGBSsCEzzap73DQ1HJaA==
dependencies:
yaml "*"

"@types/yargs-parser@*":
version "21.0.0"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-21.0.0.tgz#0c60e537fa790f5f9472ed2776c2b71ec117351b"
Expand Down Expand Up @@ -1571,7 +1601,7 @@ asynckit@^0.4.0:
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha1-x57Zf380y48robyXkLzDZkdLS3k=

axios@0.26.1:
axios@0.26.1, axios@^0.26.0:
version "0.26.1"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.26.1.tgz#1ede41c51fcf51bbbd6fd43669caaa4f0495aaa9"
integrity sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==
Expand Down Expand Up @@ -2257,6 +2287,11 @@ domexception@^4.0.0:
dependencies:
webidl-conversions "^7.0.0"

dotenv@^16.0.0:
version "16.0.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.0.0.tgz#c619001253be89ebb638d027b609c75c26e47411"
integrity sha512-qD9WU0MPM4SWLPJy/r2Be+2WgQj8plChsyrCNQzW/0WjvcJQiKQJ9mH3ZgB3fxbUUxgc/11ZJ0Fi5KiimWGz2Q==

ee-first@1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
Expand Down Expand Up @@ -5737,7 +5772,7 @@ yallist@^4.0.0:
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==

yaml@^1.10.0:
yaml@*, yaml@^1.10.0, yaml@^1.10.2:
version "1.10.2"
resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.2.tgz#2301c5ffbf12b467de8da2333a459e29e7920e4b"
integrity sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg==
Expand Down

0 comments on commit f3d3f29

Please sign in to comment.