Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
invertedEcho committed May 20, 2024
1 parent 1f0f8bf commit 31cdb50
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 5 deletions.
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@nestjs/jwt": "^10.2.0",
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/schedule": "^4.0.2",
"bcrypt": "^5.1.1",
"drizzle-orm": "^0.30.9",
"passport": "^0.7.0",
Expand Down
36 changes: 36 additions & 0 deletions backend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { AuthController } from './auth/auth.controller';
import { APP_GUARD } from '@nestjs/core';
import { JwtAuthGuard } from './auth/jwt-auth.guard';
import { TaskGroupController } from './task-group.controller';
import { ScheduleModule } from '@nestjs/schedule';
import { AssignmentsModule } from './assignment.module';

@Module({
imports: [AuthModule],
imports: [AuthModule, ScheduleModule.forRoot(), AssignmentsModule],
controllers: [
TasksController,
AssignmentController,
Expand Down
24 changes: 24 additions & 0 deletions backend/src/assignment-scheduler.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Injectable } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import {
dbGetTaskGroupUsers,
dbGetTaskGroupsToCreateForCurrentInterval,
} from './db/functions/task-group';
import { dbGetAssignmentsForTaskGroup } from './db/functions/assignment-task-group';

@Injectable()
export class AssignmentSchedulerService {
@Cron(CronExpression.EVERY_5_SECONDS)
async handleCron() {
const taskGroupsToCreateAssignmentsFor =
await dbGetTaskGroupsToCreateForCurrentInterval();
for (const { taskGroupId } of taskGroupsToCreateAssignmentsFor) {
const users = await dbGetTaskGroupUsers(taskGroupId);
const assignments = await dbGetAssignmentsForTaskGroup(
taskGroupId,
users.length,
);
console.log({ assignments });
}
}
}
7 changes: 7 additions & 0 deletions backend/src/assignment.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Module } from '@nestjs/common';
import { AssignmentSchedulerService } from './assignment-scheduler.service';

@Module({
providers: [AssignmentSchedulerService],
})
export class AssignmentsModule {}
18 changes: 18 additions & 0 deletions backend/src/db/functions/assignment-task-group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { desc, eq } from 'drizzle-orm';
import { db } from '..';
import { taskGroupAssignmentTable } from '../schema';

export async function dbGetAssignmentsForTaskGroup(
taskGroupId: number,
limit?: number,
) {
const result = db
.select()
.from(taskGroupAssignmentTable)
.where(eq(taskGroupAssignmentTable.taskGroupId, taskGroupId))
.orderBy(desc(taskGroupAssignmentTable.createdAt));
if (limit === undefined) {
return await result;
}
return await result.limit(limit);
}
8 changes: 4 additions & 4 deletions backend/src/db/functions/task-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ export async function dbGetTaskGroupsToCreateForCurrentInterval() {
taskGroupAssignmentTable,
eq(taskGroupTable.id, taskGroupAssignmentTable.taskGroupId),
)
.groupBy(taskGroupTable.id)
.having(
sql` MAX(${taskGroupAssignmentTable.createdAt}) < (NOW() - ${taskGroupTable.interval})`,
);
.groupBy(taskGroupTable.id);
// .having(
// sql`MAX(${taskGroupAssignmentTable.createdAt}) < (NOW() - ${taskGroupTable.interval})`,
// );
return taskGroupIdsToCreateAssignmentsFor;
} catch (error) {
console.error({ error });
Expand Down

0 comments on commit 31cdb50

Please sign in to comment.