From b8260302b0752941530a7dbbef6d5d737ee3aab1 Mon Sep 17 00:00:00 2001 From: julian-wasmeier-titanom Date: Tue, 21 May 2024 19:55:20 +0200 Subject: [PATCH] refactor: move getting current assignemnts for taskgroup into function --- backend/src/assignment-scheduler.service.ts | 14 ++++---------- backend/src/db/functions/assignment.ts | 13 +++++++++++++ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/backend/src/assignment-scheduler.service.ts b/backend/src/assignment-scheduler.service.ts index 5853bc8..cc44dc1 100644 --- a/backend/src/assignment-scheduler.service.ts +++ b/backend/src/assignment-scheduler.service.ts @@ -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() @@ -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) */ diff --git a/backend/src/db/functions/assignment.ts b/backend/src/db/functions/assignment.ts index dbbda28..bbb0f29 100644 --- a/backend/src/db/functions/assignment.ts +++ b/backend/src/db/functions/assignment.ts @@ -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