From ef04f6c7af638bb29e5e7f04d2c8cabf8cdf17d1 Mon Sep 17 00:00:00 2001 From: Gabriellvl Date: Tue, 15 Apr 2025 17:33:03 +0200 Subject: [PATCH] fix: put route frontend + veranderingen delete --- .../controllers/teacher-invitations.test.ts | 26 +++++-------- common/src/interfaces/teacher-invitation.ts | 2 +- .../src/controllers/teacher-invitations.ts | 16 ++++++-- frontend/src/queries/teacher-invitations.ts | 38 +++++++++---------- 4 files changed, 40 insertions(+), 42 deletions(-) diff --git a/backend/tests/controllers/teacher-invitations.test.ts b/backend/tests/controllers/teacher-invitations.test.ts index 005dac26..c9e0d623 100644 --- a/backend/tests/controllers/teacher-invitations.test.ts +++ b/backend/tests/controllers/teacher-invitations.test.ts @@ -5,11 +5,12 @@ import { createInvitationHandler, deleteInvitationHandler, getAllInvitationsHandler, - getInvitationHandler + getInvitationHandler, updateInvitationHandler } from '../../src/controllers/teacher-invitations'; import { TeacherInvitationData } from '@dwengo-1/common/interfaces/teacher-invitation'; import { getClassHandler } from '../../src/controllers/classes'; import {BadRequestException} from "../../src/exceptions/bad-request-exception"; +import {ClassStatus} from "@dwengo-1/common/util/class-join-request"; describe('Teacher controllers', () => { let req: Partial; @@ -95,28 +96,19 @@ describe('Teacher controllers', () => { .rejects.toThrowError(BadRequestException); }); - /* - it('Create and accept invitation', async () => { + it('Accept invitation', async () => { const body = { sender: 'LimpBizkit', - receiver: 'testleerkracht1', + receiver: 'FooFighters', class: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89', } as TeacherInvitationData; req = { body }; - await createInvitationHandler(req as Request, res as Response); + await updateInvitationHandler(req as Request, res as Response); - req = { - params: { - sender: 'LimpBizkit', - receiver: 'testleerkracht1', - classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89', - }, - body: { accepted: 'true' }, - }; - - await deleteInvitationHandler(req as Request, res as Response); + const result1 = jsonMock.mock.lastCall?.[0]; + expect(result1.invitation.status).toEqual(ClassStatus.Accepted); req = { params: { @@ -127,8 +119,8 @@ describe('Teacher controllers', () => { await getClassHandler(req as Request, res as Response); const result = jsonMock.mock.lastCall?.[0]; - expect(result.class.teachers).toContain('testleerkracht1'); + expect(result.class.teachers).toContain('FooFighters'); }); - */ + }); diff --git a/common/src/interfaces/teacher-invitation.ts b/common/src/interfaces/teacher-invitation.ts index 37a4029f..0d310d0e 100644 --- a/common/src/interfaces/teacher-invitation.ts +++ b/common/src/interfaces/teacher-invitation.ts @@ -12,5 +12,5 @@ export interface TeacherInvitationData { sender: string; receiver: string; class: string; - accepted?: boolean; + accepted?: boolean; // use for put requests, else skip } diff --git a/frontend/src/controllers/teacher-invitations.ts b/frontend/src/controllers/teacher-invitations.ts index a6e2045a..8083c152 100644 --- a/frontend/src/controllers/teacher-invitations.ts +++ b/frontend/src/controllers/teacher-invitations.ts @@ -14,15 +14,23 @@ export class TeacherInvitationController extends BaseController { super("teachers/invitations"); } - async getAll(username: string, by: boolean): Promise { - return this.get(`/${username}`, { by }); + async getAll(username: string, sent: boolean): Promise { + return this.get(`/${username}`, { sent }); + } + + async getBy(data: TeacherInvitationData): Promise { + return this.get(`/${data.sender}/${data.receiver}/${data.class}`) } async create(data: TeacherInvitationData): Promise { return this.post("/", data); } - async respond(data: TeacherInvitationData, accepted: boolean): Promise { - return this.delete(`/${data.sender}/${data.receiver}/${data.class}`, { accepted }); + async remove(data: TeacherInvitationData): Promise { + return this.delete(`/${data.sender}/${data.receiver}/${data.class}`); + } + + async respond(data: TeacherInvitationData) { + return this.put("/", data); } } diff --git a/frontend/src/queries/teacher-invitations.ts b/frontend/src/queries/teacher-invitations.ts index a56ea9cd..59357c32 100644 --- a/frontend/src/queries/teacher-invitations.ts +++ b/frontend/src/queries/teacher-invitations.ts @@ -6,15 +6,15 @@ import { type TeacherInvitationResponse, type TeacherInvitationsResponse, } from "@/controllers/teacher-invitations.ts"; -import type { TeacherInvitationData } from "@dwengo-1/common/dist/interfaces/teacher-invitation.ts"; -import type { TeacherDTO } from "@dwengo-1/common/dist/interfaces/teacher.ts"; +import type { TeacherInvitationData } from "@dwengo-1/common/interfaces/teacher-invitation"; +import type { TeacherDTO } from "@dwengo-1/common/interfaces/teacher"; const controller = new TeacherInvitationController(); /** - All the invitations the teacher send + All the invitations the teacher sent **/ -export function useTeacherInvitationsByQuery( +export function useTeacherInvitationsSentQuery( username: MaybeRefOrGetter, ): UseQueryReturnType { return useQuery({ @@ -24,9 +24,9 @@ export function useTeacherInvitationsByQuery( } /** - All the pending invitations send to this teacher + All the pending invitations sent to this teacher */ -export function useTeacherInvitationsForQuery( +export function useTeacherInvitationsReceivedQuery( username: MaybeRefOrGetter, ): UseQueryReturnType { return useQuery({ @@ -35,6 +35,15 @@ export function useTeacherInvitationsForQuery( }); } +export function useTeacherInvitationQuery( + data: MaybeRefOrGetter, +): UseQueryReturnType { + return useQuery({ + queryFn: computed(async () => controller.getBy(toValue(data))), + enabled: () => Boolean(toValue(data)), + }); +} + export function useCreateTeacherInvitationMutation(): UseMutationReturnType< TeacherInvitationResponse, Error, @@ -46,25 +55,14 @@ export function useCreateTeacherInvitationMutation(): UseMutationReturnType< }); } -export function useAcceptTeacherInvitationMutation(): UseMutationReturnType< +export function useRespondTeacherInvitationMutation(): UseMutationReturnType< TeacherInvitationResponse, Error, TeacherDTO, unknown > { return useMutation({ - mutationFn: async (data: TeacherInvitationData) => controller.respond(data, true), - }); -} - -export function useDeclineTeacherInvitationMutation(): UseMutationReturnType< - TeacherInvitationResponse, - Error, - TeacherDTO, - unknown -> { - return useMutation({ - mutationFn: async (data: TeacherInvitationData) => controller.respond(data, false), + mutationFn: async (data: TeacherInvitationData) => controller.respond(data), }); } @@ -75,6 +73,6 @@ export function useDeleteTeacherInvitationMutation(): UseMutationReturnType< unknown > { return useMutation({ - mutationFn: async (data: TeacherInvitationData) => controller.respond(data, false), + mutationFn: async (data: TeacherInvitationData) => controller.remove(data), }); }