Skip to content

Commit

Permalink
refactor: move getting current assignemnts for taskgroup into function
Browse files Browse the repository at this point in the history
  • Loading branch information
julian-wasmeier-titanom committed May 21, 2024
1 parent 6c47c14 commit b826030
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
14 changes: 4 additions & 10 deletions backend/src/assignment-scheduler.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Injectable } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import { eq, sql } from 'drizzle-orm';
import { db } from './db';
import {
dbGetAssignmentsForTaskGroup,
dbGetCurrentAssignmentsForTaskGroup,
dbGetTasksToAssignForCurrentInterval,
} from './db/functions/assignment';
import { dbGetTaskGroupUsers } from './db/functions/task-group';
import { assignmentTable, taskGroupTable, taskTable } from './db/schema';
import { assignmentTable } from './db/schema';
import { randomFromArray } from './utils/array';

@Injectable()
Expand Down Expand Up @@ -57,14 +57,8 @@ async function getNextResponsibleUserId(
taskGroupId: number,
userIds: number[],
) {
const currentAssignments = await db
.select()
.from(assignmentTable)
.innerJoin(taskTable, eq(taskTable.id, assignmentTable.taskId))
.innerJoin(taskGroupTable, eq(taskGroupTable.id, taskTable.taskGroupId))
.where(
sql`${assignmentTable.createdAt} >= NOW() - ${taskGroupTable.interval} AND ${taskGroupTable.id} = ${taskGroupId}`,
);
const currentAssignments =
await dbGetCurrentAssignmentsForTaskGroup(taskGroupId);

/* If there already are current assignments, return the userId of one of the current assignments
(It doesn't matter which one, they should all be assigned to the same user) */
Expand Down
13 changes: 13 additions & 0 deletions backend/src/db/functions/assignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ export async function dbGetAssignmentsForTaskGroup(
return await result.limit(limit);
}

export async function dbGetCurrentAssignmentsForTaskGroup(taskGroupId: number) {
const currentAssignments = await db
.select()
.from(assignmentTable)
.innerJoin(taskTable, eq(taskTable.id, assignmentTable.taskId))
.innerJoin(taskGroupTable, eq(taskGroupTable.id, taskTable.taskGroupId))
.where(
sql`${assignmentTable.createdAt} >= NOW() - ${taskGroupTable.interval} AND ${taskGroupTable.id} = ${taskGroupId}`,
);

return currentAssignments;
}

export async function dbGetTasksToAssignForCurrentInterval() {
try {
// Get all tasks that either have no assignments yet or don't have an assignment in the current period
Expand Down

0 comments on commit b826030

Please sign in to comment.