-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
julian-wasmeier-titanom
committed
May 20, 2024
1 parent
e1db042
commit a65c28e
Showing
4 changed files
with
83 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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,24 +1,50 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Cron, CronExpression } from '@nestjs/schedule'; | ||
import { dbGetTaskGroupUsers } from './db/functions/task-group'; | ||
import { | ||
dbGetTaskGroupUsers, | ||
dbGetTaskGroupsToCreateForCurrentInterval, | ||
} from './db/functions/task-group'; | ||
import { dbGetAssignmentsForTaskGroup } from './db/functions/assignment-task-group'; | ||
dbCreateTaskGroupAssignment, | ||
dbGetAssignmentsForTaskGroup, | ||
dbGetTaskGroupsToAssignForCurrentInterval, | ||
} from './db/functions/assignment-task-group'; | ||
import { randomFromArray } from './utils/array'; | ||
|
||
@Injectable() | ||
export class AssignmentSchedulerService { | ||
@Cron(CronExpression.EVERY_5_SECONDS) | ||
async handleCron() { | ||
console.debug('CRON job running'); | ||
const taskGroupsToCreateAssignmentsFor = | ||
await dbGetTaskGroupsToCreateForCurrentInterval(); | ||
await dbGetTaskGroupsToAssignForCurrentInterval(); | ||
console.debug({ taskGroupsToCreateAssignmentsFor }); | ||
for (const { taskGroupId } of taskGroupsToCreateAssignmentsFor) { | ||
const users = await dbGetTaskGroupUsers(taskGroupId); | ||
const assignments = await dbGetAssignmentsForTaskGroup( | ||
const userIds = await dbGetTaskGroupUsers(taskGroupId); | ||
if (userIds.length === 0) { | ||
continue; | ||
} | ||
const lastAssignments = await dbGetAssignmentsForTaskGroup( | ||
taskGroupId, | ||
users.length, | ||
userIds.length, | ||
); | ||
console.log({ assignments }); | ||
const firstTimeAssignmentUsers = userIds.filter( | ||
({ userId }) => | ||
!lastAssignments.some((assignment) => assignment.userId === userId), | ||
); | ||
|
||
console.debug({ userIds }); | ||
console.debug({ firstTimeAssignmentUsers }); | ||
console.debug({ lastAssignments, taskGroupId }); | ||
|
||
let nextResponsibleUserId: number; | ||
if (firstTimeAssignmentUsers.length === 0) { | ||
nextResponsibleUserId = | ||
lastAssignments[lastAssignments.length - 1].userId; | ||
} else { | ||
nextResponsibleUserId = randomFromArray( | ||
firstTimeAssignmentUsers, | ||
).userId; | ||
} | ||
|
||
await dbCreateTaskGroupAssignment(taskGroupId, nextResponsibleUserId); | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Returns a random value from an array of data. | ||
* | ||
* @param array - An array of data from which to select a random value. | ||
* @returns A random value from the array. | ||
*/ | ||
export function randomFromArray<TItem>(array: readonly TItem[]) { | ||
const randomIndex = Math.floor(Math.random() * array.length); | ||
const randomValue = array[randomIndex]; | ||
return randomValue; | ||
} |