Skip to content

Commit

Permalink
feat(TrainingService): add training actions ws events (#548)
Browse files Browse the repository at this point in the history
  • Loading branch information
guidojw authored Sep 21, 2023
1 parent 8310c86 commit e2487af
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/services/training.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import cron, { type JobCallback } from 'node-schedule'
import { inject, injectable } from 'inversify'
import type { SortQuery } from '../util/request'
import UserService from './user'
import { WebSocketManager } from '../managers'

const { TYPES } = constants
const { getDate, getTime, getTimeZoneAbbreviation } = timeUtil
Expand All @@ -22,6 +23,7 @@ export default class TrainingService {

@inject(TYPES.TrainingTypeRepository) private readonly trainingTypeRepository!: Repository<TrainingType>
@inject(TYPES.UserService) private readonly userService!: UserService
@inject(TYPES.WebSocketManager) private readonly webSocketManager!: WebSocketManager

public async getTrainings (groupId: number, scopes?: string[], sort?: SortQuery): Promise<Training[]> {
if (!TrainingScopes.has(scopes)) {
Expand Down Expand Up @@ -67,6 +69,8 @@ export default class TrainingService {
}))
training.type = trainingType

this.webSocketManager.broadcast('trainingCreate', { groupId, training })

await this.announceTrainingsJob.run(groupId)
cron.scheduleJob(
`training_${training.id}`,
Expand Down Expand Up @@ -123,6 +127,8 @@ export default class TrainingService {
if (changeMessages.length > 0) {
await this.trainingRepository.save(training)

this.webSocketManager.broadcast('trainingUpdate', { groupId, training })

const editorName = await this.userService.getUsername(editorId)
await this.discordMessageJob.run(`**${editorName}**${changeMessages.length > 1 ? `\n- ${changeMessages.join('\n- ')}` : ` ${changeMessages[0]}`}`)

Expand Down Expand Up @@ -157,6 +163,8 @@ export default class TrainingService {
reason
}))

this.webSocketManager.broadcast('trainingCancel', { groupId, training })

await this.announceTrainingsJob.run(groupId)
const job = cron.scheduledJobs[`training_${cancellation.trainingId}`]
if (typeof job !== 'undefined') {
Expand Down

0 comments on commit e2487af

Please sign in to comment.