From 0a293932a84dbd53d31472285b2ebb1a85d26fde Mon Sep 17 00:00:00 2001 From: Jakob Stechow Date: Tue, 21 May 2024 22:59:22 +0200 Subject: [PATCH] feat: implement a scheduler that automatically creates needed assignments (#26) * feat: add taskGroupAssignment table, rename userTaskGroup table * feat: add taskGroupAssignment table, rename userTaskGroup table * feat: db functions for getting task groups that need assignments and getting taskgroupusers * wip * fix: unused table referenced * feat: add task scheduling logic * feat: create assignments as needed * fix: refetch task groups after task group creation * feat: remove task_group_assignment from schema * feat: scheduling logic * chore: formatting * refactor: move getting current assignemnts for taskgroup into function * refactor: use map, remove unnecessary variable * fix: ignore time in timestamp comparison * feat: add env to eas.json * docs: add build command to readme * feat: decrease cron job interval --------- Co-authored-by: julian-wasmeier-titanom --- README.md | 5 + backend/package.json | 1 + backend/pnpm-lock.yaml | 36 ++ backend/src/app.module.ts | 4 +- backend/src/assignment-scheduler.service.ts | 84 ++++ backend/src/assignment.module.ts | 7 + backend/src/db/functions/assignment.ts | 59 ++- backend/src/db/functions/task-group.ts | 32 +- .../migrations/0004_flippant_black_bird.sql | 19 + .../src/db/migrations/0005_new_ultimatum.sql | 1 + .../src/db/migrations/0006_tough_pandemic.sql | 2 + .../src/db/migrations/meta/0004_snapshot.json | 361 ++++++++++++++++++ .../src/db/migrations/meta/0005_snapshot.json | 361 ++++++++++++++++++ .../src/db/migrations/meta/0006_snapshot.json | 299 +++++++++++++++ backend/src/db/migrations/meta/_journal.json | 21 + backend/src/db/schema.ts | 4 +- backend/src/utils/array.ts | 11 + frontend/eas.json | 12 +- frontend/src/screens/assignments.tsx | 6 +- frontend/src/screens/create-task-group.tsx | 8 +- 20 files changed, 1318 insertions(+), 15 deletions(-) create mode 100644 backend/src/assignment-scheduler.service.ts create mode 100644 backend/src/assignment.module.ts create mode 100644 backend/src/db/migrations/0004_flippant_black_bird.sql create mode 100644 backend/src/db/migrations/0005_new_ultimatum.sql create mode 100644 backend/src/db/migrations/0006_tough_pandemic.sql create mode 100644 backend/src/db/migrations/meta/0004_snapshot.json create mode 100644 backend/src/db/migrations/meta/0005_snapshot.json create mode 100644 backend/src/db/migrations/meta/0006_snapshot.json create mode 100644 backend/src/utils/array.ts diff --git a/README.md b/README.md index e8188eb..fcbe931 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # wg-app WIP ## User flow + https://excalidraw.com/#room=2a90cf068f4d87bce613,24I3dxykUBivS35QOdgTqw ## setup @@ -59,3 +60,7 @@ The user flow should be the following - all tasks in the group will be assigned to the same user in one period This means we will remove the frequency column from the task table and add it to the task_group table. + +## How to build apk + +- `eas build --profile production --platform android --local` diff --git a/backend/package.json b/backend/package.json index 2eb5884..4a9af59 100644 --- a/backend/package.json +++ b/backend/package.json @@ -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", diff --git a/backend/pnpm-lock.yaml b/backend/pnpm-lock.yaml index 122c50e..4f5bda4 100644 --- a/backend/pnpm-lock.yaml +++ b/backend/pnpm-lock.yaml @@ -20,6 +20,9 @@ dependencies: '@nestjs/platform-express': specifier: ^10.0.0 version: 10.3.8(@nestjs/common@10.3.8)(@nestjs/core@10.3.8) + '@nestjs/schedule': + specifier: ^4.0.2 + version: 4.0.2(@nestjs/common@10.3.8)(@nestjs/core@10.3.8) bcrypt: specifier: ^5.1.1 version: 5.1.1 @@ -1452,6 +1455,18 @@ packages: transitivePeerDependencies: - supports-color + /@nestjs/schedule@4.0.2(@nestjs/common@10.3.8)(@nestjs/core@10.3.8): + resolution: {integrity: sha512-po9oauE7fO0CjhDKvVC2tzEgjOUwhxYoIsXIVkgfu+xaDMmzzpmXY2s1LT4oP90Z+PaTtPoAHmhslnYmo4mSZg==} + peerDependencies: + '@nestjs/common': ^8.0.0 || ^9.0.0 || ^10.0.0 + '@nestjs/core': ^8.0.0 || ^9.0.0 || ^10.0.0 + dependencies: + '@nestjs/common': 10.3.8(reflect-metadata@0.2.2)(rxjs@7.8.1) + '@nestjs/core': 10.3.8(@nestjs/common@10.3.8)(@nestjs/platform-express@10.3.8)(reflect-metadata@0.2.2)(rxjs@7.8.1) + cron: 3.1.7 + uuid: 9.0.1 + dev: false + /@nestjs/schematics@10.1.1(chokidar@3.6.0)(typescript@5.3.3): resolution: {integrity: sha512-o4lfCnEeIkfJhGBbLZxTuVWcGuqDCFwg5OrvpgRUBM7vI/vONvKKiB5riVNpO+JqXoH0I42NNeDb0m4V5RREig==} peerDependencies: @@ -1707,6 +1722,10 @@ packages: dependencies: '@types/node': 20.12.7 + /@types/luxon@3.4.2: + resolution: {integrity: sha512-TifLZlFudklWlMBfhubvgqTXRzLDI5pCbGa4P8a3wPyUQSW+1xQ5eDsreP9DWHX3tjq1ke96uYG/nwundroWcA==} + dev: false + /@types/methods@1.1.4: resolution: {integrity: sha512-ymXWVrDiCxTBE3+RIrrP533E70eA+9qu7zdWoHuOmGujkYtzf4HQF96b8nwHLqhuf4ykX61IGRIB38CC6/sImQ==} dev: true @@ -2740,6 +2759,13 @@ packages: resolution: {integrity: sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==} dev: true + /cron@3.1.7: + resolution: {integrity: sha512-tlBg7ARsAMQLzgwqVxy8AZl/qlTc5nibqYwtNGoCrd+cV+ugI+tvZC1oT/8dFH8W455YrywGykx/KMmAqOr7Jw==} + dependencies: + '@types/luxon': 3.4.2 + luxon: 3.4.4 + dev: false + /cross-spawn@7.0.3: resolution: {integrity: sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==} engines: {node: '>= 8'} @@ -4774,6 +4800,11 @@ packages: es5-ext: 0.10.64 dev: true + /luxon@3.4.4: + resolution: {integrity: sha512-zobTr7akeGHnv7eBOXcRgMeCP6+uyYsczwmeRCauvpvaAltgNyTbLH/+VaEAPUeWBT+1GuNmz4wC/6jtQzbbVA==} + engines: {node: '>=12'} + dev: false + /magic-string@0.30.5: resolution: {integrity: sha512-7xlpfBaQaP/T6Vh8MO/EqXSW5En6INHEvEXQiuff7Gku0PWjU3uf6w/j9o7O+SpB5fOAkrI5HeoNgwjEO0pFsA==} engines: {node: '>=12'} @@ -6258,6 +6289,11 @@ packages: resolution: {integrity: sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA==} engines: {node: '>= 0.4.0'} + /uuid@9.0.1: + resolution: {integrity: sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==} + hasBin: true + dev: false + /v8-compile-cache-lib@3.0.1: resolution: {integrity: sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==} dev: true diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index 50ad833..75054d9 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -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, diff --git a/backend/src/assignment-scheduler.service.ts b/backend/src/assignment-scheduler.service.ts new file mode 100644 index 0000000..511f6cd --- /dev/null +++ b/backend/src/assignment-scheduler.service.ts @@ -0,0 +1,84 @@ +import { Injectable } from '@nestjs/common'; +import { Cron, CronExpression } from '@nestjs/schedule'; +import { db } from './db'; +import { + dbGetAssignmentsForTaskGroup, + dbGetCurrentAssignmentsForTaskGroup, + dbGetTasksToAssignForCurrentInterval, +} from './db/functions/assignment'; +import { dbGetTaskGroupUsers } from './db/functions/task-group'; +import { assignmentTable } from './db/schema'; +import { randomFromArray } from './utils/array'; + +@Injectable() +export class AssignmentSchedulerService { + @Cron(CronExpression.EVERY_30_SECONDS) + async handleCron() { + const tasksToCreateAssignmentsFor = + await dbGetTasksToAssignForCurrentInterval(); + + console.debug( + 'Running task scheduling cron job for', + tasksToCreateAssignmentsFor, + ); + + const tasksByGroup = tasksToCreateAssignmentsFor.reduce< + Map + >((acc, curr) => { + if (!acc.get(curr.taskGroupId)) { + acc.set(curr.taskGroupId, []); + } + acc.get(curr.taskGroupId)?.push(curr.taskId); + return acc; + }, new Map()); + + for (const [taskGroupId, taskIds] of tasksByGroup) { + const userIds = await dbGetTaskGroupUsers(taskGroupId); + if (userIds.length === 0) { + continue; + } + + const nextResponsibleUserId = await getNextResponsibleUserId( + taskGroupId, + userIds.map(({ userId }) => userId), + ); + + await db + .insert(assignmentTable) + .values( + taskIds.map((taskId) => ({ taskId, userId: nextResponsibleUserId })), + ); + } + } +} + +async function getNextResponsibleUserId( + taskGroupId: number, + userIds: number[], +) { + 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) */ + if (currentAssignments.length != 0) { + return currentAssignments[0].assignment.userId; + } + + const lastAssignments = await dbGetAssignmentsForTaskGroup( + taskGroupId, + userIds.length, + ); + const userIdsWithoutAnyAssignments = userIds.filter( + (userId) => + !lastAssignments.some(({ assignment }) => assignment.userId === userId), + ); + + /* If all users were already assigned the task, the next responsible user is the one who was assigned the task the longest ago. + Otherwise it is randomly chosen from the users that were not assigned the task yet */ + if (userIdsWithoutAnyAssignments.length === 0) { + return lastAssignments[lastAssignments.length - 1].assignment.userId; + } else { + return randomFromArray(userIdsWithoutAnyAssignments); + } +} diff --git a/backend/src/assignment.module.ts b/backend/src/assignment.module.ts new file mode 100644 index 0000000..3889c3f --- /dev/null +++ b/backend/src/assignment.module.ts @@ -0,0 +1,7 @@ +import { Module } from '@nestjs/common'; +import { AssignmentSchedulerService } from './assignment-scheduler.service'; + +@Module({ + providers: [AssignmentSchedulerService], +}) +export class AssignmentsModule {} diff --git a/backend/src/db/functions/assignment.ts b/backend/src/db/functions/assignment.ts index 5a87600..3022885 100644 --- a/backend/src/db/functions/assignment.ts +++ b/backend/src/db/functions/assignment.ts @@ -3,10 +3,11 @@ import { db } from '..'; import { AssignmentState, assignmentTable, + taskGroupTable, taskTable, userTable, } from '../schema'; -import { eq } from 'drizzle-orm'; +import { count, desc, eq, or, sql } from 'drizzle-orm'; export async function dbGetAllAssignments(): Promise { try { @@ -47,3 +48,59 @@ export async function dbChangeAssignmentState( throw error; } } + +export async function dbGetAssignmentsForTaskGroup( + taskGroupId: number, + limit?: number, +) { + const result = db + .select({ assignment: { ...assignmentTable } }) + .from(taskGroupTable) + .innerJoin(taskTable, eq(taskGroupTable.id, taskTable.taskGroupId)) + .innerJoin(assignmentTable, eq(taskTable.id, assignmentTable.taskId)) + .where(eq(taskGroupTable.id, taskGroupId)) + .orderBy(desc(assignmentTable.createdAt)); + if (limit === undefined) { + return await result; + } + 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}::date >= NOW()::date - ${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 + const taskIdsToCreateAssignmentsFor = await db + .select({ + taskId: taskTable.id, + taskGroupId: taskGroupTable.id, + }) + .from(taskGroupTable) + .innerJoin(taskTable, eq(taskGroupTable.id, taskTable.taskGroupId)) + .leftJoin(assignmentTable, eq(taskTable.id, assignmentTable.taskId)) + .groupBy(taskGroupTable.id, taskTable.id) + .having( + or( + eq(count(assignmentTable.id), 0), + sql`MAX(${assignmentTable.createdAt})::date <= (NOW()::date - ${taskGroupTable.interval})`, + ), + ); + + return taskIdsToCreateAssignmentsFor; + } catch (error) { + console.error({ error }); + throw error; + } +} diff --git a/backend/src/db/functions/task-group.ts b/backend/src/db/functions/task-group.ts index 5ab314d..708eb8a 100644 --- a/backend/src/db/functions/task-group.ts +++ b/backend/src/db/functions/task-group.ts @@ -1,6 +1,12 @@ +import { eq } from 'drizzle-orm'; import { CreateTaskGroup } from 'src/task-group.controller'; import { db } from '..'; -import { taskGroupTable, userTaskGroupTable } from '../schema'; +import { + taskGroupTable, + taskGroupUserTable, + taskTable, + userTable, +} from '../schema'; export async function dbGetTaskGroups() { return await db.select().from(taskGroupTable); @@ -26,7 +32,7 @@ export async function dbCreateTaskGroup({ const { taskGroupId } = res[0]; - await db.insert(userTaskGroupTable).values( + await db.insert(taskGroupUserTable).values( userIds.map((userId) => ({ taskGroupId, userId, @@ -37,3 +43,25 @@ export async function dbCreateTaskGroup({ throw error; } } + +export async function dbGetTaskGroupUsers(taskGroupId: number) { + try { + const taskGroupUsers = await db + .select({ userId: userTable.id }) + .from(taskGroupUserTable) + .innerJoin(userTable, eq(taskGroupUserTable.userId, userTable.id)) + .where(eq(taskGroupUserTable.taskGroupId, taskGroupId)); + + return taskGroupUsers; + } catch (error) { + console.error({ error }); + throw error; + } +} + +export async function dbGetTasksOfTaskGroup(taskGroupId: number) { + return await db + .select() + .from(taskTable) + .where(eq(taskTable.taskGroupId, taskGroupId)); +} diff --git a/backend/src/db/migrations/0004_flippant_black_bird.sql b/backend/src/db/migrations/0004_flippant_black_bird.sql new file mode 100644 index 0000000..50fc4af --- /dev/null +++ b/backend/src/db/migrations/0004_flippant_black_bird.sql @@ -0,0 +1,19 @@ +CREATE TABLE IF NOT EXISTS "task_group_assignment_table" ( + "id" serial PRIMARY KEY NOT NULL, + "task_group_id" integer NOT NULL, + "user_id" integer NOT NULL, + "created_at" timestamp DEFAULT now() NOT NULL +); +--> statement-breakpoint +ALTER TABLE "user_task_group" RENAME TO "task_group_user";--> statement-breakpoint +DO $$ BEGIN + ALTER TABLE "task_group_assignment_table" ADD CONSTRAINT "task_group_assignment_table_task_group_id_task_group_id_fk" FOREIGN KEY ("task_group_id") REFERENCES "public"."task_group"("id") ON DELETE no action ON UPDATE no action; +EXCEPTION + WHEN duplicate_object THEN null; +END $$; +--> statement-breakpoint +DO $$ BEGIN + ALTER TABLE "task_group_assignment_table" ADD CONSTRAINT "task_group_assignment_table_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action; +EXCEPTION + WHEN duplicate_object THEN null; +END $$; diff --git a/backend/src/db/migrations/0005_new_ultimatum.sql b/backend/src/db/migrations/0005_new_ultimatum.sql new file mode 100644 index 0000000..e6d8210 --- /dev/null +++ b/backend/src/db/migrations/0005_new_ultimatum.sql @@ -0,0 +1 @@ +ALTER TABLE "task_group_assignment_table" RENAME TO "task_group_assignment"; \ No newline at end of file diff --git a/backend/src/db/migrations/0006_tough_pandemic.sql b/backend/src/db/migrations/0006_tough_pandemic.sql new file mode 100644 index 0000000..d2d3556 --- /dev/null +++ b/backend/src/db/migrations/0006_tough_pandemic.sql @@ -0,0 +1,2 @@ +DROP TABLE "task_group_assignment";--> statement-breakpoint +ALTER TABLE "assignment" ALTER COLUMN "state" SET NOT NULL; \ No newline at end of file diff --git a/backend/src/db/migrations/meta/0004_snapshot.json b/backend/src/db/migrations/meta/0004_snapshot.json new file mode 100644 index 0000000..5170f78 --- /dev/null +++ b/backend/src/db/migrations/meta/0004_snapshot.json @@ -0,0 +1,361 @@ +{ + "id": "195761e6-c0c7-4afa-827e-dc8c0be2d9b3", + "prevId": "828bbbdd-14ff-4f0e-862a-97de9d3b154f", + "version": "6", + "dialect": "postgresql", + "tables": { + "public.assignment": { + "name": "assignment", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "task_id": { + "name": "task_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "state": { + "name": "state", + "type": "state", + "typeSchema": "public", + "primaryKey": false, + "notNull": false, + "default": "'pending'" + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "assignment_task_id_task_id_fk": { + "name": "assignment_task_id_task_id_fk", + "tableFrom": "assignment", + "tableTo": "task", + "columnsFrom": [ + "task_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "assignment_user_id_user_id_fk": { + "name": "assignment_user_id_user_id_fk", + "tableFrom": "assignment", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.task_group_assignment_table": { + "name": "task_group_assignment_table", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "task_group_id": { + "name": "task_group_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "task_group_assignment_table_task_group_id_task_group_id_fk": { + "name": "task_group_assignment_table_task_group_id_task_group_id_fk", + "tableFrom": "task_group_assignment_table", + "tableTo": "task_group", + "columnsFrom": [ + "task_group_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "task_group_assignment_table_user_id_user_id_fk": { + "name": "task_group_assignment_table_user_id_user_id_fk", + "tableFrom": "task_group_assignment_table", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.task_group": { + "name": "task_group", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "interval": { + "name": "interval", + "type": "interval", + "primaryKey": false, + "notNull": true + }, + "initial_start_date": { + "name": "initial_start_date", + "type": "timestamp", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.task_group_user": { + "name": "task_group_user", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "task_group_id": { + "name": "task_group_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "task_group_user_task_group_id_task_group_id_fk": { + "name": "task_group_user_task_group_id_task_group_id_fk", + "tableFrom": "task_group_user", + "tableTo": "task_group", + "columnsFrom": [ + "task_group_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "task_group_user_user_id_user_id_fk": { + "name": "task_group_user_user_id_user_id_fk", + "tableFrom": "task_group_user", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.task": { + "name": "task", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "task_group_id": { + "name": "task_group_id", + "type": "integer", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": { + "task_task_group_id_task_group_id_fk": { + "name": "task_task_group_id_task_group_id_fk", + "tableFrom": "task", + "tableTo": "task_group", + "columnsFrom": [ + "task_group_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.user": { + "name": "user", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "username": { + "name": "username", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "password": { + "name": "password", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + } + }, + "enums": { + "public.state": { + "name": "state", + "schema": "public", + "values": [ + "pending", + "completed" + ] + } + }, + "schemas": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/backend/src/db/migrations/meta/0005_snapshot.json b/backend/src/db/migrations/meta/0005_snapshot.json new file mode 100644 index 0000000..197885c --- /dev/null +++ b/backend/src/db/migrations/meta/0005_snapshot.json @@ -0,0 +1,361 @@ +{ + "id": "c1709635-b0ce-48f9-a398-b7438e58d649", + "prevId": "195761e6-c0c7-4afa-827e-dc8c0be2d9b3", + "version": "6", + "dialect": "postgresql", + "tables": { + "public.assignment": { + "name": "assignment", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "task_id": { + "name": "task_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "state": { + "name": "state", + "type": "state", + "typeSchema": "public", + "primaryKey": false, + "notNull": false, + "default": "'pending'" + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "assignment_task_id_task_id_fk": { + "name": "assignment_task_id_task_id_fk", + "tableFrom": "assignment", + "tableTo": "task", + "columnsFrom": [ + "task_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "assignment_user_id_user_id_fk": { + "name": "assignment_user_id_user_id_fk", + "tableFrom": "assignment", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.task_group_assignment": { + "name": "task_group_assignment", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "task_group_id": { + "name": "task_group_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "task_group_assignment_task_group_id_task_group_id_fk": { + "name": "task_group_assignment_task_group_id_task_group_id_fk", + "tableFrom": "task_group_assignment", + "tableTo": "task_group", + "columnsFrom": [ + "task_group_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "task_group_assignment_user_id_user_id_fk": { + "name": "task_group_assignment_user_id_user_id_fk", + "tableFrom": "task_group_assignment", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.task_group": { + "name": "task_group", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "interval": { + "name": "interval", + "type": "interval", + "primaryKey": false, + "notNull": true + }, + "initial_start_date": { + "name": "initial_start_date", + "type": "timestamp", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.task_group_user": { + "name": "task_group_user", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "task_group_id": { + "name": "task_group_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "task_group_user_task_group_id_task_group_id_fk": { + "name": "task_group_user_task_group_id_task_group_id_fk", + "tableFrom": "task_group_user", + "tableTo": "task_group", + "columnsFrom": [ + "task_group_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "task_group_user_user_id_user_id_fk": { + "name": "task_group_user_user_id_user_id_fk", + "tableFrom": "task_group_user", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.task": { + "name": "task", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "task_group_id": { + "name": "task_group_id", + "type": "integer", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": { + "task_task_group_id_task_group_id_fk": { + "name": "task_task_group_id_task_group_id_fk", + "tableFrom": "task", + "tableTo": "task_group", + "columnsFrom": [ + "task_group_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.user": { + "name": "user", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "username": { + "name": "username", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "password": { + "name": "password", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + } + }, + "enums": { + "public.state": { + "name": "state", + "schema": "public", + "values": [ + "pending", + "completed" + ] + } + }, + "schemas": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/backend/src/db/migrations/meta/0006_snapshot.json b/backend/src/db/migrations/meta/0006_snapshot.json new file mode 100644 index 0000000..09eee7a --- /dev/null +++ b/backend/src/db/migrations/meta/0006_snapshot.json @@ -0,0 +1,299 @@ +{ + "id": "61ddc4b2-c231-4514-872f-6483fe5e7d11", + "prevId": "c1709635-b0ce-48f9-a398-b7438e58d649", + "version": "6", + "dialect": "postgresql", + "tables": { + "public.assignment": { + "name": "assignment", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "task_id": { + "name": "task_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "state": { + "name": "state", + "type": "state", + "typeSchema": "public", + "primaryKey": false, + "notNull": true, + "default": "'pending'" + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "assignment_task_id_task_id_fk": { + "name": "assignment_task_id_task_id_fk", + "tableFrom": "assignment", + "tableTo": "task", + "columnsFrom": [ + "task_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "assignment_user_id_user_id_fk": { + "name": "assignment_user_id_user_id_fk", + "tableFrom": "assignment", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.task_group": { + "name": "task_group", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "interval": { + "name": "interval", + "type": "interval", + "primaryKey": false, + "notNull": true + }, + "initial_start_date": { + "name": "initial_start_date", + "type": "timestamp", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.task_group_user": { + "name": "task_group_user", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "task_group_id": { + "name": "task_group_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "task_group_user_task_group_id_task_group_id_fk": { + "name": "task_group_user_task_group_id_task_group_id_fk", + "tableFrom": "task_group_user", + "tableTo": "task_group", + "columnsFrom": [ + "task_group_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "task_group_user_user_id_user_id_fk": { + "name": "task_group_user_user_id_user_id_fk", + "tableFrom": "task_group_user", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.task": { + "name": "task", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "task_group_id": { + "name": "task_group_id", + "type": "integer", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": { + "task_task_group_id_task_group_id_fk": { + "name": "task_task_group_id_task_group_id_fk", + "tableFrom": "task", + "tableTo": "task_group", + "columnsFrom": [ + "task_group_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.user": { + "name": "user", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "username": { + "name": "username", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "password": { + "name": "password", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + } + }, + "enums": { + "public.state": { + "name": "state", + "schema": "public", + "values": [ + "pending", + "completed" + ] + } + }, + "schemas": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/backend/src/db/migrations/meta/_journal.json b/backend/src/db/migrations/meta/_journal.json index 7ab7f8c..7b6068e 100644 --- a/backend/src/db/migrations/meta/_journal.json +++ b/backend/src/db/migrations/meta/_journal.json @@ -29,6 +29,27 @@ "when": 1716208018244, "tag": "0003_aspiring_the_hunter", "breakpoints": true + }, + { + "idx": 4, + "version": "6", + "when": 1716209187106, + "tag": "0004_flippant_black_bird", + "breakpoints": true + }, + { + "idx": 5, + "version": "6", + "when": 1716209687796, + "tag": "0005_new_ultimatum", + "breakpoints": true + }, + { + "idx": 6, + "version": "6", + "when": 1716234766177, + "tag": "0006_tough_pandemic", + "breakpoints": true } ] } \ No newline at end of file diff --git a/backend/src/db/schema.ts b/backend/src/db/schema.ts index d252229..d405286 100644 --- a/backend/src/db/schema.ts +++ b/backend/src/db/schema.ts @@ -38,7 +38,7 @@ export const taskGroupTable = pgTable('task_group', { createdAt: timestamp('created_at').notNull().defaultNow(), }); -export const userTaskGroupTable = pgTable('user_task_group', { +export const taskGroupUserTable = pgTable('task_group_user', { id: serial('id').primaryKey(), taskGroupId: integer('task_group_id') .references(() => taskGroupTable.id) @@ -57,7 +57,7 @@ export const assignmentTable = pgTable('assignment', { userId: integer('user_id') .references(() => userTable.id) .notNull(), - state: assigmentStateEnum('state').default('pending'), + state: assigmentStateEnum('state').notNull().default('pending'), createdAt: timestamp('created_at').notNull().defaultNow(), }); diff --git a/backend/src/utils/array.ts b/backend/src/utils/array.ts new file mode 100644 index 0000000..0a82a0e --- /dev/null +++ b/backend/src/utils/array.ts @@ -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(array: readonly TItem[]) { + const randomIndex = Math.floor(Math.random() * array.length); + const randomValue = array[randomIndex]; + return randomValue; +} diff --git a/frontend/eas.json b/frontend/eas.json index 511527f..23d6280 100644 --- a/frontend/eas.json +++ b/frontend/eas.json @@ -5,12 +5,20 @@ "build": { "development": { "developmentClient": true, - "distribution": "internal" + "distribution": "internal", + "env": { + "EXPO_PUBLIC_API_URL": "https://wg.mainfraeme.com" + } }, "preview": { "distribution": "internal" }, - "production": {} + "production": { + "distribution": "internal", + "env": { + "EXPO_PUBLIC_API_URL": "https://wg.mainfraeme.com" + } + } }, "submit": { "production": {} diff --git a/frontend/src/screens/assignments.tsx b/frontend/src/screens/assignments.tsx index 918a1a6..067ab37 100644 --- a/frontend/src/screens/assignments.tsx +++ b/frontend/src/screens/assignments.tsx @@ -55,7 +55,7 @@ export async function getUsers() { async function updateAssignmentStatus( assignmentId: number, - state: AssignmentState, + state: AssignmentState ) { await fetchWrapper.post(`assignments/${assignmentId}/${state}`); } @@ -95,11 +95,11 @@ export function AssigmentsScreen() { } const sortedAssignments = assignments.sort( - (a, b) => b.createdAt.getTime() - a.createdAt.getTime(), + (a, b) => b.createdAt.getTime() - a.createdAt.getTime() ); const filteredAssignments = sortedAssignments.filter( - (assignment) => assignment.assigneeId === selectedUserId, + (assignment) => assignment.assigneeId === selectedUserId ); async function refreshAssignments() { diff --git a/frontend/src/screens/create-task-group.tsx b/frontend/src/screens/create-task-group.tsx index 47f80aa..447fd84 100644 --- a/frontend/src/screens/create-task-group.tsx +++ b/frontend/src/screens/create-task-group.tsx @@ -2,7 +2,7 @@ import * as React from "react"; import { zodResolver } from "@hookform/resolvers/zod"; import RNDateTimePicker from "@react-native-community/datetimepicker"; -import { useMutation, useQuery } from "@tanstack/react-query"; +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { Controller, useForm } from "react-hook-form"; import { Button, @@ -67,6 +67,8 @@ export function CreateTaskGroupScreen() { queryFn: getUsers, }); + const queryClient = useQueryClient(); + const [selectedUserIds, setSelectedUserIds] = React.useState([]); const [date, setDate] = React.useState(new Date()); const [showDatePicker, setShowDatePicker] = React.useState(false); @@ -88,6 +90,7 @@ export function CreateTaskGroupScreen() { resetForm({ ...defaultValues }); setSelectedUserIds([]); setDate(new Date()); + queryClient.refetchQueries({ queryKey: ["taskGroup"] }); }, onError: (err) => { console.error(err); @@ -100,9 +103,6 @@ export function CreateTaskGroupScreen() { mutate({ ...data, }); - - // TODO: refetch taskgroups - // queryClient.refetchQueries({ queryKey: ["tasks"] }); } if (users === undefined || isLoading) {